Project

General

Profile

1 1514 tao
/**
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$'
11
 *     '$Date$'
12
 * '$Revision$'
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 1526 tao
import java.util.Enumeration;
32
import java.util.Collections;
33 1519 tao
import java.util.Hashtable;
34 1514 tao
import java.util.Stack;
35 1519 tao
import java.util.Vector;
36 1514 tao
import java.sql.PreparedStatement;
37
import java.sql.SQLException;
38
import java.sql.ResultSet;
39
40
41
/**
42
 * A Class that represents an Filtered XML Subtree, it means a subtree which
43
 * contains another subtree has different access behavor to it.
44
 * for example, we have subtree A, it is not readable to user John. But in
45
 * subtree A, it contains a subtree B which is readable to user John
46
 */
47
public class FilteredSubTree
48
{
49
  private String docId = null;
50
  private String subTreeId = null;
51
  private String startElementName = null;
52
  private long   startNodeId = -1;
53
  private long   endNodeId =   -1;
54 1526 tao
  private Vector sortedFilteredSubTree = new Vector();
55 1514 tao
56
57
    /**
58 1519 tao
     * Constructor of filteredsubtree
59 1514 tao
     */
60
    public FilteredSubTree(String myDocId, String mySubTreeId,
61
                  long myStartNodeId, long myEndNodeId)
62
                  throws McdbException
63
    {
64
      this.docId = myDocId;
65
      MetaCatUtil.debugMessage("Docid of FilteredSubtree: " + docId, 30);
66
      this.subTreeId = mySubTreeId;
67
      MetaCatUtil.debugMessage("id of FilteredSubtree: " + subTreeId, 30);
68
      this.startNodeId = myStartNodeId;
69
      MetaCatUtil.debugMessage("start node id of FilteredSubtree: " +
70
                                startNodeId, 30);
71
      this.endNodeId = myEndNodeId;
72
      MetaCatUtil.debugMessage("end node id of FilteredSubtree: " + endNodeId, 30);
73
     }
74
75
76
77
    /** Set the a docId */
78
    public void setDocId(String myId)
79
    {
80
      MetaCatUtil.debugMessage("set doc id: "+myId, 35);
81
      this.docId = myId;
82
    }
83
84
    /** Get the docId */
85
    public String getDocId()
86
    {
87
      return this.docId;
88
    }
89
90
91
    /** Set the a subtreeId */
92
    public void setSubTreeId(String myId)
93
    {
94
      MetaCatUtil.debugMessage("set sub tree id: "+myId, 35);
95
      this.subTreeId = myId;
96
    }
97
98
    /** Get the subTreeId */
99
    public String getSubTreeId()
100
    {
101
      return this.subTreeId;
102
    }
103
104
    /**
105
     * Set a startElementName
106
     */
107
    public void setStartElementName(String elementName)
108
    {
109
      MetaCatUtil.debugMessage("set start elementname: "+elementName, 35);
110
      this.startElementName = elementName;
111
    }
112
113
    /**
114
     * Get startElementName
115
     */
116
    public String getStartElementName()
117
    {
118
      return this.startElementName;
119
    }
120
121
    /** Set a start node id */
122
    public void setStartNodeId(long nodeId)
123
    {
124
      MetaCatUtil.debugMessage("set start node id: "+nodeId, 35);
125
      this.startNodeId = nodeId;
126
    }
127
128
    /** Get start node id */
129
    public long getStartNodeId()
130
    {
131
      return this.startNodeId;
132
    }
133
134
    /** Set a end node id */
135
    public void setEndNodeId(long nodeId)
136
    {
137
      MetaCatUtil.debugMessage("set end node id: "+nodeId, 35);
138
      this.endNodeId = nodeId;
139
    }
140
141
    /** Get end node id */
142
    public long getEndNodeId()
143
    {
144
      return this.endNodeId;
145
    }
146
147 1519 tao
    /** Set a filtered subtree(subtree should be merger equivlent */
148 1526 tao
    public void setFilteredSubTree(Hashtable  tree)
149 1514 tao
    {
150 1526 tao
       // order the subtree vector by start node id
151
       // because the subtree is mergeEquivlent, so there is no nested subtree
152
       Enumeration en = tree.elements();
153
       while (en.hasMoreElements())
154
       {
155
         SubTree sTree = (SubTree) en.nextElement();
156
         sortedFilteredSubTree.add(sTree);
157
       }
158
       Collections.sort(sortedFilteredSubTree, new SubTree());
159 1519 tao
    }
160
161 1526 tao
    /** Get the filted subtree vector*/
162
    public Vector getFilteredSubTree()
163
    {
164
      return sortedFilteredSubTree;
165
    }
166
167 1519 tao
   /** Get unfiltered subtree node list*/
168
    public Stack getUnfilteredSubTreeNodeList() throws McdbException
169
    {
170
171 1514 tao
       PreparedStatement pstmt = null;
172
       DBConnection dbconn = null;
173
       int serialNumber = -1;
174
       Stack nodeRecordList = new Stack();
175
       long nodeid = 0;
176
       long parentnodeid = 0;
177
       long nodeindex = 0;
178
       String nodetype = null;
179
       String nodename = null;
180
       String nodeprefix = null;
181
       String nodedata = null;
182
       String sql = "SELECT nodeid, parentnodeid, nodeindex, " +
183
                    "nodetype, nodename, nodeprefix, nodedata " +
184 1526 tao
                    "FROM xml_nodes WHERE docid = ? AND ((nodeid >= ? AND ";
185
       for ( int i =0; i< sortedFilteredSubTree.size(); i++)
186
       {
187
         sql = sql + "nodeid < ? ) OR (nodeid > ? AND ";
188
       }
189
       sql = sql + "nodeid <= ? )) ORDER BY nodeid DESC";
190
       MetaCatUtil.debugMessage("sql command for selecting filtere subtree is: "
191
                                 +sql, 25);
192
193 1514 tao
       try
194
       {
195
         dbconn=DBConnectionPool.
196
                    getDBConnection("SubTree.getSubTreeNodeList");
197
         serialNumber=dbconn.getCheckOutSerialNumber();
198
         pstmt = dbconn.prepareStatement(sql);
199
200
         // Bind the values to the query
201
         pstmt.setString(1, docId);
202
         pstmt.setLong(2, startNodeId);
203 1526 tao
         int i =0;
204
         boolean inLoop = false;
205
         for (i=0; i< sortedFilteredSubTree.size(); i++)
206
         {
207
           SubTree tree = (SubTree)sortedFilteredSubTree.elementAt(i);
208
           long startId = tree.getStartNodeId();
209
           long endId   = tree.getEndNodeId();
210
           pstmt.setLong((2*i+3), startId);
211
           pstmt.setLong((2*i+4), endId);
212
           inLoop = true;
213
         }
214
         if (inLoop)
215
         {
216
           pstmt.setLong((2*i+5), endNodeId);
217
         }
218
         else
219
         {
220
           pstmt.setLong(3, endNodeId);
221
         }
222 1514 tao
223
         pstmt.execute();
224
         ResultSet rs = pstmt.getResultSet();
225
         boolean tableHasRows = rs.next();
226
227
         while (tableHasRows)
228
         {
229
           nodeid = rs.getLong(1);
230
           parentnodeid = rs.getLong(2);
231
           nodeindex = rs.getLong(3);
232
           nodetype = rs.getString(4);
233
           nodename = rs.getString(5);
234
           nodeprefix = rs.getString(6);
235
           nodedata = rs.getString(7);
236
           nodedata = MetaCatUtil.normalize(nodedata);
237
           // add the data to the node record list hashtable
238
           NodeRecord currentRecord = new NodeRecord(nodeid,parentnodeid,nodeindex,
239
                                      nodetype, nodename, nodeprefix, nodedata);
240
           nodeRecordList.push(currentRecord);
241
242
           // Advance to the next node
243
           tableHasRows = rs.next();
244
         }//while
245
         pstmt.close();
246
247
      } //try
248
      catch (SQLException e)
249
      {
250 1526 tao
        throw new McdbException("Error in FilteredSubTree.getSubTreeNodeList 1 " +
251 1514 tao
                              e.getMessage());
252
      }//catch
253
      finally
254
      {
255
        try
256
        {
257
          pstmt.close();
258
        }
259
        catch (SQLException ee)
260
        {
261 1526 tao
          MetaCatUtil.debugMessage("error in FilteredSubTree.getSubTreeNodeList 2: "
262 1514 tao
                                    +ee.getMessage(), 30);
263
        }
264
        finally
265
        {
266
          DBConnectionPool.returnDBConnection(dbconn, serialNumber);
267
        }
268
      }//finally
269
270
      return nodeRecordList;
271
272
    }//getSubtreeNodeList
273
274
    public static void main (String agus)
275
    {
276
277
    }
278
}