Project

General

Profile

1 15 jones
/**
2 168 jones
 *      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 15 jones
 *
8 168 jones
 *   Version: '$Id$'
9 15 jones
 */
10
11 75 jones
package edu.ucsb.nceas.metacat;
12 15 jones
13
import java.sql.*;
14
import java.io.IOException;
15
import java.util.Hashtable;
16
import java.util.Enumeration;
17
18 31 jones
/**
19 137 jones
 * A Class that represents an XML node and its contents and
20 31 jones
 * can write its own representation to a database connection
21
 */
22 137 jones
public class DBSAXNode extends BasicNode {
23 15 jones
24 133 jones
  private Connection		conn;
25 15 jones
26 133 jones
  /**
27 136 jones
   * Construct a new node instance for DOCUMENT nodes
28 133 jones
   *
29
   * @param conn the JDBC Connection to which all information is written
30 136 jones
   * @param tagname the name of the node
31 133 jones
   */
32 137 jones
  public DBSAXNode (Connection conn, String tagname) {
33 136 jones
    super(tagname);
34
    this.conn = conn;
35
    writeChildNodeToDB("DOCUMENT", tagname, null);
36
    setNodeID(getAssignedNodeID());
37 149 bojilova
    setRootNodeID(getNodeID());
38 136 jones
  }
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 137 jones
  public DBSAXNode (Connection conn, String tagname,
48 149 bojilova
                    DBSAXNode parentNode, DBSAXNode rootNode, DBSAXDocument currentDocument) {
49 21 jones
50 136 jones
    super(tagname);
51
    setParentID(parentNode.getNodeID());
52 149 bojilova
    setRootNodeID(rootNode.getNodeID());
53
    setDocID(currentDocument.getDocID());
54 136 jones
    setNodeIndex(parentNode.incChildNum());
55 133 jones
    this.conn = conn;
56 136 jones
    writeChildNodeToDB("ELEMENT", getTagName(), null);
57 134 jones
    setNodeID(getAssignedNodeID());
58 133 jones
  }
59 15 jones
60 133 jones
  /** creates SQL code and inserts new node into DB connection */
61
  public void writeChildNodeToDB(String nodetype, String nodename,
62 136 jones
                                 String data) {
63 133 jones
    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 149 bojilova
            "parentnodeid, rootnodeid, docid, nodedata, nodeindex) " +
75
            "VALUES (null, ?, ?, ?, ?, ?, ?, ?)");
76 133 jones
      }
77 19 jones
78 133 jones
      // 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 149 bojilova
          pstmt.setLong(4, getRootNodeID());
85 162 bojilova
          pstmt.setString(5, getDocID());
86 149 bojilova
          pstmt.setString(6, data);
87
          pstmt.setInt(7, getNodeIndex());
88 133 jones
        } else {
89 134 jones
          pstmt.setLong(3, getNodeID());
90 149 bojilova
          pstmt.setLong(4, getRootNodeID());
91 162 bojilova
          pstmt.setString(5, getDocID());
92 149 bojilova
          pstmt.setString(6, data);
93
          pstmt.setInt(7, incChildNum());
94 72 bojilova
        }
95 133 jones
      }
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 136 jones
                                                     data + ")" );
103 133 jones
      System.err.println(e.getMessage());
104 110 bojilova
    }
105 133 jones
  }
106 110 bojilova
107 133 jones
  /**
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 16 jones
118 133 jones
        // Bind the values to the query
119
        pstmt.setString(1, nodename);
120 134 jones
        pstmt.setLong(2, getNodeID());
121 133 jones
        // Do the insertion
122
        pstmt.execute();
123
        pstmt.close();
124
      } catch (SQLException e) {
125
        System.out.println(e.getMessage());
126
      }
127
  }
128 16 jones
129 149 bojilova
  /**
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 162 bojilova
  public void writeDocID(String doc_id) {
155 149 bojilova
      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 162 bojilova
        pstmt.setString(1, doc_id);
163 149 bojilova
        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 162 bojilova
        pstmt.setString(1, doc_id);
174 149 bojilova
        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 137 jones
  /** look up the assigned node id from DB connection */
184 134 jones
  private long getAssignedNodeID() {
185 133 jones
      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 19 jones
        }
195 133 jones
        stmt.close();
196
      } catch (SQLException e) {
197
        System.out.println("Error getting id: " + e.getMessage());
198 15 jones
      }
199
200 133 jones
      // assign the new ID number
201
      return assigned_id;
202
  }
203 18 jones
204 137 jones
  /** Add a new attribute to this node, or set its value */
205 133 jones
  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 18 jones
210 133 jones
      // And enter the attribute in the database
211 136 jones
      writeChildNodeToDB("ATTRIBUTE", attName, attValue);
212 133 jones
    } else {
213
      System.err.println("Attribute name must not be null!");
214 18 jones
    }
215 133 jones
  }
216 15 jones
}