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 74 2000-05-05 00:37:58Z 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 BasicElement {
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
      writeElementToDB();
40
    }
41
    
42
    /** creates SQL code and inserts new element into DB connection */
43
    private void writeElementToDB() {
44
        try {
45
          conn.setAutoCommit(false);
46
          PreparedStatement pstmt;
47
          if (getParentID() != 0) {
48
            pstmt = conn.prepareStatement(
49
                "INSERT INTO xml_nodes (nodeid, nodetype, " +
50
                "nodename, parentnodeid, nodeindex) VALUES (null, ?, ?, ?, ?)");
51
          } else {
52
            pstmt = conn.prepareStatement(
53
                "INSERT INTO xml_nodes (nodeid, nodetype, nodename) " +
54
                "VALUES (null, ?, ?)");
55
          }
56

    
57
          // Bind the values to the query
58
          pstmt.setString(1, "ELEMENT");
59
          pstmt.setString(2, getTagName());
60
          //pstmt.setInt(4, null);
61
          if (getParentID() != 0) {
62
            pstmt.setLong(3, getParentID());
63
            pstmt.setInt(4, getNodeIndex());
64
          }
65
          // Do the insertion
66
          pstmt.execute();
67
          pstmt.close();
68
          setElementID(getAssignedElementID());
69
          conn.commit();
70
          conn.setAutoCommit(true);
71
        } catch (SQLException e) {
72
          System.out.println(e.getMessage());
73
        }
74
    }
75

    
76
    /** creates SQL code and inserts comment into DB connection */
77
    void writeCommentToDB(String data) {
78
        try {
79
          PreparedStatement pstmt;
80
          pstmt = conn.prepareStatement(
81
                "INSERT INTO xml_nodes (nodeid, nodetype, " +
82
                "nodename, parentnodeid, nodedata) VALUES (null, ?, null, ?, ?)");
83

    
84
          // Bind the values to the query
85
          pstmt.setString(1, "COMMENT");
86
          pstmt.setLong(2, getElementID());
87
          pstmt.setString(3, data);
88
          // Do the insertion
89
          pstmt.execute();
90
          pstmt.close();
91
        } catch (SQLException e) {
92
          System.out.println(e.getMessage());
93
        }
94
    }
95

    
96
    /** look up the assigned element id from DB connection */
97
    private long getAssignedElementID() {
98
        long assigned_id=0;
99
        Statement stmt;
100
        try {
101
          stmt = conn.createStatement();
102
          stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
103
          try {
104
            ResultSet rs = stmt.getResultSet();
105
            try {
106
              boolean tableHasRows = rs.next();
107
              if (tableHasRows) {
108
                try {
109
                  assigned_id = rs.getLong(1);
110
                } catch (SQLException e) {
111
                  System.out.println("Error with getInt: " + e.getMessage());
112
                }
113
              }
114
            } catch (SQLException e) {
115
              System.out.println("Error with next: " + e.getMessage());
116
            }
117
          } catch (SQLException e) {
118
            System.out.println("Error with getrset: " + e.getMessage());
119
          }
120
          stmt.close();
121
        } catch (SQLException e) {
122
          System.out.println("Error getting id: " + e.getMessage());
123
        }
124

    
125
        // assign the new ID number
126
        return assigned_id;
127
    }
128

    
129
    /** Add a new attribute to this element, or set its value */
130
    public void setAttribute(String attName, String attValue) {
131
      if (attName != null) {
132
        // Enter the attribute in the hash table
133
        super.setAttribute(attName, attValue);
134
 
135
        // And enter the attribute in the database
136
        try {
137
          PreparedStatement pstmt = conn.prepareStatement(
138
              "INSERT INTO xml_nodes (nodeid, nodetype, " +
139
              "nodename, nodedata, parentnodeid) VALUES (null, ?, ?, ?, ?)");
140

    
141
          // Bind the values to the query
142
          pstmt.setString(1, "ATTRIBUTE");
143
          pstmt.setString(2, attName);
144
          pstmt.setString(3, attValue);
145
          pstmt.setLong(4, getElementID());
146
 
147
          // Do the insertion
148
          pstmt.execute();
149
          pstmt.close();
150
        } catch (SQLException e) {
151
          System.out.println(e.getMessage());
152
        }
153

    
154
      } else {
155
        System.err.println("Attribute name must not be null!");
156
      }
157
    }
158

    
159
    /** Write the element content to the db connection */
160
    public void writeContentToDB() {
161
        try {
162
          PreparedStatement pstmt = conn.prepareStatement(
163
                "UPDATE xml_nodes SET nodedata = ? WHERE nodeid = ?");
164

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

    
169
          // Do the update
170
          pstmt.execute();
171
          pstmt.close();
172

    
173
        } catch (SQLException e) {
174
          System.out.println(e.getMessage());
175
        }
176
    }
177
}
(8-8/30)