1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that represents an XML document
|
4
|
* Copyright: 2000 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Matt Jones
|
7
|
* Release: @release@
|
8
|
*
|
9
|
* '$Author: bojilova $'
|
10
|
* '$Date: 2000-08-31 17:01:30 -0700 (Thu, 31 Aug 2000) $'
|
11
|
* '$Revision: 425 $'
|
12
|
*/
|
13
|
|
14
|
package edu.ucsb.nceas.metacat;
|
15
|
|
16
|
import java.sql.*;
|
17
|
import java.io.PrintWriter;
|
18
|
import java.util.TreeSet;
|
19
|
|
20
|
import java.io.*;
|
21
|
import java.util.Stack;
|
22
|
|
23
|
import org.xml.sax.AttributeList;
|
24
|
import org.xml.sax.ContentHandler;
|
25
|
import org.xml.sax.DTDHandler;
|
26
|
import org.xml.sax.EntityResolver;
|
27
|
import org.xml.sax.ErrorHandler;
|
28
|
import org.xml.sax.InputSource;
|
29
|
import org.xml.sax.XMLReader;
|
30
|
import org.xml.sax.SAXException;
|
31
|
import org.xml.sax.SAXParseException;
|
32
|
import org.xml.sax.helpers.XMLReaderFactory;
|
33
|
|
34
|
/**
|
35
|
* A class that represents an XML document. It can be created with a simple
|
36
|
* document identifier from a database connection. It also will write an
|
37
|
* XML text document to a database connection using SAX.
|
38
|
*/
|
39
|
public class DocumentImpl {
|
40
|
|
41
|
static final int ALL = 1;
|
42
|
static final int WRITE = 2;
|
43
|
static final int READ = 4;
|
44
|
|
45
|
private Connection conn = null;
|
46
|
private String docid = null;
|
47
|
private String docname = null;
|
48
|
private String doctype = null;
|
49
|
private String system_id = null;
|
50
|
private long rootnodeid;
|
51
|
private ElementNode rootNode = null;
|
52
|
private String doctitle = null;
|
53
|
|
54
|
/**
|
55
|
* Constructor, creates document from database connection, used
|
56
|
* for reading the document
|
57
|
*
|
58
|
* @param conn the database connection from which to read the document
|
59
|
* @param docid the identifier of the document to be created
|
60
|
*/
|
61
|
public DocumentImpl(Connection conn, String docid) throws McdbException
|
62
|
{
|
63
|
try {
|
64
|
this.conn = conn;
|
65
|
this.docid = docid;
|
66
|
|
67
|
// Look up the document information
|
68
|
getDocumentInfo(docid);
|
69
|
|
70
|
// Download all of the document nodes using a single SQL query
|
71
|
TreeSet nodeRecordList = getNodeRecordList(rootnodeid);
|
72
|
|
73
|
// Create the elements from the downloaded data in the TreeSet
|
74
|
rootNode = new ElementNode(nodeRecordList, rootnodeid);
|
75
|
|
76
|
} catch (McdbException ex) {
|
77
|
throw ex;
|
78
|
} catch (Throwable t) {
|
79
|
throw new McdbException("Error reading document " + docid + ".");
|
80
|
}
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* Construct a new document instance, writing the contents to the database.
|
85
|
* This method is called from DBSAXHandler because we need to know the
|
86
|
* root element name for documents without a DOCTYPE before creating it.
|
87
|
*
|
88
|
* @param conn the JDBC Connection to which all information is written
|
89
|
* @param rootnodeid - sequence id of the root node in the document
|
90
|
* @param docname - the name of DTD, i.e. the name immediately following
|
91
|
* the DOCTYPE keyword ( should be the root element name ) or
|
92
|
* the root element name if no DOCTYPE declaration provided
|
93
|
* (Oracle's and IBM parsers are not aware if it is not the
|
94
|
* root element name)
|
95
|
* @param doctype - Public ID of the DTD, i.e. the name immediately
|
96
|
* following the PUBLIC keyword in DOCTYPE declaration or
|
97
|
* the docname if no Public ID provided or
|
98
|
* null if no DOCTYPE declaration provided
|
99
|
*
|
100
|
*/
|
101
|
public DocumentImpl(Connection conn, long rootnodeid, String docname,
|
102
|
String doctype, String docid, String action, String user)
|
103
|
throws AccessionNumberException
|
104
|
{
|
105
|
this.conn = conn;
|
106
|
this.rootnodeid = rootnodeid;
|
107
|
this.docname = docname;
|
108
|
this.doctype = doctype;
|
109
|
this.docid = docid;
|
110
|
writeDocumentToDB(action, user);
|
111
|
}
|
112
|
|
113
|
/**
|
114
|
* get the document name
|
115
|
*/
|
116
|
public String getDocname() {
|
117
|
return docname;
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* get the document type (which is the PublicID)
|
122
|
*/
|
123
|
public String getDoctype() {
|
124
|
return doctype;
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* get the system identifier
|
129
|
*/
|
130
|
public String getSystemID() {
|
131
|
return system_id;
|
132
|
}
|
133
|
|
134
|
/**
|
135
|
* get the root node identifier
|
136
|
*/
|
137
|
public long getRootNodeID() {
|
138
|
return rootnodeid;
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* Get the document identifier (docid)
|
143
|
*/
|
144
|
public String getDocID() {
|
145
|
return docid;
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* Create an XML document from the database for the document with ID docid
|
150
|
*/
|
151
|
public String toString()
|
152
|
{
|
153
|
StringBuffer doc = new StringBuffer();
|
154
|
|
155
|
// Append the resulting document to the StringBuffer and return it
|
156
|
doc.append("<?xml version=\"1.0\"?>\n");
|
157
|
|
158
|
if (docname != null) {
|
159
|
if ((doctype != null) && (system_id != null)) {
|
160
|
doc.append("<!DOCTYPE " + docname + " PUBLIC \"" + doctype +
|
161
|
"\" \"" + system_id + "\">\n");
|
162
|
} else {
|
163
|
doc.append("<!DOCTYPE " + docname + ">\n");
|
164
|
}
|
165
|
}
|
166
|
doc.append(rootNode.toString());
|
167
|
|
168
|
return (doc.toString());
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
* Look up the document type information from the database
|
173
|
*
|
174
|
* @param docid the id of the document to look up
|
175
|
*/
|
176
|
private void getDocumentInfo(String docid) throws McdbException
|
177
|
{
|
178
|
PreparedStatement pstmt;
|
179
|
|
180
|
try {
|
181
|
pstmt =
|
182
|
conn.prepareStatement("SELECT docname,doctype,rootnodeid " +
|
183
|
"FROM xml_documents " +
|
184
|
"WHERE docid = ?");
|
185
|
// Bind the values to the query
|
186
|
pstmt.setString(1, docid);
|
187
|
|
188
|
pstmt.execute();
|
189
|
ResultSet rs = pstmt.getResultSet();
|
190
|
boolean tableHasRows = rs.next();
|
191
|
if (tableHasRows) {
|
192
|
this.docname = rs.getString(1);
|
193
|
this.doctype = rs.getString(2);
|
194
|
this.rootnodeid = rs.getLong(3);
|
195
|
}
|
196
|
pstmt.close();
|
197
|
|
198
|
if (this.doctype != null) {
|
199
|
pstmt =
|
200
|
conn.prepareStatement("SELECT system_id " +
|
201
|
"FROM xml_catalog " +
|
202
|
"WHERE public_id = ?");
|
203
|
// Bind the values to the query
|
204
|
pstmt.setString(1, doctype);
|
205
|
|
206
|
pstmt.execute();
|
207
|
rs = pstmt.getResultSet();
|
208
|
tableHasRows = rs.next();
|
209
|
if (tableHasRows) {
|
210
|
this.system_id = rs.getString(1);
|
211
|
}
|
212
|
pstmt.close();
|
213
|
}
|
214
|
} catch (SQLException e) {
|
215
|
throw new McdbException("Error accessing database connection.", e);
|
216
|
}
|
217
|
|
218
|
if (this.docname == null) {
|
219
|
throw new McdbDocNotFoundException("Document not found: " + docid);
|
220
|
}
|
221
|
}
|
222
|
|
223
|
/**
|
224
|
* Look up the node data from the database
|
225
|
*
|
226
|
* @param rootnodeid the id of the root node of the node tree to look up
|
227
|
*/
|
228
|
private TreeSet getNodeRecordList(long rootnodeid) throws McdbException
|
229
|
{
|
230
|
PreparedStatement pstmt;
|
231
|
TreeSet nodeRecordList = new TreeSet(new NodeComparator());
|
232
|
long nodeid = 0;
|
233
|
long parentnodeid = 0;
|
234
|
long nodeindex = 0;
|
235
|
String nodetype = null;
|
236
|
String nodename = null;
|
237
|
String nodedata = null;
|
238
|
|
239
|
try {
|
240
|
pstmt =
|
241
|
conn.prepareStatement("SELECT nodeid,parentnodeid,nodeindex, " +
|
242
|
"nodetype,nodename,"+
|
243
|
"replace(" +
|
244
|
"replace(" +
|
245
|
"replace(nodedata,'&','&') " +
|
246
|
",'<','<') " +
|
247
|
",'>','>') " +
|
248
|
"FROM xml_nodes WHERE rootnodeid = ?");
|
249
|
|
250
|
// Bind the values to the query
|
251
|
pstmt.setLong(1, rootnodeid);
|
252
|
|
253
|
pstmt.execute();
|
254
|
ResultSet rs = pstmt.getResultSet();
|
255
|
boolean tableHasRows = rs.next();
|
256
|
while (tableHasRows) {
|
257
|
nodeid = rs.getLong(1);
|
258
|
parentnodeid = rs.getLong(2);
|
259
|
nodeindex = rs.getLong(3);
|
260
|
nodetype = rs.getString(4);
|
261
|
nodename = rs.getString(5);
|
262
|
nodedata = rs.getString(6);
|
263
|
|
264
|
// add the data to the node record list hashtable
|
265
|
NodeRecord currentRecord = new NodeRecord(nodeid, parentnodeid,
|
266
|
nodeindex, nodetype, nodename, nodedata);
|
267
|
nodeRecordList.add(currentRecord);
|
268
|
|
269
|
// Advance to the next node
|
270
|
tableHasRows = rs.next();
|
271
|
}
|
272
|
pstmt.close();
|
273
|
|
274
|
} catch (SQLException e) {
|
275
|
throw new McdbException("Error accessing database connection.", e);
|
276
|
}
|
277
|
|
278
|
if (nodeRecordList != null) {
|
279
|
return nodeRecordList;
|
280
|
} else {
|
281
|
throw new McdbException("Error getting node data: " + docid);
|
282
|
}
|
283
|
}
|
284
|
|
285
|
/**
|
286
|
* Write an XML file to the database, given a filename
|
287
|
*
|
288
|
* @param conn the JDBC connection to the database
|
289
|
* @param filename the filename to be loaded into the database
|
290
|
* @param action the action to be performed (INSERT OR UPDATE)
|
291
|
* @param docid the docid to use for the INSERT OR UPDATE
|
292
|
*/
|
293
|
public static String write( Connection conn, String filename, String action,
|
294
|
String docid, String user, String group )
|
295
|
throws IOException, SQLException, ClassNotFoundException,
|
296
|
SAXException, SAXParseException, Exception {
|
297
|
|
298
|
return write(conn, new FileReader(new File(filename).toString()),
|
299
|
action, docid, user, group);
|
300
|
}
|
301
|
|
302
|
/**
|
303
|
* Write an XML file to the database, given a Reader
|
304
|
*
|
305
|
* @param conn the JDBC connection to the database
|
306
|
* @param xml the xml stream to be loaded into the database
|
307
|
* @param action the action to be performed (INSERT OR UPDATE)
|
308
|
* @param docid the docid to use for the INSERT OR UPDATE
|
309
|
*/
|
310
|
public static String write( Connection conn, Reader xml, String action,
|
311
|
String docid, String user, String group )
|
312
|
throws IOException, SQLException, ClassNotFoundException,
|
313
|
SAXException, SAXParseException, Exception {
|
314
|
|
315
|
if ( action.equals("UPDATE") ) {
|
316
|
// Determine if the docid is OK for an UPDATE
|
317
|
AccessionNumber ac = new AccessionNumber();
|
318
|
String newdocid = ac.generate(docid, "UPDATE");
|
319
|
|
320
|
// b' of the command line invocation
|
321
|
if ( (user != null) && (group != null) ) {
|
322
|
if ( !hasWritePermission(conn, docid, user, group) ) {
|
323
|
throw new Exception("User " + user +
|
324
|
" does not have permission to update XML Document #" + docid);
|
325
|
}
|
326
|
}
|
327
|
}
|
328
|
|
329
|
try {
|
330
|
XMLReader parser = initializeParser(conn, action, docid, user);
|
331
|
conn.setAutoCommit(false);
|
332
|
parser.parse(new InputSource(xml));
|
333
|
conn.commit();
|
334
|
conn.setAutoCommit(true);
|
335
|
return docid;
|
336
|
} catch (SAXParseException e) {
|
337
|
conn.rollback();
|
338
|
throw e;
|
339
|
} catch (SAXException e) {
|
340
|
|
341
|
// If its a problem with the accession number its ok, just the
|
342
|
// accession number was regenerated
|
343
|
AccessionNumberGeneratedException ang = null;
|
344
|
try {
|
345
|
Exception embedded = e.getException();
|
346
|
if ((embedded != null) &&
|
347
|
(embedded instanceof AccessionNumberGeneratedException)) {
|
348
|
ang = (AccessionNumberGeneratedException)e.getException();
|
349
|
}
|
350
|
} catch (ClassCastException cce) {
|
351
|
// Do nothing and just fall through to the ang != null test
|
352
|
}
|
353
|
if (ang != null) {
|
354
|
conn.commit();
|
355
|
conn.setAutoCommit(true);
|
356
|
return (ang.getMessage());
|
357
|
} else {
|
358
|
conn.rollback();
|
359
|
throw e;
|
360
|
}
|
361
|
} catch (Exception e) {
|
362
|
conn.rollback();
|
363
|
throw e;
|
364
|
}
|
365
|
}
|
366
|
|
367
|
/**
|
368
|
* Delete an XML file from the database (actually, just make it a revision
|
369
|
* in the xml_revisions table)
|
370
|
*
|
371
|
* @param docid the ID of the document to be deleted from the database
|
372
|
*/
|
373
|
public static void delete( Connection conn, String docid,
|
374
|
String user, String group )
|
375
|
throws IOException, SQLException, ClassNotFoundException,
|
376
|
AccessionNumberException, Exception {
|
377
|
|
378
|
AccessionNumber ac = new AccessionNumber();
|
379
|
String newdocid = ac.generate(docid, "DELETE");
|
380
|
|
381
|
if ( (user != null) && (group != null) ) {
|
382
|
if ( !hasWritePermission(conn, docid, user, group) ) {
|
383
|
throw new Exception("User " + user +
|
384
|
" does not have permission to delete XML Document #" + docid);
|
385
|
}
|
386
|
}
|
387
|
|
388
|
conn.setAutoCommit(false);
|
389
|
// Copy the record to the xml_revisions table
|
390
|
DocumentImpl.archiveDocRevision( conn, docid, user );
|
391
|
|
392
|
// Now delete it from the xml_documents table
|
393
|
Statement stmt = conn.createStatement();
|
394
|
stmt.execute("DELETE FROM xml_index WHERE docid = '" + docid + "'");
|
395
|
stmt.execute("DELETE FROM xml_documents WHERE docid = '" + docid + "'");
|
396
|
stmt.close();
|
397
|
conn.commit();
|
398
|
}
|
399
|
|
400
|
/** Check for "write" permissions from DB connection */
|
401
|
private static boolean hasWritePermission(Connection conn, String docid,
|
402
|
String user, String group)
|
403
|
throws SQLException {
|
404
|
PreparedStatement pstmt;
|
405
|
// checking if user is owner of docid
|
406
|
try {
|
407
|
pstmt = conn.prepareStatement(
|
408
|
"SELECT docid FROM xml_documents " +
|
409
|
"WHERE docid LIKE ? AND user_owner LIKE ?");
|
410
|
// Bind the values to the query
|
411
|
pstmt.setString(1, docid);
|
412
|
pstmt.setString(2, user);
|
413
|
|
414
|
pstmt.execute();
|
415
|
ResultSet rs = pstmt.getResultSet();
|
416
|
boolean hasRow = rs.next();
|
417
|
pstmt.close();
|
418
|
if (hasRow) {
|
419
|
return true;
|
420
|
}
|
421
|
|
422
|
} catch (SQLException e) {
|
423
|
throw new
|
424
|
SQLException("Error getting document's owner: " + e.getMessage());
|
425
|
}
|
426
|
|
427
|
// checking access type from xml_access table
|
428
|
int accesstype = 0;
|
429
|
try {
|
430
|
pstmt = conn.prepareStatement(
|
431
|
"SELECT access_type FROM xml_access " +
|
432
|
"WHERE docid LIKE ? " +
|
433
|
"AND principal_name LIKE ? " +
|
434
|
"AND principal_type = 'user' " +
|
435
|
"AND sysdate BETWEEN begin_time AND end_time " +
|
436
|
"UNION " +
|
437
|
"SELECT access_type FROM xml_access " +
|
438
|
"WHERE docid LIKE ? " +
|
439
|
"AND principal_name LIKE ? " +
|
440
|
"AND principal_type = 'group' " +
|
441
|
"AND sysdate BETWEEN begin_time AND end_time");
|
442
|
// Bind the values to the query
|
443
|
pstmt.setString(1, docid);
|
444
|
pstmt.setString(2, user);
|
445
|
pstmt.setString(3, docid);
|
446
|
pstmt.setString(2, group);
|
447
|
|
448
|
pstmt.execute();
|
449
|
ResultSet rs = pstmt.getResultSet();
|
450
|
boolean hasRows = rs.next();
|
451
|
if ( !hasRows ) {
|
452
|
pstmt.close();
|
453
|
return false;
|
454
|
}
|
455
|
|
456
|
accesstype = rs.getInt(1);
|
457
|
pstmt.close();
|
458
|
|
459
|
accesstype = accesstype | WRITE;
|
460
|
if ( (accesstype & WRITE) == WRITE ) {
|
461
|
return true;
|
462
|
}
|
463
|
|
464
|
return false;
|
465
|
|
466
|
} catch (SQLException e) {
|
467
|
throw new
|
468
|
SQLException("Error getting document's permissions: " + e.getMessage());
|
469
|
}
|
470
|
}
|
471
|
|
472
|
/** creates SQL code and inserts new document into DB connection */
|
473
|
private void writeDocumentToDB(String action, String user)
|
474
|
throws AccessionNumberException {
|
475
|
try {
|
476
|
PreparedStatement pstmt = null;
|
477
|
|
478
|
if (action.equals("INSERT")) {
|
479
|
AccessionNumber ac = new AccessionNumber();
|
480
|
this.docid = ac.generate(docid, "INSERT");
|
481
|
pstmt = conn.prepareStatement(
|
482
|
"INSERT INTO xml_documents " +
|
483
|
"(docid, rootnodeid, docname, doctype, " +
|
484
|
"user_owner, user_updated, date_created, date_updated) " +
|
485
|
"VALUES (?, ?, ?, ?, ?, ?, sysdate, sysdate)");
|
486
|
// Bind the values to the query
|
487
|
pstmt.setString(1, this.docid);
|
488
|
pstmt.setLong(2, rootnodeid);
|
489
|
pstmt.setString(3, docname);
|
490
|
pstmt.setString(4, doctype);
|
491
|
pstmt.setString(5, user);
|
492
|
pstmt.setString(6, user);
|
493
|
} else if (action.equals("UPDATE")) {
|
494
|
|
495
|
// moved the check earlier before start of parsing
|
496
|
// // Determine if the docid is OK for an UPDATE
|
497
|
// AccessionNumber ac = new AccessionNumber();
|
498
|
// this.docid = ac.generate(docid, "UPDATE");
|
499
|
|
500
|
// Save the old document entry in a backup table
|
501
|
DocumentImpl.archiveDocRevision( conn, docid, user );
|
502
|
|
503
|
// Delete index for the old version of docid
|
504
|
// The new index is inserting on the next calls to DBSAXNode
|
505
|
pstmt = conn.prepareStatement(
|
506
|
"DELETE FROM xml_index WHERE docid='" + this.docid + "'");
|
507
|
pstmt.execute();
|
508
|
pstmt.close();
|
509
|
|
510
|
// Update the new document to reflect the new node tree
|
511
|
pstmt = conn.prepareStatement(
|
512
|
"UPDATE xml_documents " +
|
513
|
"SET rootnodeid = ?, docname = ?, doctype = ?, " +
|
514
|
"user_updated = ?, date_updated = sysdate WHERE docid LIKE ?");
|
515
|
// Bind the values to the query
|
516
|
pstmt.setLong(1, rootnodeid);
|
517
|
pstmt.setString(2, docname);
|
518
|
pstmt.setString(3, doctype);
|
519
|
pstmt.setString(4, user);
|
520
|
pstmt.setString(5, this.docid);
|
521
|
} else {
|
522
|
System.err.println("Action not supported: " + action);
|
523
|
}
|
524
|
|
525
|
// Do the insertion
|
526
|
pstmt.execute();
|
527
|
pstmt.close();
|
528
|
|
529
|
} catch (SQLException e) {
|
530
|
System.out.println(e.getMessage());
|
531
|
} catch (AccessionNumberException ane) {
|
532
|
MetaCatUtil.debugMessage("Invalid accession number.");
|
533
|
MetaCatUtil.debugMessage(ane.getMessage());
|
534
|
throw ane;
|
535
|
} catch (Exception e) {
|
536
|
System.out.println(e.getMessage());
|
537
|
}
|
538
|
}
|
539
|
|
540
|
/**
|
541
|
* Get the document title
|
542
|
*/
|
543
|
public String getTitle() {
|
544
|
return doctitle;
|
545
|
}
|
546
|
|
547
|
/**
|
548
|
* Set the document title
|
549
|
*
|
550
|
* @param title the new title for the document
|
551
|
*/
|
552
|
public void setTitle( String title ) {
|
553
|
this.doctitle = title;
|
554
|
try {
|
555
|
PreparedStatement pstmt;
|
556
|
pstmt = conn.prepareStatement(
|
557
|
"UPDATE xml_documents " +
|
558
|
" SET doctitle = ? " +
|
559
|
"WHERE docid = ?");
|
560
|
|
561
|
// Bind the values to the query
|
562
|
pstmt.setString(1, doctitle);
|
563
|
pstmt.setString(2, docid);
|
564
|
|
565
|
// Do the insertion
|
566
|
pstmt.execute();
|
567
|
pstmt.close();
|
568
|
} catch (SQLException e) {
|
569
|
System.out.println(e.getMessage());
|
570
|
}
|
571
|
}
|
572
|
|
573
|
/**
|
574
|
* Look up the title of the first child element named "title"
|
575
|
* and record it as the document title
|
576
|
*/
|
577
|
public void setTitleFromChildElement() {
|
578
|
String title = null;
|
579
|
long assigned_id=0;
|
580
|
PreparedStatement pstmt;
|
581
|
try {
|
582
|
pstmt = conn.prepareStatement(
|
583
|
"SELECT nodedata FROM xml_nodes " +
|
584
|
"WHERE nodetype = 'TEXT' " +
|
585
|
"AND rootnodeid = ? " +
|
586
|
"AND parentnodeid IN " +
|
587
|
" (SELECT nodeid FROM xml_nodes " +
|
588
|
" WHERE nodename = 'title' " +
|
589
|
" AND nodetype = 'ELEMENT' " +
|
590
|
" AND rootnodeid = ? ) " +
|
591
|
"ORDER BY nodeid");
|
592
|
|
593
|
// The above query might be slow, and probably will be because
|
594
|
// it gets ALL of the title elements while searching for one
|
595
|
// title in a small subtree but it avoids the problem of using
|
596
|
// Oracle's Hierarchical Query syntax which is not portable --
|
597
|
// the commented out SQL that follows shows an equivalent query
|
598
|
// using Oracle-specific hierarchical query
|
599
|
/*
|
600
|
pstmt = conn.prepareStatement(
|
601
|
"SELECT nodedata FROM xml_nodes " +
|
602
|
"WHERE nodetype = 'TEXT' " +
|
603
|
"AND parentnodeid IN " +
|
604
|
"(SELECT nodeid FROM xml_nodes " +
|
605
|
"WHERE nodename = 'title' " +
|
606
|
"START WITH nodeid = ? " +
|
607
|
"CONNECT BY PRIOR nodeid = parentnodeid)");
|
608
|
*/
|
609
|
|
610
|
// Bind the values to the query
|
611
|
pstmt.setLong(1, rootnodeid);
|
612
|
pstmt.setLong(2, rootnodeid);
|
613
|
|
614
|
pstmt.execute();
|
615
|
ResultSet rs = pstmt.getResultSet();
|
616
|
boolean tableHasRows = rs.next();
|
617
|
if (tableHasRows) {
|
618
|
title = rs.getString(1);
|
619
|
}
|
620
|
pstmt.close();
|
621
|
} catch (SQLException e) {
|
622
|
System.out.println("Error getting title: " + e.getMessage());
|
623
|
}
|
624
|
|
625
|
// assign the new title
|
626
|
this.setTitle(title);
|
627
|
}
|
628
|
|
629
|
/** Save a document entry in the xml_revisions table */
|
630
|
private static void archiveDocRevision(Connection conn, String docid,
|
631
|
String user) throws SQLException {
|
632
|
// First get all of the values we need
|
633
|
long rnodeid = -1;
|
634
|
String docname = null;
|
635
|
String doctype = null;
|
636
|
String doctitle = null;
|
637
|
String user_owner = null;
|
638
|
Date date_created = null;
|
639
|
PreparedStatement pstmt = conn.prepareStatement(
|
640
|
"SELECT rootnodeid,docname,doctype,doctitle,user_owner,date_created " +
|
641
|
"FROM xml_documents " +
|
642
|
"WHERE docid = ?");
|
643
|
// Bind the values to the query and execute it
|
644
|
pstmt.setString(1, docid);
|
645
|
pstmt.execute();
|
646
|
|
647
|
ResultSet rs = pstmt.getResultSet();
|
648
|
boolean tableHasRows = rs.next();
|
649
|
if (tableHasRows) {
|
650
|
rnodeid = rs.getLong(1);
|
651
|
docname = rs.getString(2);
|
652
|
doctype = rs.getString(3);
|
653
|
doctitle = rs.getString(4);
|
654
|
user_owner = rs.getString(5);
|
655
|
date_created = rs.getDate(6);
|
656
|
}
|
657
|
pstmt.close();
|
658
|
|
659
|
MetaCatUtil.debugMessage(new Long(rnodeid).toString());
|
660
|
MetaCatUtil.debugMessage(docname);
|
661
|
MetaCatUtil.debugMessage(doctitle);
|
662
|
//MetaCatUtil.debugMessage(date_created.toString());
|
663
|
|
664
|
// Next create the new record in the other table using the values selected
|
665
|
pstmt = conn.prepareStatement(
|
666
|
"INSERT INTO xml_revisions " +
|
667
|
"(revisionid, docid, rootnodeid, docname, doctype, doctitle, " +
|
668
|
"user_owner, user_updated, date_created, date_updated) " +
|
669
|
"VALUES (null, ?, ?, ?, ?, ?, ?, ?, sysdate, sysdate)");
|
670
|
// Bind the values to the query and execute it
|
671
|
pstmt.setString(1, docid);
|
672
|
pstmt.setLong(2, rnodeid);
|
673
|
pstmt.setString(3, docname);
|
674
|
pstmt.setString(4, doctype);
|
675
|
pstmt.setString(5, doctitle);
|
676
|
pstmt.setString(6, user_owner);
|
677
|
pstmt.setString(7, user);
|
678
|
//pstmt.setDate(6, date_created);
|
679
|
pstmt.execute();
|
680
|
pstmt.close();
|
681
|
}
|
682
|
|
683
|
/**
|
684
|
* Set up the parser handlers for writing the document to the database
|
685
|
*/
|
686
|
private static XMLReader initializeParser(Connection conn,
|
687
|
String action, String docid, String user) {
|
688
|
XMLReader parser = null;
|
689
|
//
|
690
|
// Set up the SAX document handlers for parsing
|
691
|
//
|
692
|
try {
|
693
|
ContentHandler chandler = new DBSAXHandler(conn, action, docid, user);
|
694
|
EntityResolver dbresolver = new DBEntityResolver(conn,
|
695
|
(DBSAXHandler)chandler);
|
696
|
DTDHandler dtdhandler = new DBDTDHandler(conn);
|
697
|
|
698
|
// Get an instance of the parser
|
699
|
MetaCatUtil util = new MetaCatUtil();
|
700
|
String parserName = util.getOption("saxparser");
|
701
|
parser = XMLReaderFactory.createXMLReader(parserName);
|
702
|
|
703
|
// Turn off validation
|
704
|
parser.setFeature("http://xml.org/sax/features/validation", false);
|
705
|
|
706
|
// Set Handlers in the parser
|
707
|
parser.setProperty("http://xml.org/sax/properties/declaration-handler",
|
708
|
chandler);
|
709
|
parser.setProperty("http://xml.org/sax/properties/lexical-handler",
|
710
|
chandler);
|
711
|
parser.setContentHandler((ContentHandler)chandler);
|
712
|
parser.setEntityResolver((EntityResolver)dbresolver);
|
713
|
parser.setDTDHandler((DTDHandler)dtdhandler);
|
714
|
parser.setErrorHandler((ErrorHandler)chandler);
|
715
|
|
716
|
} catch (Exception e) {
|
717
|
System.err.println(e.toString());
|
718
|
}
|
719
|
|
720
|
return parser;
|
721
|
}
|
722
|
|
723
|
/**
|
724
|
* the main routine used to test the DBWriter utility.
|
725
|
* <p>
|
726
|
* Usage: java DocumentImpl <-f filename -a action -d docid>
|
727
|
*
|
728
|
* @param filename the filename to be loaded into the database
|
729
|
* @param action the action to perform (READ, INSERT, UPDATE, DELETE)
|
730
|
* @param docid the id of the document to process
|
731
|
*/
|
732
|
static public void main(String[] args) {
|
733
|
|
734
|
try {
|
735
|
String filename = null;
|
736
|
String action = null;
|
737
|
String docid = null;
|
738
|
|
739
|
// Parse the command line arguments
|
740
|
for ( int i=0 ; i < args.length; ++i ) {
|
741
|
if ( args[i].equals( "-f" ) ) {
|
742
|
filename = args[++i];
|
743
|
} else if ( args[i].equals( "-a" ) ) {
|
744
|
action = args[++i];
|
745
|
} else if ( args[i].equals( "-d" ) ) {
|
746
|
docid = args[++i];
|
747
|
} else {
|
748
|
System.err.println
|
749
|
( " args[" +i+ "] '" +args[i]+ "' ignored." );
|
750
|
}
|
751
|
}
|
752
|
|
753
|
// Check if the required arguments are provided
|
754
|
boolean argsAreValid = false;
|
755
|
if (action != null) {
|
756
|
if (action.equals("INSERT")) {
|
757
|
if (filename != null) {
|
758
|
argsAreValid = true;
|
759
|
}
|
760
|
} else if (action.equals("UPDATE")) {
|
761
|
if ((filename != null) && (docid != null)) {
|
762
|
argsAreValid = true;
|
763
|
}
|
764
|
} else if (action.equals("DELETE")) {
|
765
|
if (docid != null) {
|
766
|
argsAreValid = true;
|
767
|
}
|
768
|
} else if (action.equals("READ")) {
|
769
|
if (docid != null) {
|
770
|
argsAreValid = true;
|
771
|
}
|
772
|
}
|
773
|
}
|
774
|
|
775
|
// Print usage message if the arguments are not valid
|
776
|
if (!argsAreValid) {
|
777
|
System.err.println("Wrong number of arguments!!!");
|
778
|
System.err.println(
|
779
|
"USAGE: java DocumentImpl <-a INSERT> [-d docid] <-f filename>");
|
780
|
System.err.println(
|
781
|
" OR: java DocumentImpl <-a UPDATE -d docid -f filename>");
|
782
|
System.err.println(
|
783
|
" OR: java DocumentImpl <-a DELETE -d docid>");
|
784
|
System.err.println(
|
785
|
" OR: java DocumentImpl <-a READ -d docid>");
|
786
|
return;
|
787
|
}
|
788
|
|
789
|
// Open a connection to the database
|
790
|
MetaCatUtil util = new MetaCatUtil();
|
791
|
Connection dbconn = util.openDBConnection();
|
792
|
|
793
|
// Execute the action requested (READ, INSERT, UPDATE, DELETE)
|
794
|
if (action.equals("READ")) {
|
795
|
DocumentImpl xmldoc = new DocumentImpl( dbconn, docid );
|
796
|
System.out.println(xmldoc.toString());
|
797
|
} else if (action.equals("DELETE")) {
|
798
|
DocumentImpl.delete(dbconn, docid, null, null);
|
799
|
System.out.println("Document deleted: " + docid);
|
800
|
} else {
|
801
|
String newdocid = DocumentImpl.write(dbconn, filename, action, docid,
|
802
|
null, null);
|
803
|
if ((docid != null) && (!docid.equals(newdocid))) {
|
804
|
if (action.equals("INSERT")) {
|
805
|
System.out.println("New document ID generated!!! ");
|
806
|
} else if (action.equals("UPDATE")) {
|
807
|
System.out.println("ERROR: Couldn't update document!!! ");
|
808
|
}
|
809
|
} else if ((docid == null) && (action.equals("UPDATE"))) {
|
810
|
System.out.println("ERROR: Couldn't update document!!! ");
|
811
|
}
|
812
|
System.out.println("Document processing finished for: " + filename
|
813
|
+ " (" + newdocid + ")");
|
814
|
}
|
815
|
|
816
|
} catch (McdbException me) {
|
817
|
me.toXml(new PrintWriter(System.err));
|
818
|
} catch (AccessionNumberException ane) {
|
819
|
System.out.println("ERROR: Couldn't delete document!!! ");
|
820
|
System.out.println(ane.getMessage());
|
821
|
} catch (Exception e) {
|
822
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
823
|
System.err.println(e.getMessage());
|
824
|
e.printStackTrace(System.err);
|
825
|
}
|
826
|
}
|
827
|
}
|