1 |
15
|
jones
|
/**
|
2 |
|
|
* Name: DBSAXElement.java
|
3 |
|
|
* Purpose: A Class that represents an XML element and its contents
|
4 |
35
|
jones
|
* Copyright: 2000 Regents of the University of California and the
|
5 |
|
|
* National Center for Ecological Analysis and Synthesis
|
6 |
15
|
jones
|
* Authors: Matt Jones
|
7 |
|
|
*
|
8 |
|
|
* Version: '$Id$'
|
9 |
|
|
*/
|
10 |
|
|
|
11 |
51
|
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 |
|
|
* A Class that represents an XML element and its contents and
|
20 |
|
|
* can write its own representation to a database connection
|
21 |
|
|
*/
|
22 |
21
|
jones
|
public class DBSAXElement extends BasicElement {
|
23 |
15
|
jones
|
|
24 |
16
|
jones
|
private Connection conn;
|
25 |
15
|
jones
|
|
26 |
31
|
jones
|
/**
|
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 |
18
|
jones
|
public DBSAXElement (Connection conn, String tagname,
|
34 |
|
|
long parent_id) {
|
35 |
21
|
jones
|
|
36 |
|
|
super(tagname, parent_id);
|
37 |
|
|
|
38 |
16
|
jones
|
this.conn = conn;
|
39 |
|
|
writeElementToDB();
|
40 |
22
|
jones
|
}
|
41 |
15
|
jones
|
|
42 |
19
|
jones
|
/** creates SQL code and inserts new element into DB connection */
|
43 |
|
|
private void writeElementToDB() {
|
44 |
|
|
try {
|
45 |
|
|
conn.setAutoCommit(false);
|
46 |
|
|
PreparedStatement pstmt;
|
47 |
29
|
jones
|
if (getParentID() != 0) {
|
48 |
19
|
jones
|
pstmt = conn.prepareStatement(
|
49 |
20
|
jones
|
"INSERT INTO xml_nodes (nodeid, nodetype, " +
|
50 |
|
|
"nodename, parentnodeid) VALUES (null, ?, ?, ?)");
|
51 |
19
|
jones
|
} else {
|
52 |
|
|
pstmt = conn.prepareStatement(
|
53 |
20
|
jones
|
"INSERT INTO xml_nodes (nodeid, nodetype, nodename) " +
|
54 |
|
|
"VALUES (null, ?, ?)");
|
55 |
19
|
jones
|
}
|
56 |
|
|
|
57 |
|
|
// Bind the values to the query
|
58 |
20
|
jones
|
pstmt.setString(1, "ELEMENT");
|
59 |
|
|
pstmt.setString(2, getTagName());
|
60 |
29
|
jones
|
if (getParentID() != 0) {
|
61 |
|
|
pstmt.setLong(3, getParentID());
|
62 |
19
|
jones
|
}
|
63 |
|
|
// Do the insertion
|
64 |
|
|
pstmt.execute();
|
65 |
|
|
pstmt.close();
|
66 |
29
|
jones
|
setElementID(getAssignedElementID());
|
67 |
19
|
jones
|
conn.commit();
|
68 |
|
|
conn.setAutoCommit(true);
|
69 |
|
|
} catch (SQLException e) {
|
70 |
|
|
System.out.println(e.getMessage());
|
71 |
|
|
}
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
/** look up the assigned element id from DB connection */
|
75 |
|
|
private long getAssignedElementID() {
|
76 |
|
|
long assigned_id=0;
|
77 |
16
|
jones
|
Statement stmt;
|
78 |
|
|
try {
|
79 |
|
|
stmt = conn.createStatement();
|
80 |
20
|
jones
|
stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
|
81 |
16
|
jones
|
try {
|
82 |
|
|
ResultSet rs = stmt.getResultSet();
|
83 |
|
|
try {
|
84 |
|
|
boolean tableHasRows = rs.next();
|
85 |
|
|
if (tableHasRows) {
|
86 |
|
|
try {
|
87 |
19
|
jones
|
assigned_id = rs.getLong(1);
|
88 |
16
|
jones
|
} catch (SQLException e) {
|
89 |
|
|
System.out.println("Error with getInt: " + e.getMessage());
|
90 |
|
|
}
|
91 |
|
|
}
|
92 |
|
|
} catch (SQLException e) {
|
93 |
|
|
System.out.println("Error with next: " + e.getMessage());
|
94 |
|
|
}
|
95 |
|
|
} catch (SQLException e) {
|
96 |
|
|
System.out.println("Error with getrset: " + e.getMessage());
|
97 |
|
|
}
|
98 |
|
|
stmt.close();
|
99 |
|
|
} catch (SQLException e) {
|
100 |
|
|
System.out.println("Error getting id: " + e.getMessage());
|
101 |
|
|
}
|
102 |
|
|
|
103 |
19
|
jones
|
// assign the new ID number
|
104 |
|
|
return assigned_id;
|
105 |
16
|
jones
|
}
|
106 |
|
|
|
107 |
15
|
jones
|
/** Add a new attribute to this element, or set its value */
|
108 |
|
|
public void setAttribute(String attName, String attValue) {
|
109 |
|
|
if (attName != null) {
|
110 |
19
|
jones
|
// Enter the attribute in the hash table
|
111 |
29
|
jones
|
super.setAttribute(attName, attValue);
|
112 |
19
|
jones
|
|
113 |
|
|
// And enter the attribute in the database
|
114 |
|
|
try {
|
115 |
|
|
PreparedStatement pstmt = conn.prepareStatement(
|
116 |
20
|
jones
|
"INSERT INTO xml_nodes (nodeid, nodetype, " +
|
117 |
|
|
"nodename, nodedata, parentnodeid) VALUES (null, ?, ?, ?, ?)");
|
118 |
19
|
jones
|
|
119 |
|
|
// Bind the values to the query
|
120 |
20
|
jones
|
pstmt.setString(1, "ATTRIBUTE");
|
121 |
19
|
jones
|
pstmt.setString(2, attName);
|
122 |
|
|
pstmt.setString(3, attValue);
|
123 |
20
|
jones
|
pstmt.setLong(4, getElementID());
|
124 |
19
|
jones
|
|
125 |
|
|
// Do the insertion
|
126 |
|
|
pstmt.execute();
|
127 |
|
|
pstmt.close();
|
128 |
|
|
} catch (SQLException e) {
|
129 |
|
|
System.out.println(e.getMessage());
|
130 |
|
|
}
|
131 |
|
|
|
132 |
15
|
jones
|
} else {
|
133 |
|
|
System.err.println("Attribute name must not be null!");
|
134 |
|
|
}
|
135 |
|
|
}
|
136 |
|
|
|
137 |
18
|
jones
|
/** Write the element content to the db connection */
|
138 |
|
|
public void writeContentToDB() {
|
139 |
|
|
try {
|
140 |
|
|
PreparedStatement pstmt = conn.prepareStatement(
|
141 |
20
|
jones
|
"UPDATE xml_nodes SET nodedata = ? WHERE nodeid = ?");
|
142 |
18
|
jones
|
|
143 |
|
|
// Bind the values to the query
|
144 |
29
|
jones
|
pstmt.setString(1, getContent());
|
145 |
|
|
pstmt.setLong(2, getElementID());
|
146 |
18
|
jones
|
|
147 |
|
|
// Do the update
|
148 |
|
|
pstmt.execute();
|
149 |
|
|
pstmt.close();
|
150 |
|
|
|
151 |
|
|
} catch (SQLException e) {
|
152 |
|
|
System.out.println(e.getMessage());
|
153 |
|
|
}
|
154 |
|
|
}
|
155 |
15
|
jones
|
}
|