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: sgarg $'
11
 *     '$Date: 2005-10-10 11:06:55 -0700 (Mon, 10 Oct 2005) $'
12
 * '$Revision: 2663 $'
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.Enumeration;
32
import java.util.Collections;
33
import java.util.Hashtable;
34
import java.util.Stack;
35
import java.util.Vector;
36
import java.sql.PreparedStatement;
37
import java.sql.SQLException;
38
import java.sql.ResultSet;
39

    
40
import org.apache.log4j.Logger;
41

    
42

    
43
/**
44
 * A Class that represents an Filtered XML Subtree, it means a subtree which 
45
 * contains another subtree has different access behavor to it.
46
 * for example, we have subtree A, it is not readable to user John. But in 
47
 * subtree A, it contains a subtree B which is readable to user John 
48
 */
49
public class FilteredSubTree
50
{
51
  private String docId = null;
52
  private String subTreeId = null;
53
  private String startElementName = null;
54
  private long   startNodeId = -1;
55
  private long   endNodeId =   -1;
56
  private Vector sortedFilteredSubTree = new Vector();
57
  private Logger logMetacat = Logger.getLogger(FilteredSubTree.class);
58
    
59
    /**
60
     * Constructor of filteredsubtree
61
     */
62
    public FilteredSubTree(String myDocId, String mySubTreeId, 
63
                  long myStartNodeId, long myEndNodeId)
64
                  throws McdbException
65
    {
66
      this.docId = myDocId;
67
      logMetacat.info("Docid of FilteredSubtree: " + docId);
68
      this.subTreeId = mySubTreeId;
69
      logMetacat.info("id of FilteredSubtree: " + subTreeId);
70
      this.startNodeId = myStartNodeId;
71
      logMetacat.info("start node id of FilteredSubtree: " + 
72
                                startNodeId);
73
      this.endNodeId = myEndNodeId;
74
      logMetacat.info("end node id of FilteredSubtree: " + endNodeId);
75
     }
76
    
77
  
78
 
79
    /** Set the a docId */
80
    public void setDocId(String myId)
81
    {
82
      logMetacat.info("set doc id: "+myId);
83
      this.docId = myId;
84
    }
85

    
86
    /** Get the docId */
87
    public String getDocId() 
88
    {
89
      return this.docId;
90
    }
91

    
92
 
93
    /** Set the a subtreeId */
94
    public void setSubTreeId(String myId)
95
    {
96
      logMetacat.info("set sub tree id: "+myId);
97
      this.subTreeId = myId;
98
    }
99

    
100
    /** Get the subTreeId */
101
    public String getSubTreeId() 
102
    {
103
      return this.subTreeId;
104
    }
105

    
106
    /** 
107
     * Set a startElementName
108
     */
109
    public void setStartElementName(String elementName) 
110
    {
111
      logMetacat.info("set start elementname: "+elementName);
112
      this.startElementName = elementName;
113
    }
114
    
115
    /**
116
     * Get startElementName
117
     */
118
    public String getStartElementName()
119
    {
120
      return this.startElementName;
121
    }
122
    
123
    /** Set a start node id */
124
    public void setStartNodeId(long nodeId)
125
    {
126
      logMetacat.info("set start node id: "+nodeId);
127
      this.startNodeId = nodeId;
128
    }
129
    
130
    /** Get start node id */
131
    public long getStartNodeId()
132
    {
133
      return this.startNodeId;
134
    }
135
    
136
    /** Set a end node id */
137
    public void setEndNodeId(long nodeId)
138
    {
139
      logMetacat.info("set end node id: "+nodeId);
140
      this.endNodeId = nodeId;
141
    }
142
    
143
    /** Get end node id */
144
    public long getEndNodeId()
145
    {
146
      return this.endNodeId;
147
    }
148
    
149
    /** Set a filtered subtree(subtree should be merger equivlent */
150
    public void setFilteredSubTree(Hashtable  tree)
151
    {
152
       // order the subtree vector by start node id
153
       // because the subtree is mergeEquivlent, so there is no nested subtree
154
       Enumeration en = tree.elements();
155
       while (en.hasMoreElements())
156
       {
157
         SubTree sTree = (SubTree) en.nextElement();
158
         sortedFilteredSubTree.add(sTree);
159
       }
160
       Collections.sort(sortedFilteredSubTree, new SubTree());
161
    }
162
    
163
    /** Get the filted subtree vector*/
164
    public Vector getFilteredSubTree()
165
    {
166
      return sortedFilteredSubTree;
167
    }
168
    
169
   /** Get unfiltered subtree node list*/
170
    public Stack getUnfilteredSubTreeNodeList() throws McdbException
171
    {
172
       
173
       PreparedStatement pstmt = null;
174
       DBConnection dbconn = null;
175
       int serialNumber = -1;
176
       Stack nodeRecordList = new Stack();
177
       long nodeid = 0;
178
       long parentnodeid = 0;
179
       long nodeindex = 0;
180
       String nodetype = null;
181
       String nodename = null;
182
       String nodeprefix = null;
183
       String nodedata = null;
184
       String sql = "SELECT nodeid, parentnodeid, nodeindex, " +
185
                    "nodetype, nodename, nodeprefix, nodedata " +               
186
                    "FROM xml_nodes WHERE docid = ? AND ((nodeid >= ? AND ";
187
       for ( int i =0; i< sortedFilteredSubTree.size(); i++)
188
       {
189
         sql = sql + "nodeid < ? ) OR (nodeid > ? AND "; 
190
       }
191
       sql = sql + "nodeid <= ? )) ORDER BY nodeid DESC";
192
       logMetacat.info("sql command for selecting filtere subtree is: "
193
                                 +sql);
194
       
195
       try 
196
       {
197
         dbconn=DBConnectionPool.
198
                    getDBConnection("SubTree.getSubTreeNodeList");
199
         serialNumber=dbconn.getCheckOutSerialNumber();
200
         pstmt = dbconn.prepareStatement(sql);
201

    
202
         // Bind the values to the query
203
         pstmt.setString(1, docId);
204
         pstmt.setLong(2, startNodeId);
205
         int i =0;
206
         boolean inLoop = false;
207
         for (i=0; i< sortedFilteredSubTree.size(); i++)
208
         {
209
           SubTree tree = (SubTree)sortedFilteredSubTree.elementAt(i);
210
           long startId = tree.getStartNodeId();
211
           long endId   = tree.getEndNodeId();
212
           pstmt.setLong((2*i+3), startId);
213
           pstmt.setLong((2*i+4), endId);
214
           inLoop = true;
215
         }
216
         if (inLoop)
217
         {
218
           pstmt.setLong((2*i+5), endNodeId);
219
         }
220
         else
221
         {
222
           pstmt.setLong(3, endNodeId);
223
         }
224

    
225
         pstmt.execute();
226
         ResultSet rs = pstmt.getResultSet();
227
         boolean tableHasRows = rs.next();
228
        
229
         while (tableHasRows) 
230
         {
231
           nodeid = rs.getLong(1);
232
           parentnodeid = rs.getLong(2);
233
           nodeindex = rs.getLong(3);
234
           nodetype = rs.getString(4);
235
           nodename = rs.getString(5);
236
           nodeprefix = rs.getString(6);
237
           nodedata = rs.getString(7);
238
           nodedata = MetaCatUtil.normalize(nodedata);
239
           // add the data to the node record list hashtable
240
           NodeRecord currentRecord = new NodeRecord(nodeid,parentnodeid,nodeindex,
241
                                      nodetype, nodename, nodeprefix, nodedata);
242
           nodeRecordList.push(currentRecord);
243

    
244
           // Advance to the next node
245
           tableHasRows = rs.next();
246
         }//while
247
         pstmt.close();
248

    
249
      } //try
250
      catch (SQLException e) 
251
      {
252
        throw new McdbException("Error in FilteredSubTree.getSubTreeNodeList 1 " +
253
                              e.getMessage());
254
      }//catch
255
      finally
256
      {
257
        try
258
        {
259
          pstmt.close();
260
        }
261
        catch (SQLException ee)
262
        {
263
          logMetacat.error("error in FilteredSubTree.getSubTreeNodeList 2: "
264
                                    +ee.getMessage());
265
        }
266
        finally
267
        {
268
          DBConnectionPool.returnDBConnection(dbconn, serialNumber);
269
        }
270
      }//finally
271
  
272
      return nodeRecordList;
273
   
274
    }//getSubtreeNodeList
275
    
276
    public static void main (String agus)
277
    {
278
      
279
    }
280
}
(38-38/63)