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 1577 tao
32 797 bojilova
public class RelationHandler //implements Runnable
33 523 berkley
{
34 1217 tao
  private DBConnection connection = null;
35 797 bojilova
  private String docid = null;
36 1592 tao
  private String docType = null;
37 523 berkley
38 1592 tao
  TripleCollection tripleForPackage = null;
39
40 523 berkley
  /**
41
   * Constructor for this class.  finds all of the relations to a single xml
42
   * document and writes them to the database.  This includes transitive
43
   * relations.
44 797 bojilova
   * @param docid the ID of the XML document to index.
45 1592 tao
   * @param doctype the doctype of this document
46
   * @param conn the db connection
47
   * @param list the triple list
48 523 berkley
   */
49 1592 tao
  public RelationHandler(String docid, String doctype,
50
                        DBConnection conn, TripleCollection list)
51 819 bojilova
              throws McdbException, SQLException, AccessionNumberException
52 523 berkley
  {
53 1217 tao
    this.connection = conn;
54 797 bojilova
    this.docid = docid;
55 1592 tao
    this.docType = doctype;
56
    tripleForPackage = list;
57 1577 tao
    createRelations();
58 523 berkley
  }
59
60 1577 tao
   /**
61 797 bojilova
   * insert the relations specified in the triples into xml_relation table
62 683 berkley
   */
63 1577 tao
  private void createRelations()
64 819 bojilova
              throws McdbException, SQLException, AccessionNumberException
65
  {
66 1592 tao
    String packagetype = docType;
67 819 bojilova
    String subject = null;
68 1388 tao
    String subjectParentId = null;
69 819 bojilova
    String subDoctype = null;
70
    String relationship = null;
71 1388 tao
    String relationshipParentId = null;
72 819 bojilova
    String object = null;
73
    String objDoctype = null;
74
    PreparedStatement tstmt = null; // to insert each relation into xml_relation
75 1388 tao
76
    MetaCatUtil.debugMessage("Running relation handler!", 40);
77 1592 tao
78 819 bojilova
    // first delete the relations for this package document if any
79
    deleteRelations(docid);
80 1592 tao
81 1577 tao
    //get the vetor of triples
82 1592 tao
    Vector tripleList= new Vector();
83
    //get vector form tripleCollection
84
    if (tripleForPackage != null)
85
    {
86
      tripleList =tripleForPackage.getCollection();
87
    }
88 1577 tao
89
    if (tripleList != null && tripleList.size()>0)
90
    {
91 1388 tao
92 1577 tao
       tstmt = connection.prepareStatement("INSERT INTO xml_relation (" +
93 819 bojilova
                                    "docid,packagetype,subject,subdoctype," +
94
                                    "relationship, object, objdoctype) " +
95
                                    "VALUES (?, ?, ?, ?, ?, ?, ?)");
96 1592 tao
97
      // go through tripe list
98
      for (int i= 0; i<tripleList.size(); i++)
99
      {
100
        //increase usage count
101
        connection.increaseUsageCount(1);
102
        // Get the triple
103
        Triple triple = (Triple)tripleList.elementAt(i);
104
        MetaCatUtil.debugMessage("Info from triple: ", 30);
105
        MetaCatUtil.debugMessage("subject from triple:"+triple.getSubject(), 30);
106
        MetaCatUtil.debugMessage("relationship from triple:"+triple.getRelationship(),30);
107
        MetaCatUtil.debugMessage("object from triple: "+triple.getObject(),30);
108
        subject = (new DocumentIdentifier(triple.getSubject())).getIdentifier();
109
        relationship = triple.getRelationship();
110
        object = (new DocumentIdentifier(triple.getObject())).getIdentifier();
111 1388 tao
112 1592 tao
        //put the new relation into xml_relation
113
        MetaCatUtil.debugMessage("Insert into xml_relation table", 30);
114
        tstmt.setString(1, docid);
115
        MetaCatUtil.debugMessage("Insert docid into xml_relation table" +
116 1559 tao
                                docid, 30);
117 1592 tao
        tstmt.setString(2, packagetype);
118
        tstmt.setString(3, subject);
119
        MetaCatUtil.debugMessage("Insert subject into xml_relation table" +
120 1559 tao
                               subject, 30);
121 1592 tao
        tstmt.setString(4, subDoctype);
122
        tstmt.setString(5, relationship);
123
        MetaCatUtil.debugMessage("Insert relationship into xml_relation table" +
124 1559 tao
                                relationship, 30);
125 1592 tao
        tstmt.setString(6, object);
126
        MetaCatUtil.debugMessage("Insert object into xml_relation table" +
127
                                object, 30);
128
        tstmt.setString(7, objDoctype);
129
        tstmt.execute();
130 1577 tao
131 1592 tao
      }//for
132
    }//if
133 1577 tao
134
    if ( tstmt != null )
135
    {
136 819 bojilova
      tstmt.close();
137 523 berkley
    }
138 1577 tao
139
140 523 berkley
  }
141 1577 tao
142 634 berkley
  /**
143
   * Deletes all of the relations with a docid of 'docid'.
144 819 bojilova
   * @param docid the docid of the package which relations to delete.
145 634 berkley
   */
146 797 bojilova
  public void deleteRelations(String docid) throws SQLException
147 634 berkley
  {
148 797 bojilova
    try {
149 1217 tao
      PreparedStatement pstmt = connection.prepareStatement(
150 819 bojilova
                                "DELETE FROM xml_relation " +
151
                                "WHERE docid = '" + docid + "'");
152 1217 tao
      //increase usage count
153
      connection.increaseUsageCount(1);
154 634 berkley
      pstmt.execute();
155 645 bojilova
      pstmt.close();
156 797 bojilova
    } catch(SQLException e) {
157 743 jones
      MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " +
158 1499 tao
                          e.getMessage(), 30);
159
160 797 bojilova
      throw e;
161 634 berkley
    }
162
  }
163 819 bojilova
164
  /**
165
   * Get the access file id for a package
166
   * @param docid the document identifier of the package
167
   * @return the document identifier of the access file for that package
168
   */
169 1592 tao
  public static String getAccessFileID(String docid) throws SQLException
170 819 bojilova
  {
171
    String aclid = null;
172 1217 tao
    PreparedStatement pstmt = null;
173
    DBConnection dbConn = null;
174
    int serialNumber = -1;
175 887 berkley
176
    StringBuffer sql = new StringBuffer();
177
    sql.append("SELECT docid FROM xml_documents WHERE docid in (SELECT subject ");
178
    sql.append("FROM xml_relation WHERE docid='").append(docid);
179
    sql.append("' AND (");
180
    Vector accessdoctypes = MetaCatUtil.getOptionList(
181
                              MetaCatUtil.getOption("accessdoctype"));
182
    for(int i=0; i<accessdoctypes.size(); i++)
183
    {
184
      String atype = (String)accessdoctypes.elementAt(i);
185
      sql.append("doctype='").append(atype).append("'");
186
      if(i < accessdoctypes.size()-1)
187
      {
188
        sql.append(" OR ");
189
      }
190
    }
191
    sql.append("))");
192
    //System.out.println("new sql script: " + sql.toString());
193
194 1217 tao
    try
195
    {
196
      dbConn=DBConnectionPool.
197
                  getDBConnection("RelationHandler.getAccessFileID");
198
      serialNumber=dbConn.getCheckOutSerialNumber();
199
      pstmt = dbConn.prepareStatement(sql.toString());
200
      pstmt.execute();
201
      ResultSet rs = pstmt.getResultSet();
202
      boolean hasRow = rs.next();
203
      if (hasRow) {
204
        aclid = rs.getString(1);
205
      }
206
      pstmt.close();
207
    }//try
208
    finally
209
    {
210
      try
211
      {
212
        pstmt.close();
213
      }//try
214
      finally
215
      {
216
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
217
      }//finally
218
    }//finally
219
220 1559 tao
    MetaCatUtil.debugMessage("The access docid get from xml_relation is: "+
221
                              aclid, 30);
222 819 bojilova
    return aclid;
223
  }
224
225 523 berkley
}