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 152 2000-06-13 21:23:35Z bojilova $'
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 long            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 (null, ?, ?, ?)");
64

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

    
73
          long assigned_id=0;
74
          Statement stmt;
75
          stmt = conn.createStatement();
76
          stmt.execute("SELECT xml_documents_id_seq.currval FROM dual");
77
          ResultSet rs = stmt.getResultSet();
78
          boolean tableHasRows = rs.next();
79
          if (tableHasRows) {
80
            assigned_id = rs.getLong(1);
81
            this.docid = assigned_id;
82
          }
83
          stmt.close();
84

    
85
        } catch (SQLException e) {
86
          System.out.println(e.getMessage());
87
        }
88
    }
89

    
90
    /** Get doc id */
91
    public long getDocID() {
92
      return docid;
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(
138
                  "SELECT nodedata FROM xml_nodes " +
139
                  "WHERE nodetype = 'TEXT' " +
140
                  "AND rootnodeid = ? " +
141
                  "AND parentnodeid IN " +
142
                  "  (SELECT nodeid FROM xml_nodes " +
143
                  "  WHERE nodename = 'title' " +
144
                  "  AND nodetype =  'ELEMENT' " +
145
                  "  AND rootnodeid = ? ) " +
146
                  "ORDER BY nodeid");
147

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

    
165
          // Bind the values to the query
166
          pstmt.setLong(1, rootnodeid);
167
          pstmt.setLong(2, rootnodeid);
168

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

    
180
        // assign the new title
181
        this.setTitle(title);
182
    }
183
}
(7-7/20)