Project

General

Profile

1 523 berkley
/**
2
 *   '$Author$'
3
 *     '$Date$'
4
 * '$Revision$'
5 669 jones
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 523 berkley
 */
20
21
package edu.ucsb.nceas.metacat;
22
23 1577 tao
import edu.ucsb.nceas.morpho.datapackage.Triple;
24
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
25
26
import java.io.StringReader;
27 523 berkley
import java.sql.*;
28
import java.util.*;
29 662 berkley
import java.net.*;
30 523 berkley
31 2663 sgarg
import org.apache.log4j.Logger;
32 1577 tao
33 2663 sgarg
34 797 bojilova
public class RelationHandler //implements Runnable
35 523 berkley
{
36 1217 tao
  private DBConnection connection = null;
37 797 bojilova
  private String docid = null;
38 1592 tao
  private String docType = null;
39 2663 sgarg
  private static Logger logMetacat = Logger.getLogger(RelationHandler.class);
40
41 1592 tao
  TripleCollection tripleForPackage = null;
42
43 523 berkley
  /**
44
   * Constructor for this class.  finds all of the relations to a single xml
45
   * document and writes them to the database.  This includes transitive
46
   * relations.
47 797 bojilova
   * @param docid the ID of the XML document to index.
48 1592 tao
   * @param doctype the doctype of this document
49
   * @param conn the db connection
50
   * @param list the triple list
51 523 berkley
   */
52 1592 tao
  public RelationHandler(String docid, String doctype,
53
                        DBConnection conn, TripleCollection list)
54 819 bojilova
              throws McdbException, SQLException, AccessionNumberException
55 523 berkley
  {
56 1217 tao
    this.connection = conn;
57 797 bojilova
    this.docid = docid;
58 1592 tao
    this.docType = doctype;
59
    tripleForPackage = list;
60 1577 tao
    createRelations();
61 523 berkley
  }
62
63 1577 tao
   /**
64 797 bojilova
   * insert the relations specified in the triples into xml_relation table
65 683 berkley
   */
66 1577 tao
  private void createRelations()
67 819 bojilova
              throws McdbException, SQLException, AccessionNumberException
68
  {
69 1592 tao
    String packagetype = docType;
70 819 bojilova
    String subject = null;
71 1388 tao
    String subjectParentId = null;
72 819 bojilova
    String subDoctype = null;
73
    String relationship = null;
74 1388 tao
    String relationshipParentId = null;
75 819 bojilova
    String object = null;
76
    String objDoctype = null;
77
    PreparedStatement tstmt = null; // to insert each relation into xml_relation
78 1388 tao
79 2663 sgarg
    logMetacat.info("Running relation handler!");
80 1592 tao
81 819 bojilova
    // first delete the relations for this package document if any
82
    deleteRelations(docid);
83 1592 tao
84 1577 tao
    //get the vetor of triples
85 1592 tao
    Vector tripleList= new Vector();
86
    //get vector form tripleCollection
87
    if (tripleForPackage != null)
88
    {
89
      tripleList =tripleForPackage.getCollection();
90
    }
91 1577 tao
92
    if (tripleList != null && tripleList.size()>0)
93
    {
94 1388 tao
95 1577 tao
       tstmt = connection.prepareStatement("INSERT INTO xml_relation (" +
96 819 bojilova
                                    "docid,packagetype,subject,subdoctype," +
97
                                    "relationship, object, objdoctype) " +
98
                                    "VALUES (?, ?, ?, ?, ?, ?, ?)");
99 1592 tao
100
      // go through tripe list
101
      for (int i= 0; i<tripleList.size(); i++)
102
      {
103
        //increase usage count
104
        connection.increaseUsageCount(1);
105
        // Get the triple
106
        Triple triple = (Triple)tripleList.elementAt(i);
107 2663 sgarg
        logMetacat.info("Info from triple: ");
108
        logMetacat.info("subject from triple:"+triple.getSubject());
109
        logMetacat.info("relationship from triple:"+triple.getRelationship());
110
        logMetacat.info("object from triple: "+triple.getObject());
111 1595 tao
        //subject = (new DocumentIdentifier(triple.getSubject())).getIdentifier();
112
        subject = MetaCatUtil.getDocIdFromString(triple.getSubject());
113 1592 tao
        relationship = triple.getRelationship();
114 1595 tao
        //object = (new DocumentIdentifier(triple.getObject())).getIdentifier();
115
        object = MetaCatUtil.getDocIdFromString(triple.getObject());
116
117 1619 tao
        if (subject != null && relationship != null && object != null)
118
        {
119
120
          //put the new relation into xml_relation
121 2663 sgarg
          logMetacat.info("Insert into xml_relation table");
122 1619 tao
          tstmt.setString(1, docid);
123 2663 sgarg
          logMetacat.info("Insert docid into xml_relation table" +
124
                                docid);
125 1619 tao
          tstmt.setString(2, packagetype);
126
          tstmt.setString(3, subject);
127 2663 sgarg
          logMetacat.info("Insert subject into xml_relation table" +
128
                               subject);
129 1619 tao
          tstmt.setString(4, subDoctype);
130
          tstmt.setString(5, relationship);
131 2663 sgarg
          logMetacat.info("Insert relationship into xml_relation table" +
132
                                relationship);
133 1619 tao
          tstmt.setString(6, object);
134 2663 sgarg
          logMetacat.info("Insert object into xml_relation table" +
135
                                object);
136 1619 tao
          tstmt.setString(7, objDoctype);
137
          tstmt.execute();
138
        }//if
139 1577 tao
140 1592 tao
      }//for
141
    }//if
142 1577 tao
143
    if ( tstmt != null )
144
    {
145 819 bojilova
      tstmt.close();
146 523 berkley
    }
147 1577 tao
148
149 523 berkley
  }
150 1577 tao
151 634 berkley
  /**
152
   * Deletes all of the relations with a docid of 'docid'.
153 819 bojilova
   * @param docid the docid of the package which relations to delete.
154 634 berkley
   */
155 797 bojilova
  public void deleteRelations(String docid) throws SQLException
156 634 berkley
  {
157 797 bojilova
    try {
158 1217 tao
      PreparedStatement pstmt = connection.prepareStatement(
159 819 bojilova
                                "DELETE FROM xml_relation " +
160
                                "WHERE docid = '" + docid + "'");
161 1217 tao
      //increase usage count
162
      connection.increaseUsageCount(1);
163 634 berkley
      pstmt.execute();
164 645 bojilova
      pstmt.close();
165 797 bojilova
    } catch(SQLException e) {
166 2663 sgarg
      logMetacat.error("error in RelationHandler.deleteRelations(): " +
167
                          e.getMessage());
168 1499 tao
169 797 bojilova
      throw e;
170 634 berkley
    }
171
  }
172 819 bojilova
173
  /**
174
   * Get the access file id for a package
175
   * @param docid the document identifier of the package
176
   * @return the document identifier of the access file for that package
177
   */
178 2653 tao
  public static String getAccessFileIDWithRevision(String docid) throws SQLException
179 819 bojilova
  {
180
    String aclid = null;
181 2653 tao
    int rev;
182 1217 tao
    PreparedStatement pstmt = null;
183
    DBConnection dbConn = null;
184
    int serialNumber = -1;
185 887 berkley
186
    StringBuffer sql = new StringBuffer();
187 2653 tao
    sql.append("SELECT docid, rev FROM xml_documents WHERE docid in (SELECT subject ");
188 887 berkley
    sql.append("FROM xml_relation WHERE docid='").append(docid);
189
    sql.append("' AND (");
190
    Vector accessdoctypes = MetaCatUtil.getOptionList(
191
                              MetaCatUtil.getOption("accessdoctype"));
192
    for(int i=0; i<accessdoctypes.size(); i++)
193
    {
194
      String atype = (String)accessdoctypes.elementAt(i);
195
      sql.append("doctype='").append(atype).append("'");
196
      if(i < accessdoctypes.size()-1)
197
      {
198
        sql.append(" OR ");
199
      }
200
    }
201
    sql.append("))");
202
    //System.out.println("new sql script: " + sql.toString());
203
204 1217 tao
    try
205
    {
206
      dbConn=DBConnectionPool.
207
                  getDBConnection("RelationHandler.getAccessFileID");
208
      serialNumber=dbConn.getCheckOutSerialNumber();
209
      pstmt = dbConn.prepareStatement(sql.toString());
210
      pstmt.execute();
211
      ResultSet rs = pstmt.getResultSet();
212
      boolean hasRow = rs.next();
213
      if (hasRow) {
214
        aclid = rs.getString(1);
215 2653 tao
        rev = rs.getInt(2);
216
        aclid = aclid +MetaCatUtil.getOption("accNumSeparator")+rev;
217 1217 tao
      }
218
      pstmt.close();
219
    }//try
220
    finally
221
    {
222
      try
223
      {
224
        pstmt.close();
225
      }//try
226
      finally
227
      {
228
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
229
      }//finally
230
    }//finally
231
232 2663 sgarg
    logMetacat.info("The access docid get from xml_relation is: "+
233
                              aclid);
234 819 bojilova
    return aclid;
235
  }
236
237 523 berkley
}