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
 *
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 5015 daigle
import edu.ucsb.nceas.metacat.database.DBConnection;
40
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
41 4698 daigle
import edu.ucsb.nceas.metacat.util.MetacatUtil;
42 4080 daigle
43 1414 tao
/**
44
 * A Class that represents an XML Subtree
45
 */
46 1520 tao
public class SubTree implements Comparator
47 1414 tao
{
48 1542 tao
  protected String docId = null;
49
  protected String subTreeId = null;
50
  protected String startElementName = null;
51
  protected long   startNodeId = -1;
52
  protected long   endNodeId =   -1;
53 4466 daigle
  private Stack<NodeRecord>  subTreeNodeStack = null;
54 2663 sgarg
55
  private static Logger logMetacat = Logger.getLogger(SubTree.class);
56 2245 sgarg
57 1502 tao
    /**
58
     * Defualt constructor
59
     */
60
    public SubTree()
61
    {
62 2245 sgarg
63 1502 tao
    }
64 2245 sgarg
65 1502 tao
    /**
66
     * Constructor of subtree
67
     */
68 2245 sgarg
    public SubTree(String myDocId, String mySubTreeId,
69 1502 tao
                  long myStartNodeId, long myEndNodeId)
70
    {
71
      this.docId = myDocId;
72 2663 sgarg
      logMetacat.info("Docid of Subtree: " + docId);
73 1502 tao
      this.subTreeId = mySubTreeId;
74 2663 sgarg
      logMetacat.info("id of Subtree: " + subTreeId);
75 1502 tao
      this.startNodeId = myStartNodeId;
76 2663 sgarg
      logMetacat.info("start node id of Subtree: " + startNodeId);
77 1502 tao
      this.endNodeId = myEndNodeId;
78 2663 sgarg
      logMetacat.info("end node id of subtree: " + endNodeId);
79 2245 sgarg
80 1502 tao
    }
81 2245 sgarg
82 1502 tao
    /**
83
     * Get subtree node stack
84
     */
85 4466 daigle
    public Stack<NodeRecord> getSubTreeNodeStack() throws SAXException
86 1502 tao
    {
87 2245 sgarg
      try
88
      {
89
        subTreeNodeStack = getSubTreeNodeList();
90
      }
91
      catch (McdbException e)
92
      {
93
        throw new SAXException(e.getMessage());
94
      }
95 1502 tao
      return this.subTreeNodeStack;
96
    }
97 2245 sgarg
98 1542 tao
    /**
99
     * Set subtree node stack
100
     */
101
    public void setSubTreeNodeStack(Stack myStack)
102
    {
103
      this.subTreeNodeStack = myStack;
104
    }
105 2245 sgarg
106 1502 tao
    /** Set the a docId */
107
    public void setDocId(String myId)
108
    {
109 2663 sgarg
      logMetacat.info("set doc id: "+myId);
110 1502 tao
      this.docId = myId;
111
    }
112
113
    /** Get the docId */
114 2245 sgarg
    public String getDocId()
115 1502 tao
    {
116
      return this.docId;
117
    }
118
119 2245 sgarg
120 1414 tao
    /** Set the a subtreeId */
121
    public void setSubTreeId(String myId)
122
    {
123 2663 sgarg
      logMetacat.info("set sub tree id: "+myId);
124 1414 tao
      this.subTreeId = myId;
125
    }
126
127
    /** Get the subTreeId */
128 2245 sgarg
    public String getSubTreeId()
129 1414 tao
    {
130
      return this.subTreeId;
131
    }
132
133 2245 sgarg
    /**
134 1414 tao
     * Set a startElementName
135
     */
136 2245 sgarg
    public void setStartElementName(String elementName)
137 1414 tao
    {
138 2663 sgarg
      logMetacat.info("set start elementname: "+elementName);
139 1414 tao
      this.startElementName = elementName;
140
    }
141 2245 sgarg
142 1414 tao
    /**
143
     * Get startElementName
144
     */
145
    public String getStartElementName()
146
    {
147
      return this.startElementName;
148
    }
149 2245 sgarg
150 1414 tao
    /** Set a start node id */
151
    public void setStartNodeId(long nodeId)
152
    {
153 2663 sgarg
      logMetacat.info("set start node id: "+nodeId);
154 1414 tao
      this.startNodeId = nodeId;
155
    }
156 2245 sgarg
157 1414 tao
    /** Get start node id */
158
    public long getStartNodeId()
159
    {
160
      return this.startNodeId;
161
    }
162 2245 sgarg
163 1414 tao
    /** Set a end node id */
164
    public void setEndNodeId(long nodeId)
165
    {
166 2663 sgarg
      logMetacat.info("set end node id: "+nodeId);
167 1414 tao
      this.endNodeId = nodeId;
168
    }
169 2245 sgarg
170 1414 tao
    /** Get end node id */
171
    public long getEndNodeId()
172
    {
173
      return this.endNodeId;
174
    }
175 2245 sgarg
176 1502 tao
    /* Put a subtree node into a stack, on top is the start point of subtree*/
177 2245 sgarg
    private Stack getSubTreeNodeList() throws McdbException
178 1502 tao
    {
179 1542 tao
       Stack nodeRecordList = new Stack();
180
       // make sure it works
181
       if ( docId == null || startNodeId == -1 || endNodeId == -1)
182
       {
183
         return nodeRecordList;
184
       }
185 1502 tao
       PreparedStatement pstmt = null;
186
       DBConnection dbconn = null;
187
       int serialNumber = -1;
188 2245 sgarg
189 1502 tao
       long nodeid = 0;
190
       long parentnodeid = 0;
191
       long nodeindex = 0;
192
       String nodetype = null;
193
       String nodename = null;
194
       String nodeprefix = null;
195
       String nodedata = null;
196
       String sql = "SELECT nodeid, parentnodeid, nodeindex, " +
197 2245 sgarg
                    "nodetype, nodename, nodeprefix, nodedata " +
198 1502 tao
                    "FROM xml_nodes WHERE docid = ? AND nodeid >= ? AND " +
199
                    "nodeid <= ? ORDER BY nodeid DESC";
200 2245 sgarg
       try
201 1502 tao
       {
202
         dbconn=DBConnectionPool.
203
                    getDBConnection("SubTree.getSubTreeNodeList");
204
         serialNumber=dbconn.getCheckOutSerialNumber();
205
         pstmt = dbconn.prepareStatement(sql);
206
207
         // Bind the values to the query
208
         pstmt.setString(1, docId);
209
         pstmt.setLong(2, startNodeId);
210
         pstmt.setLong(3, endNodeId);
211
212
         pstmt.execute();
213
         ResultSet rs = pstmt.getResultSet();
214
         boolean tableHasRows = rs.next();
215 2245 sgarg
216
         while (tableHasRows)
217 1502 tao
         {
218
           nodeid = rs.getLong(1);
219
           parentnodeid = rs.getLong(2);
220
           nodeindex = rs.getLong(3);
221
           nodetype = rs.getString(4);
222
           nodename = rs.getString(5);
223
           nodeprefix = rs.getString(6);
224
           nodedata = rs.getString(7);
225 4698 daigle
           nodedata = MetacatUtil.normalize(nodedata);
226 1502 tao
           // add the data to the node record list hashtable
227
           NodeRecord currentRecord = new NodeRecord(nodeid,parentnodeid,nodeindex,
228
                                      nodetype, nodename, nodeprefix, nodedata);
229
           nodeRecordList.push(currentRecord);
230
231
           // Advance to the next node
232
           tableHasRows = rs.next();
233
         }//while
234
         pstmt.close();
235
236
      } //try
237 2245 sgarg
      catch (SQLException e)
238 1502 tao
      {
239
        throw new McdbException("Error in SubTree.getSubTreeNodeList 1 " +
240
                              e.getMessage());
241
      }//catch
242
      finally
243
      {
244
        try
245
        {
246
          pstmt.close();
247
        }
248
        catch (SQLException ee)
249
        {
250 2663 sgarg
          logMetacat.error("error in SubTree.getSubTreeNodeList 2: "
251
                                    +ee.getMessage());
252 1502 tao
        }
253
        finally
254
        {
255
          DBConnectionPool.returnDBConnection(dbconn, serialNumber);
256
        }
257
      }//finally
258 2245 sgarg
259 1502 tao
      return nodeRecordList;
260 2245 sgarg
261 1502 tao
    }//getSubtreeNodeList
262 2245 sgarg
263 1520 tao
   /** methods from Comparator interface */
264
   public int compare(Object o1, Object o2)
265
   {
266
     SubTree tree1 = (SubTree) o1;
267
     SubTree tree2 = (SubTree) o2;
268
     if (tree1.getStartNodeId() > tree2.getStartNodeId())
269
     {
270
       return 1;
271
     }
272
     else if (tree1.getStartNodeId() < tree2.getStartNodeId())
273
     {
274
       return -1;
275
     }
276
     else
277
     {
278
       return 0;
279
     }
280 2245 sgarg
281 1520 tao
   }//cpmpare
282 2245 sgarg
283 1520 tao
   /** method from Comparator interface */
284
   public boolean equals(Object obj)
285
   {
286
     SubTree tree = (SubTree)obj;
287
     if (startNodeId == tree.getStartNodeId())
288
     {
289
       return true;
290
     }
291
     else
292
     {
293
       return false;
294
     }
295
   }
296 2245 sgarg
297 1414 tao
}