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 |
|
|
import java.sql.*;
|
24 |
|
|
import java.util.*;
|
25 |
|
|
import java.lang.Thread;
|
26 |
662
|
berkley
|
import java.net.*;
|
27 |
523
|
berkley
|
|
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 |
601
|
berkley
|
//this.conn = conn;
|
44 |
|
|
try
|
45 |
|
|
{
|
46 |
683
|
berkley
|
conn = MetacatReplication.getDBConnection("RelationHandler." +
|
47 |
|
|
"relationHandler");
|
48 |
601
|
berkley
|
}
|
49 |
|
|
catch(Exception e)
|
50 |
|
|
{
|
51 |
743
|
jones
|
MetaCatUtil.debugMessage("unable to get db connection in " +
|
52 |
683
|
berkley
|
"relationhandler: " + e.getMessage());
|
53 |
601
|
berkley
|
}
|
54 |
523
|
berkley
|
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 |
683
|
berkley
|
*/
|
66 |
523
|
berkley
|
public void run()
|
67 |
|
|
{
|
68 |
743
|
jones
|
MetaCatUtil.debugMessage("Running relation handler!");
|
69 |
683
|
berkley
|
Connection dbconn = null;
|
70 |
645
|
bojilova
|
String docid = xmldoc.getDocID();
|
71 |
743
|
jones
|
String packagetype = xmldoc.getDoctype();
|
72 |
645
|
bojilova
|
// deletes all of the relations with a docid of @docid.
|
73 |
|
|
deleteRelations(docid);
|
74 |
523
|
berkley
|
//pseudo-code algorithm
|
75 |
|
|
//for each new relation r in xml_nodes
|
76 |
|
|
// put r into xml_relation
|
77 |
|
|
// compare r to each relation already in xml_relation
|
78 |
|
|
// if r isrelatedto a row in xml_relation
|
79 |
|
|
// add a new row to xml_relation that represents this new relation
|
80 |
|
|
try
|
81 |
|
|
{
|
82 |
683
|
berkley
|
dbconn = MetacatReplication.getDBConnection("RelationHandler." +
|
83 |
|
|
"run");
|
84 |
743
|
jones
|
|
85 |
683
|
berkley
|
PreparedStatement pstmt = dbconn.prepareStatement(
|
86 |
523
|
berkley
|
QuerySpecification.printPackageSQL(docid));
|
87 |
|
|
pstmt.execute();
|
88 |
662
|
berkley
|
|
89 |
523
|
berkley
|
//get the new relations out of xml_nodes
|
90 |
|
|
ResultSet rs = pstmt.getResultSet();
|
91 |
|
|
boolean hasmorerows = rs.next();
|
92 |
|
|
while(hasmorerows)
|
93 |
|
|
{
|
94 |
|
|
String subject = rs.getString(1);
|
95 |
743
|
jones
|
String relationship = rs.getString(2);
|
96 |
|
|
String object = rs.getString(3);
|
97 |
|
|
|
98 |
523
|
berkley
|
String subjectDoctype = null;
|
99 |
662
|
berkley
|
String paramDocid = null;
|
100 |
|
|
URL subjectMurl = null;
|
101 |
|
|
|
102 |
523
|
berkley
|
//get the current relations information
|
103 |
662
|
berkley
|
String subDocid = null;
|
104 |
|
|
String objDocid = null;
|
105 |
523
|
berkley
|
String subDoctype = null;
|
106 |
|
|
String objDoctype = null;
|
107 |
662
|
berkley
|
|
108 |
|
|
try
|
109 |
523
|
berkley
|
{
|
110 |
662
|
berkley
|
URL subMurl = new URL(subject);
|
111 |
679
|
berkley
|
if(subMurl.getQuery() != null)
|
112 |
|
|
{
|
113 |
|
|
Hashtable subMurlParams = util.parseQuery(subMurl.getQuery());
|
114 |
|
|
subDocid = (String)subMurlParams.get("docid");
|
115 |
|
|
if(subMurl.getProtocol().equals("metacat"))
|
116 |
|
|
{
|
117 |
743
|
jones
|
DocumentImpl subDoc = new DocumentImpl(dbconn, subDocid);
|
118 |
679
|
berkley
|
subDoctype = subDoc.getDoctype();
|
119 |
|
|
}
|
120 |
|
|
}
|
121 |
|
|
else
|
122 |
|
|
{
|
123 |
|
|
subDocid = subject;
|
124 |
|
|
}
|
125 |
523
|
berkley
|
}
|
126 |
662
|
berkley
|
catch(MalformedURLException murle)
|
127 |
|
|
{ //assume this is just a docid not a url
|
128 |
|
|
subDocid = subject;
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
try
|
132 |
523
|
berkley
|
{
|
133 |
662
|
berkley
|
URL objMurl = new URL(object);
|
134 |
679
|
berkley
|
if(objMurl.getQuery() != null)
|
135 |
|
|
{
|
136 |
|
|
Hashtable objMurlParams = util.parseQuery(objMurl.getQuery());
|
137 |
|
|
objDocid = (String)objMurlParams.get("docid");
|
138 |
|
|
if(objMurl.getProtocol().equals("metacat"))
|
139 |
|
|
{
|
140 |
683
|
berkley
|
DocumentImpl objDoc = new DocumentImpl(dbconn, objDocid);
|
141 |
679
|
berkley
|
objDoctype = objDoc.getDoctype();
|
142 |
|
|
}
|
143 |
|
|
}
|
144 |
|
|
else
|
145 |
|
|
{
|
146 |
|
|
objDocid = object;
|
147 |
|
|
}
|
148 |
523
|
berkley
|
}
|
149 |
662
|
berkley
|
catch(MalformedURLException murle)
|
150 |
|
|
{ //assume this is just a docid
|
151 |
|
|
objDocid = object;
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
|
155 |
523
|
berkley
|
//now that the comparisons are done, the new relation can be put
|
156 |
|
|
//into xml_relation
|
157 |
|
|
StringBuffer insertStmt = new StringBuffer();
|
158 |
743
|
jones
|
insertStmt.append("insert into xml_relation (docid, packagetype, ");
|
159 |
|
|
insertStmt.append("subject, subdoctype, ");
|
160 |
523
|
berkley
|
insertStmt.append("relationship, object, objdoctype) values ('");
|
161 |
634
|
berkley
|
insertStmt.append(docid).append("', '");
|
162 |
743
|
jones
|
insertStmt.append(packagetype).append("', '");
|
163 |
523
|
berkley
|
insertStmt.append(subject).append("', '");
|
164 |
|
|
insertStmt.append(subDoctype).append("', '");
|
165 |
|
|
insertStmt.append(relationship).append("', '");
|
166 |
|
|
insertStmt.append(object).append("', '");
|
167 |
|
|
insertStmt.append(objDoctype).append("')");
|
168 |
671
|
berkley
|
|
169 |
683
|
berkley
|
pstmt = dbconn.prepareStatement(insertStmt.toString());
|
170 |
523
|
berkley
|
pstmt.execute();
|
171 |
|
|
|
172 |
|
|
hasmorerows = rs.next();
|
173 |
|
|
}
|
174 |
|
|
|
175 |
683
|
berkley
|
dbconn.commit();
|
176 |
671
|
berkley
|
pstmt.close();
|
177 |
683
|
berkley
|
dbconn.close();
|
178 |
523
|
berkley
|
btThread = null;
|
179 |
|
|
|
180 |
601
|
berkley
|
}
|
181 |
|
|
catch(Exception e)
|
182 |
|
|
{
|
183 |
743
|
jones
|
MetaCatUtil.debugMessage("Error in relationHandler.run: " +
|
184 |
|
|
e.getMessage());
|
185 |
683
|
berkley
|
btThread = null;
|
186 |
601
|
berkley
|
try
|
187 |
|
|
{
|
188 |
523
|
berkley
|
conn.rollback();
|
189 |
601
|
berkley
|
}
|
190 |
|
|
catch (SQLException sqle) {}
|
191 |
523
|
berkley
|
}
|
192 |
|
|
}
|
193 |
634
|
berkley
|
|
194 |
|
|
/**
|
195 |
|
|
* Deletes all of the relations with a docid of 'docid'.
|
196 |
|
|
* @param docid the docid to delete.
|
197 |
|
|
*/
|
198 |
645
|
bojilova
|
public void deleteRelations(String docid)
|
199 |
634
|
berkley
|
{
|
200 |
683
|
berkley
|
Connection dbconn = null;
|
201 |
634
|
berkley
|
try
|
202 |
|
|
{
|
203 |
683
|
berkley
|
dbconn = MetacatReplication.getDBConnection("RelationHandler."+
|
204 |
|
|
"deleteRelations");
|
205 |
|
|
}
|
206 |
|
|
catch(Exception ee)
|
207 |
|
|
{
|
208 |
743
|
jones
|
MetaCatUtil.debugMessage("error in relationHandler.deleteRelations: " +
|
209 |
683
|
berkley
|
ee.getMessage());
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
try
|
213 |
|
|
{
|
214 |
|
|
PreparedStatement pstmt = dbconn.prepareStatement("delete from " +
|
215 |
634
|
berkley
|
"xml_relation where docid like '" + docid + "'");
|
216 |
|
|
pstmt.execute();
|
217 |
645
|
bojilova
|
pstmt.close();
|
218 |
683
|
berkley
|
dbconn.close();
|
219 |
634
|
berkley
|
}
|
220 |
|
|
catch(Exception e)
|
221 |
|
|
{
|
222 |
743
|
jones
|
MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " +
|
223 |
683
|
berkley
|
e.getMessage());
|
224 |
645
|
bojilova
|
try
|
225 |
|
|
{
|
226 |
683
|
berkley
|
dbconn.rollback();
|
227 |
|
|
dbconn.close();
|
228 |
645
|
bojilova
|
}
|
229 |
|
|
catch (SQLException sqle) {}
|
230 |
634
|
berkley
|
}
|
231 |
|
|
}
|
232 |
523
|
berkley
|
}
|