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 110 2000-05-31 17:50:59Z bojilova $'
|
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
|
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
|
void writeCommentToDB(String data, int nodeIndex) {
|
78
|
try {
|
79
|
PreparedStatement pstmt;
|
80
|
pstmt = conn.prepareStatement(
|
81
|
"INSERT INTO xml_nodes (nodeid, nodetype, " +
|
82
|
"nodename, parentnodeid, nodedata, nodeindex) 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
|
pstmt.setInt(4, nodeIndex);
|
89
|
// Do the insertion
|
90
|
pstmt.execute();
|
91
|
pstmt.close();
|
92
|
} catch (SQLException e) {
|
93
|
System.out.println("Long Comment: " + e.getMessage());
|
94
|
}
|
95
|
}
|
96
|
|
97
|
/** creates SQL code and inserts PI into DB connection */
|
98
|
void writePIToDB(String target, String data, int nodeIndex) {
|
99
|
try {
|
100
|
PreparedStatement pstmt;
|
101
|
pstmt = conn.prepareStatement(
|
102
|
"INSERT INTO xml_nodes (nodeid, nodetype, " +
|
103
|
"nodename, parentnodeid, nodedata, nodeindex) VALUES (null, ?, ?, ?, ?, ?)");
|
104
|
|
105
|
// Bind the values to the query
|
106
|
pstmt.setString(1, "PI");
|
107
|
pstmt.setString(2, target);
|
108
|
pstmt.setLong(3, getElementID());
|
109
|
pstmt.setString(4, data);
|
110
|
pstmt.setInt(5, nodeIndex);
|
111
|
// Do the insertion
|
112
|
pstmt.execute();
|
113
|
pstmt.close();
|
114
|
} catch (SQLException e) {
|
115
|
System.out.println(e.getMessage());
|
116
|
}
|
117
|
}
|
118
|
|
119
|
/** creates SQL code to put nodename for the document node into DB connection */
|
120
|
void writeNodename(String nodename) {
|
121
|
try {
|
122
|
PreparedStatement pstmt;
|
123
|
pstmt = conn.prepareStatement(
|
124
|
"UPDATE xml_nodes set nodename = ? " +
|
125
|
"WHERE nodeid = ?");
|
126
|
|
127
|
// Bind the values to the query
|
128
|
pstmt.setString(1, nodename);
|
129
|
pstmt.setLong(2, getElementID());
|
130
|
// Do the insertion
|
131
|
pstmt.execute();
|
132
|
pstmt.close();
|
133
|
} catch (SQLException e) {
|
134
|
System.out.println(e.getMessage());
|
135
|
}
|
136
|
}
|
137
|
|
138
|
/** look up the assigned element id from DB connection */
|
139
|
private long getAssignedElementID() {
|
140
|
long assigned_id=0;
|
141
|
Statement stmt;
|
142
|
try {
|
143
|
stmt = conn.createStatement();
|
144
|
stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
|
145
|
try {
|
146
|
ResultSet rs = stmt.getResultSet();
|
147
|
try {
|
148
|
boolean tableHasRows = rs.next();
|
149
|
if (tableHasRows) {
|
150
|
try {
|
151
|
assigned_id = rs.getLong(1);
|
152
|
} catch (SQLException e) {
|
153
|
System.out.println("Error with getInt: " + e.getMessage());
|
154
|
}
|
155
|
}
|
156
|
} catch (SQLException e) {
|
157
|
System.out.println("Error with next: " + e.getMessage());
|
158
|
}
|
159
|
} catch (SQLException e) {
|
160
|
System.out.println("Error with getrset: " + e.getMessage());
|
161
|
}
|
162
|
stmt.close();
|
163
|
} catch (SQLException e) {
|
164
|
System.out.println("Error getting id: " + e.getMessage());
|
165
|
}
|
166
|
|
167
|
// assign the new ID number
|
168
|
return assigned_id;
|
169
|
}
|
170
|
|
171
|
/** Add a new attribute to this element, or set its value */
|
172
|
public void setAttribute(String attName, String attValue) {
|
173
|
if (attName != null) {
|
174
|
// Enter the attribute in the hash table
|
175
|
super.setAttribute(attName, attValue);
|
176
|
|
177
|
// And enter the attribute in the database
|
178
|
try {
|
179
|
PreparedStatement pstmt = conn.prepareStatement(
|
180
|
"INSERT INTO xml_nodes (nodeid, nodetype, " +
|
181
|
"nodename, nodedata, parentnodeid) VALUES (null, ?, ?, ?, ?)");
|
182
|
|
183
|
// Bind the values to the query
|
184
|
pstmt.setString(1, "ATTRIBUTE");
|
185
|
pstmt.setString(2, attName);
|
186
|
pstmt.setString(3, attValue);
|
187
|
pstmt.setLong(4, getElementID());
|
188
|
|
189
|
// Do the insertion
|
190
|
pstmt.execute();
|
191
|
pstmt.close();
|
192
|
} catch (SQLException e) {
|
193
|
System.out.println(e.getMessage());
|
194
|
}
|
195
|
|
196
|
} else {
|
197
|
System.err.println("Attribute name must not be null!");
|
198
|
}
|
199
|
}
|
200
|
|
201
|
/** Write the element content to the db connection */
|
202
|
public void writeContentToDB() {
|
203
|
try {
|
204
|
PreparedStatement pstmt = conn.prepareStatement(
|
205
|
"UPDATE xml_nodes SET nodedata = ? WHERE nodeid = ?");
|
206
|
|
207
|
// Bind the values to the query
|
208
|
pstmt.setString(1, getContent());
|
209
|
pstmt.setLong(2, getElementID());
|
210
|
|
211
|
// Do the update
|
212
|
pstmt.execute();
|
213
|
pstmt.close();
|
214
|
|
215
|
} catch (SQLException e) {
|
216
|
System.out.println(e.getMessage());
|
217
|
}
|
218
|
}
|
219
|
}
|