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: 2010-04-14 11:31:03 -0700 (Wed, 14 Apr 2010) $'
11
 * '$Revision: 5311 $'
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.database.DBConnection;
40
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
41
import edu.ucsb.nceas.metacat.util.MetacatUtil;
42

    
43
/**
44
 * A Class that represents an XML Subtree
45
 */
46
public class SubTree implements Comparator
47
{
48
  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
  private Stack<NodeRecord>  subTreeNodeStack = null;
54
  
55
  private static Logger logMetacat = Logger.getLogger(SubTree.class);
56

    
57
    /**
58
     * Defualt constructor
59
     */
60
    public SubTree()
61
    {
62

    
63
    }
64

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

    
80
    }
81

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

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

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

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

    
119

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

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

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

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

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

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

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

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

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

    
189
       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
                    "nodetype, nodename, nodeprefix, nodedata " +
198
                    "FROM xml_nodes WHERE docid = ? AND nodeid >= ? AND " +
199
                    "nodeid <= ? ORDER BY nodeid DESC";
200
       try
201
       {
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

    
216
         while (tableHasRows)
217
         {
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
           nodedata = MetacatUtil.normalize(nodedata);
226
           // 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
      catch (SQLException e)
238
      {
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
          logMetacat.error("error in SubTree.getSubTreeNodeList 2: "
251
                                    +ee.getMessage());
252
        }
253
        finally
254
        {
255
          DBConnectionPool.returnDBConnection(dbconn, serialNumber);
256
        }
257
      }//finally
258

    
259
      return nodeRecordList;
260

    
261
    }//getSubtreeNodeList
262

    
263
   /** 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

    
281
   }//cpmpare
282

    
283
   /** 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

    
297
}
(61-61/64)