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