Project

General

Profile

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