1
|
/**
|
2
|
* '$Author: jones $'
|
3
|
* '$Date: 2001-07-20 11:23:06 -0700 (Fri, 20 Jul 2001) $'
|
4
|
* '$Revision: 800 $'
|
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.net.*;
|
26
|
|
27
|
public class RelationHandler //implements Runnable
|
28
|
{
|
29
|
private Thread btThread = null;
|
30
|
private Connection conn = null;
|
31
|
private String docid = null;
|
32
|
MetaCatUtil util = new MetaCatUtil();
|
33
|
|
34
|
/**
|
35
|
* Constructor for this class. finds all of the relations to a single xml
|
36
|
* document and writes them to the database. This includes transitive
|
37
|
* relations.
|
38
|
* @param docid the ID of the XML document to index.
|
39
|
*/
|
40
|
public RelationHandler(String docid, Connection conn)
|
41
|
{
|
42
|
this.conn = conn;
|
43
|
this.docid = docid;
|
44
|
putRelations();
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* insert the relations specified in the triples into xml_relation table
|
49
|
*/
|
50
|
public void putRelations() {
|
51
|
|
52
|
MetaCatUtil.debugMessage("Running relation handler!");
|
53
|
|
54
|
// deletes all of the relations with a docid of @docid.
|
55
|
//pseudo-code algorithm
|
56
|
//for each new relation r in xml_nodes
|
57
|
// put r into xml_relation
|
58
|
// compare r to each relation already in xml_relation
|
59
|
// if r isrelatedto a row in xml_relation
|
60
|
// add a new row to xml_relation that represents this new relation
|
61
|
try {
|
62
|
|
63
|
DocumentImpl xmldoc = new DocumentImpl(conn,docid,false);
|
64
|
//DocumentImpl xmldoc = new DocumentImpl(conn);
|
65
|
//xmldoc.getDocumentInfo(docid);
|
66
|
String packagetype = xmldoc.getDoctype();
|
67
|
|
68
|
// first delete the relations for this package document if any
|
69
|
deleteRelations(docid);
|
70
|
// put the new relations
|
71
|
PreparedStatement pstmt = conn.prepareStatement(
|
72
|
QuerySpecification.printPackageSQL(docid));
|
73
|
pstmt.execute();
|
74
|
|
75
|
//get the new relations out of xml_nodes
|
76
|
ResultSet rs = pstmt.getResultSet();
|
77
|
boolean hasmorerows = rs.next();
|
78
|
while(hasmorerows) {
|
79
|
String subject = rs.getString(1);
|
80
|
String relationship = rs.getString(2);
|
81
|
String object = rs.getString(3);
|
82
|
|
83
|
String subjectDoctype = null;
|
84
|
String paramDocid = null;
|
85
|
URL subjectMurl = null;
|
86
|
|
87
|
//get the current relations information
|
88
|
String subDocid = null;
|
89
|
String objDocid = null;
|
90
|
String subDoctype = null;
|
91
|
String objDoctype = null;
|
92
|
|
93
|
try {
|
94
|
URL subMurl = new URL(subject);
|
95
|
if(subMurl.getQuery() != null) {
|
96
|
Hashtable subMurlParams = util.parseQuery(subMurl.getQuery());
|
97
|
subDocid = (String)subMurlParams.get("docid");
|
98
|
if(subMurl.getProtocol().equals("metacat")) {
|
99
|
DocumentImpl subDoc = new DocumentImpl(conn, subDocid);
|
100
|
subDoctype = subDoc.getDoctype();
|
101
|
}
|
102
|
} else {
|
103
|
subDocid = subject;
|
104
|
}
|
105
|
} catch(MalformedURLException murle) {
|
106
|
//assume this is just a docid not a url
|
107
|
subDocid = subject;
|
108
|
}
|
109
|
|
110
|
try {
|
111
|
URL objMurl = new URL(object);
|
112
|
if(objMurl.getQuery() != null) {
|
113
|
Hashtable objMurlParams = util.parseQuery(objMurl.getQuery());
|
114
|
objDocid = (String)objMurlParams.get("docid");
|
115
|
if(objMurl.getProtocol().equals("metacat")) {
|
116
|
DocumentImpl objDoc = new DocumentImpl(conn, objDocid);
|
117
|
objDoctype = objDoc.getDoctype();
|
118
|
}
|
119
|
} else {
|
120
|
objDocid = object;
|
121
|
}
|
122
|
} catch(MalformedURLException murle) {
|
123
|
//assume this is just a docid
|
124
|
objDocid = object;
|
125
|
}
|
126
|
|
127
|
|
128
|
//now that the comparisons are done, the new relation can be put
|
129
|
//into xml_relation
|
130
|
StringBuffer insertStmt = new StringBuffer();
|
131
|
insertStmt.append("insert into xml_relation (docid, packagetype, ");
|
132
|
insertStmt.append("subject, subdoctype, ");
|
133
|
insertStmt.append("relationship, object, objdoctype) values ('");
|
134
|
insertStmt.append(docid).append("', '");
|
135
|
insertStmt.append(packagetype).append("', '");
|
136
|
insertStmt.append(subject).append("', '");
|
137
|
insertStmt.append(subDoctype).append("', '");
|
138
|
insertStmt.append(relationship).append("', '");
|
139
|
insertStmt.append(object).append("', '");
|
140
|
insertStmt.append(objDoctype).append("')");
|
141
|
|
142
|
pstmt = conn.prepareStatement(insertStmt.toString());
|
143
|
pstmt.execute();
|
144
|
|
145
|
hasmorerows = rs.next();
|
146
|
}
|
147
|
|
148
|
pstmt.close();
|
149
|
conn.commit();
|
150
|
|
151
|
} catch(Exception e) {
|
152
|
MetaCatUtil.debugMessage("Error in RelationHandler.run(): " +
|
153
|
e.getMessage());
|
154
|
System.out.println("Error in RelationHandler.run(): " +
|
155
|
e.getMessage());
|
156
|
try {
|
157
|
conn.rollback();
|
158
|
} catch (SQLException sqle) {
|
159
|
System.out.println("Error in RelationHandler.run(): " +
|
160
|
sqle.getMessage());
|
161
|
}
|
162
|
}
|
163
|
}
|
164
|
|
165
|
/**
|
166
|
* Deletes all of the relations with a docid of 'docid'.
|
167
|
* @param docid the docid to delete.
|
168
|
*/
|
169
|
public void deleteRelations(String docid) throws SQLException
|
170
|
{
|
171
|
try {
|
172
|
PreparedStatement pstmt = conn.prepareStatement("delete from " +
|
173
|
"xml_relation where docid like '" + docid + "'");
|
174
|
pstmt.execute();
|
175
|
pstmt.close();
|
176
|
} catch(SQLException e) {
|
177
|
MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " +
|
178
|
e.getMessage());
|
179
|
System.out.println("error in RelationHandler.deleteRelations(): " +
|
180
|
e.getMessage());
|
181
|
throw e;
|
182
|
}
|
183
|
}
|
184
|
}
|