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 1595 tao
        //subject = (new DocumentIdentifier(triple.getSubject())).getIdentifier();
109
        subject = MetaCatUtil.getDocIdFromString(triple.getSubject());
110 1592 tao
        relationship = triple.getRelationship();
111 1595 tao
        //object = (new DocumentIdentifier(triple.getObject())).getIdentifier();
112
        object = MetaCatUtil.getDocIdFromString(triple.getObject());
113
114 1619 tao
        if (subject != null && relationship != null && object != null)
115
        {
116
117
          //put the new relation into xml_relation
118
          MetaCatUtil.debugMessage("Insert into xml_relation table", 30);
119
          tstmt.setString(1, docid);
120
          MetaCatUtil.debugMessage("Insert docid into xml_relation table" +
121 1559 tao
                                docid, 30);
122 1619 tao
          tstmt.setString(2, packagetype);
123
          tstmt.setString(3, subject);
124
          MetaCatUtil.debugMessage("Insert subject into xml_relation table" +
125 1559 tao
                               subject, 30);
126 1619 tao
          tstmt.setString(4, subDoctype);
127
          tstmt.setString(5, relationship);
128
          MetaCatUtil.debugMessage("Insert relationship into xml_relation table" +
129 1559 tao
                                relationship, 30);
130 1619 tao
          tstmt.setString(6, object);
131
          MetaCatUtil.debugMessage("Insert object into xml_relation table" +
132 1592 tao
                                object, 30);
133 1619 tao
          tstmt.setString(7, objDoctype);
134
          tstmt.execute();
135
        }//if
136 1577 tao
137 1592 tao
      }//for
138
    }//if
139 1577 tao
140
    if ( tstmt != null )
141
    {
142 819 bojilova
      tstmt.close();
143 523 berkley
    }
144 1577 tao
145
146 523 berkley
  }
147 1577 tao
148 634 berkley
  /**
149
   * Deletes all of the relations with a docid of 'docid'.
150 819 bojilova
   * @param docid the docid of the package which relations to delete.
151 634 berkley
   */
152 797 bojilova
  public void deleteRelations(String docid) throws SQLException
153 634 berkley
  {
154 797 bojilova
    try {
155 1217 tao
      PreparedStatement pstmt = connection.prepareStatement(
156 819 bojilova
                                "DELETE FROM xml_relation " +
157
                                "WHERE docid = '" + docid + "'");
158 1217 tao
      //increase usage count
159
      connection.increaseUsageCount(1);
160 634 berkley
      pstmt.execute();
161 645 bojilova
      pstmt.close();
162 797 bojilova
    } catch(SQLException e) {
163 743 jones
      MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " +
164 1499 tao
                          e.getMessage(), 30);
165
166 797 bojilova
      throw e;
167 634 berkley
    }
168
  }
169 819 bojilova
170
  /**
171
   * Get the access file id for a package
172
   * @param docid the document identifier of the package
173
   * @return the document identifier of the access file for that package
174
   */
175 1592 tao
  public static String getAccessFileID(String docid) throws SQLException
176 819 bojilova
  {
177
    String aclid = null;
178 1217 tao
    PreparedStatement pstmt = null;
179
    DBConnection dbConn = null;
180
    int serialNumber = -1;
181 887 berkley
182
    StringBuffer sql = new StringBuffer();
183
    sql.append("SELECT docid FROM xml_documents WHERE docid in (SELECT subject ");
184
    sql.append("FROM xml_relation WHERE docid='").append(docid);
185
    sql.append("' AND (");
186
    Vector accessdoctypes = MetaCatUtil.getOptionList(
187
                              MetaCatUtil.getOption("accessdoctype"));
188
    for(int i=0; i<accessdoctypes.size(); i++)
189
    {
190
      String atype = (String)accessdoctypes.elementAt(i);
191
      sql.append("doctype='").append(atype).append("'");
192
      if(i < accessdoctypes.size()-1)
193
      {
194
        sql.append(" OR ");
195
      }
196
    }
197
    sql.append("))");
198
    //System.out.println("new sql script: " + sql.toString());
199
200 1217 tao
    try
201
    {
202
      dbConn=DBConnectionPool.
203
                  getDBConnection("RelationHandler.getAccessFileID");
204
      serialNumber=dbConn.getCheckOutSerialNumber();
205
      pstmt = dbConn.prepareStatement(sql.toString());
206
      pstmt.execute();
207
      ResultSet rs = pstmt.getResultSet();
208
      boolean hasRow = rs.next();
209
      if (hasRow) {
210
        aclid = rs.getString(1);
211
      }
212
      pstmt.close();
213
    }//try
214
    finally
215
    {
216
      try
217
      {
218
        pstmt.close();
219
      }//try
220
      finally
221
      {
222
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
223
      }//finally
224
    }//finally
225
226 1559 tao
    MetaCatUtil.debugMessage("The access docid get from xml_relation is: "+
227
                              aclid, 30);
228 819 bojilova
    return aclid;
229
  }
230
231 523 berkley
}