Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *    Authors: Jivka Bojilova
6
 *    Release: @release@
7
 *
8
 *   '$Author: tao $'
9
 *     '$Date: 2002-11-01 18:09:24 -0800 (Fri, 01 Nov 2002) $'
10
 * '$Revision: 1322 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat;
28

    
29
import java.io.*;
30
import java.sql.*;
31
import java.util.Stack;
32
import java.util.Vector;
33
import java.util.Hashtable;
34
import java.net.URL;
35
import java.net.MalformedURLException;
36

    
37

    
38
/** 
39
 * A Class to get access rules from xml_access table
40
 */
41
public class AccessRulesFromDB {
42

    
43
  private String aclid      = null;  
44
  private String principal  = null;
45
  private int    permision  = 0;
46
  private String allowType  = null;
47
  private String allowOrder = null;
48
  
49
  private Vector docidAccessRuleApplied = new Vector();
50
  private Vector accessRuleVector = new Vector();
51

    
52
  /**
53
   * Construct an instance of the AccessRulesFromDB
54
   * @param aclid the Accession# of the document with the acl data
55
   */
56
  public AccessRulesFromDB(String aclid)
57
                  throws IOException, McdbException, SQLException
58
  {
59
    this.aclid = aclid;
60
    selectAccessRules();
61
    selectDocIdAccessRuleApplied();
62
  }
63

    
64

    
65
  /**
66
   * Method to get accessRuleVector
67
   */
68
  public Vector getAccessRuleVector()
69
  {
70
    return accessRuleVector;
71
  }
72
  
73
  /**
74
   * Method to get docidAccessRuleApplied
75
   */
76
  public Vector getDocidAccessRuleApplied()
77
  {
78
    return docidAccessRuleApplied;
79
  }
80
  
81
  /* Get all objects associated with @aclid from db.*/
82
  private void selectAccessRules() throws SQLException 
83
  {
84
    String accessRule = null;
85
    DBConnection conn = null;
86
    int serialNumber = -1;
87
    PreparedStatement pstmt = null;
88
    try
89
    {
90
      //get connection from DBConnectionPool
91
      conn = DBConnectionPool.
92
                getDBConnection("AccessRulesFromDB.selectAccessRules");
93
      serialNumber=conn.getCheckOutSerialNumber();
94
      
95
      // delete all acl records for resources related to @aclid if any
96
      pstmt = conn.prepareStatement("SELECT principal_name, perm_type, " +
97
                                    "permission, perm_order  FROM xml_access " +
98
                                    "WHERE accessfileid = ? ");
99
      pstmt.setString(1,aclid);
100
      pstmt.execute();
101
      ResultSet rs = pstmt.getResultSet();
102
      boolean hasRows = rs.next();
103
      int i = 0;
104
      while (hasRows) 
105
      {
106
        principal  = rs.getString(1);
107
        if (principal != null)
108
        {
109
          principal = principal.trim();
110
        }
111
        allowType  = rs.getString(2);
112
        if ( allowType != null)
113
        {
114
          allowType = allowType.trim();
115
        }
116
        permision  = rs.getInt(3);
117
        allowOrder = rs.getString(4);
118
        if (allowOrder != null)
119
        {
120
          allowOrder.trim();
121
        }
122
        //combine access rule
123
        accessRule = principal + CleanupAccessTable.DELIMITER;
124
        accessRule = accessRule + allowType + CleanupAccessTable.DELIMITER;
125
        accessRule = accessRule + permision + CleanupAccessTable.DELIMITER;
126
        accessRule = accessRule + allowOrder;
127
        // if hashtable doesn't has the rule add to hashtable
128
        if (!accessRuleVector.contains(accessRule))
129
        {
130
          accessRuleVector.addElement(accessRule);
131
          i++;
132
          
133
        }
134
        hasRows = rs.next();
135
        // reset value
136
        accessRule = null;
137
        principal  = null;
138
        permision  = 0;
139
        allowType  = null;
140
        allowOrder = null;
141
        
142
      }//while
143
  
144
    }
145
    catch (SQLException e)
146
    {
147
      throw e;
148
    }
149
    finally
150
    {
151
      try
152
      {
153
        pstmt.close();
154
      }
155
      finally
156
      {
157
        //retrun DBConnection
158
        DBConnectionPool.returnDBConnection(conn,serialNumber);
159
      }
160
    }
161
 
162
  }
163

    
164
 /* Get all objects associated with @aclid from db.*/
165
  private void selectDocIdAccessRuleApplied() throws SQLException 
166
  {
167
    String docid = null;
168
    DBConnection conn = null;
169
    int serialNumber = -1;
170
    PreparedStatement pstmt = null;
171
    try
172
    {
173
      //get connection from DBConnectionPool
174
      conn = DBConnectionPool.
175
              getDBConnection("AccessRulesFromDB.selectDocIdAccessRuleApplied");
176
      serialNumber=conn.getCheckOutSerialNumber();
177
      
178
      // delete all acl records for resources related to @aclid if any
179
      pstmt = conn.prepareStatement("SELECT docid FROM xml_access " +
180
                                    "WHERE accessfileid = ? ");
181
      pstmt.setString(1,aclid);
182
      pstmt.execute();
183
      ResultSet rs = pstmt.getResultSet();
184
      boolean hasRows = rs.next();
185
      int i = 0;
186
      while (hasRows) 
187
      {
188
        docid  = rs.getString(1);
189
        // if hashtable doesn't has the rule add to hashtable
190
        if (!docidAccessRuleApplied.contains(docid) && !docid.equals(aclid))
191
        {
192
          docidAccessRuleApplied.addElement(docid);
193
          i++;
194
          
195
        }
196
        hasRows = rs.next();
197
        // reset value
198
        docid = null;
199
        
200
      }//while
201
  
202
    }
203
    catch (SQLException e)
204
    {
205
      throw e;
206
    }
207
    finally
208
    {
209
      try
210
      {
211
        pstmt.close();
212
      }
213
      finally
214
      {
215
        //retrun DBConnection
216
        DBConnectionPool.returnDBConnection(conn,serialNumber);
217
      }
218
    }
219
 
220
  }
221

    
222
 
223
 
224
 
225

    
226
  public static void main(String[] agus)
227
  {
228
    if(agus.length == 0)
229
    {
230
      System.out.println("you should specify a docid!");
231
      return;
232
    }
233
    String docID = agus[0];
234
    try
235
    {
236
      DBConnectionPool pool = DBConnectionPool.getInstance();
237
      AccessRulesFromDocument xmlDocument = new AccessRulesFromDocument(docID);
238
      Vector rules = xmlDocument.getAccessRuleVector();
239
      Vector docid = xmlDocument.getACLObjects();
240
      AccessRulesFromDB db = new AccessRulesFromDB(docID);
241
      Vector rulesFromDB = db.getAccessRuleVector();
242
      Vector docidFromDB = db.getDocidAccessRuleApplied();
243
      for (int i = 0; i<rulesFromDB.size(); i++)
244
      {
245
        System.out.println("rule: "+(String)rulesFromDB.elementAt(i));
246
        if (!rules.contains(rulesFromDB.elementAt(i)))
247
        {
248
          System.out.println("find a new rule!!!");
249
        }
250
      }
251
      for (int i = 0; i<docidFromDB.size(); i++)
252
      {
253
        System.out.println("docid: "+(String)docidFromDB.elementAt(i));
254
        if (!docid.contains(docidFromDB.elementAt(i)))
255
        {
256
          System.out.println("find a new triple");
257
        }
258
      }
259
    }
260
    catch(Exception e)
261
    {
262
      System.out.println("exception is: "+e.getMessage());
263
    }
264
  }
265
  
266
}
(2-2/48)