1
|
/**
|
2
|
* '$Author: tao $'
|
3
|
* '$Date: 2002-06-13 11:54:51 -0700 (Thu, 13 Jun 2002) $'
|
4
|
* '$Revision: 1217 $'
|
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 DBConnection connection = 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, DBConnection conn)
|
41
|
throws McdbException, SQLException, AccessionNumberException
|
42
|
{
|
43
|
this.connection = conn;
|
44
|
this.docid = docid;
|
45
|
putRelations();
|
46
|
}
|
47
|
|
48
|
/**
|
49
|
* insert the relations specified in the triples into xml_relation table
|
50
|
*/
|
51
|
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
|
MetaCatUtil.debugMessage("Running relation handler!");
|
63
|
|
64
|
/*
|
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
|
|
71
|
//DocumentImpl xmldoc = new DocumentImpl(conn,docid,false);
|
72
|
packagetype = (new DocumentImpl(docid,false)).getDoctype();
|
73
|
|
74
|
// first delete the relations for this package document if any
|
75
|
deleteRelations(docid);
|
76
|
|
77
|
// to put the new relations get them out of xml_nodes
|
78
|
pstmt = connection.prepareStatement
|
79
|
(QuerySpecification.printPackageSQL(docid));
|
80
|
//increase usage cont
|
81
|
connection.increaseUsageCount(1);
|
82
|
pstmt.execute();
|
83
|
ResultSet rs = pstmt.getResultSet();
|
84
|
boolean hasmorerows = rs.next();
|
85
|
if (hasmorerows) {
|
86
|
tstmt = connection.prepareStatement("INSERT INTO xml_relation (" +
|
87
|
"docid,packagetype,subject,subdoctype," +
|
88
|
"relationship, object, objdoctype) " +
|
89
|
"VALUES (?, ?, ?, ?, ?, ?, ?)");
|
90
|
//increase usage count
|
91
|
connection.increaseUsageCount(1);
|
92
|
}
|
93
|
while(hasmorerows) {
|
94
|
subject = rs.getString(1);
|
95
|
relationship = rs.getString(2);
|
96
|
object = rs.getString(3);
|
97
|
|
98
|
// cut out the revision number for subject and object
|
99
|
subject = (new DocumentIdentifier(subject)).getIdentifier();
|
100
|
object = (new DocumentIdentifier(object)).getIdentifier();
|
101
|
|
102
|
//subDoctype and objDoctype are N/A
|
103
|
subDoctype = null;
|
104
|
objDoctype = null;
|
105
|
|
106
|
//put the new relation into xml_relation
|
107
|
tstmt.setString(1, docid);
|
108
|
tstmt.setString(2, packagetype);
|
109
|
tstmt.setString(3, subject);
|
110
|
tstmt.setString(4, subDoctype);
|
111
|
tstmt.setString(5, relationship);
|
112
|
tstmt.setString(6, object);
|
113
|
tstmt.setString(7, objDoctype);
|
114
|
tstmt.execute();
|
115
|
|
116
|
hasmorerows = rs.next();
|
117
|
}
|
118
|
|
119
|
if ( tstmt != null ) {
|
120
|
tstmt.close();
|
121
|
}
|
122
|
pstmt.close();
|
123
|
connection.commit();
|
124
|
}
|
125
|
|
126
|
/**
|
127
|
* Deletes all of the relations with a docid of 'docid'.
|
128
|
* @param docid the docid of the package which relations to delete.
|
129
|
*/
|
130
|
public void deleteRelations(String docid) throws SQLException
|
131
|
{
|
132
|
try {
|
133
|
PreparedStatement pstmt = connection.prepareStatement(
|
134
|
"DELETE FROM xml_relation " +
|
135
|
"WHERE docid = '" + docid + "'");
|
136
|
//increase usage count
|
137
|
connection.increaseUsageCount(1);
|
138
|
pstmt.execute();
|
139
|
pstmt.close();
|
140
|
} catch(SQLException e) {
|
141
|
MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " +
|
142
|
e.getMessage());
|
143
|
System.out.println("error in RelationHandler.deleteRelations(): " +
|
144
|
e.getMessage());
|
145
|
throw e;
|
146
|
}
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* Get the access file id for a package
|
151
|
* @param docid the document identifier of the package
|
152
|
* @return the document identifier of the access file for that package
|
153
|
*/
|
154
|
public String getAccessFileID(String docid) throws SQLException
|
155
|
{
|
156
|
String aclid = null;
|
157
|
PreparedStatement pstmt = null;
|
158
|
DBConnection dbConn = null;
|
159
|
int serialNumber = -1;
|
160
|
|
161
|
StringBuffer sql = new StringBuffer();
|
162
|
sql.append("SELECT docid FROM xml_documents WHERE docid in (SELECT subject ");
|
163
|
sql.append("FROM xml_relation WHERE docid='").append(docid);
|
164
|
sql.append("' AND (");
|
165
|
Vector accessdoctypes = MetaCatUtil.getOptionList(
|
166
|
MetaCatUtil.getOption("accessdoctype"));
|
167
|
for(int i=0; i<accessdoctypes.size(); i++)
|
168
|
{
|
169
|
String atype = (String)accessdoctypes.elementAt(i);
|
170
|
sql.append("doctype='").append(atype).append("'");
|
171
|
if(i < accessdoctypes.size()-1)
|
172
|
{
|
173
|
sql.append(" OR ");
|
174
|
}
|
175
|
}
|
176
|
sql.append("))");
|
177
|
//System.out.println("new sql script: " + sql.toString());
|
178
|
|
179
|
/*
|
180
|
PreparedStatement pstmt =
|
181
|
conn.prepareStatement("SELECT docid FROM xml_documents " +
|
182
|
"WHERE docid in " +
|
183
|
"(SELECT subject FROM xml_relation " +
|
184
|
"WHERE docid = ?) " +
|
185
|
"AND doctype = ?");
|
186
|
pstmt.setString(1, docid);
|
187
|
pstmt.setString(2, MetaCatUtil.getOption("accessdoctype"));
|
188
|
*/
|
189
|
try
|
190
|
{
|
191
|
dbConn=DBConnectionPool.
|
192
|
getDBConnection("RelationHandler.getAccessFileID");
|
193
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
194
|
pstmt = dbConn.prepareStatement(sql.toString());
|
195
|
pstmt.execute();
|
196
|
ResultSet rs = pstmt.getResultSet();
|
197
|
boolean hasRow = rs.next();
|
198
|
if (hasRow) {
|
199
|
aclid = rs.getString(1);
|
200
|
}
|
201
|
pstmt.close();
|
202
|
}//try
|
203
|
finally
|
204
|
{
|
205
|
try
|
206
|
{
|
207
|
pstmt.close();
|
208
|
}//try
|
209
|
finally
|
210
|
{
|
211
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
212
|
}//finally
|
213
|
}//finally
|
214
|
|
215
|
|
216
|
return aclid;
|
217
|
}
|
218
|
|
219
|
}
|