Project

General

Profile

« Previous | Next » 

Revision 203

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.

View differences:

DBSAXDocument.java
1 1
/**
2
 *      Name: DBSAXDocument.java
3
 *   Purpose: A Class that writes an XML document main data
4
 * Copyright: 2000 Regents of the University of California and the
5
 *            National Center for Ecological Analysis and Synthesis
6
 *   Authors: Jivka Bojilova
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that writes an XML document main data
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Jivka Bojilova, Matt Jones
7 7
 *
8
 *   Version: '$Id$'
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
9 11
 */
10 12

  
11 13
package edu.ucsb.nceas.metacat;
......
44 46
     *
45 47
     */
46 48
    public DBSAXDocument (Connection conn, long rootnodeid, String docname, 
47
                          String doctype)
48
    {
49
                          String doctype, String docid, String action)
50
                          throws AccessionNumberException {
49 51
      this.conn = conn;
50 52
      this.rootnodeid = rootnodeid;
51 53
      this.docname = docname;
52 54
      this.doctype = doctype;
53
      writeDocumentToDB();
55
      this.docid = docid;
56
      writeDocumentToDB(action);
54 57
    }
55 58
    
59
    /** 
60
     * Construct a new document instance, used for deleting documents
61
     *
62
     * @param conn the JDBC Connection to which all information is written
63
     */
64
    public DBSAXDocument (Connection conn) {
65
      this.conn = conn;
66
    }
67

  
56 68
    /** creates SQL code and inserts new document into DB connection */
57
    private void writeDocumentToDB() {
69
    private void writeDocumentToDB(String action) 
70
                          throws AccessionNumberException {
58 71
        try {
59
          PreparedStatement pstmt;
60
          pstmt = conn.prepareStatement(
72
          PreparedStatement pstmt = null;
73

  
74
          if (action.equals("INSERT")) {
75
            this.docid = AccessionNumber.generate(docid, "INSERT");
76
            pstmt = conn.prepareStatement(
61 77
                "INSERT INTO xml_documents " +
62
                "(docid, rootnodeid, docname, doctype) " +
63
                "VALUES (?, ?, ?, ?)");
78
                "(docid, rootnodeid, docname, doctype, " +
79
                "date_created, date_updated) " +
80
                "VALUES (?, ?, ?, ?, sysdate, sysdate)");
81
            // Bind the values to the query
82
            pstmt.setString(1, this.docid);
83
            pstmt.setLong(2, rootnodeid);
84
            pstmt.setString(3, docname);
85
            pstmt.setString(4, doctype);
86
          } else if (action.equals("UPDATE")) {
64 87

  
65
          // Bind the values to the query
66
          this.docid = AccessionNumber.generate(null, "INSERT");
67
          pstmt.setString(1, this.docid);
68
          pstmt.setLong(2, rootnodeid);
69
          pstmt.setString(3, docname);
70
          pstmt.setString(4, doctype);
88
            // Determine if the docid is OK for an UPDATE
89
            this.docid = AccessionNumber.generate(docid, "UPDATE");
90

  
91
            // Save the old document entry in a backup table
92
            saveDocument(docid);
93

  
94
            // Update the new document to reflect the new node tree
95
            pstmt = conn.prepareStatement(
96
                "UPDATE xml_documents " +
97
                "SET rootnodeid = ?, docname = ?, doctype = ?, " +
98
                "date_updated = sysdate WHERE docid LIKE ?");
99
            // Bind the values to the query
100
            pstmt.setLong(1, rootnodeid);
101
            pstmt.setString(2, docname);
102
            pstmt.setString(3, doctype);
103
            pstmt.setString(4, this.docid);
104
          } else {
105
            System.err.println("Action not supported: " + action);
106
          }
107

  
71 108
          // Do the insertion
72 109
          pstmt.execute();
73 110
          pstmt.close();
74
/*
75
          long assigned_id=0;
76
          Statement stmt;
77
          stmt = conn.createStatement();
78
          stmt.execute("SELECT xml_documents_id_seq.currval FROM dual");
79
          ResultSet rs = stmt.getResultSet();
80
          boolean tableHasRows = rs.next();
81
          if (tableHasRows) {
82
            assigned_id = rs.getLong(1);
83
            this.docid = assigned_id;
84
          }
85
          stmt.close();
86
*/
111

  
87 112
        } catch (SQLException e) {
88 113
          System.out.println(e.getMessage());
114
        } catch (AccessionNumberException ane) {
115
          MetaCatUtil.debugMessage("Invalid accession number.");
116
          MetaCatUtil.debugMessage(ane.getMessage());
117
          throw ane;
89 118
        } catch (Exception e) {
90 119
          System.out.println(e.getMessage());
91 120
        }
......
184 213
        // assign the new title
185 214
        this.setTitle(title);
186 215
    }
216

  
217
  /** Save a document entry in the xml_revisions table */
218
  public void saveDocument(String docid) throws SQLException {
219
    // First get all of the values we need
220
    long rnodeid = -1;
221
    String docname = null;
222
    String doctype = null;
223
    String doctitle = null;
224
    Date date_created = null;
225
    PreparedStatement pstmt = conn.prepareStatement(
226
       "SELECT rootnodeid, docname, doctype, doctitle, date_created " +
227
       "FROM xml_documents " +
228
       "WHERE docid = ?");
229
    // Bind the values to the query and execute it
230
    pstmt.setString(1, docid);
231
    pstmt.execute();
232

  
233
    ResultSet rs = pstmt.getResultSet();
234
    boolean tableHasRows = rs.next();
235
    if (tableHasRows) {
236
      rnodeid      = rs.getLong(1);
237
      docname      = rs.getString(2);
238
      doctype      = rs.getString(3);
239
      doctitle     = rs.getString(4);
240
      date_created = rs.getDate(5);
241
    }
242
    pstmt.close();
243

  
244
    MetaCatUtil.debugMessage(new Long(rnodeid).toString());
245
    MetaCatUtil.debugMessage(docname);
246
    MetaCatUtil.debugMessage(doctitle);
247
    //MetaCatUtil.debugMessage(date_created.toString());
248

  
249
    // Next create the new record in the other table using the values selected
250
    pstmt = conn.prepareStatement(
251
       "INSERT INTO xml_revisions " +
252
       "(revisionid, docid, rootnodeid, docname, doctype, doctitle, " +
253
       "date_created, date_updated) " +
254
       "VALUES (null, ?, ?, ?, ?, ?, sysdate, sysdate)");
255
    // Bind the values to the query and execute it
256
    pstmt.setString(1, docid);
257
    pstmt.setLong(2, rnodeid);
258
    pstmt.setString(3, docname);
259
    pstmt.setString(4, doctype);
260
    pstmt.setString(5, doctitle);
261
    //pstmt.setDate(6, date_created);
262
    pstmt.execute();
263
    pstmt.close();
264
  }
187 265
}
266

  
267
/**
268
 * '$Log$
269
 * 'Revision 1.13.2.8  2000/06/26 08:38:02  jones
270
 * 'Added DELETE feature to DBWriter.  Now takes an action "DELETE" and a
271
 * 'docid and will move the record from the xml_documents table to the
272
 * 'xml_revisions table.
273
 * 'Modified option parsing to support option symbols on command line.
274
 * '
275
 * 'Revision 1.13.2.7  2000/06/26 03:04:31  jones
276
 * 'Completed UPDATE function.  Included new table "xml_revisions" to store a
277
 * 'single document record for each revision to a document.  It is identical
278
 * 'to "xml_documents" with the addition of a "revisionid" column to
279
 * 'allow more than one revision per docid.
280
 * '
281
 * 'Revision 1.13.2.6  2000/06/26 02:02:20  jones
282
 * 'Continued fixing problems with exception handling that deals
283
 * 'with INSERT and UPDATE actions and the docid passed to DBWriter
284
 * '
285
 * 'Revision 1.13.2.5  2000/06/25 23:38:16  jones
286
 * 'Added RCSfile keyword
287
 * '
288
 * 'Revision 1.13.2.4  2000/06/25 23:34:17  jones
289
 * 'Changed documentation formatting, added log entries at bottom of source files
290
 * ''
291
 */

Also available in: Unified diff