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: 2001-01-31 10:05:23 -0800 (Wed, 31 Jan 2001) $'
|
11
|
* '$Revision: 688 $'
|
12
|
*
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26
|
*/
|
27
|
|
28
|
package edu.ucsb.nceas.metacat;
|
29
|
|
30
|
import java.sql.*;
|
31
|
import java.io.File;
|
32
|
import java.io.FileReader;
|
33
|
import java.io.IOException;
|
34
|
import java.io.PrintWriter;
|
35
|
import java.io.Reader;
|
36
|
import java.io.StringWriter;
|
37
|
import java.io.Writer;
|
38
|
import java.io.InputStreamReader;
|
39
|
|
40
|
import java.util.Iterator;
|
41
|
import java.util.Stack;
|
42
|
import java.util.TreeSet;
|
43
|
import java.util.Enumeration;
|
44
|
|
45
|
import org.xml.sax.AttributeList;
|
46
|
import org.xml.sax.ContentHandler;
|
47
|
import org.xml.sax.DTDHandler;
|
48
|
import org.xml.sax.EntityResolver;
|
49
|
import org.xml.sax.ErrorHandler;
|
50
|
import org.xml.sax.InputSource;
|
51
|
import org.xml.sax.XMLReader;
|
52
|
import org.xml.sax.SAXException;
|
53
|
import org.xml.sax.SAXParseException;
|
54
|
import org.xml.sax.helpers.XMLReaderFactory;
|
55
|
|
56
|
import java.net.URL;
|
57
|
|
58
|
/**
|
59
|
* A class that represents an XML document. It can be created with a simple
|
60
|
* document identifier from a database connection. It also will write an
|
61
|
* XML text document to a database connection using SAX.
|
62
|
*/
|
63
|
public class DocumentImpl {
|
64
|
|
65
|
static final int ALL = 1;
|
66
|
static final int WRITE = 2;
|
67
|
static final int READ = 4;
|
68
|
|
69
|
private Connection conn = null;
|
70
|
private String docid = null;
|
71
|
private String docname = null;
|
72
|
private String doctype = null;
|
73
|
private String doctitle = null;
|
74
|
private String createdate = null;
|
75
|
private String updatedate = null;
|
76
|
private String system_id = null;
|
77
|
private String userowner = null;
|
78
|
private String userupdated = null;
|
79
|
private int rev;
|
80
|
private int serverlocation;
|
81
|
private int publicaccess;
|
82
|
private long rootnodeid;
|
83
|
private ElementNode rootNode = null;
|
84
|
private TreeSet nodeRecordList = null;
|
85
|
|
86
|
/**
|
87
|
* Constructor, creates document from database connection, used
|
88
|
* for reading the document
|
89
|
*
|
90
|
* @param conn the database connection from which to read the document
|
91
|
* @param docid the identifier of the document to be created
|
92
|
*/
|
93
|
public DocumentImpl(Connection conn, String docid) throws McdbException
|
94
|
{
|
95
|
try {
|
96
|
this.conn = conn;
|
97
|
this.docid = docid;
|
98
|
|
99
|
DocumentIdentifier id = new DocumentIdentifier(docid);
|
100
|
|
101
|
|
102
|
// Look up the document information
|
103
|
getDocumentInfo(docid);
|
104
|
|
105
|
// Download all of the document nodes using a single SQL query
|
106
|
// The sort order of the records is determined by the NodeComparator
|
107
|
// class, and needs to represent a depth-first traversal for the
|
108
|
// toXml() method to work properly
|
109
|
nodeRecordList = getNodeRecordList(rootnodeid);
|
110
|
|
111
|
} catch (McdbException ex) {
|
112
|
throw ex;
|
113
|
} catch (Throwable t) {
|
114
|
throw new McdbException("Error reading document from " +
|
115
|
"DocumentImpl.DocumentImpl: " + docid);
|
116
|
}
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Construct a new document instance, writing the contents to the database.
|
121
|
* This method is called from DBSAXHandler because we need to know the
|
122
|
* root element name for documents without a DOCTYPE before creating it.
|
123
|
*
|
124
|
* @param conn the JDBC Connection to which all information is written
|
125
|
* @param rootnodeid - sequence id of the root node in the document
|
126
|
* @param docname - the name of DTD, i.e. the name immediately following
|
127
|
* the DOCTYPE keyword ( should be the root element name ) or
|
128
|
* the root element name if no DOCTYPE declaration provided
|
129
|
* (Oracle's and IBM parsers are not aware if it is not the
|
130
|
* root element name)
|
131
|
* @param doctype - Public ID of the DTD, i.e. the name immediately
|
132
|
* following the PUBLIC keyword in DOCTYPE declaration or
|
133
|
* the docname if no Public ID provided or
|
134
|
* null if no DOCTYPE declaration provided
|
135
|
* @param docid the docid to use for the INSERT OR UPDATE
|
136
|
* @param action the action to be performed (INSERT OR UPDATE)
|
137
|
* @param user the user that owns the document
|
138
|
* @param pub flag for public "read" access on document
|
139
|
* @param serverCode the serverid from xml_replication on which this document
|
140
|
* resides.
|
141
|
*
|
142
|
*/
|
143
|
public DocumentImpl(Connection conn, long rootnodeid, String docname,
|
144
|
String doctype, String docid, String action, String user,
|
145
|
String pub, int serverCode)
|
146
|
throws SQLException, Exception
|
147
|
{
|
148
|
this.conn = conn;
|
149
|
this.rootnodeid = rootnodeid;
|
150
|
this.docname = docname;
|
151
|
this.doctype = doctype;
|
152
|
this.docid = docid;
|
153
|
writeDocumentToDB(action, user, pub, serverCode);
|
154
|
}
|
155
|
|
156
|
// NOT USED ANY MORE
|
157
|
// public DocumentImpl(Connection conn, long rootnodeid, String docname,
|
158
|
// String doctype, String docid, String action, String user)
|
159
|
// throws SQLException, Exception
|
160
|
// {
|
161
|
// this.conn = conn;
|
162
|
// this.rootnodeid = rootnodeid;
|
163
|
// this.docname = docname;
|
164
|
// this.doctype = doctype;
|
165
|
// this.docid = docid;
|
166
|
// writeDocumentToDB(action, user);
|
167
|
// }
|
168
|
|
169
|
/**
|
170
|
* get the document name
|
171
|
*/
|
172
|
public String getDocname() {
|
173
|
return docname;
|
174
|
}
|
175
|
|
176
|
/**
|
177
|
* get the document type (which is the PublicID)
|
178
|
*/
|
179
|
public String getDoctype() {
|
180
|
return doctype;
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* get the system identifier
|
185
|
*/
|
186
|
public String getSystemID() {
|
187
|
return system_id;
|
188
|
}
|
189
|
|
190
|
/**
|
191
|
* get the root node identifier
|
192
|
*/
|
193
|
public long getRootNodeID() {
|
194
|
return rootnodeid;
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* get the creation date
|
199
|
*/
|
200
|
public String getCreateDate() {
|
201
|
return createdate;
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* get the update date
|
206
|
*/
|
207
|
public String getUpdateDate() {
|
208
|
return updatedate;
|
209
|
}
|
210
|
|
211
|
/**
|
212
|
* Get the document identifier (docid)
|
213
|
*/
|
214
|
public String getDocID() {
|
215
|
return docid;
|
216
|
}
|
217
|
|
218
|
/**
|
219
|
*get the document title
|
220
|
*/
|
221
|
public String getDocTitle() {
|
222
|
return doctitle;
|
223
|
}
|
224
|
|
225
|
public String getUserowner() {
|
226
|
return userowner;
|
227
|
}
|
228
|
|
229
|
public String getUserupdated() {
|
230
|
return userupdated;
|
231
|
}
|
232
|
|
233
|
public int getServerlocation() {
|
234
|
return serverlocation;
|
235
|
}
|
236
|
|
237
|
public int getPublicaccess() {
|
238
|
return publicaccess;
|
239
|
}
|
240
|
|
241
|
public int getRev() {
|
242
|
return rev;
|
243
|
}
|
244
|
|
245
|
/**
|
246
|
* Print a string representation of the XML document
|
247
|
*/
|
248
|
public String toString()
|
249
|
{
|
250
|
StringWriter docwriter = new StringWriter();
|
251
|
this.toXml(docwriter);
|
252
|
String document = docwriter.toString();
|
253
|
return document;
|
254
|
}
|
255
|
|
256
|
/**
|
257
|
* Get a text representation of the XML document as a string
|
258
|
* This older algorithm uses a recursive tree of Objects to represent the
|
259
|
* nodes of the tree. Each object is passed the data for the document
|
260
|
* and searches all of the document data to find its children nodes and
|
261
|
* recursively build. Thus, because each node reads the whole document,
|
262
|
* this algorithm is extremely slow for larger documents, and the time
|
263
|
* to completion is O(N^N) wrt the number of nodes. See toXml() for a
|
264
|
* better algorithm.
|
265
|
*/
|
266
|
public String readUsingSlowAlgorithm()
|
267
|
{
|
268
|
StringBuffer doc = new StringBuffer();
|
269
|
|
270
|
// Create the elements from the downloaded data in the TreeSet
|
271
|
rootNode = new ElementNode(nodeRecordList, rootnodeid);
|
272
|
|
273
|
// Append the resulting document to the StringBuffer and return it
|
274
|
doc.append("<?xml version=\"1.0\"?>\n");
|
275
|
|
276
|
if (docname != null) {
|
277
|
if ((doctype != null) && (system_id != null)) {
|
278
|
doc.append("<!DOCTYPE " + docname + " PUBLIC \"" + doctype +
|
279
|
"\" \"" + system_id + "\">\n");
|
280
|
} else {
|
281
|
doc.append("<!DOCTYPE " + docname + ">\n");
|
282
|
}
|
283
|
}
|
284
|
doc.append(rootNode.toString());
|
285
|
|
286
|
return (doc.toString());
|
287
|
}
|
288
|
|
289
|
/**
|
290
|
* Print a text representation of the XML document to a Writer
|
291
|
*
|
292
|
* @param pw the Writer to which we print the document
|
293
|
*/
|
294
|
public void toXml(Writer pw)
|
295
|
{
|
296
|
PrintWriter out = null;
|
297
|
if (pw instanceof PrintWriter) {
|
298
|
out = (PrintWriter)pw;
|
299
|
} else {
|
300
|
out = new PrintWriter(pw);
|
301
|
}
|
302
|
|
303
|
MetaCatUtil util = new MetaCatUtil();
|
304
|
|
305
|
Stack openElements = new Stack();
|
306
|
boolean atRootElement = true;
|
307
|
boolean previousNodeWasElement = false;
|
308
|
|
309
|
// Step through all of the node records we were given
|
310
|
Iterator it = nodeRecordList.iterator();
|
311
|
while (it.hasNext()) {
|
312
|
NodeRecord currentNode = (NodeRecord)it.next();
|
313
|
//util.debugMessage("[Got Node ID: " + currentNode.nodeid +
|
314
|
//" (" + currentNode.parentnodeid +
|
315
|
//", " + currentNode.nodeindex +
|
316
|
//", " + currentNode.nodetype +
|
317
|
//", " + currentNode.nodename +
|
318
|
//", " + currentNode.nodedata + ")]");
|
319
|
|
320
|
// Print the end tag for the previous node if needed
|
321
|
//
|
322
|
// This is determined by inspecting the parent nodeid for the
|
323
|
// currentNode. If it is the same as the nodeid of the last element
|
324
|
// that was pushed onto the stack, then we are still in that previous
|
325
|
// parent element, and we do nothing. However, if it differs, then we
|
326
|
// have returned to a level above the previous parent, so we go into
|
327
|
// a loop and pop off nodes and print out their end tags until we get
|
328
|
// the node on the stack to match the currentNode parentnodeid
|
329
|
//
|
330
|
// So, this of course means that we rely on the list of elements
|
331
|
// having been sorted in a depth first traversal of the nodes, which
|
332
|
// is handled by the NodeComparator class used by the TreeSet
|
333
|
if (!atRootElement) {
|
334
|
NodeRecord currentElement = (NodeRecord)openElements.peek();
|
335
|
if ( currentNode.parentnodeid != currentElement.nodeid ) {
|
336
|
while ( currentNode.parentnodeid != currentElement.nodeid ) {
|
337
|
currentElement = (NodeRecord)openElements.pop();
|
338
|
util.debugMessage("\n POPPED: " + currentElement.nodename);
|
339
|
if (previousNodeWasElement) {
|
340
|
out.print(">");
|
341
|
previousNodeWasElement = false;
|
342
|
}
|
343
|
out.print("</" + currentElement.nodename + ">" );
|
344
|
currentElement = (NodeRecord)openElements.peek();
|
345
|
}
|
346
|
}
|
347
|
}
|
348
|
|
349
|
// Handle the DOCUMENT node
|
350
|
if (currentNode.nodetype.equals("DOCUMENT")) {
|
351
|
out.println("<?xml version=\"1.0\"?>");
|
352
|
|
353
|
if (docname != null) {
|
354
|
if ((doctype != null) && (system_id != null)) {
|
355
|
out.println("<!DOCTYPE " + docname + " PUBLIC \"" + doctype +
|
356
|
"\" \"" + system_id + "\">");
|
357
|
} else {
|
358
|
out.println("<!DOCTYPE " + docname + ">");
|
359
|
}
|
360
|
}
|
361
|
|
362
|
// Handle the ELEMENT nodes
|
363
|
} else if (currentNode.nodetype.equals("ELEMENT")) {
|
364
|
if (atRootElement) {
|
365
|
atRootElement = false;
|
366
|
} else {
|
367
|
if (previousNodeWasElement) {
|
368
|
out.print(">");
|
369
|
}
|
370
|
}
|
371
|
openElements.push(currentNode);
|
372
|
util.debugMessage("\n PUSHED: " + currentNode.nodename);
|
373
|
previousNodeWasElement = true;
|
374
|
out.print("<" + currentNode.nodename);
|
375
|
|
376
|
// Handle the ATTRIBUTE nodes
|
377
|
} else if (currentNode.nodetype.equals("ATTRIBUTE")) {
|
378
|
out.print(" " + currentNode.nodename + "=\""
|
379
|
+ currentNode.nodedata + "\"");
|
380
|
} else if (currentNode.nodetype.equals("TEXT")) {
|
381
|
if (previousNodeWasElement) {
|
382
|
out.print(">");
|
383
|
}
|
384
|
out.print(currentNode.nodedata);
|
385
|
previousNodeWasElement = false;
|
386
|
|
387
|
// Handle the COMMENT nodes
|
388
|
} else if (currentNode.nodetype.equals("COMMENT")) {
|
389
|
if (previousNodeWasElement) {
|
390
|
out.print(">");
|
391
|
}
|
392
|
out.print("<!--" + currentNode.nodedata + "-->");
|
393
|
previousNodeWasElement = false;
|
394
|
|
395
|
// Handle the PI nodes
|
396
|
} else if (currentNode.nodetype.equals("PI")) {
|
397
|
if (previousNodeWasElement) {
|
398
|
out.print(">");
|
399
|
}
|
400
|
out.print("<?" + currentNode.nodename + " " +
|
401
|
currentNode.nodedata + "?>");
|
402
|
previousNodeWasElement = false;
|
403
|
|
404
|
// Handle any other node type (do nothing)
|
405
|
} else {
|
406
|
// Any other types of nodes are not handled.
|
407
|
// Probably should throw an exception here to indicate this
|
408
|
}
|
409
|
out.flush();
|
410
|
}
|
411
|
|
412
|
// Print the final end tag for the root element
|
413
|
while(!openElements.empty())
|
414
|
{
|
415
|
NodeRecord currentElement = (NodeRecord)openElements.pop();
|
416
|
util.debugMessage("\n POPPED: " + currentElement.nodename);
|
417
|
out.print("</" + currentElement.nodename + ">" );
|
418
|
}
|
419
|
out.flush();
|
420
|
}
|
421
|
|
422
|
private boolean isRevisionOnly(DocumentIdentifier docid) throws Exception
|
423
|
{
|
424
|
//System.out.println("inRevisionOnly");
|
425
|
PreparedStatement pstmt;
|
426
|
String rev = docid.getRev();
|
427
|
String newid = docid.getIdentifier();
|
428
|
pstmt = conn.prepareStatement("select rev from xml_documents " +
|
429
|
"where docid like '" + newid + "'");
|
430
|
pstmt.execute();
|
431
|
ResultSet rs = pstmt.getResultSet();
|
432
|
boolean tablehasrows = rs.next();
|
433
|
if(rev.equals("newest") || rev.equals("all"))
|
434
|
{
|
435
|
return false;
|
436
|
}
|
437
|
|
438
|
if(tablehasrows)
|
439
|
{
|
440
|
int r = rs.getInt(1);
|
441
|
pstmt.close();
|
442
|
if(new Integer(rev).intValue() == r)
|
443
|
{ //the current revision in in xml_documents
|
444
|
//System.out.println("returning false");
|
445
|
return false;
|
446
|
}
|
447
|
else if(new Integer(rev).intValue() < r)
|
448
|
{ //the current revision is in xml_revisions.
|
449
|
//System.out.println("returning true");
|
450
|
return true;
|
451
|
}
|
452
|
else if(new Integer(rev).intValue() > r)
|
453
|
{ //error, rev cannot be greater than r
|
454
|
throw new Exception("requested revision cannot be greater than " +
|
455
|
"the latest revision number.");
|
456
|
}
|
457
|
}
|
458
|
throw new Exception("the requested docid '" + docid.toString() +
|
459
|
"' does not exist");
|
460
|
}
|
461
|
|
462
|
private void getDocumentInfo(String docid) throws McdbException,
|
463
|
AccessionNumberException
|
464
|
{
|
465
|
getDocumentInfo(new DocumentIdentifier(docid));
|
466
|
}
|
467
|
|
468
|
/**
|
469
|
* Look up the document type information from the database
|
470
|
*
|
471
|
* @param docid the id of the document to look up
|
472
|
*/
|
473
|
private void getDocumentInfo(DocumentIdentifier docid) throws McdbException
|
474
|
{
|
475
|
PreparedStatement pstmt;
|
476
|
String table = "xml_documents";
|
477
|
|
478
|
try
|
479
|
{
|
480
|
if(isRevisionOnly(docid))
|
481
|
{ //pull the document from xml_revisions instead of from xml_documents;
|
482
|
table = "xml_revisions";
|
483
|
}
|
484
|
}
|
485
|
catch(Exception e)
|
486
|
{
|
487
|
System.out.println("error in DocumentImpl.getDocumentInfo: " +
|
488
|
e.getMessage());
|
489
|
}
|
490
|
|
491
|
//deal with the key words here.
|
492
|
|
493
|
if(docid.getRev().equals("all"))
|
494
|
{
|
495
|
|
496
|
}
|
497
|
|
498
|
try {
|
499
|
StringBuffer sql = new StringBuffer();
|
500
|
sql.append("SELECT docname, doctype, rootnodeid,doctitle, ");
|
501
|
sql.append("date_created, date_updated, ");
|
502
|
sql.append("user_owner, user_updated, server_location, ");
|
503
|
sql.append("rev FROM ").append(table);
|
504
|
sql.append(" WHERE docid LIKE '").append(docid.getIdentifier());
|
505
|
sql.append("' and rev like '").append(docid.getRev()).append("'");
|
506
|
//System.out.println(sql.toString());
|
507
|
pstmt =
|
508
|
conn.prepareStatement(sql.toString());
|
509
|
// Bind the values to the query
|
510
|
//pstmt.setString(1, docid.getIdentifier());
|
511
|
//pstmt.setString(2, docid.getRev());
|
512
|
|
513
|
pstmt.execute();
|
514
|
ResultSet rs = pstmt.getResultSet();
|
515
|
boolean tableHasRows = rs.next();
|
516
|
if (tableHasRows) {
|
517
|
this.docname = rs.getString(1);
|
518
|
this.doctype = rs.getString(2);
|
519
|
this.rootnodeid = rs.getLong(3);
|
520
|
this.doctitle = rs.getString(4);
|
521
|
this.createdate = rs.getString(5);
|
522
|
this.updatedate = rs.getString(6);
|
523
|
this.userowner = rs.getString(7);
|
524
|
this.userupdated = rs.getString(8);
|
525
|
this.serverlocation = rs.getInt(9);
|
526
|
//this.publicaccess = rs.getInt(10);
|
527
|
this.rev = rs.getInt(10);
|
528
|
}
|
529
|
|
530
|
if (this.doctype != null) {
|
531
|
pstmt =
|
532
|
conn.prepareStatement("SELECT system_id " +
|
533
|
"FROM xml_catalog " +
|
534
|
"WHERE public_id LIKE ?");
|
535
|
// Bind the values to the query
|
536
|
pstmt.setString(1, doctype);
|
537
|
|
538
|
pstmt.execute();
|
539
|
rs = pstmt.getResultSet();
|
540
|
tableHasRows = rs.next();
|
541
|
if (tableHasRows) {
|
542
|
this.system_id = rs.getString(1);
|
543
|
}
|
544
|
//pstmt.close();
|
545
|
}
|
546
|
} catch (SQLException e) {
|
547
|
System.out.println("error in DocumentImpl.getDocumentInfo: " +
|
548
|
e.getMessage());
|
549
|
e.printStackTrace(System.out);
|
550
|
throw new McdbException("Error accessing database connection in " +
|
551
|
"DocumentImpl.getDocumentInfo: ", e);
|
552
|
}
|
553
|
|
554
|
if (this.docname == null) {
|
555
|
throw new McdbDocNotFoundException("Document not found: " + docid);
|
556
|
}
|
557
|
}
|
558
|
|
559
|
/**
|
560
|
* Look up the node data from the database
|
561
|
*
|
562
|
* @param rootnodeid the id of the root node of the node tree to look up
|
563
|
*/
|
564
|
private TreeSet getNodeRecordList(long rootnodeid) throws McdbException
|
565
|
{
|
566
|
PreparedStatement pstmt;
|
567
|
TreeSet nodeRecordList = new TreeSet(new NodeComparator());
|
568
|
long nodeid = 0;
|
569
|
long parentnodeid = 0;
|
570
|
long nodeindex = 0;
|
571
|
String nodetype = null;
|
572
|
String nodename = null;
|
573
|
String nodedata = null;
|
574
|
|
575
|
try {
|
576
|
pstmt =
|
577
|
conn.prepareStatement("SELECT nodeid,parentnodeid,nodeindex, " +
|
578
|
"nodetype,nodename,"+
|
579
|
"replace(" +
|
580
|
"replace(" +
|
581
|
"replace(nodedata,'&','&') " +
|
582
|
",'<','<') " +
|
583
|
",'>','>') " +
|
584
|
"FROM xml_nodes WHERE rootnodeid = ?");
|
585
|
|
586
|
// Bind the values to the query
|
587
|
pstmt.setLong(1, rootnodeid);
|
588
|
|
589
|
pstmt.execute();
|
590
|
ResultSet rs = pstmt.getResultSet();
|
591
|
boolean tableHasRows = rs.next();
|
592
|
while (tableHasRows) {
|
593
|
nodeid = rs.getLong(1);
|
594
|
parentnodeid = rs.getLong(2);
|
595
|
nodeindex = rs.getLong(3);
|
596
|
nodetype = rs.getString(4);
|
597
|
nodename = rs.getString(5);
|
598
|
nodedata = rs.getString(6);
|
599
|
|
600
|
// add the data to the node record list hashtable
|
601
|
NodeRecord currentRecord = new NodeRecord(nodeid, parentnodeid,
|
602
|
nodeindex, nodetype, nodename, nodedata);
|
603
|
nodeRecordList.add(currentRecord);
|
604
|
|
605
|
// Advance to the next node
|
606
|
tableHasRows = rs.next();
|
607
|
}
|
608
|
pstmt.close();
|
609
|
|
610
|
} catch (SQLException e) {
|
611
|
throw new McdbException("Error accessing database connection from " +
|
612
|
"DocumentImpl.getNodeRecordList ", e);
|
613
|
}
|
614
|
|
615
|
if (nodeRecordList != null) {
|
616
|
return nodeRecordList;
|
617
|
} else {
|
618
|
throw new McdbException("Error getting node data: " + docid);
|
619
|
}
|
620
|
}
|
621
|
|
622
|
/** creates SQL code and inserts new document into DB connection
|
623
|
default serverCode of 1*/
|
624
|
private void writeDocumentToDB(String action, String user)
|
625
|
throws SQLException, Exception
|
626
|
{
|
627
|
writeDocumentToDB(action, user, null, 1);
|
628
|
}
|
629
|
|
630
|
/** creates SQL code and inserts new document into DB connection */
|
631
|
private void writeDocumentToDB(String action, String user, String pub,
|
632
|
int serverCode)
|
633
|
throws SQLException, Exception {
|
634
|
try {
|
635
|
PreparedStatement pstmt = null;
|
636
|
|
637
|
if (action.equals("INSERT")) {
|
638
|
//AccessionNumber ac = new AccessionNumber();
|
639
|
//this.docid = ac.generate(docid, "INSERT");
|
640
|
pstmt = conn.prepareStatement(
|
641
|
"INSERT INTO xml_documents " +
|
642
|
"(docid, rootnodeid, docname, doctype, user_owner, user_updated, " +
|
643
|
"date_created, date_updated, public_access, server_location) " +
|
644
|
"VALUES (?, ?, ?, ?, ?, ?, sysdate, sysdate, ?, ?)");
|
645
|
//note that the server_location is set to 1.
|
646
|
//this means that "localhost" in the xml_replication table must
|
647
|
//always be the first entry!!!!!
|
648
|
|
649
|
// Bind the values to the query
|
650
|
pstmt.setString(1, this.docid);
|
651
|
pstmt.setLong(2, rootnodeid);
|
652
|
pstmt.setString(3, docname);
|
653
|
pstmt.setString(4, doctype);
|
654
|
pstmt.setString(5, user);
|
655
|
pstmt.setString(6, user);
|
656
|
if ( pub == null ) {
|
657
|
pstmt.setString(7, null);
|
658
|
} else if ( pub.toUpperCase().equals("YES") ) {
|
659
|
pstmt.setInt(7, 1);
|
660
|
} else if ( pub.toUpperCase().equals("NO") ) {
|
661
|
pstmt.setInt(7, 0);
|
662
|
}
|
663
|
pstmt.setInt(8, serverCode);
|
664
|
} else if (action.equals("UPDATE")) {
|
665
|
|
666
|
// Save the old document entry in a backup table
|
667
|
DocumentImpl.archiveDocRevision( conn, docid, user );
|
668
|
DocumentImpl thisdoc = new DocumentImpl(conn, docid);
|
669
|
int thisrev = thisdoc.getRev();
|
670
|
thisrev++;
|
671
|
// Delete index for the old version of docid
|
672
|
// The new index is inserting on the next calls to DBSAXNode
|
673
|
pstmt = conn.prepareStatement(
|
674
|
"DELETE FROM xml_index WHERE docid='" + this.docid + "'");
|
675
|
pstmt.execute();
|
676
|
//pstmt.close();
|
677
|
|
678
|
// Update the new document to reflect the new node tree
|
679
|
pstmt = conn.prepareStatement(
|
680
|
"UPDATE xml_documents " +
|
681
|
"SET rootnodeid = ?, docname = ?, doctype = ?, " +
|
682
|
"user_updated = ?, date_updated = sysdate, " +
|
683
|
"server_location = ?, rev = ?, public_access = ? " +
|
684
|
"WHERE docid LIKE ?");
|
685
|
// Bind the values to the query
|
686
|
pstmt.setLong(1, rootnodeid);
|
687
|
pstmt.setString(2, docname);
|
688
|
pstmt.setString(3, doctype);
|
689
|
pstmt.setString(4, user);
|
690
|
pstmt.setInt(5, serverCode);
|
691
|
pstmt.setInt(6, thisrev);
|
692
|
if ( pub == null ) {
|
693
|
pstmt.setString(7, null);
|
694
|
} else if ( pub.toUpperCase().equals("YES") ) {
|
695
|
pstmt .setInt(7, 1);
|
696
|
} else if ( pub.toUpperCase().equals("NO") ) {
|
697
|
pstmt.setInt(7, 0);
|
698
|
}
|
699
|
pstmt.setString(8, this.docid);
|
700
|
|
701
|
} else {
|
702
|
System.err.println("Action not supported: " + action);
|
703
|
}
|
704
|
|
705
|
// Do the insertion
|
706
|
pstmt.execute();
|
707
|
pstmt.close();
|
708
|
|
709
|
} catch (SQLException sqle) {
|
710
|
throw sqle;
|
711
|
} catch (Exception e) {
|
712
|
throw e;
|
713
|
}
|
714
|
}
|
715
|
|
716
|
/**
|
717
|
* Get the document title
|
718
|
*/
|
719
|
public String getTitle() {
|
720
|
return doctitle;
|
721
|
}
|
722
|
|
723
|
/**
|
724
|
* Set the document title
|
725
|
*
|
726
|
* @param title the new title for the document
|
727
|
*/
|
728
|
|
729
|
public void setTitle( String title ) {
|
730
|
this.doctitle = title;
|
731
|
try {
|
732
|
PreparedStatement pstmt;
|
733
|
pstmt = conn.prepareStatement(
|
734
|
"UPDATE xml_documents " +
|
735
|
" SET doctitle = ? " +
|
736
|
"WHERE docid = ?");
|
737
|
|
738
|
// Bind the values to the query
|
739
|
pstmt.setString(1, doctitle);
|
740
|
pstmt.setString(2, docid);
|
741
|
|
742
|
// Do the insertion
|
743
|
pstmt.execute();
|
744
|
pstmt.close();
|
745
|
} catch (SQLException e) {
|
746
|
System.out.println("Error in DocumentImpl.setTitle: " + e.getMessage());
|
747
|
}
|
748
|
}
|
749
|
|
750
|
/**
|
751
|
* Write an XML file to the database, given a filename
|
752
|
*
|
753
|
* @param conn the JDBC connection to the database
|
754
|
* @param filename the filename to be loaded into the database
|
755
|
* @param pub flag for public "read" access on document
|
756
|
* @param dtdfilename the dtd to be uploaded on server's file system
|
757
|
* @param action the action to be performed (INSERT OR UPDATE)
|
758
|
* @param docid the docid to use for the INSERT OR UPDATE
|
759
|
* @param user the user that owns the document
|
760
|
* @param group the group to which user belongs
|
761
|
*/
|
762
|
public static String write(Connection conn,String filename,
|
763
|
String pub, String dtdfilename,
|
764
|
String action, String docid, String user,
|
765
|
String group )
|
766
|
throws Exception {
|
767
|
|
768
|
Reader dtd = null;
|
769
|
if ( dtdfilename != null ) {
|
770
|
dtd = new FileReader(new File(dtdfilename).toString());
|
771
|
}
|
772
|
return write ( conn, new FileReader(new File(filename).toString()),
|
773
|
pub, dtd, action, docid, user, group);
|
774
|
}
|
775
|
|
776
|
public static String write(Connection conn,Reader xml,String pub,Reader dtd,
|
777
|
String action, String docid, String user,
|
778
|
String group )
|
779
|
throws Exception {
|
780
|
return write ( conn, xml, pub, dtd, action, docid, user, group, 1, false);
|
781
|
}
|
782
|
|
783
|
public static String write(Connection conn, Reader xml, String pub,
|
784
|
String action, String docid, String user,
|
785
|
String group )
|
786
|
throws Exception {
|
787
|
if(action.equals("UPDATE"))
|
788
|
{//if the document is being updated then use the servercode from the
|
789
|
//originally inserted document.
|
790
|
DocumentImpl doc = new DocumentImpl(conn, docid);
|
791
|
int servercode = doc.getServerlocation();
|
792
|
return write(conn, xml, pub, action, docid, user, group, servercode);
|
793
|
}
|
794
|
else
|
795
|
{//if the file is being inserted then the servercode is always 1
|
796
|
return write(conn, xml, pub, action, docid, user, group, 1);
|
797
|
}
|
798
|
}
|
799
|
|
800
|
public static String write( Connection conn, Reader xml,
|
801
|
String action, String docid, String user,
|
802
|
String group, int serverCode )
|
803
|
throws Exception
|
804
|
{
|
805
|
return write(conn,xml,null,action,docid,user,group,serverCode);
|
806
|
}
|
807
|
|
808
|
public static String write( Connection conn, Reader xml, String pub,
|
809
|
String action, String docid, String user,
|
810
|
String group, int serverCode)
|
811
|
throws Exception
|
812
|
{
|
813
|
return write(conn,xml,pub,null,action,docid,user,group,serverCode,false);
|
814
|
}
|
815
|
|
816
|
public static String write( Connection conn, Reader xml, String pub,
|
817
|
String action, String docid, String user,
|
818
|
String group, int serverCode, boolean override)
|
819
|
throws Exception
|
820
|
{
|
821
|
return write(conn,xml,pub,null,action,docid,user,group,serverCode,override);
|
822
|
}
|
823
|
|
824
|
/**
|
825
|
* Write an XML file to the database, given a Reader
|
826
|
*
|
827
|
* @param conn the JDBC connection to the database
|
828
|
* @param xml the xml stream to be loaded into the database
|
829
|
* @param pub flag for public "read" access on xml document
|
830
|
* @param dtd the dtd to be uploaded on server's file system
|
831
|
* @param action the action to be performed (INSERT OR UPDATE)
|
832
|
* @param docid the docid to use for the INSERT OR UPDATE
|
833
|
* @param user the user that owns the document
|
834
|
* @param group the group to which user belongs
|
835
|
* @param serverCode the serverid from xml_replication on which this document
|
836
|
* resides.
|
837
|
* @param override flag to stop insert replication checking.
|
838
|
* if override = true then a document not belonging to the local server
|
839
|
* will not be checked upon update for a file lock.
|
840
|
* if override = false then a document not from this server, upon
|
841
|
* update will be locked and version checked.
|
842
|
*/
|
843
|
|
844
|
public static String write( Connection conn,Reader xml,String pub,Reader dtd,
|
845
|
String action, String docid, String user,
|
846
|
String group, int serverCode, boolean override)
|
847
|
throws Exception
|
848
|
{
|
849
|
//MOVED DOWN IN RelationHandler obj before write of relations
|
850
|
//if(action.equals("UPDATE"))
|
851
|
//{
|
852
|
// RelationHandler.deleteRelations(docid);
|
853
|
//}
|
854
|
|
855
|
int updaterev;
|
856
|
MetaCatUtil util = new MetaCatUtil();
|
857
|
// Determine if the docid is OK for INSERT or UPDATE
|
858
|
AccessionNumber ac = new AccessionNumber(conn);
|
859
|
String newdocid = ac.generate(docid, action);
|
860
|
|
861
|
MetaCatUtil.debugMessage("action: " + action + " servercode: " +
|
862
|
serverCode + " override: " + override);
|
863
|
|
864
|
if((serverCode != 1 && action.equals("UPDATE")) && !override)
|
865
|
{ //if this document being written is not a resident of this server then
|
866
|
//we need to try to get a lock from it's resident server. If the
|
867
|
//resident server will not give a lock then we send the user a message
|
868
|
//saying that he/she needs to download a new copy of the file and
|
869
|
//merge the differences manually.
|
870
|
int istreamInt;
|
871
|
char istreamChar;
|
872
|
DocumentImpl newdoc = new DocumentImpl(conn, docid);
|
873
|
updaterev = newdoc.getRev();
|
874
|
String server = MetacatReplication.getServer(serverCode);
|
875
|
MetacatReplication.replLog("attempting to lock " + docid);
|
876
|
URL u = new URL("http://" + server + "?action=getlock&updaterev=" +
|
877
|
updaterev + "&docid=" + docid);
|
878
|
System.out.println("sending message: " + u.toString());
|
879
|
String serverResStr = MetacatReplication.getURLContent(u);
|
880
|
String openingtag = serverResStr.substring(0, serverResStr.indexOf(">")+1);
|
881
|
|
882
|
if(openingtag.equals("<lockgranted>"))
|
883
|
{//the lock was granted go ahead with the insert
|
884
|
try
|
885
|
{
|
886
|
MetacatReplication.replLog("lock granted for " + docid + " from " +
|
887
|
server);
|
888
|
XMLReader parser = initializeParser(conn, action, newdocid,
|
889
|
user, group, pub, serverCode, dtd);
|
890
|
conn.setAutoCommit(false);
|
891
|
parser.parse(new InputSource(xml));
|
892
|
conn.commit();
|
893
|
conn.setAutoCommit(true);
|
894
|
}
|
895
|
catch (Exception e)
|
896
|
{
|
897
|
conn.rollback();
|
898
|
conn.setAutoCommit(true);
|
899
|
throw e;
|
900
|
}
|
901
|
|
902
|
//after inserting the document locally, tell the document's home server
|
903
|
//to come get a copy from here.
|
904
|
ForceReplicationHandler frh = new ForceReplicationHandler(docid);
|
905
|
|
906
|
if ( (docid != null) && !(newdocid.equals(docid)) )
|
907
|
{
|
908
|
return new String("New document ID generated:" + newdocid);
|
909
|
}
|
910
|
else
|
911
|
{
|
912
|
return newdocid;
|
913
|
}
|
914
|
}
|
915
|
else if(openingtag.equals("<filelocked>"))
|
916
|
{//the file is currently locked by another user
|
917
|
//notify our user to wait a few minutes, check out a new copy and try
|
918
|
//again.
|
919
|
//System.out.println("file locked");
|
920
|
MetacatReplication.replLog("lock denied for " + docid + " on " +
|
921
|
server + " reason: file already locked");
|
922
|
throw new Exception("The file specified is already locked by another " +
|
923
|
"user. Please wait 30 seconds, checkout the " +
|
924
|
"newer document, merge your changes and try " +
|
925
|
"again.");
|
926
|
}
|
927
|
else if(openingtag.equals("<outdatedfile>"))
|
928
|
{//our file is outdated. notify our user to check out a new copy of the
|
929
|
//file and merge his version with the new version.
|
930
|
//System.out.println("outdated file");
|
931
|
MetacatReplication.replLog("lock denied for " + docid + " on " +
|
932
|
server + " reason: local file outdated");
|
933
|
throw new Exception("The file you are trying to update is an outdated" +
|
934
|
" version. Please checkout the newest document, " +
|
935
|
"merge your changes and try again.");
|
936
|
}
|
937
|
}
|
938
|
|
939
|
if ( action.equals("UPDATE") ) {
|
940
|
// check for 'write' permission for 'user' to update this document
|
941
|
|
942
|
if ( !hasPermission(conn, user, group, docid) ) {
|
943
|
throw new Exception("User " + user +
|
944
|
" does not have permission to update XML Document #" + docid);
|
945
|
}
|
946
|
}
|
947
|
|
948
|
try
|
949
|
{
|
950
|
XMLReader parser = initializeParser(conn, action, newdocid,
|
951
|
user, group, pub, serverCode, dtd);
|
952
|
conn.setAutoCommit(false);
|
953
|
parser.parse(new InputSource(xml));
|
954
|
conn.commit();
|
955
|
conn.setAutoCommit(true);
|
956
|
}
|
957
|
catch (Exception e)
|
958
|
{
|
959
|
conn.rollback();
|
960
|
conn.setAutoCommit(true);
|
961
|
throw e;
|
962
|
}
|
963
|
|
964
|
//force replicate out the new document to each server in our server list.
|
965
|
if(serverCode == 1)
|
966
|
{ //start the thread to replicate this new document out to the other servers
|
967
|
ForceReplicationHandler frh = new ForceReplicationHandler(newdocid,
|
968
|
action);
|
969
|
}
|
970
|
|
971
|
if ( (docid != null) && !(newdocid.equals(docid)) )
|
972
|
{
|
973
|
return new String("New document ID generated:" + newdocid);
|
974
|
}
|
975
|
else
|
976
|
{
|
977
|
return newdocid;
|
978
|
}
|
979
|
}
|
980
|
|
981
|
/**
|
982
|
* Delete an XML file from the database (actually, just make it a revision
|
983
|
* in the xml_revisions table)
|
984
|
*
|
985
|
* @param docid the ID of the document to be deleted from the database
|
986
|
*/
|
987
|
public static void delete( Connection conn, String docid,
|
988
|
String user, String group )
|
989
|
throws Exception {
|
990
|
DocumentIdentifier id = new DocumentIdentifier(docid);
|
991
|
docid = id.getSiteCode() + id.getSeparator() + id.getUniqueId();
|
992
|
|
993
|
// Determine if the docid is OK for DELETE
|
994
|
AccessionNumber ac = new AccessionNumber(conn);
|
995
|
String newdocid = ac.generate(docid, "DELETE");
|
996
|
|
997
|
// check for 'write' permission for 'user' to delete this document
|
998
|
if ( !hasPermission(conn, user, group, docid) ) {
|
999
|
throw new Exception("User " + user +
|
1000
|
" does not have permission to delete XML Document #" + docid);
|
1001
|
}
|
1002
|
|
1003
|
conn.setAutoCommit(false);
|
1004
|
// Copy the record to the xml_revisions table
|
1005
|
DocumentImpl.archiveDocRevision( conn, docid, user );
|
1006
|
|
1007
|
// Now delete it from the xml_documents table
|
1008
|
|
1009
|
Statement stmt = conn.createStatement();
|
1010
|
stmt.execute("DELETE FROM xml_index WHERE docid = '" + docid + "'");
|
1011
|
stmt.execute("DELETE FROM xml_documents WHERE docid = '" + docid + "'");
|
1012
|
//stmt.execute("DELETE FROM xml_access WHERE docid = '" + docid + "'");
|
1013
|
stmt.execute("DELETE FROM xml_access WHERE accessfileid = '" + docid + "'");
|
1014
|
stmt.execute("DELETE FROM xml_relation WHERE docid = '" + docid + "'");
|
1015
|
stmt.close();
|
1016
|
conn.commit();
|
1017
|
conn.setAutoCommit(true);
|
1018
|
//IF this is a package document:
|
1019
|
//delete all of the relations that this document created.
|
1020
|
//if the deleted document is a package document its relations should
|
1021
|
//no longer be active if it has been deleted from the system.
|
1022
|
|
1023
|
// MOVED UP IN THE TRANSACTION
|
1024
|
// RelationHandler.deleteRelations(docid);
|
1025
|
}
|
1026
|
|
1027
|
/**
|
1028
|
* Check for "WRITE" permission on @docid for @user and/or @group
|
1029
|
* from DB connection
|
1030
|
*/
|
1031
|
private static boolean hasPermission( Connection conn, String user,
|
1032
|
String group, String docid)
|
1033
|
throws SQLException
|
1034
|
{
|
1035
|
// b' of the command line invocation
|
1036
|
if ( (user == null) && (group == null) ) {
|
1037
|
return true;
|
1038
|
}
|
1039
|
|
1040
|
// Check for WRITE permission on @docid for @user and/or @group
|
1041
|
AccessControlList aclobj = new AccessControlList(conn);
|
1042
|
boolean hasPermission = aclobj.hasPermission("WRITE",user,docid);
|
1043
|
if ( !hasPermission && group != null ) {
|
1044
|
hasPermission = aclobj.hasPermission("WRITE",group,docid);
|
1045
|
}
|
1046
|
|
1047
|
return hasPermission;
|
1048
|
}
|
1049
|
|
1050
|
/**
|
1051
|
* Set up the parser handlers for writing the document to the database
|
1052
|
*/
|
1053
|
private static XMLReader initializeParser(Connection conn, String action,
|
1054
|
String docid, String user, String group,
|
1055
|
String pub, int serverCode, Reader dtd)
|
1056
|
throws Exception
|
1057
|
{
|
1058
|
XMLReader parser = null;
|
1059
|
//
|
1060
|
// Set up the SAX document handlers for parsing
|
1061
|
//
|
1062
|
try {
|
1063
|
ContentHandler chandler = new DBSAXHandler(conn, action, docid,
|
1064
|
user, group, pub, serverCode);
|
1065
|
EntityResolver eresolver= new DBEntityResolver(conn,
|
1066
|
(DBSAXHandler)chandler, dtd);
|
1067
|
DTDHandler dtdhandler = new DBDTDHandler(conn);
|
1068
|
|
1069
|
// Get an instance of the parser
|
1070
|
MetaCatUtil util = new MetaCatUtil();
|
1071
|
String parserName = util.getOption("saxparser");
|
1072
|
parser = XMLReaderFactory.createXMLReader(parserName);
|
1073
|
|
1074
|
// Turn on validation
|
1075
|
parser.setFeature("http://xml.org/sax/features/validation", false);
|
1076
|
// Turn off Including all external parameter entities
|
1077
|
// (the external DTD subset also)
|
1078
|
// Doesn't work well, probably the feature name is not correct
|
1079
|
// parser.setFeature(
|
1080
|
// "http://xml.org/sax/features/external-parameter-entities", false);
|
1081
|
|
1082
|
// Set Handlers in the parser
|
1083
|
parser.setProperty("http://xml.org/sax/properties/declaration-handler",
|
1084
|
chandler);
|
1085
|
parser.setProperty("http://xml.org/sax/properties/lexical-handler",
|
1086
|
chandler);
|
1087
|
parser.setContentHandler((ContentHandler)chandler);
|
1088
|
parser.setEntityResolver((EntityResolver)eresolver);
|
1089
|
parser.setDTDHandler((DTDHandler)dtdhandler);
|
1090
|
parser.setErrorHandler((ErrorHandler)chandler);
|
1091
|
|
1092
|
} catch (Exception e) {
|
1093
|
throw e;
|
1094
|
}
|
1095
|
|
1096
|
return parser;
|
1097
|
}
|
1098
|
|
1099
|
/** Save a document entry in the xml_revisions table */
|
1100
|
private static void archiveDocRevision(Connection conn, String docid,
|
1101
|
String user)
|
1102
|
throws SQLException {
|
1103
|
// create a record in xml_revisions table
|
1104
|
// for that document as selected from xml_documents
|
1105
|
PreparedStatement pstmt = conn.prepareStatement(
|
1106
|
"INSERT INTO xml_revisions " +
|
1107
|
"(revisionid, docid, rootnodeid, docname, doctype, doctitle, " +
|
1108
|
"user_owner, user_updated, date_created, date_updated, server_location, " +
|
1109
|
"rev)" +
|
1110
|
"SELECT null, ?, rootnodeid, docname, doctype, doctitle," +
|
1111
|
"user_owner, ?, sysdate, sysdate, server_location, rev "+
|
1112
|
"FROM xml_documents " +
|
1113
|
"WHERE docid = ?");
|
1114
|
// Bind the values to the query and execute it
|
1115
|
pstmt.setString(1, docid);
|
1116
|
pstmt.setString(2, user);
|
1117
|
pstmt.setString(3, docid);
|
1118
|
pstmt.execute();
|
1119
|
pstmt.close();
|
1120
|
|
1121
|
}
|
1122
|
|
1123
|
/**
|
1124
|
* the main routine used to test the DBWriter utility.
|
1125
|
* <p>
|
1126
|
* Usage: java DocumentImpl <-f filename -a action -d docid>
|
1127
|
*
|
1128
|
* @param filename the filename to be loaded into the database
|
1129
|
* @param action the action to perform (READ, INSERT, UPDATE, DELETE)
|
1130
|
* @param docid the id of the document to process
|
1131
|
*/
|
1132
|
static public void main(String[] args) {
|
1133
|
|
1134
|
try {
|
1135
|
String filename = null;
|
1136
|
String dtdfilename = null;
|
1137
|
String action = null;
|
1138
|
String docid = null;
|
1139
|
boolean showRuntime = false;
|
1140
|
boolean useOldReadAlgorithm = false;
|
1141
|
|
1142
|
// Parse the command line arguments
|
1143
|
for ( int i=0 ; i < args.length; ++i ) {
|
1144
|
if ( args[i].equals( "-f" ) ) {
|
1145
|
filename = args[++i];
|
1146
|
} else if ( args[i].equals( "-r" ) ) {
|
1147
|
dtdfilename = args[++i];
|
1148
|
} else if ( args[i].equals( "-a" ) ) {
|
1149
|
action = args[++i];
|
1150
|
} else if ( args[i].equals( "-d" ) ) {
|
1151
|
docid = args[++i];
|
1152
|
} else if ( args[i].equals( "-t" ) ) {
|
1153
|
showRuntime = true;
|
1154
|
} else if ( args[i].equals( "-old" ) ) {
|
1155
|
useOldReadAlgorithm = true;
|
1156
|
} else {
|
1157
|
System.err.println
|
1158
|
( " args[" +i+ "] '" +args[i]+ "' ignored." );
|
1159
|
}
|
1160
|
}
|
1161
|
|
1162
|
// Check if the required arguments are provided
|
1163
|
boolean argsAreValid = false;
|
1164
|
if (action != null) {
|
1165
|
if (action.equals("INSERT")) {
|
1166
|
if (filename != null) {
|
1167
|
argsAreValid = true;
|
1168
|
}
|
1169
|
} else if (action.equals("UPDATE")) {
|
1170
|
if ((filename != null) && (docid != null)) {
|
1171
|
argsAreValid = true;
|
1172
|
}
|
1173
|
} else if (action.equals("DELETE")) {
|
1174
|
if (docid != null) {
|
1175
|
argsAreValid = true;
|
1176
|
}
|
1177
|
} else if (action.equals("READ")) {
|
1178
|
if (docid != null) {
|
1179
|
argsAreValid = true;
|
1180
|
}
|
1181
|
}
|
1182
|
}
|
1183
|
|
1184
|
// Print usage message if the arguments are not valid
|
1185
|
if (!argsAreValid) {
|
1186
|
System.err.println("Wrong number of arguments!!!");
|
1187
|
System.err.println(
|
1188
|
"USAGE: java DocumentImpl [-t] <-a INSERT> [-d docid] <-f filename> "+
|
1189
|
"[-r dtdfilename]");
|
1190
|
System.err.println(
|
1191
|
" OR: java DocumentImpl [-t] <-a UPDATE -d docid -f filename> " +
|
1192
|
"[-r dtdfilename]");
|
1193
|
System.err.println(
|
1194
|
" OR: java DocumentImpl [-t] <-a DELETE -d docid>");
|
1195
|
System.err.println(
|
1196
|
" OR: java DocumentImpl [-t] [-old] <-a READ -d docid>");
|
1197
|
return;
|
1198
|
}
|
1199
|
|
1200
|
// Time the request if asked for
|
1201
|
double startTime = System.currentTimeMillis();
|
1202
|
|
1203
|
// Open a connection to the database
|
1204
|
MetaCatUtil util = new MetaCatUtil();
|
1205
|
Connection dbconn = util.openDBConnection();
|
1206
|
|
1207
|
double connTime = System.currentTimeMillis();
|
1208
|
// Execute the action requested (READ, INSERT, UPDATE, DELETE)
|
1209
|
if (action.equals("READ")) {
|
1210
|
DocumentImpl xmldoc = new DocumentImpl( dbconn, docid );
|
1211
|
if (useOldReadAlgorithm) {
|
1212
|
System.out.println(xmldoc.readUsingSlowAlgorithm());
|
1213
|
} else {
|
1214
|
xmldoc.toXml(new PrintWriter(System.out));
|
1215
|
}
|
1216
|
} else if (action.equals("DELETE")) {
|
1217
|
DocumentImpl.delete(dbconn, docid, null, null);
|
1218
|
System.out.println("Document deleted: " + docid);
|
1219
|
} else {
|
1220
|
String newdocid = DocumentImpl.write(dbconn, filename, null,
|
1221
|
dtdfilename, action, docid,
|
1222
|
null, null);
|
1223
|
if ((docid != null) && (!docid.equals(newdocid))) {
|
1224
|
if (action.equals("INSERT")) {
|
1225
|
System.out.println("New document ID generated!!! ");
|
1226
|
} else if (action.equals("UPDATE")) {
|
1227
|
System.out.println("ERROR: Couldn't update document!!! ");
|
1228
|
}
|
1229
|
} else if ((docid == null) && (action.equals("UPDATE"))) {
|
1230
|
System.out.println("ERROR: Couldn't update document!!! ");
|
1231
|
}
|
1232
|
System.out.println("Document processing finished for: " + filename
|
1233
|
+ " (" + newdocid + ")");
|
1234
|
}
|
1235
|
|
1236
|
double stopTime = System.currentTimeMillis();
|
1237
|
double dbOpenTime = (connTime - startTime)/1000;
|
1238
|
double insertTime = (stopTime - connTime)/1000;
|
1239
|
double executionTime = (stopTime - startTime)/1000;
|
1240
|
if (showRuntime) {
|
1241
|
System.out.println("\n\nTotal Execution time was: " +
|
1242
|
executionTime + " seconds.");
|
1243
|
System.out.println("Time to open DB connection was: " + dbOpenTime +
|
1244
|
" seconds.");
|
1245
|
System.out.println("Time to insert document was: " + insertTime +
|
1246
|
" seconds.");
|
1247
|
}
|
1248
|
dbconn.close();
|
1249
|
} catch (McdbException me) {
|
1250
|
me.toXml(new PrintWriter(System.err));
|
1251
|
} catch (AccessionNumberException ane) {
|
1252
|
System.out.println("ERROR: Couldn't delete document!!! ");
|
1253
|
System.out.println(ane.getMessage());
|
1254
|
} catch (Exception e) {
|
1255
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
1256
|
System.err.println(e.getMessage());
|
1257
|
e.printStackTrace(System.err);
|
1258
|
}
|
1259
|
}
|
1260
|
}
|