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 |
662
|
berkley
|
import java.net.*;
|
26 |
523
|
berkley
|
|
27 |
797
|
bojilova
|
public class RelationHandler //implements Runnable
|
28 |
523
|
berkley
|
{
|
29 |
|
|
private Thread btThread = null;
|
30 |
|
|
private Connection conn = null;
|
31 |
797
|
bojilova
|
private String docid = null;
|
32 |
523
|
berkley
|
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 |
797
|
bojilova
|
* @param docid the ID of the XML document to index.
|
39 |
523
|
berkley
|
*/
|
40 |
797
|
bojilova
|
public RelationHandler(String docid, Connection conn)
|
41 |
819
|
bojilova
|
throws McdbException, SQLException, AccessionNumberException
|
42 |
523
|
berkley
|
{
|
43 |
797
|
bojilova
|
this.conn = conn;
|
44 |
|
|
this.docid = docid;
|
45 |
|
|
putRelations();
|
46 |
523
|
berkley
|
}
|
47 |
|
|
|
48 |
|
|
/**
|
49 |
797
|
bojilova
|
* insert the relations specified in the triples into xml_relation table
|
50 |
683
|
berkley
|
*/
|
51 |
819
|
bojilova
|
private void putRelations()
|
52 |
|
|
throws McdbException, SQLException, AccessionNumberException
|
53 |
|
|
{
|
54 |
|
|
String packagetype = null;
|
55 |
|
|
String subject = null;
|
56 |
|
|
String subDoctype = null;
|
57 |
|
|
String relationship = null;
|
58 |
|
|
String object = null;
|
59 |
|
|
String objDoctype = null;
|
60 |
|
|
PreparedStatement pstmt = null; // to get the relations from xml_nodes
|
61 |
|
|
PreparedStatement tstmt = null; // to insert each relation into xml_relation
|
62 |
743
|
jones
|
MetaCatUtil.debugMessage("Running relation handler!");
|
63 |
797
|
bojilova
|
|
64 |
819
|
bojilova
|
/*
|
65 |
|
|
* PSEUDO-CODE ALGORITHM:
|
66 |
|
|
* deletes all of the relations with a docid of @docid.
|
67 |
|
|
* for each new relation r in xml_nodes
|
68 |
|
|
* put r into xml_relation
|
69 |
|
|
*/
|
70 |
743
|
jones
|
|
71 |
819
|
bojilova
|
//DocumentImpl xmldoc = new DocumentImpl(conn,docid,false);
|
72 |
|
|
packagetype = (new DocumentImpl(conn,docid,false)).getDoctype();
|
73 |
797
|
bojilova
|
|
74 |
819
|
bojilova
|
// first delete the relations for this package document if any
|
75 |
|
|
deleteRelations(docid);
|
76 |
743
|
jones
|
|
77 |
819
|
bojilova
|
// to put the new relations get them out of xml_nodes
|
78 |
|
|
pstmt = conn.prepareStatement(QuerySpecification.printPackageSQL(docid));
|
79 |
|
|
pstmt.execute();
|
80 |
|
|
ResultSet rs = pstmt.getResultSet();
|
81 |
|
|
boolean hasmorerows = rs.next();
|
82 |
|
|
if (hasmorerows) {
|
83 |
|
|
tstmt = conn.prepareStatement("INSERT INTO xml_relation (" +
|
84 |
|
|
"docid,packagetype,subject,subdoctype," +
|
85 |
|
|
"relationship, object, objdoctype) " +
|
86 |
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?)");
|
87 |
|
|
}
|
88 |
|
|
while(hasmorerows) {
|
89 |
|
|
subject = rs.getString(1);
|
90 |
|
|
relationship = rs.getString(2);
|
91 |
|
|
object = rs.getString(3);
|
92 |
662
|
berkley
|
|
93 |
819
|
bojilova
|
// cut out the revision number for subject and object
|
94 |
|
|
subject = (new DocumentIdentifier(subject)).getIdentifier();
|
95 |
|
|
object = (new DocumentIdentifier(object)).getIdentifier();
|
96 |
|
|
|
97 |
|
|
//subDoctype and objDoctype are N/A
|
98 |
|
|
subDoctype = null;
|
99 |
|
|
objDoctype = null;
|
100 |
|
|
|
101 |
|
|
//put the new relation into xml_relation
|
102 |
|
|
tstmt.setString(1, docid);
|
103 |
|
|
tstmt.setString(2, packagetype);
|
104 |
|
|
tstmt.setString(3, subject);
|
105 |
|
|
tstmt.setString(4, subDoctype);
|
106 |
|
|
tstmt.setString(5, relationship);
|
107 |
|
|
tstmt.setString(6, object);
|
108 |
|
|
tstmt.setString(7, objDoctype);
|
109 |
|
|
tstmt.execute();
|
110 |
662
|
berkley
|
|
111 |
819
|
bojilova
|
hasmorerows = rs.next();
|
112 |
|
|
}
|
113 |
523
|
berkley
|
|
114 |
819
|
bojilova
|
if ( tstmt != null ) {
|
115 |
|
|
tstmt.close();
|
116 |
523
|
berkley
|
}
|
117 |
819
|
bojilova
|
pstmt.close();
|
118 |
|
|
conn.commit();
|
119 |
523
|
berkley
|
}
|
120 |
634
|
berkley
|
|
121 |
|
|
/**
|
122 |
|
|
* Deletes all of the relations with a docid of 'docid'.
|
123 |
819
|
bojilova
|
* @param docid the docid of the package which relations to delete.
|
124 |
634
|
berkley
|
*/
|
125 |
797
|
bojilova
|
public void deleteRelations(String docid) throws SQLException
|
126 |
634
|
berkley
|
{
|
127 |
797
|
bojilova
|
try {
|
128 |
819
|
bojilova
|
PreparedStatement pstmt = conn.prepareStatement(
|
129 |
|
|
"DELETE FROM xml_relation " +
|
130 |
|
|
"WHERE docid = '" + docid + "'");
|
131 |
634
|
berkley
|
pstmt.execute();
|
132 |
645
|
bojilova
|
pstmt.close();
|
133 |
797
|
bojilova
|
} catch(SQLException e) {
|
134 |
743
|
jones
|
MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " +
|
135 |
683
|
berkley
|
e.getMessage());
|
136 |
797
|
bojilova
|
System.out.println("error in RelationHandler.deleteRelations(): " +
|
137 |
|
|
e.getMessage());
|
138 |
|
|
throw e;
|
139 |
634
|
berkley
|
}
|
140 |
|
|
}
|
141 |
819
|
bojilova
|
|
142 |
|
|
/**
|
143 |
|
|
* Get the access file id for a package
|
144 |
|
|
* @param docid the document identifier of the package
|
145 |
|
|
* @return the document identifier of the access file for that package
|
146 |
|
|
*/
|
147 |
|
|
public String getAccessFileID(String docid) throws SQLException
|
148 |
|
|
{
|
149 |
|
|
String aclid = null;
|
150 |
887
|
berkley
|
|
151 |
|
|
StringBuffer sql = new StringBuffer();
|
152 |
|
|
sql.append("SELECT docid FROM xml_documents WHERE docid in (SELECT subject ");
|
153 |
|
|
sql.append("FROM xml_relation WHERE docid='").append(docid);
|
154 |
|
|
sql.append("' AND (");
|
155 |
|
|
Vector accessdoctypes = MetaCatUtil.getOptionList(
|
156 |
|
|
MetaCatUtil.getOption("accessdoctype"));
|
157 |
|
|
for(int i=0; i<accessdoctypes.size(); i++)
|
158 |
|
|
{
|
159 |
|
|
String atype = (String)accessdoctypes.elementAt(i);
|
160 |
|
|
sql.append("doctype='").append(atype).append("'");
|
161 |
|
|
if(i < accessdoctypes.size()-1)
|
162 |
|
|
{
|
163 |
|
|
sql.append(" OR ");
|
164 |
|
|
}
|
165 |
|
|
}
|
166 |
|
|
sql.append("))");
|
167 |
|
|
//System.out.println("new sql script: " + sql.toString());
|
168 |
|
|
|
169 |
|
|
/*
|
170 |
819
|
bojilova
|
PreparedStatement pstmt =
|
171 |
|
|
conn.prepareStatement("SELECT docid FROM xml_documents " +
|
172 |
|
|
"WHERE docid in " +
|
173 |
|
|
"(SELECT subject FROM xml_relation " +
|
174 |
|
|
"WHERE docid = ?) " +
|
175 |
|
|
"AND doctype = ?");
|
176 |
|
|
pstmt.setString(1, docid);
|
177 |
|
|
pstmt.setString(2, MetaCatUtil.getOption("accessdoctype"));
|
178 |
887
|
berkley
|
*/
|
179 |
|
|
PreparedStatement pstmt = conn.prepareStatement(sql.toString());
|
180 |
819
|
bojilova
|
pstmt.execute();
|
181 |
|
|
ResultSet rs = pstmt.getResultSet();
|
182 |
|
|
boolean hasRow = rs.next();
|
183 |
|
|
if (hasRow) {
|
184 |
|
|
aclid = rs.getString(1);
|
185 |
|
|
}
|
186 |
|
|
pstmt.close();
|
187 |
|
|
|
188 |
|
|
return aclid;
|
189 |
|
|
}
|
190 |
|
|
|
191 |
523
|
berkley
|
}
|