1 |
1414
|
tao
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A Class that represents an XML Text node and its contents,
|
4 |
|
|
* and can build itself from a database connection
|
5 |
|
|
* Copyright: 2000 Regents of the University of California and the
|
6 |
|
|
* National Center for Ecological Analysis and Synthesis
|
7 |
|
|
* Authors: Matt Jones
|
8 |
|
|
*
|
9 |
|
|
* '$Author$'
|
10 |
|
|
* '$Date$'
|
11 |
|
|
* '$Revision$'
|
12 |
|
|
*
|
13 |
|
|
* This program is free software; you can redistribute it and/or modify
|
14 |
|
|
* it under the terms of the GNU General Public License as published by
|
15 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
16 |
|
|
* (at your option) any later version.
|
17 |
|
|
*
|
18 |
|
|
* This program is distributed in the hope that it will be useful,
|
19 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21 |
|
|
* GNU General Public License for more details.
|
22 |
|
|
*
|
23 |
|
|
* You should have received a copy of the GNU General Public License
|
24 |
|
|
* along with this program; if not, write to the Free Software
|
25 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26 |
|
|
*/
|
27 |
|
|
|
28 |
|
|
package edu.ucsb.nceas.metacat;
|
29 |
|
|
|
30 |
1520
|
tao
|
import java.util.Comparator;
|
31 |
1502
|
tao
|
import java.util.Stack;
|
32 |
|
|
import java.sql.PreparedStatement;
|
33 |
|
|
import java.sql.SQLException;
|
34 |
|
|
import java.sql.ResultSet;
|
35 |
|
|
|
36 |
2663
|
sgarg
|
import org.apache.log4j.Logger;
|
37 |
2245
|
sgarg
|
import org.xml.sax.SAXException;
|
38 |
1502
|
tao
|
|
39 |
1414
|
tao
|
/**
|
40 |
|
|
* A Class that represents an XML Subtree
|
41 |
|
|
*/
|
42 |
1520
|
tao
|
public class SubTree implements Comparator
|
43 |
1414
|
tao
|
{
|
44 |
1542
|
tao
|
protected String docId = null;
|
45 |
|
|
protected String subTreeId = null;
|
46 |
|
|
protected String startElementName = null;
|
47 |
|
|
protected long startNodeId = -1;
|
48 |
|
|
protected long endNodeId = -1;
|
49 |
1502
|
tao
|
private Stack subTreeNodeStack = null;
|
50 |
2663
|
sgarg
|
|
51 |
|
|
private static Logger logMetacat = Logger.getLogger(SubTree.class);
|
52 |
2245
|
sgarg
|
|
53 |
1502
|
tao
|
/**
|
54 |
|
|
* Defualt constructor
|
55 |
|
|
*/
|
56 |
|
|
public SubTree()
|
57 |
|
|
{
|
58 |
2245
|
sgarg
|
|
59 |
1502
|
tao
|
}
|
60 |
2245
|
sgarg
|
|
61 |
1502
|
tao
|
/**
|
62 |
|
|
* Constructor of subtree
|
63 |
|
|
*/
|
64 |
2245
|
sgarg
|
public SubTree(String myDocId, String mySubTreeId,
|
65 |
1502
|
tao
|
long myStartNodeId, long myEndNodeId)
|
66 |
|
|
{
|
67 |
|
|
this.docId = myDocId;
|
68 |
2663
|
sgarg
|
logMetacat.info("Docid of Subtree: " + docId);
|
69 |
1502
|
tao
|
this.subTreeId = mySubTreeId;
|
70 |
2663
|
sgarg
|
logMetacat.info("id of Subtree: " + subTreeId);
|
71 |
1502
|
tao
|
this.startNodeId = myStartNodeId;
|
72 |
2663
|
sgarg
|
logMetacat.info("start node id of Subtree: " + startNodeId);
|
73 |
1502
|
tao
|
this.endNodeId = myEndNodeId;
|
74 |
2663
|
sgarg
|
logMetacat.info("end node id of subtree: " + endNodeId);
|
75 |
2245
|
sgarg
|
|
76 |
1502
|
tao
|
}
|
77 |
2245
|
sgarg
|
|
78 |
1502
|
tao
|
/**
|
79 |
|
|
* Get subtree node stack
|
80 |
|
|
*/
|
81 |
2245
|
sgarg
|
public Stack getSubTreeNodeStack() throws SAXException
|
82 |
1502
|
tao
|
{
|
83 |
2245
|
sgarg
|
try
|
84 |
|
|
{
|
85 |
|
|
subTreeNodeStack = getSubTreeNodeList();
|
86 |
|
|
}
|
87 |
|
|
catch (McdbException e)
|
88 |
|
|
{
|
89 |
|
|
throw new SAXException(e.getMessage());
|
90 |
|
|
}
|
91 |
1502
|
tao
|
return this.subTreeNodeStack;
|
92 |
|
|
}
|
93 |
2245
|
sgarg
|
|
94 |
1542
|
tao
|
/**
|
95 |
|
|
* Set subtree node stack
|
96 |
|
|
*/
|
97 |
|
|
public void setSubTreeNodeStack(Stack myStack)
|
98 |
|
|
{
|
99 |
|
|
this.subTreeNodeStack = myStack;
|
100 |
|
|
}
|
101 |
2245
|
sgarg
|
|
102 |
1502
|
tao
|
/** Set the a docId */
|
103 |
|
|
public void setDocId(String myId)
|
104 |
|
|
{
|
105 |
2663
|
sgarg
|
logMetacat.info("set doc id: "+myId);
|
106 |
1502
|
tao
|
this.docId = myId;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
/** Get the docId */
|
110 |
2245
|
sgarg
|
public String getDocId()
|
111 |
1502
|
tao
|
{
|
112 |
|
|
return this.docId;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
2245
|
sgarg
|
|
116 |
1414
|
tao
|
/** Set the a subtreeId */
|
117 |
|
|
public void setSubTreeId(String myId)
|
118 |
|
|
{
|
119 |
2663
|
sgarg
|
logMetacat.info("set sub tree id: "+myId);
|
120 |
1414
|
tao
|
this.subTreeId = myId;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
/** Get the subTreeId */
|
124 |
2245
|
sgarg
|
public String getSubTreeId()
|
125 |
1414
|
tao
|
{
|
126 |
|
|
return this.subTreeId;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
2245
|
sgarg
|
/**
|
130 |
1414
|
tao
|
* Set a startElementName
|
131 |
|
|
*/
|
132 |
2245
|
sgarg
|
public void setStartElementName(String elementName)
|
133 |
1414
|
tao
|
{
|
134 |
2663
|
sgarg
|
logMetacat.info("set start elementname: "+elementName);
|
135 |
1414
|
tao
|
this.startElementName = elementName;
|
136 |
|
|
}
|
137 |
2245
|
sgarg
|
|
138 |
1414
|
tao
|
/**
|
139 |
|
|
* Get startElementName
|
140 |
|
|
*/
|
141 |
|
|
public String getStartElementName()
|
142 |
|
|
{
|
143 |
|
|
return this.startElementName;
|
144 |
|
|
}
|
145 |
2245
|
sgarg
|
|
146 |
1414
|
tao
|
/** Set a start node id */
|
147 |
|
|
public void setStartNodeId(long nodeId)
|
148 |
|
|
{
|
149 |
2663
|
sgarg
|
logMetacat.info("set start node id: "+nodeId);
|
150 |
1414
|
tao
|
this.startNodeId = nodeId;
|
151 |
|
|
}
|
152 |
2245
|
sgarg
|
|
153 |
1414
|
tao
|
/** Get start node id */
|
154 |
|
|
public long getStartNodeId()
|
155 |
|
|
{
|
156 |
|
|
return this.startNodeId;
|
157 |
|
|
}
|
158 |
2245
|
sgarg
|
|
159 |
1414
|
tao
|
/** Set a end node id */
|
160 |
|
|
public void setEndNodeId(long nodeId)
|
161 |
|
|
{
|
162 |
2663
|
sgarg
|
logMetacat.info("set end node id: "+nodeId);
|
163 |
1414
|
tao
|
this.endNodeId = nodeId;
|
164 |
|
|
}
|
165 |
2245
|
sgarg
|
|
166 |
1414
|
tao
|
/** Get end node id */
|
167 |
|
|
public long getEndNodeId()
|
168 |
|
|
{
|
169 |
|
|
return this.endNodeId;
|
170 |
|
|
}
|
171 |
2245
|
sgarg
|
|
172 |
1502
|
tao
|
/* Put a subtree node into a stack, on top is the start point of subtree*/
|
173 |
2245
|
sgarg
|
private Stack getSubTreeNodeList() throws McdbException
|
174 |
1502
|
tao
|
{
|
175 |
1542
|
tao
|
Stack nodeRecordList = new Stack();
|
176 |
|
|
// make sure it works
|
177 |
|
|
if ( docId == null || startNodeId == -1 || endNodeId == -1)
|
178 |
|
|
{
|
179 |
|
|
return nodeRecordList;
|
180 |
|
|
}
|
181 |
1502
|
tao
|
PreparedStatement pstmt = null;
|
182 |
|
|
DBConnection dbconn = null;
|
183 |
|
|
int serialNumber = -1;
|
184 |
2245
|
sgarg
|
|
185 |
1502
|
tao
|
long nodeid = 0;
|
186 |
|
|
long parentnodeid = 0;
|
187 |
|
|
long nodeindex = 0;
|
188 |
|
|
String nodetype = null;
|
189 |
|
|
String nodename = null;
|
190 |
|
|
String nodeprefix = null;
|
191 |
|
|
String nodedata = null;
|
192 |
|
|
String sql = "SELECT nodeid, parentnodeid, nodeindex, " +
|
193 |
2245
|
sgarg
|
"nodetype, nodename, nodeprefix, nodedata " +
|
194 |
1502
|
tao
|
"FROM xml_nodes WHERE docid = ? AND nodeid >= ? AND " +
|
195 |
|
|
"nodeid <= ? ORDER BY nodeid DESC";
|
196 |
2245
|
sgarg
|
try
|
197 |
1502
|
tao
|
{
|
198 |
|
|
dbconn=DBConnectionPool.
|
199 |
|
|
getDBConnection("SubTree.getSubTreeNodeList");
|
200 |
|
|
serialNumber=dbconn.getCheckOutSerialNumber();
|
201 |
|
|
pstmt = dbconn.prepareStatement(sql);
|
202 |
|
|
|
203 |
|
|
// Bind the values to the query
|
204 |
|
|
pstmt.setString(1, docId);
|
205 |
|
|
pstmt.setLong(2, startNodeId);
|
206 |
|
|
pstmt.setLong(3, endNodeId);
|
207 |
|
|
|
208 |
|
|
pstmt.execute();
|
209 |
|
|
ResultSet rs = pstmt.getResultSet();
|
210 |
|
|
boolean tableHasRows = rs.next();
|
211 |
2245
|
sgarg
|
|
212 |
|
|
while (tableHasRows)
|
213 |
1502
|
tao
|
{
|
214 |
|
|
nodeid = rs.getLong(1);
|
215 |
|
|
parentnodeid = rs.getLong(2);
|
216 |
|
|
nodeindex = rs.getLong(3);
|
217 |
|
|
nodetype = rs.getString(4);
|
218 |
|
|
nodename = rs.getString(5);
|
219 |
|
|
nodeprefix = rs.getString(6);
|
220 |
|
|
nodedata = rs.getString(7);
|
221 |
|
|
nodedata = MetaCatUtil.normalize(nodedata);
|
222 |
|
|
// add the data to the node record list hashtable
|
223 |
|
|
NodeRecord currentRecord = new NodeRecord(nodeid,parentnodeid,nodeindex,
|
224 |
|
|
nodetype, nodename, nodeprefix, nodedata);
|
225 |
|
|
nodeRecordList.push(currentRecord);
|
226 |
|
|
|
227 |
|
|
// Advance to the next node
|
228 |
|
|
tableHasRows = rs.next();
|
229 |
|
|
}//while
|
230 |
|
|
pstmt.close();
|
231 |
|
|
|
232 |
|
|
} //try
|
233 |
2245
|
sgarg
|
catch (SQLException e)
|
234 |
1502
|
tao
|
{
|
235 |
|
|
throw new McdbException("Error in SubTree.getSubTreeNodeList 1 " +
|
236 |
|
|
e.getMessage());
|
237 |
|
|
}//catch
|
238 |
|
|
finally
|
239 |
|
|
{
|
240 |
|
|
try
|
241 |
|
|
{
|
242 |
|
|
pstmt.close();
|
243 |
|
|
}
|
244 |
|
|
catch (SQLException ee)
|
245 |
|
|
{
|
246 |
2663
|
sgarg
|
logMetacat.error("error in SubTree.getSubTreeNodeList 2: "
|
247 |
|
|
+ee.getMessage());
|
248 |
1502
|
tao
|
}
|
249 |
|
|
finally
|
250 |
|
|
{
|
251 |
|
|
DBConnectionPool.returnDBConnection(dbconn, serialNumber);
|
252 |
|
|
}
|
253 |
|
|
}//finally
|
254 |
2245
|
sgarg
|
|
255 |
1502
|
tao
|
return nodeRecordList;
|
256 |
2245
|
sgarg
|
|
257 |
1502
|
tao
|
}//getSubtreeNodeList
|
258 |
2245
|
sgarg
|
|
259 |
1520
|
tao
|
/** methods from Comparator interface */
|
260 |
|
|
public int compare(Object o1, Object o2)
|
261 |
|
|
{
|
262 |
|
|
SubTree tree1 = (SubTree) o1;
|
263 |
|
|
SubTree tree2 = (SubTree) o2;
|
264 |
|
|
if (tree1.getStartNodeId() > tree2.getStartNodeId())
|
265 |
|
|
{
|
266 |
|
|
return 1;
|
267 |
|
|
}
|
268 |
|
|
else if (tree1.getStartNodeId() < tree2.getStartNodeId())
|
269 |
|
|
{
|
270 |
|
|
return -1;
|
271 |
|
|
}
|
272 |
|
|
else
|
273 |
|
|
{
|
274 |
|
|
return 0;
|
275 |
|
|
}
|
276 |
2245
|
sgarg
|
|
277 |
1520
|
tao
|
}//cpmpare
|
278 |
2245
|
sgarg
|
|
279 |
1520
|
tao
|
/** method from Comparator interface */
|
280 |
|
|
public boolean equals(Object obj)
|
281 |
|
|
{
|
282 |
|
|
SubTree tree = (SubTree)obj;
|
283 |
|
|
if (startNodeId == tree.getStartNodeId())
|
284 |
|
|
{
|
285 |
|
|
return true;
|
286 |
|
|
}
|
287 |
|
|
else
|
288 |
|
|
{
|
289 |
|
|
return false;
|
290 |
|
|
}
|
291 |
|
|
}
|
292 |
2245
|
sgarg
|
|
293 |
1414
|
tao
|
}
|