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-03-21 17:45:34 -0800 (Fri, 21 Mar 2003) $'
12
 * '$Revision: 1502 $'
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.Stack;
32
import java.sql.PreparedStatement;
33
import java.sql.SQLException;
34
import java.sql.ResultSet;
35

    
36

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

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

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

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

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

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

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

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

    
207
      } //try
208
      catch (SQLException e) 
209
      {
210
        throw new McdbException("Error in SubTree.getSubTreeNodeList 1 " +
211
                              e.getMessage());
212
      }//catch
213
      finally
214
      {
215
        try
216
        {
217
          pstmt.close();
218
        }
219
        catch (SQLException ee)
220
        {
221
          MetaCatUtil.debugMessage("error in SubTree.getSubTreeNodeList 2: "
222
                                    +ee.getMessage(), 30);
223
        }
224
        finally
225
        {
226
          DBConnectionPool.returnDBConnection(dbconn, serialNumber);
227
        }
228
      }//finally
229
  
230
      return nodeRecordList;
231
   
232
    }//getSubtreeNodeList
233
    
234
    public static void main (String agus)
235
    {
236
      
237
    }
238
}
(54-54/55)