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: DBSAXElement.java 133 2000-06-07 22:40:05Z 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 element instance
28
   *
29
   * @param conn the JDBC Connection to which all information is written
30
   * @param tagname the name of the element
31
   * @param parent_id the parent id number for this element
32
   */
33
  public DBSAXElement (Connection conn, String tagname, 
34
                       long parent_id, int nodeIndex) {
35

    
36
    super(tagname, parent_id, nodeIndex);
37

    
38
    this.conn = conn;
39
    if (getParentID() != 0) {
40
      writeChildNodeToDB("ELEMENT", getTagName(), null, getNodeIndex());
41
    } else {
42
      writeChildNodeToDB("DOCUMENT", getTagName(), null, 0);
43
    }
44

    
45
    setElementID(getAssignedElementID());
46
  }
47
    
48
  /** creates SQL code and inserts new node into DB connection */
49
  public void writeChildNodeToDB(String nodetype, String nodename,
50
                                  String data, int nodeIndex) {
51
    try {
52
      PreparedStatement pstmt;
53
      if (nodetype == "DOCUMENT") {
54
        pstmt = conn.prepareStatement(
55
            "INSERT INTO xml_nodes " +
56
            "(nodeid, nodetype, nodename) " +
57
            "VALUES (null, ?, ?)");
58
      } else {
59
        pstmt = conn.prepareStatement(
60
            "INSERT INTO xml_nodes " +
61
            "(nodeid, nodetype, nodename, " +
62
            "parentnodeid, nodedata, nodeindex) " +
63
            "VALUES (null, ?, ?, ?, ?, ?)");
64
      }
65

    
66
      // Bind the values to the query
67
      pstmt.setString(1, nodetype);
68
      pstmt.setString(2, nodename);
69
      if (nodetype != "DOCUMENT") {
70
        if (nodetype == "ELEMENT") {
71
          pstmt.setLong(3, getParentID());
72
        } else {
73
          pstmt.setLong(3, getElementID());
74
        }
75
        pstmt.setString(4, data);
76
        pstmt.setInt(5, nodeIndex);
77
      }
78
      // Do the insertion
79
      pstmt.execute();
80
      pstmt.close();
81
    } catch (SQLException e) {
82
      System.err.println("Error inserting node: (" + nodetype + ", " +
83
                                                     nodename + ", " + 
84
                                                     data + ", " + 
85
                                                     nodeIndex + ")" );
86

    
87
      System.err.println(e.getMessage());
88
    }
89
  }
90

    
91
  /** 
92
   * creates SQL code to put nodename for the document node 
93
   * into DB connection 
94
   */
95
  public void writeNodename(String nodename) {
96
      try {
97
        PreparedStatement pstmt;
98
        pstmt = conn.prepareStatement(
99
              "UPDATE xml_nodes set nodename = ? " +
100
              "WHERE nodeid = ?");
101

    
102
        // Bind the values to the query
103
        pstmt.setString(1, nodename);
104
        pstmt.setLong(2, getElementID());
105
        // Do the insertion
106
        pstmt.execute();
107
        pstmt.close();
108
      } catch (SQLException e) {
109
        System.out.println(e.getMessage());
110
      }
111
  }
112

    
113
  /** look up the assigned element id from DB connection */
114
  private long getAssignedElementID() {
115
      long assigned_id=0;
116
      Statement stmt;
117
      try {
118
        stmt = conn.createStatement();
119
        stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
120
        ResultSet rs = stmt.getResultSet();
121
        boolean tableHasRows = rs.next();
122
        if (tableHasRows) {
123
          assigned_id = rs.getLong(1);
124
        }
125
        stmt.close();
126
      } catch (SQLException e) {
127
        System.out.println("Error getting id: " + e.getMessage());
128
      }
129

    
130
      // assign the new ID number
131
      return assigned_id;
132
  }
133

    
134
  /** Add a new attribute to this element, or set its value */
135
  public void setAttribute(String attName, String attValue) {
136
    if (attName != null) {
137
      // Enter the attribute in the hash table
138
      super.setAttribute(attName, attValue);
139

    
140
      writeChildNodeToDB("ATTRIBUTE", attName, attValue, 0);
141
      // And enter the attribute in the database
142
    } else {
143
      System.err.println("Attribute name must not be null!");
144
    }
145
  }
146
}
(6-6/19)