Project

General

Profile

« Previous | Next » 

Revision 1016

Added by Jing Tao over 22 years ago

Currently, some document in xml_document table doesn't have entries in
xml_access table. This is okay during the old access policy.
But we changed the access policy( see bug 437 ) and if there is no entry in
xml_access table, except owner, other person can not access it.
So this file will associate access policy to the documents which don't have by
running main method.
The policy is:
If it belong to data set, associate the dataset file acces rule for it.
If it not belong to any dataset, default rule will associate it: owner has full
permission and public have read permission.
It Workes fine it test data base. We need to talk with administrator to backup Oracle
database in KNB. Then run it.

View differences:

src/edu/ucsb/nceas/metacat/AssociateAccessPolicy.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: This class is in order to fix a problem. It doesn't 
4
 *             has functionality for Metacat.
5
 *             In Currently, some document in xml_document table 
6
 *             doesn't have entries in xml_access table. This is 
7
 *             okay during the old access policy.
8
 *             But we changed the access policy and if there is no 
9
 *             entry in xml_access table, except owner, other person 
10
 *             can not access it. So we need to associate with access 
11
 *             policy in xml_access table for these doc ids.
12
 *             The same access policy of these doc' dataset will associate
13
 *             to them. 
14
 *  Copyright: 2000 Regents of the University of California and the
15
 *             National Center for Ecological Analysis and Synthesis
16
 *    Authors: Jing Tao
17
 *    Release: @release@
18
 *
19
 *   '$Author$'
20
 *     '$Date$'
21
 * '$Revision$'
22
 *
23
 * This program is free software; you can redistribute it and/or modify
24
 * it under the terms of the GNU General Public License as published by
25
 * the Free Software Foundation; either version 2 of the License, or
26
 * (at your option) any later version.
27
 *
28
 * This program is distributed in the hope that it will be useful,
29
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31
 * GNU General Public License for more details.
32
 *
33
 * You should have received a copy of the GNU General Public License
34
 * along with this program; if not, write to the Free Software
35
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
36
 */
37

  
38
package edu.ucsb.nceas.metacat;
39

  
40

  
41
import java.io.*;
42
import java.util.Vector;
43
import java.net.URL;
44
import java.net.MalformedURLException;
45
import java.sql.*;
46
import java.util.Stack;
47
import java.util.Hashtable;
48
import java.util.Enumeration;
49
import java.io.BufferedWriter;
50

  
51

  
52
/**This class is in order to fix a problem. It doesn't has functionality for 
53
 *Metacat. 
54
 *In Currently, some document in xml_document table doesn't have entries in 
55
 *xml_access table. This is okay during the old access policy.
56
 *But we changed the access policy and if there is no entry in xml_access table,
57
 * except owner, other person can not access it. So we need to associate with 
58
 *access policy in xml_access table for these doc ids. The same access policy 
59
 *of these docoments' data set will associate to them. 
60
 */
61
public class AssociateAccessPolicy {
62

  
63
 
64
 
65
  private Connection  conn = null;
66
  private Vector docIdInAccessTable=null;
67
  private Vector docIdWithoutAccessEntry=null;
68
  private Vector notFoundDataSetId=null;
69
  private Vector itsDataSetIdWithouAccessEntry=null;
70
  private Hashtable docIdMapDataSetId=null;
71
  private MetaCatUtil util=null;
72
 
73
  /**
74
   * the main routine used to associate access policy
75
   */
76
  static public void main(String[] args) 
77
  {
78
     Connection localConn=null;
79
     MetaCatUtil localUtil=new MetaCatUtil();
80
     AssociateAccessPolicy policy=null;
81
     try
82
     {
83
      localConn=localUtil.openDBConnection();
84
      policy = new AssociateAccessPolicy(localConn);
85
      policy.associateAccess();
86
      localConn.close();   
87
     }//try
88
     catch (Exception e) 
89
     {
90
        System.err.println("Error in AssociateAccessPolicy.main");
91
        System.err.println(e.getMessage());
92
        e.printStackTrace(System.err);
93
     } 
94
     if (!(policy.getNotFoundDataSetId()).isEmpty())
95
     {
96
         
97
         System.out.println("Following docid which could not find a mapped" 
98
                  +" dataset was associated with defualt policy:");
99
         for (int i=0; i<(policy.getNotFoundDataSetId()).size(); i++)
100
        {
101
           String str=(String)(policy.getNotFoundDataSetId()).elementAt(i);
102
           System.out.println(str);
103
        }//for
104
     }//if
105
     if (!(policy.geItsDataSetIdWithouAccessEntry()).isEmpty())
106
     {
107
         
108
         System.out.println("Following docid which's mapped dataset doesn't has"
109
                  +" an access entry was associated with defualt policy:");
110
         for (int i=0; i<(policy.geItsDataSetIdWithouAccessEntry()).size(); i++)
111
        {
112
           String str=(String)
113
                        (policy.geItsDataSetIdWithouAccessEntry()).elementAt(i);
114
           System.out.println(str);
115
        }//for
116
     }//if
117
  }
118
  
119
  /**
120
   * construct an instance of the DBQuery class 
121
   *
122
   * <p>Generally, one would call the findDocuments() routine after creating 
123
   * an instance to specify the search query</p>
124
   *
125
   * @param conn the JDBC connection that we use for the query
126
   * @param parserName the fully qualified name of a Java class implementing
127
   *                   the org.xml.sax.XMLReader interface
128
   */
129
  public AssociateAccessPolicy( Connection conn) 
130
                  throws IOException, 
131
                         SQLException, Exception
132
  {   
133
    this.conn = conn;
134
    this.docIdInAccessTable=new Vector();
135
    this.docIdWithoutAccessEntry=new Vector();
136
    this.notFoundDataSetId=new Vector();
137
    this.itsDataSetIdWithouAccessEntry=new Vector();
138
    this.docIdMapDataSetId=new Hashtable();
139
    this.util=new MetaCatUtil();
140
    getDocIdInAccessTable();
141
    getDocIdWithoutAccessEntry();
142
    getDocIdMapDataSetId();
143
   }
144
  
145
  /**
146
   * Get the docid which didn't found a dataset id to map it
147
   */
148
  public Vector getNotFoundDataSetId()
149
  {
150
    return notFoundDataSetId;
151
  }
152
  /**
153
   * Get the docid which it's mapped dataset doesn't has access entry
154
   */
155
  public Vector geItsDataSetIdWithouAccessEntry()
156
  {
157
    return itsDataSetIdWithouAccessEntry;
158
  }
159
 
160
  /**
161
   * Get all docIds list in xml_access table
162
   */
163
  private void getDocIdInAccessTable()
164
                      throws SQLException, McdbException,Exception
165
  {
166
    PreparedStatement pStmt;
167
    String docId;
168
    ResultSet rs=null;
169
    Connection dbConn=null;
170
    if (conn == null || conn.isClosed())
171
    {
172
      dbConn = util.openDBConnection();
173
    }
174
    else
175
    {
176
      dbConn=conn;
177
    }
178
    //the query stirng
179
    String query="SELECT docid from xml_access";
180
    try
181
    {
182
      pStmt=conn.prepareStatement(query);
183
      //excute the query
184
      pStmt.execute();
185
      //get the result set
186
      rs=pStmt.getResultSet();
187
      //process the result
188
      while (rs.next())
189
      {
190
        
191
        docId=rs.getString(1);//the result docId 
192
        //put the result into docIdInAccessTable vetor
193
        if (!docIdInAccessTable.contains(docId))// delete duplicate docid
194
        {
195
          docIdInAccessTable.add(docId);
196
        }
197
      }//while
198
      //close the pStmt
199
      pStmt.close();
200
    }//try
201
    catch (SQLException e)
202
    {
203
      util.debugMessage("Error in getDocidListForDataPackage: "
204
                            +e.getMessage());
205
    }//catch
206
    //System.out.println("docid in access table");
207
    /*for (int i=0; i<docIdInAccessTable.size(); i++)
208
    {
209
         String str=(String)docIdInAccessTable.elementAt(i);
210
         System.out.println(str);
211
    }*/
212
    //for
213
  }// getDocIdInAccessTable()
214
  
215
  /**
216
   * associateDefaultValue to docid
217
   * This docid either couldn't find a mapped dataset or it self is a dataset
218
   * @param docId, the docid which will be associate default access value
219
   */
220
  private void associateDefaultValue(String docId)
221
                      throws SQLException, McdbException,Exception
222
  {
223
    PreparedStatement pStmt;
224
    Connection dbConn=null;
225
    String query=null;
226
    if (conn == null || conn.isClosed())
227
    {
228
      dbConn = util.openDBConnection();
229
    }
230
    else
231
    {
232
      dbConn=conn;
233
    }
234
    //the query stirng
235
    //we let accessfileid blank becuause we couldn't know access file
236
    query="INSERT INTO xml_access " + 
237
            "(docid, principal_name, permission, perm_type, perm_order)" +
238
             "VALUES (?,'public','4','allow','allowFirst')";
239
    
240
    try
241
    {
242
      pStmt=conn.prepareStatement(query);
243
      //bind value
244
      pStmt.setString(1, docId);
245
      //excute the query
246
      pStmt.execute();
247
      pStmt.close();
248
    }//try
249
    catch (SQLException e)
250
    {
251
      System.out.println("Error in associateDefaultValue: "
252
                            +e.getMessage());
253
    }//catch
254
    
255
  }// associateDefaultValue
256
  /**
257
   * Get all docIds which don't have an entry in xml_access table
258
   */
259
  private void getDocIdWithoutAccessEntry()
260
                                 throws SQLException, McdbException,Exception
261
  {
262
    PreparedStatement pStmt=null;
263
    String docId;
264
    ResultSet rs=null;
265
    Connection dbConn=null;
266
    if (conn == null || conn.isClosed())
267
    {
268
      dbConn = util.openDBConnection();
269
    }
270
    else
271
    {
272
      dbConn=conn;
273
    }
274
    //the query stirng
275
    String query="SELECT docid from xml_documents";
276
    try
277
    {
278
      pStmt=conn.prepareStatement(query);
279
      //excute the query
280
      pStmt.execute();
281
      //get the result set
282
      rs=pStmt.getResultSet();
283
      //process the result
284
      while (rs.next())
285
      {
286
       
287
        docId=rs.getString(1);//the result docId 
288
        //System.out.println("docid in document talbe "+docId);
289
        //If this docId is not in the docIdInAccessTable list,
290
        //put the result into docIdInAccessTable vetor
291
        if (!docIdInAccessTable.contains(docId))
292
        {
293
           docIdWithoutAccessEntry.add(docId);
294
        }
295
     
296
      }//while
297
      //close the pStmt
298
      pStmt.close();
299
    }//try
300
    catch (SQLException e)
301
    {
302
      pStmt.close();
303
      util.debugMessage("Error in getDocidListForDataPackage: "
304
                            +e.getMessage());
305
    }//catch
306
    //System.out.println("docid without access entry:");
307
    /*for (int i=0; i<docIdWithoutAccessEntry.size(); i++)
308
    {
309
         String str=(String)docIdWithoutAccessEntry.elementAt(i);
310
         System.out.println(str);
311
    }*/
312
    //for
313
  }//getDocIdWithoutAccessEntry()  
314
  
315
  /**
316
   * Find dataset docid for these id which doesn't have an entry in access table
317
   * The access policy of dataset docid will apply the id which doesn't have an
318
   * an entry in access table.
319
   * docid and datasetid which will be stored in a hashtable.
320
   * docid as a key, datasetid as value
321
   */
322
  private void getDocIdMapDataSetId()
323
                              throws SQLException, McdbException,Exception
324
  {
325
    
326
    PreparedStatement pStmt=null;
327
    ResultSet rs=null;
328
    String docId=null;
329
    String dataSetId=null;
330
    //make sure there is some documents ids which doesn't has an access entry
331
    if ( docIdWithoutAccessEntry.isEmpty())
332
    {
333
      throw new 
334
          Exception("Every docid in xml_documents table has access policy");
335
    }
336
    String query="SELECT docid from xml_relation where subject= ? or object= ?";
337
    try
338
    {
339
      //find a dataset id for those id which doesn't have access entry
340
      for (int i=0;i<docIdWithoutAccessEntry.size();i++)
341
      {
342
        docId=(String)docIdWithoutAccessEntry.elementAt(i);
343
        pStmt=conn.prepareStatement(query);
344
        //bind the value to query
345
        pStmt.setString(1, docId);
346
        pStmt.setString(2, docId);
347
        //execute the query
348
        pStmt.execute();
349
        rs=pStmt.getResultSet();
350
        //process the result
351
        if (rs.next()) //There are some records for the id in docId fields
352
        {
353
          dataSetId=rs.getString(1);
354
          //System.out.println("dataset id: "+dataSetId);
355
          //put docid and dataset id into hashtable
356
          docIdMapDataSetId.put(docId, dataSetId);
357
        }
358
        else
359
        {
360
         //if not found a dataset id, associateput it into notFoundDataSetId
361
         //and associate with default value
362
          //System.out.println("the id couldn't find data set id: "+docId);
363
          associateDefaultValue(docId);
364
          notFoundDataSetId.add(docId);
365
        }
366
        pStmt.close();
367
      }//for
368
    }//try
369
    catch (SQLException e)
370
    {
371
      pStmt.close();
372
      System.out.println("Error ingetDocIdMapDataSetId: "
373
                            +e.getMessage());
374
    }
375
   
376
  }//getDocIdMapDataSetId()
377
  
378
  /**
379
   * Associate the access policy of dataset to the docid which the data set id
380
   * mapped
381
   */ 
382
   public void associateAccess()
383
                           throws SQLException, McdbException,Exception
384
   {
385
      String docId=null;
386
      String dataSetId=null;
387
      String accessFileId=null;
388
      String principal=null;
389
      int    permission=0;
390
      String permType=null;
391
      String permOrder=null;
392
      String beginTime=null;
393
      String endTime=null;
394
      int    ticketCount=-1;
395
      PreparedStatement pStmt=null;
396
      PreparedStatement insertStatement=null;
397
      ResultSet rs=null;
398
      String query=null;
399
      boolean hasRecord=false;
400
   
401
      //make sure there is some documents ids which doesn't has access entry
402
      if ( docIdWithoutAccessEntry.isEmpty())
403
      {
404
          throw new 
405
            Exception("Every docid in xml_documents table has access policy");
406
      }
407
      //every docid without access policy couldn't find a dataset to map
408
      //assign them default access policy value this aleady done in
409
      //getDocidMapDataSetId
410
      else if (docIdMapDataSetId.isEmpty())
411
      {
412
        
413
      }
414
      else
415
      {
416
        try
417
        {
418
          Enumeration docList = docIdMapDataSetId.keys();
419
          while (docList.hasMoreElements())
420
          {
421
            docId=(String)docList.nextElement();
422
            dataSetId=(String)docIdMapDataSetId.get(docId);
423
            query="select accessfileid, principal_name,permission,perm_type,"
424
                +"perm_order,begin_time,end_time,ticket_count from xml_access "
425
                +"where docid =?";
426
            pStmt=conn.prepareStatement(query);
427
            //bind the value to query
428
            pStmt.setString(1, dataSetId);
429
            //excute the query
430
            pStmt.execute();
431
            rs=pStmt.getResultSet();
432
            //every entry for data set id 
433
            hasRecord=rs.next();
434
            //couldn't find access entry for dataset the docid mapped
435
            //assing defualt value to this docid
436
            if (!hasRecord)
437
            {
438
              
439
              itsDataSetIdWithouAccessEntry.add(docId);
440
              associateDefaultValue(docId);
441
            }
442
            //find the dataset access entry, apply this entry to docid
443
            else
444
            {
445
              
446
              while (hasRecord)
447
              {
448
                //get datasetid's access policy and store them into variables
449
                accessFileId=rs.getString(1);
450
                //System.out.println("accessfileid: "+accessFileId);
451
                principal=rs.getString(2);
452
                //System.out.println("principal: "+principal);
453
                permission=rs.getInt(3);
454
                //System.out.println("permission: "+permission);
455
                permType=rs.getString(4);
456
                //System.out.println("permType: "+permType);
457
                permOrder=rs.getString(5);
458
                //System.out.println("permOrder: "+permOrder);
459
                beginTime=rs.getString(6);
460
                //System.out.println("beginTime: "+beginTime);
461
                endTime=rs.getString(7);
462
                //System.out.println("endTime: "+endTime);
463
                ticketCount=rs.getInt(8);
464
                //System.out.println("ticketCount: "+ticketCount);
465
            
466
                insertStatement = conn.prepareStatement(
467
                  "INSERT INTO xml_access " + 
468
                  "(docid, principal_name, permission, perm_type, perm_order,"+
469
                  "begin_time,end_time,ticket_count, accessfileid) VALUES "+
470
               //"(?,?,?,?,?,to_date(?,'mm/dd/yy'),to_date(?,'mm/dd/yy'),?,?)");
471
                  "(?,?,?,?,?,?,?,?,?)");
472
                // Bind the values to the query
473
                insertStatement.setString(1, docId);
474
                insertStatement.setString(2, principal);
475
                insertStatement.setInt(3, permission);
476
                insertStatement.setString(4, permType);
477
                insertStatement.setString(5, permOrder);
478
                insertStatement.setString(6, beginTime);
479
                insertStatement.setString(7, endTime);
480
                insertStatement.setString(9, accessFileId);
481
                if ( ticketCount > 0 ) 
482
                { 
483
                  insertStatement.setString(8, "" + ticketCount);
484
                } 
485
                else 
486
                {
487
                  insertStatement.setString(8, null);
488
                }
489
                insertStatement.execute();
490
                hasRecord=rs.next();
491
              }//while
492
              insertStatement.close();
493
            }//else
494
            
495
          }//while
496
          pStmt.close();
497
        }//try
498
        catch (SQLException e)
499
        {
500
          pStmt.close();
501
          insertStatement.close();
502
          util.debugMessage("Error in getDocidListForDataPackadge: "
503
                            +e.getMessage());
504
        }//catch
505
        
506
      }//else
507
      
508
  }//AccociateAccess
509
 
510
}//class
0 511

  

Also available in: Unified diff