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-10-30 16:41:37 -0800 (Wed, 30 Oct 2002) $'
10
 * '$Revision: 1321 $'
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
        allowType  = rs.getString(2);
108
        permision  = rs.getInt(3);
109
        allowOrder = rs.getString(4);
110
        //combine access rule
111
        accessRule = principal + " ";
112
        accessRule = accessRule + allowType + " ";
113
        accessRule = accessRule + permision + " ";
114
        accessRule = accessRule + allowOrder;
115
        // if hashtable doesn't has the rule add to hashtable
116
        if (!accessRuleVector.contains(accessRule))
117
        {
118
          accessRuleVector.addElement(accessRule);
119
          i++;
120
          
121
        }
122
        hasRows = rs.next();
123
        // reset value
124
        accessRule = null;
125
        principal  = null;
126
        permision  = 0;
127
        allowType  = null;
128
        allowOrder = null;
129
        
130
      }//while
131
  
132
    }
133
    catch (SQLException e)
134
    {
135
      throw e;
136
    }
137
    finally
138
    {
139
      try
140
      {
141
        pstmt.close();
142
      }
143
      finally
144
      {
145
        //retrun DBConnection
146
        DBConnectionPool.returnDBConnection(conn,serialNumber);
147
      }
148
    }
149
 
150
  }
151

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

    
210
 
211
 
212
 
213

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