Revision 1388
Added by Jing Tao almost 22 years ago
src/edu/ucsb/nceas/metacat/RelationHandler.java | ||
---|---|---|
30 | 30 |
private DBConnection connection = null; |
31 | 31 |
private String docid = null; |
32 | 32 |
MetaCatUtil util = new MetaCatUtil(); |
33 |
// The hashtable's key is String vector, subject parentid , relation ship |
|
34 |
// prarent id and object parent id. Element is a vetor too. subject, relation |
|
35 |
// and object |
|
36 |
private Hashtable parentNodeIdList = new Hashtable(); |
|
37 |
private static final int SUBJECTINDEX = 0; |
|
38 |
private static final int RELATIONINDEX = 1; |
|
39 |
private static final int OBJECTINDEX = 2; |
|
33 | 40 |
|
34 | 41 |
/** |
35 | 42 |
* Constructor for this class. finds all of the relations to a single xml |
... | ... | |
53 | 60 |
{ |
54 | 61 |
String packagetype = null; |
55 | 62 |
String subject = null; |
63 |
String subjectParentId = null; |
|
56 | 64 |
String subDoctype = null; |
57 | 65 |
String relationship = null; |
66 |
String relationshipParentId = null; |
|
58 | 67 |
String object = null; |
68 |
String objectParentId = null; |
|
59 | 69 |
String objDoctype = null; |
60 | 70 |
PreparedStatement pstmt = null; // to get the relations from xml_nodes |
61 | 71 |
PreparedStatement tstmt = null; // to insert each relation into xml_relation |
62 |
MetaCatUtil.debugMessage("Running relation handler!"); |
|
72 |
|
|
73 |
MetaCatUtil.debugMessage("Running relation handler!", 40); |
|
74 |
|
|
63 | 75 |
|
64 | 76 |
/* |
65 | 77 |
* PSEUDO-CODE ALGORITHM: |
... | ... | |
77 | 89 |
// to put the new relations get them out of xml_nodes |
78 | 90 |
pstmt = connection.prepareStatement |
79 | 91 |
(QuerySpecification.printPackageSQL(docid)); |
92 |
|
|
80 | 93 |
//increase usage cont |
81 | 94 |
connection.increaseUsageCount(1); |
82 | 95 |
pstmt.execute(); |
83 | 96 |
ResultSet rs = pstmt.getResultSet(); |
84 | 97 |
boolean hasmorerows = rs.next(); |
85 |
if (hasmorerows) { |
|
98 |
|
|
99 |
while(hasmorerows) { |
|
100 |
Vector parentId = new Vector(); |
|
101 |
Vector sbjReObj = new Vector(); |
|
102 |
boolean mergeSubject = false; |
|
103 |
boolean mergeRelation = false; |
|
104 |
boolean mergerObject = false; |
|
105 |
subject = rs.getString(1); |
|
106 |
subjectParentId = rs.getString(2); |
|
107 |
relationship = rs.getString(3); |
|
108 |
relationshipParentId = rs.getString(4); |
|
109 |
object = rs.getString(5); |
|
110 |
objectParentId = rs.getString(6); |
|
111 |
|
|
112 |
|
|
113 |
MetaCatUtil.debugMessage("oringal subjectparentid: " + |
|
114 |
subjectParentId, 40); |
|
115 |
MetaCatUtil.debugMessage("oringinal subject: "+subject); |
|
116 |
MetaCatUtil.debugMessage("oringal relation parentid: "+ |
|
117 |
relationshipParentId, 40); |
|
118 |
MetaCatUtil.debugMessage("oringinal relationship: "+relationship, 40); |
|
119 |
MetaCatUtil.debugMessage("oringal objectparentid: "+ |
|
120 |
objectParentId, 40); |
|
121 |
MetaCatUtil.debugMessage("oringinal object: "+object, 40); |
|
122 |
|
|
123 |
if ( hasSameId(subjectParentId) ) |
|
124 |
{ |
|
125 |
// subject was splited and need to conbined |
|
126 |
String prefixSubject = getValueFromParentIdList(subjectParentId); |
|
127 |
if (prefixSubject !=null && !prefixSubject.equals(subject)) |
|
128 |
{ |
|
129 |
MetaCatUtil.debugMessage("merger subject: "+prefixSubject + |
|
130 |
subject, 40); |
|
131 |
subject = prefixSubject + subject; |
|
132 |
mergeSubject = true; |
|
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
if ( hasSameId(relationshipParentId) ) |
|
137 |
{ |
|
138 |
// relationship need to be combined |
|
139 |
String relationPrefix = getValueFromParentIdList(relationshipParentId); |
|
140 |
if (relationPrefix != null && !relationPrefix.equals(relationship)) |
|
141 |
{ |
|
142 |
MetaCatUtil.debugMessage("merge relation: "+ relationPrefix + |
|
143 |
relationship); |
|
144 |
relationship = relationPrefix + relationship; |
|
145 |
mergeRelation = true; |
|
146 |
} |
|
147 |
} |
|
148 |
|
|
149 |
if ( hasSameId(objectParentId) ) |
|
150 |
{ |
|
151 |
String objectPrefix = getValueFromParentIdList(objectParentId); |
|
152 |
if (objectPrefix != null && !objectPrefix.equals(object)) |
|
153 |
{ |
|
154 |
MetaCatUtil.debugMessage("merge object: "+ objectPrefix + object); |
|
155 |
object = objectPrefix + object; |
|
156 |
mergerObject = true; |
|
157 |
} |
|
158 |
} |
|
159 |
|
|
160 |
if (mergeSubject) |
|
161 |
{ |
|
162 |
removeEntryFromParentIdList(subjectParentId); |
|
163 |
} |
|
164 |
if (mergeRelation) |
|
165 |
{ |
|
166 |
removeEntryFromParentIdList(relationshipParentId); |
|
167 |
} |
|
168 |
if (mergerObject) |
|
169 |
{ |
|
170 |
removeEntryFromParentIdList(objectParentId); |
|
171 |
} |
|
172 |
// Store the parentids into parentId vector |
|
173 |
parentId.addElement(subjectParentId); |
|
174 |
parentId.addElement(relationshipParentId); |
|
175 |
parentId.addElement(objectParentId); |
|
176 |
// Store the value into sbjRelObj vector |
|
177 |
sbjReObj.addElement(subject); |
|
178 |
sbjReObj.addElement(relationship); |
|
179 |
sbjReObj.addElement(object); |
|
180 |
// Store the parent id and value into hashtable |
|
181 |
parentNodeIdList.put(parentId, sbjReObj); |
|
182 |
hasmorerows = rs.next(); |
|
183 |
} |
|
184 |
Enumeration en = parentNodeIdList.elements(); |
|
185 |
if (en.hasMoreElements()) { |
|
86 | 186 |
tstmt = connection.prepareStatement("INSERT INTO xml_relation (" + |
87 | 187 |
"docid,packagetype,subject,subdoctype," + |
88 | 188 |
"relationship, object, objdoctype) " + |
... | ... | |
90 | 190 |
//increase usage count |
91 | 191 |
connection.increaseUsageCount(1); |
92 | 192 |
} |
93 |
while(hasmorerows) { |
|
94 |
subject = rs.getString(1); |
|
95 |
relationship = rs.getString(2); |
|
96 |
object = rs.getString(3); |
|
97 |
|
|
193 |
|
|
194 |
while (en.hasMoreElements()) |
|
195 |
{ |
|
196 |
Vector values = (Vector)en.nextElement(); |
|
98 | 197 |
// cut out the revision number for subject and object |
99 |
subject = (new DocumentIdentifier(subject)).getIdentifier(); |
|
100 |
object = (new DocumentIdentifier(object)).getIdentifier(); |
|
101 |
|
|
198 |
subject = (new DocumentIdentifier((String)values.elementAt(SUBJECTINDEX))) |
|
199 |
.getIdentifier(); |
|
200 |
relationship = (String)values.elementAt(RELATIONINDEX); |
|
201 |
object = (new DocumentIdentifier((String)values.elementAt(OBJECTINDEX))) |
|
202 |
.getIdentifier(); |
|
203 |
|
|
102 | 204 |
//subDoctype and objDoctype are N/A |
103 | 205 |
subDoctype = null; |
104 | 206 |
objDoctype = null; |
105 |
|
|
106 | 207 |
//put the new relation into xml_relation |
107 | 208 |
tstmt.setString(1, docid); |
108 | 209 |
tstmt.setString(2, packagetype); |
... | ... | |
112 | 213 |
tstmt.setString(6, object); |
113 | 214 |
tstmt.setString(7, objDoctype); |
114 | 215 |
tstmt.execute(); |
115 |
|
|
116 |
hasmorerows = rs.next(); |
|
117 | 216 |
} |
118 |
|
|
119 | 217 |
if ( tstmt != null ) { |
120 | 218 |
tstmt.close(); |
121 | 219 |
} |
... | ... | |
123 | 221 |
connection.commit(); |
124 | 222 |
} |
125 | 223 |
|
224 |
/* |
|
225 |
* A method to check if a parentid is already in the paretnIdlist |
|
226 |
*/ |
|
227 |
public boolean hasSameId(String parentId) |
|
228 |
{ |
|
229 |
boolean hasSame = false; |
|
230 |
if (parentId == null || parentId.equals("")) |
|
231 |
{ |
|
232 |
return hasSame; |
|
233 |
} |
|
234 |
Enumeration en = parentNodeIdList.keys(); |
|
235 |
// go throught hashtable |
|
236 |
while (en.hasMoreElements()) |
|
237 |
{ |
|
238 |
Vector keys = (Vector)en.nextElement(); |
|
239 |
for (int i=0; i<keys.size(); i++) |
|
240 |
{ |
|
241 |
String obj = (String)keys.elementAt(i); |
|
242 |
if ( obj != null && obj.equals(parentId) ) |
|
243 |
{ |
|
244 |
hasSame = true; |
|
245 |
}//if |
|
246 |
}//for |
|
247 |
}//while |
|
248 |
return hasSame; |
|
249 |
} |
|
250 |
|
|
251 |
/* |
|
252 |
* Get a value from parentidlist for a given paretnIdlist |
|
253 |
*/ |
|
254 |
public String getValueFromParentIdList(String parentId) |
|
255 |
{ |
|
256 |
String value = null; |
|
257 |
int index = -1; // index in vector keys which has same parentid to |
|
258 |
// given id |
|
259 |
// Check the parameter |
|
260 |
if ( parentId == null || parentId.equals("") ) |
|
261 |
{ |
|
262 |
return value; |
|
263 |
} |
|
264 |
|
|
265 |
Enumeration en = parentNodeIdList.keys(); |
|
266 |
// go throught hashtable |
|
267 |
while (en.hasMoreElements()) |
|
268 |
{ |
|
269 |
Vector keys = (Vector)en.nextElement(); |
|
270 |
for (int i=0; i<keys.size(); i++) |
|
271 |
{ |
|
272 |
String obj = (String)keys.elementAt(i); |
|
273 |
if ( obj != null && obj.equals(parentId) ) |
|
274 |
{ |
|
275 |
index = i; |
|
276 |
}//if |
|
277 |
if (index != -1) |
|
278 |
{ |
|
279 |
// get the element vector to the key |
|
280 |
Vector element = (Vector)parentNodeIdList.get(keys); |
|
281 |
// get string |
|
282 |
value = (String)element.elementAt(index); |
|
283 |
} |
|
284 |
// reset index |
|
285 |
index = -1; |
|
286 |
}//for |
|
287 |
}//while |
|
288 |
MetaCatUtil.debugMessage("The value for given parent id " + parentId + |
|
289 |
"is " + value, 40); |
|
290 |
return value; |
|
291 |
} |
|
292 |
|
|
293 |
/* |
|
294 |
* A method to remove a entry in hashtable for given parentid |
|
295 |
*/ |
|
296 |
private void removeEntryFromParentIdList(String parentId) |
|
297 |
{ |
|
298 |
if (parentId==null || parentId.equals("")) |
|
299 |
{ |
|
300 |
return; |
|
301 |
} |
|
302 |
|
|
303 |
Enumeration en = parentNodeIdList.keys(); |
|
304 |
// go throught hashtable |
|
305 |
while (en.hasMoreElements()) |
|
306 |
{ |
|
307 |
Vector keys = (Vector)en.nextElement(); |
|
308 |
for (int i=0; i<keys.size(); i++) |
|
309 |
{ |
|
310 |
String obj = (String)keys.elementAt(i); |
|
311 |
if ( obj != null && obj.equals(parentId) ) |
|
312 |
{ |
|
313 |
parentNodeIdList.remove(keys); |
|
314 |
}//if |
|
315 |
|
|
316 |
}//for |
|
317 |
}//while |
|
318 |
} |
|
126 | 319 |
/** |
127 | 320 |
* Deletes all of the relations with a docid of 'docid'. |
128 | 321 |
* @param docid the docid of the package which relations to delete. |
Also available in: Unified diff
Code to handle text node was splited.