1
|
/**
|
2
|
* '$Author: berkley $'
|
3
|
* '$Date: 2001-01-18 15:15:21 -0800 (Thu, 18 Jan 2001) $'
|
4
|
* '$Revision: 675 $'
|
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.*;
|
24
|
import java.util.*;
|
25
|
import java.lang.Thread;
|
26
|
import java.net.*;
|
27
|
|
28
|
public class RelationHandler implements Runnable
|
29
|
{
|
30
|
private Thread btThread = null;
|
31
|
private Connection conn = null;
|
32
|
private DocumentImpl xmldoc = null;
|
33
|
MetaCatUtil util = new MetaCatUtil();
|
34
|
|
35
|
/**
|
36
|
* Constructor for this class. finds all of the relations to a single xml
|
37
|
* document and writes them to the database. This includes transitive
|
38
|
* relations.
|
39
|
* @param xmldoc the xml document to index.
|
40
|
*/
|
41
|
public RelationHandler(DocumentImpl xmldoc, Connection conn)
|
42
|
{
|
43
|
//this.conn = conn;
|
44
|
try
|
45
|
{
|
46
|
this.conn = util.openDBConnection();
|
47
|
}
|
48
|
catch(Exception e)
|
49
|
{
|
50
|
System.out.println("error opening connection in " +
|
51
|
"relationHandler.relationHandler: " + e.getMessage());
|
52
|
}
|
53
|
|
54
|
this.xmldoc = xmldoc;
|
55
|
if(xmldoc.getDoctype().equals(util.getOption("packagedoctype")))
|
56
|
{ //make sure this doctype is a package document then run the thread
|
57
|
btThread = new Thread(this);
|
58
|
btThread.setPriority(Thread.MIN_PRIORITY);
|
59
|
btThread.start();
|
60
|
}
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* The thread handler
|
65
|
*/
|
66
|
public void run()
|
67
|
{
|
68
|
String docid = xmldoc.getDocID();
|
69
|
|
70
|
// deletes all of the relations with a docid of @docid.
|
71
|
deleteRelations(docid);
|
72
|
//pseudo-code algorithm
|
73
|
//for each new relation r in xml_nodes
|
74
|
// put r into xml_relation
|
75
|
// compare r to each relation already in xml_relation
|
76
|
// if r isrelatedto a row in xml_relation
|
77
|
// add a new row to xml_relation that represents this new relation
|
78
|
try
|
79
|
{
|
80
|
PreparedStatement pstmt = conn.prepareStatement(
|
81
|
QuerySpecification.printPackageSQL(docid));
|
82
|
pstmt.execute();
|
83
|
|
84
|
//get the new relations out of xml_nodes
|
85
|
ResultSet rs = pstmt.getResultSet();
|
86
|
boolean hasmorerows = rs.next();
|
87
|
while(hasmorerows)
|
88
|
{
|
89
|
String subject = rs.getString(1);
|
90
|
String subjectDoctype = null;
|
91
|
String paramDocid = null;
|
92
|
URL subjectMurl = null;
|
93
|
|
94
|
try
|
95
|
{
|
96
|
subjectMurl = new URL(subject);
|
97
|
subjectDoctype = null;
|
98
|
Hashtable murlParams = util.parseQuery(subjectMurl.getQuery());
|
99
|
paramDocid = (String)murlParams.get("docid");
|
100
|
}
|
101
|
catch(MalformedURLException murle)
|
102
|
{ //assume this is just a docid not a url
|
103
|
paramDocid = subject;
|
104
|
}
|
105
|
|
106
|
DocumentImpl subDoc = new DocumentImpl(conn, paramDocid);
|
107
|
subjectDoctype = subDoc.getDoctype();
|
108
|
String relationship = rs.getString(2);
|
109
|
String object = rs.getString(3);
|
110
|
|
111
|
//compare r to each relation in xml_relation
|
112
|
|
113
|
pstmt = conn.prepareStatement("select subject, subdoctype, " +
|
114
|
"relationship, object, objdoctype " +
|
115
|
"from xml_relation");
|
116
|
pstmt.execute();
|
117
|
//get each relation in xml_relation for comparison
|
118
|
ResultSet relations = pstmt.getResultSet();
|
119
|
boolean hasmorerelations = relations.next();
|
120
|
while(hasmorerelations)
|
121
|
{
|
122
|
String currentSub = relations.getString(1);
|
123
|
String currentSubDoctype = relations.getString(2);
|
124
|
String currentRelationship = relations.getString(3);
|
125
|
String currentObj = relations.getString(4);
|
126
|
String currentObjDoctype = relations.getString(5);
|
127
|
|
128
|
if(object.equals(currentObj))
|
129
|
{//there is a transitive relation so add a new relation to the table
|
130
|
StringBuffer insertTransRelation = new StringBuffer();
|
131
|
insertTransRelation.append("insert into xml_relation (docid, ");
|
132
|
insertTransRelation.append(" subject, ");
|
133
|
insertTransRelation.append("subdoctype, relationship, object, ");
|
134
|
insertTransRelation.append("objdoctype) values ('");
|
135
|
insertTransRelation.append(docid).append("', '");
|
136
|
insertTransRelation.append(currentSub).append("', '");
|
137
|
insertTransRelation.append(currentSubDoctype).append("', ");
|
138
|
insertTransRelation.append("'hasTransitiveRelationTo', '");
|
139
|
insertTransRelation.append(subject).append("', '");
|
140
|
insertTransRelation.append(subjectDoctype).append("')");
|
141
|
//System.out.println("sql1: " + insertTransRelation.toString());
|
142
|
pstmt = conn.prepareStatement(insertTransRelation.toString());
|
143
|
pstmt.execute();
|
144
|
|
145
|
insertTransRelation = new StringBuffer();
|
146
|
//put the same relation in with the subject and object switched
|
147
|
insertTransRelation.append("insert into xml_relation (docid, ");
|
148
|
insertTransRelation.append(" subject, ");
|
149
|
insertTransRelation.append("subdoctype, relationship, object, ");
|
150
|
insertTransRelation.append("objdoctype) values ('");
|
151
|
insertTransRelation.append(docid).append("', '");
|
152
|
insertTransRelation.append(subject).append("', '");
|
153
|
insertTransRelation.append(subjectDoctype).append("', ");
|
154
|
insertTransRelation.append("'hasTransitiveRelationTo', '");
|
155
|
insertTransRelation.append(currentSub).append("', '");
|
156
|
insertTransRelation.append(currentSubDoctype).append("')");
|
157
|
//System.out.println("sql2: " + insertTransRelation.toString());
|
158
|
pstmt = conn.prepareStatement(insertTransRelation.toString());
|
159
|
pstmt.execute();
|
160
|
}
|
161
|
|
162
|
hasmorerelations = relations.next();
|
163
|
}
|
164
|
|
165
|
//get the current relations information
|
166
|
String subDocid = null;
|
167
|
String objDocid = null;
|
168
|
String subDoctype = null;
|
169
|
String objDoctype = null;
|
170
|
|
171
|
try
|
172
|
{
|
173
|
URL subMurl = new URL(subject);
|
174
|
Hashtable subMurlParams = util.parseQuery(subMurl.getQuery());
|
175
|
subDocid = (String)subMurlParams.get("docid");
|
176
|
}
|
177
|
catch(MalformedURLException murle)
|
178
|
{ //assume this is just a docid not a url
|
179
|
subDocid = subject;
|
180
|
}
|
181
|
|
182
|
try
|
183
|
{
|
184
|
URL objMurl = new URL(object);
|
185
|
Hashtable objMurlParams = util.parseQuery(objMurl.getQuery());
|
186
|
objDocid = (String)objMurlParams.get("docid");
|
187
|
}
|
188
|
catch(MalformedURLException murle)
|
189
|
{ //assume this is just a docid
|
190
|
objDocid = object;
|
191
|
}
|
192
|
|
193
|
subDoc = new DocumentImpl(conn, subDocid);
|
194
|
subDoctype = subDoc.getDoctype();
|
195
|
DocumentImpl objDoc = new DocumentImpl(conn, objDocid);
|
196
|
objDoctype = objDoc.getDoctype();
|
197
|
|
198
|
//now that the comparisons are done, the new relation can be put
|
199
|
//into xml_relation
|
200
|
StringBuffer insertStmt = new StringBuffer();
|
201
|
insertStmt.append("insert into xml_relation (docid, subject, ");
|
202
|
insertStmt.append("subdoctype, ");
|
203
|
insertStmt.append("relationship, object, objdoctype) values ('");
|
204
|
insertStmt.append(docid).append("', '");
|
205
|
insertStmt.append(subject).append("', '");
|
206
|
insertStmt.append(subDoctype).append("', '");
|
207
|
insertStmt.append(relationship).append("', '");
|
208
|
insertStmt.append(object).append("', '");
|
209
|
insertStmt.append(objDoctype).append("')");
|
210
|
|
211
|
pstmt = conn.prepareStatement(insertStmt.toString());
|
212
|
pstmt.execute();
|
213
|
|
214
|
hasmorerows = rs.next();
|
215
|
}
|
216
|
|
217
|
conn.commit();
|
218
|
pstmt.close();
|
219
|
conn.close();
|
220
|
btThread = null;
|
221
|
|
222
|
}
|
223
|
catch(Exception e)
|
224
|
{
|
225
|
try
|
226
|
{
|
227
|
conn.rollback();
|
228
|
}
|
229
|
catch (SQLException sqle) {}
|
230
|
System.out.println("Error in relationHandler.run: " + e.getMessage());
|
231
|
util.debugMessage("Error in relationHandler.run: " + e.getMessage());
|
232
|
e.printStackTrace(System.out);
|
233
|
btThread = null;
|
234
|
}
|
235
|
}
|
236
|
|
237
|
/**
|
238
|
* Deletes all of the relations with a docid of 'docid'.
|
239
|
* @param docid the docid to delete.
|
240
|
*/
|
241
|
public void deleteRelations(String docid)
|
242
|
{
|
243
|
try
|
244
|
{
|
245
|
PreparedStatement pstmt = conn.prepareStatement("delete from " +
|
246
|
"xml_relation where docid like '" + docid + "'");
|
247
|
pstmt.execute();
|
248
|
pstmt.close();
|
249
|
}
|
250
|
catch(Exception e)
|
251
|
{
|
252
|
try
|
253
|
{
|
254
|
conn.rollback();
|
255
|
}
|
256
|
catch (SQLException sqle) {}
|
257
|
System.out.println("error in RelationHandler.deleteRelations(): " +
|
258
|
e.getMessage());
|
259
|
e.printStackTrace(System.out);
|
260
|
}
|
261
|
|
262
|
}
|
263
|
}
|