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