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: jones $'
10
 *     '$Date: 2006-11-10 10:25:38 -0800 (Fri, 10 Nov 2006) $'
11
 * '$Revision: 3077 $'
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
/**
40
 * A Class that represents an XML Subtree
41
 */
42
public class SubTree implements Comparator
43
{
44
  protected String docId = null;
45
  protected String subTreeId = null;
46
  protected String startElementName = null;
47
  protected long   startNodeId = -1;
48
  protected long   endNodeId =   -1;
49
  private Stack  subTreeNodeStack = null;
50
  
51
  private static Logger logMetacat = Logger.getLogger(SubTree.class);
52

    
53
    /**
54
     * Defualt constructor
55
     */
56
    public SubTree()
57
    {
58

    
59
    }
60

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

    
76
    }
77

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

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

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

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

    
115

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

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

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

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

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

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

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

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

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

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

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

    
208
         pstmt.execute();
209
         ResultSet rs = pstmt.getResultSet();
210
         boolean tableHasRows = rs.next();
211

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

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

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

    
255
      return nodeRecordList;
256

    
257
    }//getSubtreeNodeList
258

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

    
277
   }//cpmpare
278

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

    
293
}
(64-64/66)