Project

General

Profile

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
7
 *
8
 *     Version: '$Id: DBSAXDocument.java 98 2000-05-13 01:29:51Z jones $'
9
 */
10

    
11
package edu.ucsb.nceas.metacat;
12

    
13
import java.sql.*;
14
import java.io.IOException;
15
import java.util.Hashtable;
16
import java.util.Enumeration;
17

    
18
/** 
19
 * A Class that writes an XML document main data to a database connection
20
 */
21
public class DBSAXDocument {
22

    
23
    private Connection	    conn;
24
    private long            rootnodeid;
25
    private long            docid;
26
    private String          docname;
27
    private String          doctype;
28
    private String          doctitle;
29

    
30
    /** 
31
     * Construct a new document instance
32
     *
33
     * @param conn the JDBC Connection to which all information is written
34
     * @param rootnodeid - sequence id of the root node in the document
35
     * @param docname - the name of DTD, 
36
     *        i.e. the name immediately following the DOCTYPE keyword - 
37
     *        should be the root element name.
38
     *        (Oracle's and IBM parsers are not aware if it is not the 
39
     *        root element name)
40
     * @param doctype - Public ID of the DTD, 
41
     *        i.e. the name immediately following the PUBLIC keyword in 
42
     *        DOCTYPE declaration.
43
     * @param doctitle - document title from first child element named "title"
44
     *
45
     */
46
    public DBSAXDocument (Connection conn, long rootnodeid, String docname, 
47
                          String doctype)
48
    {
49
      this.conn = conn;
50
      this.rootnodeid = rootnodeid;
51
      this.docname = docname;
52
      this.doctype = doctype;
53
      writeDocumentToDB();
54
      System.out.println("DBSAXDocument.writeDocumentToDB");
55
    }
56
    
57
    /** creates SQL code and inserts new document into DB connection */
58
    private void writeDocumentToDB() {
59
        try {
60
          conn.setAutoCommit(false);
61
          PreparedStatement pstmt;
62
          pstmt = conn.prepareStatement(
63
                "INSERT INTO xml_documents " +
64
                "(docid, rootnodeid, docname, doctype, doctitle) " +
65
                "VALUES (null, ?, ?, ?, ?)");
66

    
67
          // Bind the values to the query
68
          pstmt.setLong(1, rootnodeid);
69
          pstmt.setString(2, docname);
70
          pstmt.setString(3, doctype);
71
          pstmt.setString(4, doctitle);
72
          // Do the insertion
73
          pstmt.execute();
74
          pstmt.close();
75

    
76
          long assigned_id=0;
77
          Statement stmt;
78
          stmt = conn.createStatement();
79
          stmt.execute("SELECT xml_documents_id_seq.currval FROM dual");
80
          ResultSet rs = stmt.getResultSet();
81
          boolean tableHasRows = rs.next();
82
          if (tableHasRows) {
83
            assigned_id = rs.getLong(1);
84
            this.docid = assigned_id;
85
          }
86
          stmt.close();
87
  
88
          conn.commit();
89
          conn.setAutoCommit(true);
90
        } catch (SQLException e) {
91
          System.out.println(e.getMessage());
92
        }
93
    }
94

    
95
    /** 
96
     * Get the document title
97
     */
98
    public String getTitle() {
99
      return doctitle;
100
    }
101

    
102
    /** 
103
     * Set the document title
104
     *
105
     * @param title the new title for the document
106
     */
107
    public void setTitle( String title ) {
108
      this.doctitle = title;
109
      try {
110
        PreparedStatement pstmt;
111
        pstmt = conn.prepareStatement(
112
              "UPDATE xml_documents " +
113
              " SET doctitle = ? " +
114
              "WHERE docid = ?");
115

    
116
        // Bind the values to the query
117
        pstmt.setString(1, doctitle);
118
        pstmt.setLong(2, docid);
119

    
120
        // Do the insertion
121
        pstmt.execute();
122
        pstmt.close();
123
      } catch (SQLException e) {
124
        System.out.println(e.getMessage());
125
      }
126
    }
127

    
128
    /**
129
     * Look up the title of the first child element named "title"
130
     * and record it as the document title
131
     */
132
    public void setTitleFromChildElement() {
133
        String title = null;
134
        long assigned_id=0;
135
        PreparedStatement pstmt;
136
        try {
137
          pstmt = conn.prepareStatement("SELECT nodedata " +
138
                    "FROM xml_nodes " +
139
                    "WHERE nodename = 'title' " +
140
                    "START WITH nodeid = ? " +
141
                    "CONNECT BY PRIOR nodeid = parentnodeid ");
142

    
143
          // Bind the values to the query
144
          pstmt.setLong(1, rootnodeid);
145

    
146
          pstmt.execute();
147
          try {
148
            ResultSet rs = pstmt.getResultSet();
149
            try {
150
              boolean tableHasRows = rs.next();
151
              if (tableHasRows) {
152
                try {
153
                  title = rs.getString(1);
154
                } catch (SQLException e) {
155
                  System.out.println("Error with getString: " + e.getMessage());                }
156
              }
157
            } catch (SQLException e) {
158
              System.out.println("Error with next: " + e.getMessage());
159
            }
160
          } catch (SQLException e) {
161
            System.out.println("Error with getrset: " + e.getMessage());
162
          }
163
          pstmt.close();
164
        } catch (SQLException e) {
165
          System.out.println("Error getting id: " + e.getMessage());
166
        }
167

    
168
        // assign the new title
169
        this.setTitle(title);
170
    }
171

    
172
}
(7-7/30)