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 129 2000-06-07 02:08: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 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
          if (getParentID() != 0) {
61
            pstmt.setLong(3, getParentID());
62
            pstmt.setInt(4, getNodeIndex());
63
          } else
64
              pstmt.setString(1, "DOCUMENT");
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
    public void writeCommentToDB(String data, int nodeIndex) {
78
        try {
79
          PreparedStatement pstmt;
80
          pstmt = conn.prepareStatement(
81
                "INSERT INTO xml_nodes (nodeid, nodetype, nodename, " +
82
                "parentnodeid, nodedata, nodeindex) " +
83
                "VALUES (null, ?, null, ?, ?, ?)");
84

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

    
98
    /** creates SQL code and inserts PI into DB connection */
99
    public void writePIToDB(String target, String data, int nodeIndex) {
100
        try {
101
          PreparedStatement pstmt;
102
          pstmt = conn.prepareStatement(
103
                "INSERT INTO xml_nodes (nodeid, nodetype, nodename, " +
104
                "parentnodeid, nodedata, nodeindex) " +
105
                "VALUES (null, ?, ?, ?, ?, ?)");
106

    
107
          // Bind the values to the query
108
          pstmt.setString(1, "PI");
109
          pstmt.setString(2, target);
110
          pstmt.setLong(3, getElementID());
111
          pstmt.setString(4, data);
112
          pstmt.setInt(5, nodeIndex);
113
          // Do the insertion
114
          pstmt.execute();
115
          pstmt.close();
116
        } catch (SQLException e) {
117
          System.out.println(e.getMessage());
118
        }
119
    }
120

    
121
    /** 
122
     * creates SQL code to put nodename for the document node 
123
     * into DB connection 
124
     */
125
    public void writeNodename(String nodename) {
126
        try {
127
          PreparedStatement pstmt;
128
          pstmt = conn.prepareStatement(
129
                "UPDATE xml_nodes set nodename = ? " +
130
                "WHERE nodeid = ?");
131

    
132
          // Bind the values to the query
133
          pstmt.setString(1, nodename);
134
          pstmt.setLong(2, getElementID());
135
          // Do the insertion
136
          pstmt.execute();
137
          pstmt.close();
138
        } catch (SQLException e) {
139
          System.out.println(e.getMessage());
140
        }
141
    }
142

    
143
    /** look up the assigned element id from DB connection */
144
    private long getAssignedElementID() {
145
        long assigned_id=0;
146
        Statement stmt;
147
        try {
148
          stmt = conn.createStatement();
149
          stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
150
          try {
151
            ResultSet rs = stmt.getResultSet();
152
            try {
153
              boolean tableHasRows = rs.next();
154
              if (tableHasRows) {
155
                try {
156
                  assigned_id = rs.getLong(1);
157
                } catch (SQLException e) {
158
                  System.out.println("Error with getInt: " + e.getMessage());
159
                }
160
              }
161
            } catch (SQLException e) {
162
              System.out.println("Error with next: " + e.getMessage());
163
            }
164
          } catch (SQLException e) {
165
            System.out.println("Error with getrset: " + e.getMessage());
166
          }
167
          stmt.close();
168
        } catch (SQLException e) {
169
          System.out.println("Error getting id: " + e.getMessage());
170
        }
171

    
172
        // assign the new ID number
173
        return assigned_id;
174
    }
175

    
176
    /** Add a new attribute to this element, or set its value */
177
    public void setAttribute(String attName, String attValue) {
178
      if (attName != null) {
179
        // Enter the attribute in the hash table
180
        super.setAttribute(attName, attValue);
181
 
182
        // And enter the attribute in the database
183
        try {
184
          PreparedStatement pstmt = conn.prepareStatement(
185
              "INSERT INTO xml_nodes (nodeid, nodetype, " +
186
              "nodename, nodedata, parentnodeid) VALUES (null, ?, ?, ?, ?)");
187

    
188
          // Bind the values to the query
189
          pstmt.setString(1, "ATTRIBUTE");
190
          pstmt.setString(2, attName);
191
          pstmt.setString(3, attValue);
192
          pstmt.setLong(4, getElementID());
193
 
194
          // Do the insertion
195
          pstmt.execute();
196
          pstmt.close();
197
        } catch (SQLException e) {
198
          System.out.println(e.getMessage());
199
        }
200

    
201
      } else {
202
        System.err.println("Attribute name must not be null!");
203
      }
204
    }
205

    
206
    /** Write the element content to the db connection */
207
    public void writeContentToDB(String content, int nodeIndex) {
208
        try {
209
          PreparedStatement pstmt = conn.prepareStatement(
210
                "INSERT INTO xml_nodes (nodeid, nodetype, nodename, " +
211
                "parentnodeid, nodedata, nodeindex) " +
212
                "VALUES (null, ?, null, ?, ?, ?)");
213
          // Bind the values to the query
214
          pstmt.setString(1, "TEXT");
215
          pstmt.setLong(2, getElementID());
216
          pstmt.setString(3, content);
217
          pstmt.setInt(4, nodeIndex);
218

    
219
          // Do the update
220
          pstmt.execute();
221
          pstmt.close();
222

    
223
        } catch (SQLException e) {
224
          System.out.println(e.getMessage());
225
        }
226
    }
227
}
(6-6/20)