Project

General

Profile

1
/**
2
 *        Name: DBSAXElement.java
3
 *     Purpose: A Class that represents an XML element 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 136 2000-06-08 00:19:37Z 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 element and its contents and
20
 * can write its own representation to a database connection
21
 */
22
public class DBSAXElement 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 DBSAXElement (Connection conn, String tagname) {
33
    super(tagname);
34
    this.conn = conn;
35
    writeChildNodeToDB("DOCUMENT", tagname, null);
36
    setNodeID(getAssignedNodeID());
37
  }
38

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

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

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

    
100
  /** 
101
   * creates SQL code to put nodename for the document node 
102
   * into DB connection 
103
   */
104
  public void writeNodename(String nodename) {
105
      try {
106
        PreparedStatement pstmt;
107
        pstmt = conn.prepareStatement(
108
              "UPDATE xml_nodes set nodename = ? " +
109
              "WHERE nodeid = ?");
110

    
111
        // Bind the values to the query
112
        pstmt.setString(1, nodename);
113
        pstmt.setLong(2, getNodeID());
114
        // Do the insertion
115
        pstmt.execute();
116
        pstmt.close();
117
      } catch (SQLException e) {
118
        System.out.println(e.getMessage());
119
      }
120
  }
121

    
122
  /** look up the assigned element id from DB connection */
123
  private long getAssignedNodeID() {
124
      long assigned_id=0;
125
      Statement stmt;
126
      try {
127
        stmt = conn.createStatement();
128
        stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
129
        ResultSet rs = stmt.getResultSet();
130
        boolean tableHasRows = rs.next();
131
        if (tableHasRows) {
132
          assigned_id = rs.getLong(1);
133
        }
134
        stmt.close();
135
      } catch (SQLException e) {
136
        System.out.println("Error getting id: " + e.getMessage());
137
      }
138

    
139
      // assign the new ID number
140
      return assigned_id;
141
  }
142

    
143
  /** Add a new attribute to this element, or set its value */
144
  public void setAttribute(String attName, String attValue) {
145
    if (attName != null) {
146
      // Enter the attribute in the hash table
147
      super.setAttribute(attName, attValue);
148

    
149
      // And enter the attribute in the database
150
      writeChildNodeToDB("ATTRIBUTE", attName, attValue);
151
    } else {
152
      System.err.println("Attribute name must not be null!");
153
    }
154
  }
155
}
(8-8/19)