Project

General

Profile

1
/**
2
 *      Name: DBSAXNode.java
3
 *   Purpose: A Class that represents an XML node and its contents
4
 * Copyright: 2000 Regents of the University of California and the
5
 *            National Center for Ecological Analysis and Synthesis
6
 *   Authors: Matt Jones
7
 *
8
 *   Version: '$Id: DBSAXNode.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 represents an XML node and its contents and
20
 * can write its own representation to a database connection
21
 */
22
public class DBSAXNode extends BasicNode {
23

    
24
  private Connection		conn;
25

    
26
  /** 
27
   * Construct a new node instance for DOCUMENT nodes
28
   *
29
   * @param conn the JDBC Connection to which all information is written
30
   * @param tagname the name of the node
31
   */
32
  public DBSAXNode (Connection conn, String tagname) {
33
    super(tagname);
34
    this.conn = conn;
35
    writeChildNodeToDB("DOCUMENT", tagname, null);
36
    setNodeID(getAssignedNodeID());
37
    setRootNodeID(getNodeID());
38
  }
39

    
40
  /** 
41
   * Construct a new node instance for ELEMENT nodes
42
   *
43
   * @param conn the JDBC Connection to which all information is written
44
   * @param tagname the name of the node
45
   * @param parentNode the parent node for this node being created
46
   */
47
  public DBSAXNode (Connection conn, String tagname, 
48
                    DBSAXNode parentNode, DBSAXNode rootNode, DBSAXDocument currentDocument) {
49

    
50
    super(tagname);
51
    setParentID(parentNode.getNodeID());
52
    setRootNodeID(rootNode.getNodeID());
53
    setDocID(currentDocument.getDocID());
54
    setNodeIndex(parentNode.incChildNum());
55
    this.conn = conn;
56
    writeChildNodeToDB("ELEMENT", getTagName(), null);
57
    setNodeID(getAssignedNodeID());
58
  }
59
    
60
  /** creates SQL code and inserts new node into DB connection */
61
  public void writeChildNodeToDB(String nodetype, String nodename,
62
                                 String data) {
63
    try {
64
      PreparedStatement pstmt;
65
      if (nodetype == "DOCUMENT") {
66
        pstmt = conn.prepareStatement(
67
            "INSERT INTO xml_nodes " +
68
            "(nodeid, nodetype, nodename) " +
69
            "VALUES (null, ?, ?)");
70
      } else {
71
        pstmt = conn.prepareStatement(
72
            "INSERT INTO xml_nodes " +
73
            "(nodeid, nodetype, nodename, " +
74
            "parentnodeid, rootnodeid, docid, nodedata, nodeindex) " +
75
            "VALUES (null, ?, ?, ?, ?, ?, ?, ?)");
76
      }
77

    
78
      // Bind the values to the query
79
      pstmt.setString(1, nodetype);
80
      pstmt.setString(2, nodename);
81
      if (nodetype != "DOCUMENT") {
82
        if (nodetype == "ELEMENT") {
83
          pstmt.setLong(3, getParentID());
84
          pstmt.setLong(4, getRootNodeID());
85
          pstmt.setString(5, getDocID());
86
          pstmt.setString(6, data);
87
          pstmt.setInt(7, getNodeIndex());
88
        } else {
89
          pstmt.setLong(3, getNodeID());
90
          pstmt.setLong(4, getRootNodeID());
91
          pstmt.setString(5, getDocID());
92
          pstmt.setString(6, data);
93
          pstmt.setInt(7, incChildNum());
94
        }
95
      }
96
      // Do the insertion
97
      pstmt.execute();
98
      pstmt.close();
99
    } catch (SQLException e) {
100
      System.err.println("Error inserting node: (" + nodetype + ", " +
101
                                                     nodename + ", " + 
102
                                                     data + ")" );
103
      System.err.println(e.getMessage());
104
    }
105
  }
106

    
107
  /** 
108
   * creates SQL code to put nodename for the document node 
109
   * into DB connection 
110
   */
111
  public void writeNodename(String nodename) {
112
      try {
113
        PreparedStatement pstmt;
114
        pstmt = conn.prepareStatement(
115
              "UPDATE xml_nodes set nodename = ? " +
116
              "WHERE nodeid = ?");
117

    
118
        // Bind the values to the query
119
        pstmt.setString(1, nodename);
120
        pstmt.setLong(2, getNodeID());
121
        // Do the insertion
122
        pstmt.execute();
123
        pstmt.close();
124
      } catch (SQLException e) {
125
        System.out.println(e.getMessage());
126
      }
127
  }
128

    
129
  /** 
130
   * creates SQL code to put root node id for the document node 
131
   * into DB connection 
132
   */
133
  public void writeRootNodeID(long rootnode_id) {
134
      try {
135
        PreparedStatement pstmt;
136
        pstmt = conn.prepareStatement(
137
              "UPDATE xml_nodes set rootnodeid = ? " +
138
              "WHERE nodeid = ?");
139

    
140
        // Bind the values to the query
141
        pstmt.setLong(1, rootnode_id);
142
        pstmt.setLong(2, getNodeID());
143
        // Do the insertion
144
        pstmt.execute();
145
        pstmt.close();
146
      } catch (SQLException e) {
147
        System.out.println(e.getMessage());
148
      }
149
  }
150
  /** 
151
   * creates SQL code to put doc ID for the document node and for comment/PI nodes under document node
152
   * into DB connection 
153
   */
154
  public void writeDocID(String doc_id) {
155
      try {
156
        PreparedStatement pstmt;
157
        pstmt = conn.prepareStatement(
158
              "UPDATE xml_nodes set docid = ? " +
159
              "WHERE nodeid = ?");
160

    
161
        // Bind the values to the query
162
        pstmt.setString(1, doc_id);
163
        pstmt.setLong(2, getNodeID());
164
        // Do the insertion
165
        pstmt.execute();
166
        pstmt.close();
167

    
168
        // for comments and PI on the top
169
        pstmt = conn.prepareStatement(
170
              "UPDATE xml_nodes set docid = ? " +
171
              "WHERE parentnodeid = ?");
172
        // Bind the values to the query
173
        pstmt.setString(1, doc_id);
174
        pstmt.setLong(2, getNodeID());
175
        // Do the insertion
176
        pstmt.execute();
177
        pstmt.close();
178
      } catch (SQLException e) {
179
        System.out.println(e.getMessage());
180
      }
181
  }
182

    
183
  /** look up the assigned node id from DB connection */
184
  private long getAssignedNodeID() {
185
      long assigned_id=0;
186
      Statement stmt;
187
      try {
188
        stmt = conn.createStatement();
189
        stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
190
        ResultSet rs = stmt.getResultSet();
191
        boolean tableHasRows = rs.next();
192
        if (tableHasRows) {
193
          assigned_id = rs.getLong(1);
194
        }
195
        stmt.close();
196
      } catch (SQLException e) {
197
        System.out.println("Error getting id: " + e.getMessage());
198
      }
199

    
200
      // assign the new ID number
201
      return assigned_id;
202
  }
203

    
204
  /** Add a new attribute to this node, or set its value */
205
  public void setAttribute(String attName, String attValue) {
206
    if (attName != null) {
207
      // Enter the attribute in the hash table
208
      super.setAttribute(attName, attValue);
209

    
210
      // And enter the attribute in the database
211
      writeChildNodeToDB("ATTRIBUTE", attName, attValue);
212
    } else {
213
      System.err.println("Attribute name must not be null!");
214
    }
215
  }
216
}
(9-9/20)