1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A class to asyncronously build the package relation index
|
4
|
* in the database table xml_relation.
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Chad Berkley
|
8
|
* Release: @release@
|
9
|
*
|
10
|
* '$Author: berkley $'
|
11
|
* '$Date: 2000-09-26 15:07:43 -0700 (Tue, 26 Sep 2000) $'
|
12
|
* '$Revision: 466 $'
|
13
|
*/
|
14
|
|
15
|
package edu.ucsb.nceas.metacat;
|
16
|
|
17
|
import java.sql.*;
|
18
|
import java.util.*;
|
19
|
import java.lang.Thread;
|
20
|
|
21
|
public class relationHandler implements Runnable
|
22
|
{
|
23
|
private Thread btThread = null;
|
24
|
private Connection conn = null;
|
25
|
private DocumentImpl xmldoc = null;
|
26
|
MetaCatUtil util = new MetaCatUtil();
|
27
|
|
28
|
/**
|
29
|
* Constructor for this class. finds all of the relations to a single xml
|
30
|
* document and writes them to the database. This includes transitive
|
31
|
* relations.
|
32
|
* @param xmldoc the xml document to index.
|
33
|
*/
|
34
|
public relationHandler(DocumentImpl xmldoc)
|
35
|
{
|
36
|
try
|
37
|
{
|
38
|
conn = util.getConnection();
|
39
|
}
|
40
|
catch(Exception e)
|
41
|
{
|
42
|
System.out.println("error getting db connection in relationhandler");
|
43
|
}
|
44
|
|
45
|
this.xmldoc = xmldoc;
|
46
|
if(xmldoc.getDoctype().equals(util.getOption("packagedoctype")))
|
47
|
{ //make sure this doctype is a package document then run the thread
|
48
|
btThread = new Thread(this);
|
49
|
btThread.setPriority(Thread.MIN_PRIORITY);
|
50
|
btThread.start();
|
51
|
}
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* The thread handler
|
56
|
*/
|
57
|
public void run()
|
58
|
{
|
59
|
//pseudo-code algorithm
|
60
|
//for each new relation r in xml_nodes
|
61
|
// put r into xml_relation
|
62
|
// compare r to each relation already in xml_relation
|
63
|
// if r isrelatedto a row in xml_relation
|
64
|
// add a new row to xml_relation that represents this new relation
|
65
|
try
|
66
|
{
|
67
|
String docid = xmldoc.getDocID();
|
68
|
PreparedStatement pstmt = conn.prepareStatement(
|
69
|
QuerySpecification.printPackageSQL(docid));
|
70
|
pstmt.execute();
|
71
|
//get the new relations out of xml_nodes
|
72
|
ResultSet rs = pstmt.getResultSet();
|
73
|
boolean hasmorerows = rs.next();
|
74
|
while(hasmorerows)
|
75
|
{
|
76
|
String subject = rs.getString(1);
|
77
|
metacatURL subjectMurl = new metacatURL(subject);
|
78
|
String subjectDoctype = null;
|
79
|
if(subjectMurl.getURLType().equals("metacat"))
|
80
|
{
|
81
|
DocumentImpl subDoc = new DocumentImpl(conn,
|
82
|
subjectMurl.getParam(0)[1]);
|
83
|
subjectDoctype = subDoc.getDoctype();
|
84
|
}
|
85
|
String relationship = rs.getString(2);
|
86
|
String object = rs.getString(3);
|
87
|
|
88
|
//compare r to each relation in xml_relation
|
89
|
pstmt = conn.prepareStatement("select subject, subdoctype, " +
|
90
|
"relationship, object, objdoctype " +
|
91
|
"from xml_relation");
|
92
|
pstmt.execute();
|
93
|
//get each relation in xml_relation for comparison
|
94
|
ResultSet relations = pstmt.getResultSet();
|
95
|
boolean hasmorerelations = relations.next();
|
96
|
while(hasmorerelations)
|
97
|
{
|
98
|
String currentSub = relations.getString(1);
|
99
|
String currentSubDoctype = relations.getString(2);
|
100
|
String currentRelationship = relations.getString(3);
|
101
|
String currentObj = relations.getString(4);
|
102
|
String currentObjDoctype = relations.getString(5);
|
103
|
|
104
|
if(object.equals(currentObj))
|
105
|
{//there is a transitive relation so add a new relation to the table
|
106
|
StringBuffer insertTransRelation = new StringBuffer();
|
107
|
insertTransRelation.append("insert into xml_relation (subject, ");
|
108
|
insertTransRelation.append("subdoctype, relationship, object, ");
|
109
|
insertTransRelation.append("objdoctype) values ('");
|
110
|
insertTransRelation.append(currentSub).append("', '");
|
111
|
insertTransRelation.append(currentSubDoctype).append("', ");
|
112
|
insertTransRelation.append("'hasTransitiveRelationTo', '");
|
113
|
insertTransRelation.append(subject).append("', '");
|
114
|
insertTransRelation.append(subjectDoctype).append("')");
|
115
|
pstmt = conn.prepareStatement(insertTransRelation.toString());
|
116
|
pstmt.execute();
|
117
|
|
118
|
insertTransRelation = new StringBuffer();
|
119
|
//put the same relation in with the subject and object switched
|
120
|
insertTransRelation.append("insert into xml_relation (subject, ");
|
121
|
insertTransRelation.append("subdoctype, relationship, object, ");
|
122
|
insertTransRelation.append("objdoctype) values ('");
|
123
|
insertTransRelation.append(subject).append("', '");
|
124
|
insertTransRelation.append(subjectDoctype).append("', ");
|
125
|
insertTransRelation.append("'hasTransitiveRelationTo', '");
|
126
|
insertTransRelation.append(currentSub).append("', '");
|
127
|
insertTransRelation.append(currentSubDoctype).append("')");
|
128
|
pstmt = conn.prepareStatement(insertTransRelation.toString());
|
129
|
pstmt.execute();
|
130
|
}
|
131
|
|
132
|
hasmorerelations = relations.next();
|
133
|
}
|
134
|
|
135
|
//get the current relations information
|
136
|
metacatURL subMurl = new metacatURL(subject);
|
137
|
metacatURL objMurl = new metacatURL(object);
|
138
|
String subDoctype = null;
|
139
|
String objDoctype = null;
|
140
|
if(subMurl.getURLType().equals("metacat"))
|
141
|
{
|
142
|
DocumentImpl subDoc = new DocumentImpl(conn, subMurl.getParam(0)[1]);
|
143
|
subDoctype = subDoc.getDoctype();
|
144
|
}
|
145
|
if(objMurl.getURLType().equals("metacat"))
|
146
|
{
|
147
|
DocumentImpl objDoc = new DocumentImpl(conn, objMurl.getParam(0)[1]);
|
148
|
objDoc.getDoctype();
|
149
|
}
|
150
|
//now that the comparisons are done, the new relation can be put
|
151
|
//into xml_relation
|
152
|
StringBuffer insertStmt = new StringBuffer();
|
153
|
insertStmt.append("insert into xml_relation (subject, subdoctype, ");
|
154
|
insertStmt.append("relationship, object, objdoctype) values ('");
|
155
|
insertStmt.append(subject).append("', '");
|
156
|
insertStmt.append(subDoctype).append("', '");
|
157
|
insertStmt.append(relationship).append("', '");
|
158
|
insertStmt.append(object).append("', '");
|
159
|
insertStmt.append(objDoctype).append("')");
|
160
|
pstmt = conn.prepareStatement(insertStmt.toString());
|
161
|
pstmt.execute();
|
162
|
|
163
|
hasmorerows = rs.next();
|
164
|
}
|
165
|
|
166
|
util.returnConnection(conn);
|
167
|
btThread = null;
|
168
|
}
|
169
|
catch(Exception e)
|
170
|
{
|
171
|
System.out.println("Error in relationHandler: " + e.getMessage());
|
172
|
util.debugMessage("Error in relationHandler: " + e.getMessage());
|
173
|
btThread = null;
|
174
|
}
|
175
|
}
|
176
|
}
|