Revision 203
Added by Matt Jones almost 24 years ago
DBWriter.java | ||
---|---|---|
1 | 1 |
/** |
2 |
* Name: DBWriter.java
|
|
3 |
* Purpose: A Class that reads in an XML text document and |
|
4 |
* write its contents to a database connection using SAX |
|
5 |
* Copyright: 2000 Regents of the University of California and the |
|
6 |
* National Center for Ecological Analysis and Synthesis |
|
7 |
* Authors: Matt Jones |
|
2 |
* '$RCSfile$'
|
|
3 |
* Purpose: A Class that reads in an XML text document and
|
|
4 |
* write its contents to a database connection using SAX
|
|
5 |
* Copyright: 2000 Regents of the University of California and the
|
|
6 |
* National Center for Ecological Analysis and Synthesis
|
|
7 |
* Authors: Matt Jones
|
|
8 | 8 |
* |
9 |
* Version: '$Id$' |
|
9 |
* '$Author$' |
|
10 |
* '$Date$' |
|
11 |
* '$Revision$' |
|
10 | 12 |
*/ |
11 | 13 |
|
12 | 14 |
package edu.ucsb.nceas.metacat; |
... | ... | |
35 | 37 |
private Connection conn = null; |
36 | 38 |
private String parserName = null; |
37 | 39 |
|
38 |
/** Default parser name. */ |
|
39 |
private static final String |
|
40 |
DEFAULT_PARSER = "org.apache.xerces.parsers.SAXParser"; |
|
41 |
//DEFAULT_PARSER = "oracle.xml.parser.v2.SAXParser"; |
|
42 |
|
|
43 | 40 |
/** |
44 | 41 |
* the main routine used to test the DBWriter utility. |
45 | 42 |
* <p> |
46 |
* Usage: java DBWriter <filename>
|
|
43 |
* Usage: java DBWriter <-f filename -a action -d docid>
|
|
47 | 44 |
* |
48 | 45 |
* @param filename the filename to be loaded into the database |
49 | 46 |
*/ |
50 | 47 |
static public void main(String[] args) { |
51 | 48 |
|
52 |
if (args.length < 1) |
|
53 |
{ |
|
49 |
try { |
|
50 |
String filename = null; |
|
51 |
String action = null; |
|
52 |
String docid = null; |
|
53 |
|
|
54 |
for ( int i=0 ; i < args.length; ++i ) { |
|
55 |
if ( args[i].equals( "-f" ) ) { |
|
56 |
filename = args[++i]; |
|
57 |
} else if ( args[i].equals( "-a" ) ) { |
|
58 |
action = args[++i]; |
|
59 |
} else if ( args[i].equals( "-d" ) ) { |
|
60 |
docid = args[++i]; |
|
61 |
} else { |
|
62 |
System.err.println |
|
63 |
( " args[" +i+ "] '" +args[i]+ "' ignored." ); |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
boolean argsAreValid = false; |
|
68 |
if (action != null) { |
|
69 |
if (action.equals("INSERT")) { |
|
70 |
if (filename != null) { |
|
71 |
argsAreValid = true; |
|
72 |
} |
|
73 |
} else if (action.equals("UPDATE")) { |
|
74 |
if ((filename != null) && (docid != null)) { |
|
75 |
argsAreValid = true; |
|
76 |
} |
|
77 |
} else if (action.equals("DELETE")) { |
|
78 |
if (docid != null) { |
|
79 |
argsAreValid = true; |
|
80 |
} |
|
81 |
} |
|
82 |
} |
|
83 |
|
|
84 |
if (!argsAreValid) { |
|
54 | 85 |
System.err.println("Wrong number of arguments!!!"); |
55 |
System.err.println("USAGE: java DBWriter <filename>"); |
|
86 |
System.err.println( |
|
87 |
"USAGE: java DBWriter <-f filename -a action -d docid>"); |
|
56 | 88 |
return; |
57 |
} else { |
|
58 |
try { |
|
59 |
|
|
60 |
String filename = args[0]; |
|
89 |
} |
|
90 |
|
|
91 |
// Open a connection to the database |
|
92 |
MetaCatUtil util = new MetaCatUtil(); |
|
93 |
Connection dbconn = util.openDBConnection(); |
|
61 | 94 |
|
62 |
// Open a connection to the database |
|
63 |
MetaCatUtil util = new MetaCatUtil(); |
|
64 |
Connection dbconn = util.openDBConnection(); |
|
95 |
DBWriter dbw = new DBWriter(dbconn, util.getOption("saxparser")); |
|
96 |
if (action.equals("DELETE")) { |
|
97 |
dbw.delete(docid); |
|
98 |
System.out.println("Document deleted: " + docid); |
|
99 |
} else { |
|
100 |
String newdocid = dbw.write(filename, action, docid); |
|
101 |
if ((docid != null) && (!docid.equals(newdocid))) { |
|
102 |
if (action.equals("INSERT")) { |
|
103 |
System.out.println("New document ID generated!!! "); |
|
104 |
} else if (action.equals("UPDATE")) { |
|
105 |
System.out.println("ERROR: Couldn't update document!!! "); |
|
106 |
} |
|
107 |
} else if ((docid == null) && (action.equals("UPDATE"))) { |
|
108 |
System.out.println("ERROR: Couldn't update document!!! "); |
|
109 |
} |
|
110 |
System.out.println("Document processing finished for: " + filename |
|
111 |
+ " (" + newdocid + ")"); |
|
112 |
} |
|
65 | 113 |
|
66 |
new DBWriter(filename, dbconn, DEFAULT_PARSER); |
|
67 |
System.out.println("Document processing finished for: " + filename); |
|
68 |
|
|
69 |
} catch (Exception e) { |
|
70 |
System.err.println("EXCEPTION HANDLING REQUIRED"); |
|
71 |
System.err.println(e.getMessage()); |
|
72 |
e.printStackTrace(System.err); |
|
73 |
} |
|
74 |
} |
|
114 |
} catch (AccessionNumberException ane) { |
|
115 |
System.out.println("ERROR: Couldn't delete document!!! "); |
|
116 |
System.out.println(ane.getMessage()); |
|
117 |
} catch (Exception e) { |
|
118 |
System.err.println("EXCEPTION HANDLING REQUIRED"); |
|
119 |
System.err.println(e.getMessage()); |
|
120 |
e.printStackTrace(System.err); |
|
121 |
} |
|
75 | 122 |
} |
76 | 123 |
|
77 | 124 |
/** |
78 | 125 |
* construct a new instance of the class to write an XML file to the database |
79 | 126 |
* |
80 |
* @param xml the xml stream to be loaded into the database |
|
81 | 127 |
* @param conn the database connection to which to write the XML file |
82 | 128 |
* @param parserName the name of a SAX2 compliant parser class |
83 | 129 |
*/ |
84 |
public DBWriter( Reader xml, Connection conn, String parserName) |
|
85 |
throws IOException, |
|
86 |
SQLException, |
|
87 |
ClassNotFoundException { |
|
130 |
public DBWriter(Connection conn, String parserName) { |
|
88 | 131 |
this.conn = conn; |
89 | 132 |
this.parserName = parserName; |
133 |
} |
|
90 | 134 |
|
135 |
/** |
|
136 |
* Insert XML stream to the database |
|
137 |
* |
|
138 |
* @param xml the xml stream to be loaded into the database |
|
139 |
*/ |
|
140 |
public String write( Reader xml, String action, String docid ) |
|
141 |
throws IOException, SQLException, ClassNotFoundException { |
|
142 |
|
|
91 | 143 |
try { |
92 |
XMLReader parser = initializeParser(); |
|
144 |
XMLReader parser = initializeParser(action, docid);
|
|
93 | 145 |
conn.setAutoCommit(false); |
94 | 146 |
parser.parse(new InputSource(xml)); |
95 | 147 |
conn.commit(); |
96 | 148 |
conn.setAutoCommit(true); |
149 |
return docid; |
|
97 | 150 |
} catch (SAXParseException e) { |
98 | 151 |
conn.rollback(); |
99 | 152 |
System.err.println(e.getMessage()); |
153 |
return null; |
|
100 | 154 |
} catch (SAXException e) { |
101 |
conn.rollback(); |
|
102 |
System.err.println(e.getMessage()); |
|
155 |
// WHAT HAPPENS HERE IF THIS ISNT ACTUALLY AN AccessionNumberException? |
|
156 |
// IT CAUSES PROBLEMS! |
|
157 |
AccessionNumberException ane = null; |
|
158 |
try { |
|
159 |
ane = (AccessionNumberException)e.getException(); |
|
160 |
} catch (ClassCastException cce) { |
|
161 |
return (e.getMessage()); |
|
162 |
} |
|
163 |
if (ane != null) { |
|
164 |
MetaCatUtil.debugMessage("DBWRITER LINE 120"); |
|
165 |
MetaCatUtil.debugMessage(ane.getMessage()); |
|
166 |
conn.commit(); |
|
167 |
conn.setAutoCommit(true); |
|
168 |
return (ane.getMessage()); |
|
169 |
} else { |
|
170 |
System.err.println(e.getMessage()); |
|
171 |
conn.rollback(); |
|
172 |
return null; |
|
173 |
} |
|
103 | 174 |
} catch (Exception e) { |
104 | 175 |
conn.rollback(); |
105 | 176 |
System.err.println(e.toString()); |
177 |
return null; |
|
106 | 178 |
} |
107 | 179 |
} |
108 | 180 |
|
109 | 181 |
/** |
110 |
* construct a new instance of the class to write an XML file to the database
|
|
182 |
* Insert an XML file to the database
|
|
111 | 183 |
* |
112 | 184 |
* @param filename the filename to be loaded into the database |
113 |
* @param conn the database connection to which to write the XML file |
|
114 |
* @param parserName the name of a SAX2 compliant parser class |
|
115 | 185 |
*/ |
116 |
public DBWriter( String filename, Connection conn, String parserName) |
|
117 |
throws IOException, |
|
118 |
SQLException, |
|
119 |
ClassNotFoundException { |
|
120 |
this(new FileReader(new File(filename).toString()), conn, parserName); |
|
186 |
public String write( String filename, String action, String docid ) |
|
187 |
throws IOException, SQLException, ClassNotFoundException { |
|
188 |
return write(new FileReader(new File(filename).toString()), action, docid); |
|
121 | 189 |
} |
122 | 190 |
|
123 |
private XMLReader initializeParser() { |
|
191 |
private XMLReader initializeParser(String action, String docid) {
|
|
124 | 192 |
XMLReader parser = null; |
125 | 193 |
// |
126 | 194 |
// Set up the SAX document handlers for parsing |
127 | 195 |
// |
128 | 196 |
try { |
129 |
ContentHandler xmlDocHandler = new DBSAXHandler(conn);
|
|
197 |
ContentHandler xmlDocHandler = new DBSAXHandler(conn, action, docid);
|
|
130 | 198 |
EntityResolver xmlEntityResolver = new DBEntityResolver(conn); |
131 | 199 |
DTDHandler xmlDTDHandler = new DBDTDHandler(conn); |
132 | 200 |
|
... | ... | |
152 | 220 |
|
153 | 221 |
return parser; |
154 | 222 |
} |
223 |
|
|
224 |
/** |
|
225 |
* Delete an XML file from the database (actually, just make it a revision |
|
226 |
* in the xml_revisions table) |
|
227 |
* |
|
228 |
* @param docid the ID of the document to be deleted from the database |
|
229 |
*/ |
|
230 |
public void delete( String docid ) |
|
231 |
throws IOException, SQLException, |
|
232 |
ClassNotFoundException, AccessionNumberException { |
|
233 |
|
|
234 |
String newdocid = AccessionNumber.generate(docid, "DELETE"); |
|
235 |
|
|
236 |
conn.setAutoCommit(false); |
|
237 |
// Copy the record to the xml_revisions table |
|
238 |
DBSAXDocument document = new DBSAXDocument(conn); |
|
239 |
document.saveDocument( docid ); |
|
240 |
|
|
241 |
// Now delete it from the xml_documents table |
|
242 |
Statement stmt = conn.createStatement(); |
|
243 |
stmt.execute("DELETE FROM xml_documents WHERE docid LIKE '" + docid + "'"); |
|
244 |
stmt.close(); |
|
245 |
conn.commit(); |
|
246 |
} |
|
155 | 247 |
} |
248 |
|
|
249 |
/** |
|
250 |
* '$Log$ |
|
251 |
* 'Revision 1.20.2.7 2000/06/26 10:18:06 jones |
|
252 |
* 'Partial fix for MetaCatServlet INSERT?UPDATE bug. Only will work on |
|
253 |
* 'the first call to the servlet. Subsequent calls fail. Seems to be |
|
254 |
* 'related to exception handling. Multiple successive DELETE actions |
|
255 |
* 'work fine. |
|
256 |
* ' |
|
257 |
* 'Revision 1.20.2.6 2000/06/26 08:38:02 jones |
|
258 |
* 'Added DELETE feature to DBWriter. Now takes an action "DELETE" and a |
|
259 |
* 'docid and will move the record from the xml_documents table to the |
|
260 |
* 'xml_revisions table. |
|
261 |
* 'Modified option parsing to support option symbols on command line. |
|
262 |
* ' |
|
263 |
* 'Revision 1.20.2.5 2000/06/26 02:02:20 jones |
|
264 |
* 'Continued fixing problems with exception handling that deals |
|
265 |
* 'with INSERT and UPDATE actions and the docid passed to DBWriter |
|
266 |
* ' |
|
267 |
* 'Revision 1.20.2.4 2000/06/26 00:51:06 jones |
|
268 |
* 'If docid passed to DBWriter.write() is not unique, classes now generate |
|
269 |
* 'an AccessionNumberException containing the new docid generated as a |
|
270 |
* 'replacement. The docid is then extracted from the exception and |
|
271 |
* 'returned to the calling application for user feedback or client processing. |
|
272 |
* ' |
|
273 |
* 'Revision 1.20.2.3 2000/06/25 23:38:16 jones |
|
274 |
* 'Added RCSfile keyword |
|
275 |
* ' |
|
276 |
* 'Revision 1.20.2.2 2000/06/25 23:34:18 jones |
|
277 |
* 'Changed documentation formatting, added log entries at bottom of source files |
|
278 |
* '' |
|
279 |
*/ |
Also available in: Unified diff
Merged in substantial changes to DBWriter and associated classes and to
the MetaCatServlet in order to accomodate the new UPDATE and DELETE
functions. The command line tools and the parameters for the
servlet have changed substantially.