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 |
74
|
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 |
72
|
bojilova
|
long parent_id, int nodeIndex) {
|
35 |
21
|
jones
|
|
36 |
72
|
bojilova
|
super(tagname, parent_id, nodeIndex);
|
37 |
21
|
jones
|
|
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 |
72
|
bojilova
|
"nodename, parentnodeid, nodeindex) 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 |
72
|
bojilova
|
//pstmt.setInt(4, null);
|
61 |
29
|
jones
|
if (getParentID() != 0) {
|
62 |
|
|
pstmt.setLong(3, getParentID());
|
63 |
72
|
bojilova
|
pstmt.setInt(4, getNodeIndex());
|
64 |
19
|
jones
|
}
|
65 |
|
|
// Do the insertion
|
66 |
|
|
pstmt.execute();
|
67 |
|
|
pstmt.close();
|
68 |
29
|
jones
|
setElementID(getAssignedElementID());
|
69 |
19
|
jones
|
conn.commit();
|
70 |
|
|
conn.setAutoCommit(true);
|
71 |
|
|
} catch (SQLException e) {
|
72 |
|
|
System.out.println(e.getMessage());
|
73 |
|
|
}
|
74 |
|
|
}
|
75 |
|
|
|
76 |
72
|
bojilova
|
/** 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 |
19
|
jones
|
/** look up the assigned element id from DB connection */
|
97 |
|
|
private long getAssignedElementID() {
|
98 |
|
|
long assigned_id=0;
|
99 |
16
|
jones
|
Statement stmt;
|
100 |
|
|
try {
|
101 |
|
|
stmt = conn.createStatement();
|
102 |
20
|
jones
|
stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
|
103 |
16
|
jones
|
try {
|
104 |
|
|
ResultSet rs = stmt.getResultSet();
|
105 |
|
|
try {
|
106 |
|
|
boolean tableHasRows = rs.next();
|
107 |
|
|
if (tableHasRows) {
|
108 |
|
|
try {
|
109 |
19
|
jones
|
assigned_id = rs.getLong(1);
|
110 |
16
|
jones
|
} 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 |
19
|
jones
|
// assign the new ID number
|
126 |
|
|
return assigned_id;
|
127 |
16
|
jones
|
}
|
128 |
|
|
|
129 |
15
|
jones
|
/** Add a new attribute to this element, or set its value */
|
130 |
|
|
public void setAttribute(String attName, String attValue) {
|
131 |
|
|
if (attName != null) {
|
132 |
19
|
jones
|
// Enter the attribute in the hash table
|
133 |
29
|
jones
|
super.setAttribute(attName, attValue);
|
134 |
19
|
jones
|
|
135 |
|
|
// And enter the attribute in the database
|
136 |
|
|
try {
|
137 |
|
|
PreparedStatement pstmt = conn.prepareStatement(
|
138 |
20
|
jones
|
"INSERT INTO xml_nodes (nodeid, nodetype, " +
|
139 |
|
|
"nodename, nodedata, parentnodeid) VALUES (null, ?, ?, ?, ?)");
|
140 |
19
|
jones
|
|
141 |
|
|
// Bind the values to the query
|
142 |
20
|
jones
|
pstmt.setString(1, "ATTRIBUTE");
|
143 |
19
|
jones
|
pstmt.setString(2, attName);
|
144 |
|
|
pstmt.setString(3, attValue);
|
145 |
20
|
jones
|
pstmt.setLong(4, getElementID());
|
146 |
19
|
jones
|
|
147 |
|
|
// Do the insertion
|
148 |
|
|
pstmt.execute();
|
149 |
|
|
pstmt.close();
|
150 |
|
|
} catch (SQLException e) {
|
151 |
|
|
System.out.println(e.getMessage());
|
152 |
|
|
}
|
153 |
|
|
|
154 |
15
|
jones
|
} else {
|
155 |
|
|
System.err.println("Attribute name must not be null!");
|
156 |
|
|
}
|
157 |
|
|
}
|
158 |
|
|
|
159 |
18
|
jones
|
/** Write the element content to the db connection */
|
160 |
|
|
public void writeContentToDB() {
|
161 |
|
|
try {
|
162 |
|
|
PreparedStatement pstmt = conn.prepareStatement(
|
163 |
20
|
jones
|
"UPDATE xml_nodes SET nodedata = ? WHERE nodeid = ?");
|
164 |
18
|
jones
|
|
165 |
|
|
// Bind the values to the query
|
166 |
29
|
jones
|
pstmt.setString(1, getContent());
|
167 |
|
|
pstmt.setLong(2, getElementID());
|
168 |
18
|
jones
|
|
169 |
|
|
// Do the update
|
170 |
|
|
pstmt.execute();
|
171 |
|
|
pstmt.close();
|
172 |
|
|
|
173 |
|
|
} catch (SQLException e) {
|
174 |
|
|
System.out.println(e.getMessage());
|
175 |
|
|
}
|
176 |
|
|
}
|
177 |
15
|
jones
|
}
|