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