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
 *    Release: @release@
9
 *
10
 *   '$Author: tao $'
11
 *     '$Date: 2003-04-07 17:48:01 -0700 (Mon, 07 Apr 2003) $'
12
 * '$Revision: 1520 $'
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
import java.util.Comparator;
32
import java.util.Stack;
33
import java.sql.PreparedStatement;
34
import java.sql.SQLException;
35
import java.sql.ResultSet;
36

    
37

    
38
/**
39
 * A Class that represents an XML Subtree
40
 */
41
public class SubTree implements Comparator
42
{
43
  private String docId = null;
44
  private String subTreeId = null;
45
  private String startElementName = null;
46
  private long   startNodeId = -1;
47
  private long   endNodeId =   -1;
48
  private Stack  subTreeNodeStack = null;
49
    
50
    /**
51
     * Defualt constructor
52
     */
53
    public SubTree()
54
    {
55
      
56
    }
57
    
58
    /**
59
     * Constructor of subtree
60
     */
61
    public SubTree(String myDocId, String mySubTreeId, 
62
                  long myStartNodeId, long myEndNodeId)
63
                  throws McdbException
64
    {
65
      this.docId = myDocId;
66
      MetaCatUtil.debugMessage("Docid of Subtree: " + docId, 30);
67
      this.subTreeId = mySubTreeId;
68
      MetaCatUtil.debugMessage("id of Subtree: " + subTreeId, 30);
69
      this.startNodeId = myStartNodeId;
70
      MetaCatUtil.debugMessage("start node id of Subtree: " + startNodeId, 30);
71
      this.endNodeId = myEndNodeId;
72
      MetaCatUtil.debugMessage("end node id of subtree: " + endNodeId, 30);
73
      subTreeNodeStack = getSubTreeNodeList();
74
    }
75
    
76
    /**
77
     * Get subtree node stack
78
     */
79
    public Stack getSubTreeNodeStack()
80
    {
81
      return this.subTreeNodeStack;
82
    }
83
 
84
    /** Set the a docId */
85
    public void setDocId(String myId)
86
    {
87
      MetaCatUtil.debugMessage("set doc id: "+myId, 35);
88
      this.docId = myId;
89
    }
90

    
91
    /** Get the docId */
92
    public String getDocId() 
93
    {
94
      return this.docId;
95
    }
96

    
97
 
98
    /** Set the a subtreeId */
99
    public void setSubTreeId(String myId)
100
    {
101
      MetaCatUtil.debugMessage("set sub tree id: "+myId, 35);
102
      this.subTreeId = myId;
103
    }
104

    
105
    /** Get the subTreeId */
106
    public String getSubTreeId() 
107
    {
108
      return this.subTreeId;
109
    }
110

    
111
    /** 
112
     * Set a startElementName
113
     */
114
    public void setStartElementName(String elementName) 
115
    {
116
      MetaCatUtil.debugMessage("set start elementname: "+elementName, 35);
117
      this.startElementName = elementName;
118
    }
119
    
120
    /**
121
     * Get startElementName
122
     */
123
    public String getStartElementName()
124
    {
125
      return this.startElementName;
126
    }
127
    
128
    /** Set a start node id */
129
    public void setStartNodeId(long nodeId)
130
    {
131
      MetaCatUtil.debugMessage("set start node id: "+nodeId, 35);
132
      this.startNodeId = nodeId;
133
    }
134
    
135
    /** Get start node id */
136
    public long getStartNodeId()
137
    {
138
      return this.startNodeId;
139
    }
140
    
141
    /** Set a end node id */
142
    public void setEndNodeId(long nodeId)
143
    {
144
      MetaCatUtil.debugMessage("set end node id: "+nodeId, 35);
145
      this.endNodeId = nodeId;
146
    }
147
    
148
    /** Get end node id */
149
    public long getEndNodeId()
150
    {
151
      return this.endNodeId;
152
    }
153
    
154
    /* Put a subtree node into a stack, on top is the start point of subtree*/
155
    private Stack getSubTreeNodeList() throws McdbException
156
    {
157
       PreparedStatement pstmt = null;
158
       DBConnection dbconn = null;
159
       int serialNumber = -1;
160
       Stack nodeRecordList = new Stack();
161
       long nodeid = 0;
162
       long parentnodeid = 0;
163
       long nodeindex = 0;
164
       String nodetype = null;
165
       String nodename = null;
166
       String nodeprefix = null;
167
       String nodedata = null;
168
       String sql = "SELECT nodeid, parentnodeid, nodeindex, " +
169
                    "nodetype, nodename, nodeprefix, nodedata " +               
170
                    "FROM xml_nodes WHERE docid = ? AND nodeid >= ? AND " +
171
                    "nodeid <= ? ORDER BY nodeid DESC";
172
       try 
173
       {
174
         dbconn=DBConnectionPool.
175
                    getDBConnection("SubTree.getSubTreeNodeList");
176
         serialNumber=dbconn.getCheckOutSerialNumber();
177
         pstmt = dbconn.prepareStatement(sql);
178

    
179
         // Bind the values to the query
180
         pstmt.setString(1, docId);
181
         pstmt.setLong(2, startNodeId);
182
         pstmt.setLong(3, endNodeId);
183

    
184
         pstmt.execute();
185
         ResultSet rs = pstmt.getResultSet();
186
         boolean tableHasRows = rs.next();
187
        
188
         while (tableHasRows) 
189
         {
190
           nodeid = rs.getLong(1);
191
           parentnodeid = rs.getLong(2);
192
           nodeindex = rs.getLong(3);
193
           nodetype = rs.getString(4);
194
           nodename = rs.getString(5);
195
           nodeprefix = rs.getString(6);
196
           nodedata = rs.getString(7);
197
           nodedata = MetaCatUtil.normalize(nodedata);
198
           // add the data to the node record list hashtable
199
           NodeRecord currentRecord = new NodeRecord(nodeid,parentnodeid,nodeindex,
200
                                      nodetype, nodename, nodeprefix, nodedata);
201
           nodeRecordList.push(currentRecord);
202

    
203
           // Advance to the next node
204
           tableHasRows = rs.next();
205
         }//while
206
         pstmt.close();
207

    
208
      } //try
209
      catch (SQLException e) 
210
      {
211
        throw new McdbException("Error in SubTree.getSubTreeNodeList 1 " +
212
                              e.getMessage());
213
      }//catch
214
      finally
215
      {
216
        try
217
        {
218
          pstmt.close();
219
        }
220
        catch (SQLException ee)
221
        {
222
          MetaCatUtil.debugMessage("error in SubTree.getSubTreeNodeList 2: "
223
                                    +ee.getMessage(), 30);
224
        }
225
        finally
226
        {
227
          DBConnectionPool.returnDBConnection(dbconn, serialNumber);
228
        }
229
      }//finally
230
  
231
      return nodeRecordList;
232
   
233
    }//getSubtreeNodeList
234
    
235
   /** methods from Comparator interface */
236
   public int compare(Object o1, Object o2)
237
   {
238
     SubTree tree1 = (SubTree) o1;
239
     SubTree tree2 = (SubTree) o2;
240
     if (tree1.getStartNodeId() > tree2.getStartNodeId())
241
     {
242
       return 1;
243
     }
244
     else if (tree1.getStartNodeId() < tree2.getStartNodeId())
245
     {
246
       return -1;
247
     }
248
     else
249
     {
250
       return 0;
251
     }
252
     
253
   }//cpmpare
254
   
255
   /** method from Comparator interface */
256
   public boolean equals(Object obj)
257
   {
258
     SubTree tree = (SubTree)obj;
259
     if (startNodeId == tree.getStartNodeId())
260
     {
261
       return true;
262
     }
263
     else
264
     {
265
       return false;
266
     }
267
   }
268
   
269
}
(55-55/56)