Revision 1502
Added by Jing Tao almost 22 years ago
src/edu/ucsb/nceas/metacat/SubTree.java | ||
---|---|---|
28 | 28 |
|
29 | 29 |
package edu.ucsb.nceas.metacat; |
30 | 30 |
|
31 |
import java.util.Stack; |
|
32 |
import java.sql.PreparedStatement; |
|
33 |
import java.sql.SQLException; |
|
34 |
import java.sql.ResultSet; |
|
35 |
|
|
36 |
|
|
31 | 37 |
/** |
32 | 38 |
* A Class that represents an XML Subtree |
33 | 39 |
*/ |
34 | 40 |
public class SubTree |
35 | 41 |
{ |
42 |
private String docId = null; |
|
36 | 43 |
private String subTreeId = null; |
37 | 44 |
private String startElementName = null; |
38 | 45 |
private long startNodeId = -1; |
39 | 46 |
private long endNodeId = -1; |
47 |
private Stack subTreeNodeStack = null; |
|
48 |
|
|
49 |
/** |
|
50 |
* Defualt constructor |
|
51 |
*/ |
|
52 |
public SubTree() |
|
53 |
{ |
|
54 |
|
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
58 |
* Constructor of subtree |
|
59 |
*/ |
|
60 |
public SubTree(String myDocId, String mySubTreeId, |
|
61 |
long myStartNodeId, long myEndNodeId) |
|
62 |
throws McdbException |
|
63 |
{ |
|
64 |
this.docId = myDocId; |
|
65 |
MetaCatUtil.debugMessage("Docid of Subtree: " + docId, 30); |
|
66 |
this.subTreeId = mySubTreeId; |
|
67 |
MetaCatUtil.debugMessage("id of Subtree: " + subTreeId, 30); |
|
68 |
this.startNodeId = myStartNodeId; |
|
69 |
MetaCatUtil.debugMessage("start node id of Subtree: " + startNodeId, 30); |
|
70 |
this.endNodeId = myEndNodeId; |
|
71 |
MetaCatUtil.debugMessage("end node id of subtree: " + endNodeId, 30); |
|
72 |
subTreeNodeStack = getSubTreeNodeList(); |
|
73 |
} |
|
74 |
|
|
75 |
/** |
|
76 |
* Get subtree node stack |
|
77 |
*/ |
|
78 |
public Stack getSubTreeNodeStack() |
|
79 |
{ |
|
80 |
return this.subTreeNodeStack; |
|
81 |
} |
|
40 | 82 |
|
83 |
/** Set the a docId */ |
|
84 |
public void setDocId(String myId) |
|
85 |
{ |
|
86 |
MetaCatUtil.debugMessage("set doc id: "+myId, 35); |
|
87 |
this.docId = myId; |
|
88 |
} |
|
89 |
|
|
90 |
/** Get the docId */ |
|
91 |
public String getDocId() |
|
92 |
{ |
|
93 |
return this.docId; |
|
94 |
} |
|
95 |
|
|
96 |
|
|
41 | 97 |
/** Set the a subtreeId */ |
42 | 98 |
public void setSubTreeId(String myId) |
43 | 99 |
{ |
... | ... | |
93 | 149 |
{ |
94 | 150 |
return this.endNodeId; |
95 | 151 |
} |
96 |
|
|
152 |
|
|
153 |
/* Put a subtree node into a stack, on top is the start point of subtree*/ |
|
154 |
private Stack getSubTreeNodeList() throws McdbException |
|
155 |
{ |
|
156 |
PreparedStatement pstmt = null; |
|
157 |
DBConnection dbconn = null; |
|
158 |
int serialNumber = -1; |
|
159 |
Stack nodeRecordList = new Stack(); |
|
160 |
long nodeid = 0; |
|
161 |
long parentnodeid = 0; |
|
162 |
long nodeindex = 0; |
|
163 |
String nodetype = null; |
|
164 |
String nodename = null; |
|
165 |
String nodeprefix = null; |
|
166 |
String nodedata = null; |
|
167 |
String sql = "SELECT nodeid, parentnodeid, nodeindex, " + |
|
168 |
"nodetype, nodename, nodeprefix, nodedata " + |
|
169 |
"FROM xml_nodes WHERE docid = ? AND nodeid >= ? AND " + |
|
170 |
"nodeid <= ? ORDER BY nodeid DESC"; |
|
171 |
try |
|
172 |
{ |
|
173 |
dbconn=DBConnectionPool. |
|
174 |
getDBConnection("SubTree.getSubTreeNodeList"); |
|
175 |
serialNumber=dbconn.getCheckOutSerialNumber(); |
|
176 |
pstmt = dbconn.prepareStatement(sql); |
|
177 |
|
|
178 |
// Bind the values to the query |
|
179 |
pstmt.setString(1, docId); |
|
180 |
pstmt.setLong(2, startNodeId); |
|
181 |
pstmt.setLong(3, endNodeId); |
|
182 |
|
|
183 |
pstmt.execute(); |
|
184 |
ResultSet rs = pstmt.getResultSet(); |
|
185 |
boolean tableHasRows = rs.next(); |
|
186 |
|
|
187 |
while (tableHasRows) |
|
188 |
{ |
|
189 |
nodeid = rs.getLong(1); |
|
190 |
parentnodeid = rs.getLong(2); |
|
191 |
nodeindex = rs.getLong(3); |
|
192 |
nodetype = rs.getString(4); |
|
193 |
nodename = rs.getString(5); |
|
194 |
nodeprefix = rs.getString(6); |
|
195 |
nodedata = rs.getString(7); |
|
196 |
nodedata = MetaCatUtil.normalize(nodedata); |
|
197 |
// add the data to the node record list hashtable |
|
198 |
NodeRecord currentRecord = new NodeRecord(nodeid,parentnodeid,nodeindex, |
|
199 |
nodetype, nodename, nodeprefix, nodedata); |
|
200 |
nodeRecordList.push(currentRecord); |
|
201 |
|
|
202 |
// Advance to the next node |
|
203 |
tableHasRows = rs.next(); |
|
204 |
}//while |
|
205 |
pstmt.close(); |
|
206 |
|
|
207 |
} //try |
|
208 |
catch (SQLException e) |
|
209 |
{ |
|
210 |
throw new McdbException("Error in SubTree.getSubTreeNodeList 1 " + |
|
211 |
e.getMessage()); |
|
212 |
}//catch |
|
213 |
finally |
|
214 |
{ |
|
215 |
try |
|
216 |
{ |
|
217 |
pstmt.close(); |
|
218 |
} |
|
219 |
catch (SQLException ee) |
|
220 |
{ |
|
221 |
MetaCatUtil.debugMessage("error in SubTree.getSubTreeNodeList 2: " |
|
222 |
+ee.getMessage(), 30); |
|
223 |
} |
|
224 |
finally |
|
225 |
{ |
|
226 |
DBConnectionPool.returnDBConnection(dbconn, serialNumber); |
|
227 |
} |
|
228 |
}//finally |
|
229 |
|
|
230 |
return nodeRecordList; |
|
231 |
|
|
232 |
}//getSubtreeNodeList |
|
233 |
|
|
234 |
public static void main (String agus) |
|
235 |
{ |
|
236 |
|
|
237 |
} |
|
97 | 238 |
} |
Also available in: Unified diff
Add code to do get subtree nodes from db.