Revision 408
Added by Matt Jones over 24 years ago
src/edu/ucsb/nceas/metacat/DBSAXHandler.java | ||
---|---|---|
79 | 79 |
MetaCatUtil.debugMessage("start Document"); |
80 | 80 |
|
81 | 81 |
// Create the document node representation as root |
82 |
rootNode = new DBSAXNode(conn, docname); |
|
83 |
MetaCatUtil.debugMessage("PRINTING DOCNAME FROM ROOTNODE: " + |
|
84 |
rootNode.getTagName()); |
|
82 |
rootNode = new DBSAXNode(conn); |
|
85 | 83 |
// Add the node to the stack, so that any text data can be |
86 | 84 |
// added as it is encountered |
87 | 85 |
nodeStack.push(rootNode); |
... | ... | |
113 | 111 |
try { |
114 | 112 |
parentNode = (DBSAXNode)nodeStack.peek(); |
115 | 113 |
} catch (EmptyStackException e) { |
114 |
parentNode = null; |
|
116 | 115 |
} |
117 | 116 |
|
118 | 117 |
// Document representation that points to the root document node |
... | ... | |
126 | 125 |
MetaCatUtil.debugMessage("DOCTYPE-a: " + doctype); |
127 | 126 |
} else if (doctype == null) { |
128 | 127 |
doctype = docname; |
129 |
//doctype = DBEntityResolver.doctype; |
|
130 | 128 |
MetaCatUtil.debugMessage("DOCTYPE-b: " + doctype); |
131 | 129 |
} |
132 | 130 |
rootNode.writeNodename(docname); |
133 |
rootNode.writeRootNodeID(rootNode.getNodeID()); |
|
134 | 131 |
try { |
135 | 132 |
currentDocument = new DocumentImpl(conn, rootNode.getNodeID(), |
136 | 133 |
docname, doctype, docid, action); |
137 |
} catch (AccessionNumberException ane) {
|
|
138 |
throw (new SAXException("Error with " + action, ane));
|
|
139 |
}
|
|
134 |
} catch (AccessionNumberException ane) { |
|
135 |
throw (new SAXException("Error with " + action, ane)); |
|
136 |
} |
|
140 | 137 |
rootNode.writeDocID(currentDocument.getDocID()); |
141 | 138 |
} |
142 | 139 |
|
src/edu/ucsb/nceas/metacat/DocumentImpl.java | ||
---|---|---|
39 | 39 |
public class DocumentImpl { |
40 | 40 |
|
41 | 41 |
private Connection conn = null; |
42 |
private MetaCatUtil util = null; |
|
43 | 42 |
private String docid = null; |
44 | 43 |
private String docname = null; |
45 | 44 |
private String doctype = null; |
... | ... | |
47 | 46 |
private long rootnodeid; |
48 | 47 |
private ElementNode rootNode = null; |
49 | 48 |
private String doctitle = null; |
50 |
private String parserName = null; |
|
51 | 49 |
|
52 | 50 |
/** |
53 | 51 |
* Constructor, creates document from database connection, used |
... | ... | |
114 | 112 |
public DocumentImpl (Connection conn) |
115 | 113 |
{ |
116 | 114 |
this.conn = conn; |
117 |
this.util = new MetaCatUtil(); |
|
118 |
this.parserName = util.getOption("saxparser"); |
|
119 | 115 |
} |
120 | 116 |
|
121 | 117 |
/** |
... | ... | |
293 | 289 |
/** |
294 | 290 |
* Write an XML file to the database, given a filename |
295 | 291 |
* |
292 |
* @param conn the JDBC connection to the database |
|
296 | 293 |
* @param filename the filename to be loaded into the database |
297 | 294 |
* @param action the action to be performed (INSERT OR UPDATE) |
298 | 295 |
* @param docid the docid to use for the INSERT OR UPDATE |
299 | 296 |
*/ |
300 |
public String write( String filename, String action, String docid ) |
|
297 |
public static String write( Connection conn, String filename, String action, |
|
298 |
String docid ) |
|
301 | 299 |
throws Exception, IOException, SQLException, |
302 | 300 |
ClassNotFoundException, SAXException, SAXParseException { |
303 |
return write(new FileReader(new File(filename).toString()), action, docid); |
|
301 |
return write(conn, new FileReader(new File(filename).toString()), |
|
302 |
action, docid); |
|
304 | 303 |
} |
305 | 304 |
|
306 | 305 |
/** |
307 | 306 |
* Write an XML file to the database, given a Reader |
308 | 307 |
* |
308 |
* @param conn the JDBC connection to the database |
|
309 | 309 |
* @param xml the xml stream to be loaded into the database |
310 | 310 |
* @param action the action to be performed (INSERT OR UPDATE) |
311 | 311 |
* @param docid the docid to use for the INSERT OR UPDATE |
312 | 312 |
*/ |
313 |
public String write( Reader xml, String action, String docid ) |
|
313 |
public static String write( Connection conn, Reader xml, String action, |
|
314 |
String docid ) |
|
314 | 315 |
throws Exception, IOException, SQLException, |
315 | 316 |
ClassNotFoundException, SAXException, SAXParseException { |
316 | 317 |
try { |
317 |
XMLReader parser = initializeParser(action, docid); |
|
318 |
XMLReader parser = initializeParser(conn, action, docid);
|
|
318 | 319 |
conn.setAutoCommit(false); |
319 | 320 |
parser.parse(new InputSource(xml)); |
320 | 321 |
conn.commit(); |
... | ... | |
357 | 358 |
* |
358 | 359 |
* @param docid the ID of the document to be deleted from the database |
359 | 360 |
*/ |
360 |
public void delete( String docid )
|
|
361 |
public static void delete( Connection conn, String docid )
|
|
361 | 362 |
throws IOException, SQLException, |
362 | 363 |
ClassNotFoundException, AccessionNumberException { |
363 | 364 |
|
... | ... | |
365 | 366 |
|
366 | 367 |
conn.setAutoCommit(false); |
367 | 368 |
// Copy the record to the xml_revisions table |
368 |
DocumentImpl document = new DocumentImpl(conn); |
|
369 |
document.archiveDocRevision( docid ); |
|
369 |
DocumentImpl.archiveDocRevision( conn, docid ); |
|
370 | 370 |
|
371 | 371 |
// Now delete it from the xml_documents table |
372 | 372 |
Statement stmt = conn.createStatement(); |
... | ... | |
400 | 400 |
this.docid = AccessionNumber.generate(docid, "UPDATE"); |
401 | 401 |
|
402 | 402 |
// Save the old document entry in a backup table |
403 |
archiveDocRevision(docid);
|
|
403 |
DocumentImpl.archiveDocRevision( conn, docid );
|
|
404 | 404 |
|
405 | 405 |
// Update the new document to reflect the new node tree |
406 | 406 |
pstmt = conn.prepareStatement( |
... | ... | |
521 | 521 |
} |
522 | 522 |
|
523 | 523 |
/** Save a document entry in the xml_revisions table */ |
524 |
private void archiveDocRevision(String docid) throws SQLException { |
|
524 |
private static void archiveDocRevision(Connection conn, String docid) |
|
525 |
throws SQLException { |
|
525 | 526 |
// First get all of the values we need |
526 | 527 |
long rnodeid = -1; |
527 | 528 |
String docname = null; |
... | ... | |
572 | 573 |
/** |
573 | 574 |
* Set up the parser handlers for writing the document to the database |
574 | 575 |
*/ |
575 |
private XMLReader initializeParser(String action, String docid) { |
|
576 |
private static XMLReader initializeParser(Connection conn, |
|
577 |
String action, String docid) { |
|
576 | 578 |
XMLReader parser = null; |
577 | 579 |
// |
578 | 580 |
// Set up the SAX document handlers for parsing |
... | ... | |
584 | 586 |
DTDHandler dtdhandler = new DBDTDHandler(conn); |
585 | 587 |
|
586 | 588 |
// Get an instance of the parser |
589 |
MetaCatUtil util = new MetaCatUtil(); |
|
590 |
String parserName = util.getOption("saxparser"); |
|
587 | 591 |
parser = XMLReaderFactory.createXMLReader(parserName); |
588 | 592 |
|
589 | 593 |
// Turn off validation |
... | ... | |
622 | 626 |
String action = null; |
623 | 627 |
String docid = null; |
624 | 628 |
|
629 |
// Parse the command line arguments |
|
625 | 630 |
for ( int i=0 ; i < args.length; ++i ) { |
626 | 631 |
if ( args[i].equals( "-f" ) ) { |
627 | 632 |
filename = args[++i]; |
... | ... | |
635 | 640 |
} |
636 | 641 |
} |
637 | 642 |
|
643 |
// Check if the required arguments are provided |
|
638 | 644 |
boolean argsAreValid = false; |
639 | 645 |
if (action != null) { |
640 | 646 |
if (action.equals("INSERT")) { |
... | ... | |
656 | 662 |
} |
657 | 663 |
} |
658 | 664 |
|
665 |
// Print usage message if the arguments are not valid |
|
659 | 666 |
if (!argsAreValid) { |
660 | 667 |
System.err.println("Wrong number of arguments!!!"); |
661 | 668 |
System.err.println( |
... | ... | |
673 | 680 |
MetaCatUtil util = new MetaCatUtil(); |
674 | 681 |
Connection dbconn = util.openDBConnection(); |
675 | 682 |
|
683 |
// Execute the action requested (READ, INSERT, UPDATE, DELETE) |
|
676 | 684 |
if (action.equals("READ")) { |
677 | 685 |
DocumentImpl xmldoc = new DocumentImpl( dbconn, docid ); |
678 | 686 |
System.out.println(xmldoc.toString()); |
687 |
} else if (action.equals("DELETE")) { |
|
688 |
DocumentImpl.delete(dbconn, docid); |
|
689 |
System.out.println("Document deleted: " + docid); |
|
679 | 690 |
} else { |
680 |
DocumentImpl doc = new DocumentImpl(dbconn); |
|
681 |
if (action.equals("DELETE")) { |
|
682 |
doc.delete(docid); |
|
683 |
System.out.println("Document deleted: " + docid); |
|
684 |
} else { |
|
685 |
String newdocid = doc.write(filename, action, docid); |
|
686 |
if ((docid != null) && (!docid.equals(newdocid))) { |
|
687 |
if (action.equals("INSERT")) { |
|
688 |
System.out.println("New document ID generated!!! "); |
|
689 |
} else if (action.equals("UPDATE")) { |
|
690 |
System.out.println("ERROR: Couldn't update document!!! "); |
|
691 |
} |
|
692 |
} else if ((docid == null) && (action.equals("UPDATE"))) { |
|
693 |
System.out.println("ERROR: Couldn't update document!!! "); |
|
691 |
String newdocid = DocumentImpl.write(dbconn, filename, action, docid); |
|
692 |
if ((docid != null) && (!docid.equals(newdocid))) { |
|
693 |
if (action.equals("INSERT")) { |
|
694 |
System.out.println("New document ID generated!!! "); |
|
695 |
} else if (action.equals("UPDATE")) { |
|
696 |
System.out.println("ERROR: Couldn't update document!!! "); |
|
694 | 697 |
} |
695 |
System.out.println("Document processing finished for: " + filename
|
|
696 |
+ " (" + newdocid + ")");
|
|
698 |
} else if ((docid == null) && (action.equals("UPDATE"))) {
|
|
699 |
System.out.println("ERROR: Couldn't update document!!! ");
|
|
697 | 700 |
} |
701 |
System.out.println("Document processing finished for: " + filename |
|
702 |
+ " (" + newdocid + ")"); |
|
698 | 703 |
} |
699 | 704 |
|
700 | 705 |
} catch (McdbException me) { |
src/edu/ucsb/nceas/metacat/DBSAXNode.java | ||
---|---|---|
31 | 31 |
* Construct a new node instance for DOCUMENT nodes |
32 | 32 |
* |
33 | 33 |
* @param conn the JDBC Connection to which all information is written |
34 |
* @param tagname the name of the node |
|
35 | 34 |
*/ |
36 |
public DBSAXNode (Connection conn, String tagname) {
|
|
37 |
super(tagname);
|
|
35 |
public DBSAXNode (Connection conn) { |
|
36 |
super(); |
|
38 | 37 |
this.conn = conn; |
39 | 38 |
this.parentNode = null; |
40 |
writeChildNodeToDB("DOCUMENT", tagname, null); |
|
41 |
setRootNodeID(getNodeID()); |
|
39 |
writeChildNodeToDB("DOCUMENT", null, null); |
|
42 | 40 |
} |
43 | 41 |
|
44 | 42 |
/** |
... | ... | |
70 | 68 |
if (nodetype == "DOCUMENT") { |
71 | 69 |
pstmt = conn.prepareStatement( |
72 | 70 |
"INSERT INTO xml_nodes " + |
73 |
"(nodeid, nodetype, nodename) " + |
|
74 |
"VALUES (?, ?, ?)"); |
|
71 |
"(nodeid, nodetype, nodename, rootnodeid) " +
|
|
72 |
"VALUES (?, ?, ?, ?)");
|
|
75 | 73 |
MetaCatUtil.debugMessage("INSERTING DOCNAME: " + nodename); |
76 | 74 |
} else { |
77 | 75 |
pstmt = conn.prepareStatement( |
78 | 76 |
"INSERT INTO xml_nodes " + |
79 | 77 |
"(nodeid, nodetype, nodename, " + |
80 |
"parentnodeid, rootnodeid, docid, nodedata, nodeindex) " +
|
|
78 |
"rootnodeid, parentnodeid, docid, nodedata, nodeindex) " +
|
|
81 | 79 |
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); |
82 | 80 |
} |
83 | 81 |
|
... | ... | |
86 | 84 |
pstmt.setLong(1, nid); |
87 | 85 |
pstmt.setString(2, nodetype); |
88 | 86 |
pstmt.setString(3, nodename); |
89 |
if (nodetype != "DOCUMENT") { |
|
87 |
if (nodetype == "DOCUMENT") { |
|
88 |
pstmt.setLong(4, nid); |
|
89 |
} else { |
|
90 | 90 |
if (nodetype == "ELEMENT") { |
91 |
pstmt.setLong(4, getParentID());
|
|
92 |
pstmt.setLong(5, getRootNodeID());
|
|
91 |
pstmt.setLong(4, getRootNodeID());
|
|
92 |
pstmt.setLong(5, getParentID());
|
|
93 | 93 |
pstmt.setString(6, getDocID()); |
94 | 94 |
pstmt.setString(7, data); |
95 | 95 |
pstmt.setInt(8, getNodeIndex()); |
96 | 96 |
} else { |
97 |
pstmt.setLong(4, getNodeID()); |
|
98 |
pstmt.setLong(5, getRootNodeID());
|
|
97 |
pstmt.setLong(4, getRootNodeID());
|
|
98 |
pstmt.setLong(5, getNodeID()); |
|
99 | 99 |
pstmt.setString(6, getDocID()); |
100 | 100 |
pstmt.setString(7, data); |
101 | 101 |
pstmt.setInt(8, incChildNum()); |
... | ... | |
105 | 105 |
pstmt.execute(); |
106 | 106 |
pstmt.close(); |
107 | 107 |
|
108 |
if (nodetype.equals("DOCUMENT")) { |
|
109 |
// Record the root node id that was generated from the database |
|
110 |
setRootNodeID(nid); |
|
111 |
} |
|
112 |
|
|
108 | 113 |
if (nodetype.equals("DOCUMENT") || nodetype.equals("ELEMENT")) { |
109 | 114 |
|
110 | 115 |
// Record the node id that was generated from the database |
... | ... | |
146 | 151 |
} |
147 | 152 |
|
148 | 153 |
/** |
149 |
* creates SQL code to put root node id for the document node |
|
150 |
* into DB connection |
|
151 |
*/ |
|
152 |
public void writeRootNodeID(long rootnode_id) { |
|
153 |
try { |
|
154 |
PreparedStatement pstmt; |
|
155 |
pstmt = conn.prepareStatement( |
|
156 |
"UPDATE xml_nodes set rootnodeid = ? " + |
|
157 |
"WHERE nodeid = ?"); |
|
158 |
|
|
159 |
// Bind the values to the query |
|
160 |
pstmt.setLong(1, rootnode_id); |
|
161 |
pstmt.setLong(2, getNodeID()); |
|
162 |
// Do the insertion |
|
163 |
pstmt.execute(); |
|
164 |
pstmt.close(); |
|
165 |
} catch (SQLException e) { |
|
166 |
System.out.println(e.getMessage()); |
|
167 |
} |
|
168 |
} |
|
169 |
/** |
|
170 | 154 |
* creates SQL code to put doc ID for the document node and for |
171 | 155 |
* comment/PI nodes under document node into DB connection |
172 | 156 |
*/ |
src/edu/ucsb/nceas/metacat/MetaCatServlet.java | ||
---|---|---|
661 | 661 |
try { |
662 | 662 |
// get a connection from the pool |
663 | 663 |
conn = util.getConnection(); |
664 |
|
|
664 | 665 |
// write the document to the database |
665 |
DocumentImpl doc = new DocumentImpl(conn); |
|
666 |
|
|
667 | 666 |
try { |
668 | 667 |
String accNumber = docid[0]; |
669 | 668 |
if (accNumber.equals("")) { |
670 | 669 |
accNumber = null; |
671 | 670 |
} |
672 |
newdocid = doc.write(xml, doAction, accNumber);
|
|
671 |
newdocid = DocumentImpl.write(conn, xml, doAction, accNumber);
|
|
673 | 672 |
} catch (NullPointerException npe) { |
674 |
newdocid = doc.write(xml, doAction, null);
|
|
673 |
newdocid = DocumentImpl.write(conn, xml, doAction, null);
|
|
675 | 674 |
} |
676 | 675 |
} catch (Exception e) { |
677 | 676 |
response.setContentType("text/html"); |
... | ... | |
724 | 723 |
try { |
725 | 724 |
// get a connection from the pool |
726 | 725 |
conn = util.getConnection(); |
727 |
DocumentImpl doc = new DocumentImpl(conn); |
|
728 | 726 |
// NOTE -- NEED TO TEST HERE |
729 |
// FOR EXISTENCE OF PARAM |
|
727 |
// FOR EXISTENCE OF DOCID PARAM
|
|
730 | 728 |
// BEFORE ACCESSING ARRAY |
731 | 729 |
try { |
732 |
doc.delete(docid[0]);
|
|
730 |
DocumentImpl.delete(conn, docid[0]);
|
|
733 | 731 |
response.setContentType("text/xml"); |
734 | 732 |
out.println("<?xml version=\"1.0\"?>"); |
735 | 733 |
out.println("<success>"); |
Also available in: Unified diff
Continued code redesign for the DocumentImpl class. Now the "delete" and
"write" methods are static, so a DocumentImpl objject need not be created
in order to initiate a INSERT, UPDATE, or DELETE action (it is created
implicitly by the DBSAXHandler). When doing a "READ" action, one still
creates a new DocumentImpl instance which represents the document that
was read from the db (and calling toString() on it returns an XML
serialized version of the DocumentImpl).
This brings us close to being able to use the DocumentImpl class as a
DOM Implementation of the DOM Document interface.