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 168 2000-06-16 03:20:41Z 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 String          docname;
26
    private String          doctype;
27
    private String          doctitle;
28
    private String          docid;
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, i.e. the name immediately following 
36
     *        the DOCTYPE keyword ( should be the root element name ) or
37
     *        the root element name if no DOCTYPE declaration provided
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, i.e. the name immediately 
41
     *                  following the PUBLIC keyword in DOCTYPE declaration or
42
     *                  the docname if no Public ID provided or
43
     *                  null if no DOCTYPE declaration provided
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
    }
55
    
56
    /** creates SQL code and inserts new document into DB connection */
57
    private void writeDocumentToDB() {
58
        try {
59
          PreparedStatement pstmt;
60
          pstmt = conn.prepareStatement(
61
                "INSERT INTO xml_documents " +
62
                "(docid, rootnodeid, docname, doctype) " +
63
                "VALUES (?, ?, ?, ?)");
64

    
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);
71
          // Do the insertion
72
          pstmt.execute();
73
          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
*/
87
        } catch (SQLException e) {
88
          System.out.println(e.getMessage());
89
        } catch (Exception e) {
90
          System.out.println(e.getMessage());
91
        }
92
    }
93

    
94
    /** Get doc id */
95
    public String getDocID() {
96
      return docid;
97
    }
98

    
99
    /**
100
     * Get the document title
101
     */
102
    public String getTitle() {
103
      return doctitle;
104
    }
105

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

    
120
        // Bind the values to the query
121
        pstmt.setString(1, doctitle);
122
        pstmt.setString(2, docid);
123

    
124
        // Do the insertion
125
        pstmt.execute();
126
        pstmt.close();
127
      } catch (SQLException e) {
128
        System.out.println(e.getMessage());
129
      }
130
    }
131

    
132
    /**
133
     * Look up the title of the first child element named "title"
134
     * and record it as the document title
135
     */
136
    public void setTitleFromChildElement() {
137
        String title = null;
138
        long assigned_id=0;
139
        PreparedStatement pstmt;
140
        try {
141
          pstmt = conn.prepareStatement(
142
                  "SELECT nodedata FROM xml_nodes " +
143
                  "WHERE nodetype = 'TEXT' " +
144
                  "AND rootnodeid = ? " +
145
                  "AND parentnodeid IN " +
146
                  "  (SELECT nodeid FROM xml_nodes " +
147
                  "  WHERE nodename = 'title' " +
148
                  "  AND nodetype =  'ELEMENT' " +
149
                  "  AND rootnodeid = ? ) " +
150
                  "ORDER BY nodeid");
151

    
152
          // The above query might be slow, and probably will be because
153
          // it gets ALL of the title elements while searching for one
154
          // title in a small subtree but it avoids the problem of using
155
          // Oracle's Hierarchical Query syntax which is not portable --
156
          // the commented out SQL that follows shows an equivalent query
157
          // using Oracle-specific hierarchical query
158
/*
159
          pstmt = conn.prepareStatement(
160
                  "SELECT nodedata FROM xml_nodes " +
161
                  "WHERE nodetype = 'TEXT' " +
162
                  "AND parentnodeid IN " +
163
                  "(SELECT nodeid FROM xml_nodes " +
164
                  "WHERE nodename = 'title' " +
165
                  "START WITH nodeid = ? " +
166
                  "CONNECT BY PRIOR nodeid = parentnodeid)");
167
*/
168

    
169
          // Bind the values to the query
170
          pstmt.setLong(1, rootnodeid);
171
          pstmt.setLong(2, rootnodeid);
172

    
173
          pstmt.execute();
174
          ResultSet rs = pstmt.getResultSet();
175
          boolean tableHasRows = rs.next();
176
          if (tableHasRows) {
177
            title = rs.getString(1);
178
          }
179
          pstmt.close();
180
        } catch (SQLException e) {
181
          System.out.println("Error getting id: " + e.getMessage());
182
        }
183

    
184
        // assign the new title
185
        this.setTitle(title);
186
    }
187
}
(7-7/20)