|
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$'
|
|
11 |
* '$Date$'
|
|
12 |
* '$Revision$'
|
|
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, Connection conn)
|
|
35 |
{
|
|
36 |
this.conn = conn;
|
|
37 |
this.xmldoc = xmldoc;
|
|
38 |
if(xmldoc.getDoctype().equals(util.getOption("packagedoctype")))
|
|
39 |
{ //make sure this doctype is a package document then run the thread
|
|
40 |
btThread = new Thread(this);
|
|
41 |
btThread.setPriority(Thread.MIN_PRIORITY);
|
|
42 |
btThread.start();
|
|
43 |
}
|
|
44 |
}
|
|
45 |
|
|
46 |
/**
|
|
47 |
* The thread handler
|
|
48 |
*/
|
|
49 |
public void run()
|
|
50 |
{
|
|
51 |
//pseudo-code algorithm
|
|
52 |
//for each new relation r in xml_nodes
|
|
53 |
// put r into xml_relation
|
|
54 |
// compare r to each relation already in xml_relation
|
|
55 |
// if r isrelatedto a row in xml_relation
|
|
56 |
// add a new row to xml_relation that represents this new relation
|
|
57 |
try
|
|
58 |
{
|
|
59 |
String docid = xmldoc.getDocID();
|
|
60 |
PreparedStatement pstmt = conn.prepareStatement(
|
|
61 |
QuerySpecification.printPackageSQL(docid));
|
|
62 |
pstmt.execute();
|
|
63 |
//get the new relations out of xml_nodes
|
|
64 |
ResultSet rs = pstmt.getResultSet();
|
|
65 |
boolean hasmorerows = rs.next();
|
|
66 |
while(hasmorerows)
|
|
67 |
{
|
|
68 |
String subject = rs.getString(1);
|
|
69 |
metacatURL subjectMurl = new metacatURL(subject);
|
|
70 |
String subjectDoctype = null;
|
|
71 |
if(subjectMurl.getURLType().equals("metacat"))
|
|
72 |
{
|
|
73 |
DocumentImpl subDoc = new DocumentImpl(conn,
|
|
74 |
subjectMurl.getParam(0)[1]);
|
|
75 |
subjectDoctype = subDoc.getDoctype();
|
|
76 |
}
|
|
77 |
String relationship = rs.getString(2);
|
|
78 |
String object = rs.getString(3);
|
|
79 |
|
|
80 |
//compare r to each relation in xml_relation
|
|
81 |
pstmt = conn.prepareStatement("select subject, subdoctype, " +
|
|
82 |
"relationship, object, objdoctype " +
|
|
83 |
"from xml_relation");
|
|
84 |
pstmt.execute();
|
|
85 |
//get each relation in xml_relation for comparison
|
|
86 |
ResultSet relations = pstmt.getResultSet();
|
|
87 |
boolean hasmorerelations = relations.next();
|
|
88 |
while(hasmorerelations)
|
|
89 |
{
|
|
90 |
String currentSub = relations.getString(1);
|
|
91 |
String currentSubDoctype = relations.getString(2);
|
|
92 |
String currentRelationship = relations.getString(3);
|
|
93 |
String currentObj = relations.getString(4);
|
|
94 |
String currentObjDoctype = relations.getString(5);
|
|
95 |
|
|
96 |
if(object.equals(currentObj))
|
|
97 |
{//there is a transitive relation so add a new relation to the table
|
|
98 |
StringBuffer insertTransRelation = new StringBuffer();
|
|
99 |
insertTransRelation.append("insert into xml_relation (subject, ");
|
|
100 |
insertTransRelation.append("subdoctype, relationship, object, ");
|
|
101 |
insertTransRelation.append("objdoctype) values ('");
|
|
102 |
insertTransRelation.append(currentSub).append("', '");
|
|
103 |
insertTransRelation.append(currentSubDoctype).append("', ");
|
|
104 |
insertTransRelation.append("'hasTransitiveRelationTo', '");
|
|
105 |
insertTransRelation.append(subject).append("', '");
|
|
106 |
insertTransRelation.append(subjectDoctype).append("')");
|
|
107 |
pstmt = conn.prepareStatement(insertTransRelation.toString());
|
|
108 |
pstmt.execute();
|
|
109 |
|
|
110 |
insertTransRelation = new StringBuffer();
|
|
111 |
//put the same relation in with the subject and object switched
|
|
112 |
insertTransRelation.append("insert into xml_relation (subject, ");
|
|
113 |
insertTransRelation.append("subdoctype, relationship, object, ");
|
|
114 |
insertTransRelation.append("objdoctype) values ('");
|
|
115 |
insertTransRelation.append(subject).append("', '");
|
|
116 |
insertTransRelation.append(subjectDoctype).append("', ");
|
|
117 |
insertTransRelation.append("'hasTransitiveRelationTo', '");
|
|
118 |
insertTransRelation.append(currentSub).append("', '");
|
|
119 |
insertTransRelation.append(currentSubDoctype).append("')");
|
|
120 |
pstmt = conn.prepareStatement(insertTransRelation.toString());
|
|
121 |
pstmt.execute();
|
|
122 |
}
|
|
123 |
|
|
124 |
hasmorerelations = relations.next();
|
|
125 |
}
|
|
126 |
|
|
127 |
//get the current relations information
|
|
128 |
metacatURL subMurl = new metacatURL(subject);
|
|
129 |
metacatURL objMurl = new metacatURL(object);
|
|
130 |
String subDoctype = null;
|
|
131 |
String objDoctype = null;
|
|
132 |
if(subMurl.getURLType().equals("metacat"))
|
|
133 |
{
|
|
134 |
DocumentImpl subDoc = new DocumentImpl(conn, subMurl.getParam(0)[1]);
|
|
135 |
subDoctype = subDoc.getDoctype();
|
|
136 |
}
|
|
137 |
if(objMurl.getURLType().equals("metacat"))
|
|
138 |
{
|
|
139 |
DocumentImpl objDoc = new DocumentImpl(conn, objMurl.getParam(0)[1]);
|
|
140 |
objDoc.getDoctype();
|
|
141 |
}
|
|
142 |
//now that the comparisons are done, the new relation can be put
|
|
143 |
//into xml_relation
|
|
144 |
StringBuffer insertStmt = new StringBuffer();
|
|
145 |
insertStmt.append("insert into xml_relation (subject, subdoctype, ");
|
|
146 |
insertStmt.append("relationship, object, objdoctype) values ('");
|
|
147 |
insertStmt.append(subject).append("', '");
|
|
148 |
insertStmt.append(subDoctype).append("', '");
|
|
149 |
insertStmt.append(relationship).append("', '");
|
|
150 |
insertStmt.append(object).append("', '");
|
|
151 |
insertStmt.append(objDoctype).append("')");
|
|
152 |
pstmt = conn.prepareStatement(insertStmt.toString());
|
|
153 |
pstmt.execute();
|
|
154 |
|
|
155 |
hasmorerows = rs.next();
|
|
156 |
}
|
|
157 |
|
|
158 |
conn.commit();
|
|
159 |
btThread = null;
|
|
160 |
|
|
161 |
} catch(Exception e) {
|
|
162 |
try {
|
|
163 |
conn.rollback();
|
|
164 |
} catch (SQLException sqle) {}
|
|
165 |
System.out.println("Error in relationHandler: " + e.getMessage());
|
|
166 |
util.debugMessage("Error in relationHandler: " + e.getMessage());
|
|
167 |
e.printStackTrace(System.out);
|
|
168 |
btThread = null;
|
|
169 |
}
|
|
170 |
}
|
|
171 |
}
|
0 |
172 |
|
changed naming scheme