Project

General

Profile

1
/**
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: daigle $'
10
 *     '$Date: 2008-12-26 13:07:40 -0800 (Fri, 26 Dec 2008) $'
11
 * '$Revision: 4698 $'
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
import java.util.Comparator;
31
import java.util.Stack;
32
import java.sql.PreparedStatement;
33
import java.sql.SQLException;
34
import java.sql.ResultSet;
35

    
36
import org.apache.log4j.Logger;
37
import org.xml.sax.SAXException;
38

    
39
import edu.ucsb.nceas.metacat.util.MetacatUtil;
40

    
41
/**
42
 * A Class that represents an XML Subtree
43
 */
44
public class SubTree implements Comparator
45
{
46
  protected String docId = null;
47
  protected String subTreeId = null;
48
  protected String startElementName = null;
49
  protected long   startNodeId = -1;
50
  protected long   endNodeId =   -1;
51
  private Stack<NodeRecord>  subTreeNodeStack = null;
52
  
53
  private static Logger logMetacat = Logger.getLogger(SubTree.class);
54

    
55
    /**
56
     * Defualt constructor
57
     */
58
    public SubTree()
59
    {
60

    
61
    }
62

    
63
    /**
64
     * Constructor of subtree
65
     */
66
    public SubTree(String myDocId, String mySubTreeId,
67
                  long myStartNodeId, long myEndNodeId)
68
    {
69
      this.docId = myDocId;
70
      logMetacat.info("Docid of Subtree: " + docId);
71
      this.subTreeId = mySubTreeId;
72
      logMetacat.info("id of Subtree: " + subTreeId);
73
      this.startNodeId = myStartNodeId;
74
      logMetacat.info("start node id of Subtree: " + startNodeId);
75
      this.endNodeId = myEndNodeId;
76
      logMetacat.info("end node id of subtree: " + endNodeId);
77

    
78
    }
79

    
80
    /**
81
     * Get subtree node stack
82
     */
83
    public Stack<NodeRecord> getSubTreeNodeStack() throws SAXException
84
    {
85
      try
86
      {
87
        subTreeNodeStack = getSubTreeNodeList();
88
      }
89
      catch (McdbException e)
90
      {
91
        throw new SAXException(e.getMessage());
92
      }
93
      return this.subTreeNodeStack;
94
    }
95

    
96
    /**
97
     * Set subtree node stack
98
     */
99
    public void setSubTreeNodeStack(Stack myStack)
100
    {
101
      this.subTreeNodeStack = myStack;
102
    }
103

    
104
    /** Set the a docId */
105
    public void setDocId(String myId)
106
    {
107
      logMetacat.info("set doc id: "+myId);
108
      this.docId = myId;
109
    }
110

    
111
    /** Get the docId */
112
    public String getDocId()
113
    {
114
      return this.docId;
115
    }
116

    
117

    
118
    /** Set the a subtreeId */
119
    public void setSubTreeId(String myId)
120
    {
121
      logMetacat.info("set sub tree id: "+myId);
122
      this.subTreeId = myId;
123
    }
124

    
125
    /** Get the subTreeId */
126
    public String getSubTreeId()
127
    {
128
      return this.subTreeId;
129
    }
130

    
131
    /**
132
     * Set a startElementName
133
     */
134
    public void setStartElementName(String elementName)
135
    {
136
      logMetacat.info("set start elementname: "+elementName);
137
      this.startElementName = elementName;
138
    }
139

    
140
    /**
141
     * Get startElementName
142
     */
143
    public String getStartElementName()
144
    {
145
      return this.startElementName;
146
    }
147

    
148
    /** Set a start node id */
149
    public void setStartNodeId(long nodeId)
150
    {
151
      logMetacat.info("set start node id: "+nodeId);
152
      this.startNodeId = nodeId;
153
    }
154

    
155
    /** Get start node id */
156
    public long getStartNodeId()
157
    {
158
      return this.startNodeId;
159
    }
160

    
161
    /** Set a end node id */
162
    public void setEndNodeId(long nodeId)
163
    {
164
      logMetacat.info("set end node id: "+nodeId);
165
      this.endNodeId = nodeId;
166
    }
167

    
168
    /** Get end node id */
169
    public long getEndNodeId()
170
    {
171
      return this.endNodeId;
172
    }
173

    
174
    /* Put a subtree node into a stack, on top is the start point of subtree*/
175
    private Stack getSubTreeNodeList() throws McdbException
176
    {
177
       Stack nodeRecordList = new Stack();
178
       // make sure it works
179
       if ( docId == null || startNodeId == -1 || endNodeId == -1)
180
       {
181
         return nodeRecordList;
182
       }
183
       PreparedStatement pstmt = null;
184
       DBConnection dbconn = null;
185
       int serialNumber = -1;
186

    
187
       long nodeid = 0;
188
       long parentnodeid = 0;
189
       long nodeindex = 0;
190
       String nodetype = null;
191
       String nodename = null;
192
       String nodeprefix = null;
193
       String nodedata = null;
194
       String sql = "SELECT nodeid, parentnodeid, nodeindex, " +
195
                    "nodetype, nodename, nodeprefix, nodedata " +
196
                    "FROM xml_nodes WHERE docid = ? AND nodeid >= ? AND " +
197
                    "nodeid <= ? ORDER BY nodeid DESC";
198
       try
199
       {
200
         dbconn=DBConnectionPool.
201
                    getDBConnection("SubTree.getSubTreeNodeList");
202
         serialNumber=dbconn.getCheckOutSerialNumber();
203
         pstmt = dbconn.prepareStatement(sql);
204

    
205
         // Bind the values to the query
206
         pstmt.setString(1, docId);
207
         pstmt.setLong(2, startNodeId);
208
         pstmt.setLong(3, endNodeId);
209

    
210
         pstmt.execute();
211
         ResultSet rs = pstmt.getResultSet();
212
         boolean tableHasRows = rs.next();
213

    
214
         while (tableHasRows)
215
         {
216
           nodeid = rs.getLong(1);
217
           parentnodeid = rs.getLong(2);
218
           nodeindex = rs.getLong(3);
219
           nodetype = rs.getString(4);
220
           nodename = rs.getString(5);
221
           nodeprefix = rs.getString(6);
222
           nodedata = rs.getString(7);
223
           nodedata = MetacatUtil.normalize(nodedata);
224
           // add the data to the node record list hashtable
225
           NodeRecord currentRecord = new NodeRecord(nodeid,parentnodeid,nodeindex,
226
                                      nodetype, nodename, nodeprefix, nodedata);
227
           nodeRecordList.push(currentRecord);
228

    
229
           // Advance to the next node
230
           tableHasRows = rs.next();
231
         }//while
232
         pstmt.close();
233

    
234
      } //try
235
      catch (SQLException e)
236
      {
237
        throw new McdbException("Error in SubTree.getSubTreeNodeList 1 " +
238
                              e.getMessage());
239
      }//catch
240
      finally
241
      {
242
        try
243
        {
244
          pstmt.close();
245
        }
246
        catch (SQLException ee)
247
        {
248
          logMetacat.error("error in SubTree.getSubTreeNodeList 2: "
249
                                    +ee.getMessage());
250
        }
251
        finally
252
        {
253
          DBConnectionPool.returnDBConnection(dbconn, serialNumber);
254
        }
255
      }//finally
256

    
257
      return nodeRecordList;
258

    
259
    }//getSubtreeNodeList
260

    
261
   /** methods from Comparator interface */
262
   public int compare(Object o1, Object o2)
263
   {
264
     SubTree tree1 = (SubTree) o1;
265
     SubTree tree2 = (SubTree) o2;
266
     if (tree1.getStartNodeId() > tree2.getStartNodeId())
267
     {
268
       return 1;
269
     }
270
     else if (tree1.getStartNodeId() < tree2.getStartNodeId())
271
     {
272
       return -1;
273
     }
274
     else
275
     {
276
       return 0;
277
     }
278

    
279
   }//cpmpare
280

    
281
   /** method from Comparator interface */
282
   public boolean equals(Object obj)
283
   {
284
     SubTree tree = (SubTree)obj;
285
     if (startNodeId == tree.getStartNodeId())
286
     {
287
       return true;
288
     }
289
     else
290
     {
291
       return false;
292
     }
293
   }
294

    
295
}
(67-67/69)