Project

General

Profile

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
 *
18
 *   '$Author: daigle $'
19
 *     '$Date: 2008-07-18 09:30:50 -0700 (Fri, 18 Jul 2008) $'
20
 * '$Revision: 4140 $'
21
 *
22
 * This program is free software; you can redistribute it and/or modify
23
 * it under the terms of the GNU General Public License as published by
24
 * the Free Software Foundation; either version 2 of the License, or
25
 * (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35
 */
36

    
37
package edu.ucsb.nceas.metacat;
38

    
39

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

    
50
import org.apache.log4j.Logger;
51

    
52

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

    
64
 
65
 
66
  private DBConnection  conn = null;
67
  private Vector docIdInAccessTable=null;
68
  private Vector docIdWithoutAccessEntry=null;
69
  private Vector notFoundDataSetId=null;
70
  private Vector itsDataSetIdWithouAccessEntry=null;
71
  private Hashtable docIdMapDataSetId=null; 
72
  private Logger logMetacat = Logger.getLogger(AssociateAccessPolicy.class);
73
  
74
  /**
75
   * the main routine used to associate access policy
76
   */
77
  static public void main(String[] args) 
78
  {
79
     DBConnection localConn=null;
80
     int serialNumber = -1;
81
     AssociateAccessPolicy policy=null;
82
     try
83
     {
84
      localConn=DBConnectionPool.getDBConnection("AssociateAccessPolict.main");
85
      serialNumber=localConn.getCheckOutSerialNumber();
86
      policy = new AssociateAccessPolicy(localConn);
87
      policy.associateAccess();
88
      //localConn.close();   
89
     }//try
90
     catch (Exception e) 
91
     {
92
        System.err.println("Error in AssociateAccessPolicy.main");
93
        System.err.println(e.getMessage());
94
        e.printStackTrace(System.err);
95
     }
96
     finally
97
     {
98
       DBConnectionPool.returnDBConnection(localConn, serialNumber);
99
     }//finally
100
     
101
     if (!(policy.getNotFoundDataSetId()).isEmpty())
102
     {
103
         
104
         System.out.println("Following docid which could not find a mapped" 
105
                  +" dataset was associated with defualt policy:");
106
         for (int i=0; i<(policy.getNotFoundDataSetId()).size(); i++)
107
        {
108
           String str=(String)(policy.getNotFoundDataSetId()).elementAt(i);
109
           System.out.println(str);
110
        }//for
111
     }//if
112
     if (!(policy.geItsDataSetIdWithouAccessEntry()).isEmpty())
113
     {
114
         
115
         System.out.println("Following docid which's mapped dataset doesn't has"
116
                  +" an access entry was associated with defualt policy:");
117
         for (int i=0; i<(policy.geItsDataSetIdWithouAccessEntry()).size(); i++)
118
        {
119
           String str=(String)
120
                        (policy.geItsDataSetIdWithouAccessEntry()).elementAt(i);
121
           System.out.println(str);
122
        }//for
123
     }//if
124
  }
125
  
126
  /**
127
   * construct an instance of the DBQuery class 
128
   *
129
   * <p>Generally, one would call the findDocuments() routine after creating 
130
   * an instance to specify the search query</p>
131
   *
132
   * @param conn the JDBC connection that we use for the query
133
   * @param parserName the fully qualified name of a Java class implementing
134
   *                   the org.xml.sax.XMLReader interface
135
   */
136
  public AssociateAccessPolicy( DBConnection conn) 
137
                  throws IOException, 
138
                         SQLException, Exception
139
  {   
140
    this.conn = conn;
141
    this.docIdInAccessTable=new Vector();
142
    this.docIdWithoutAccessEntry=new Vector();
143
    this.notFoundDataSetId=new Vector();
144
    this.itsDataSetIdWithouAccessEntry=new Vector();
145
    this.docIdMapDataSetId=new Hashtable();
146
    getDocIdInAccessTable();
147
    getDocIdWithoutAccessEntry();
148
    getDocIdMapDataSetId();
149
   }
150
  
151
  /**
152
   * Get the docid which didn't found a dataset id to map it
153
   */
154
  public Vector getNotFoundDataSetId()
155
  {
156
    return notFoundDataSetId;
157
  }
158
  /**
159
   * Get the docid which it's mapped dataset doesn't has access entry
160
   */
161
  public Vector geItsDataSetIdWithouAccessEntry()
162
  {
163
    return itsDataSetIdWithouAccessEntry;
164
  }
165
 
166
  /**
167
   * Get all docIds list in xml_access table
168
   */
169
  private void getDocIdInAccessTable()
170
                      throws SQLException, McdbException,Exception
171
  {
172
    PreparedStatement pStmt;
173
    String docId;
174
    ResultSet rs=null;
175
    
176
    //the query stirng
177
    String query="SELECT docid from xml_access";
178
    try
179
    {
180
      pStmt=conn.prepareStatement(query);
181
      //excute the query
182
      pStmt.execute();
183
      //get the result set
184
      rs=pStmt.getResultSet();
185
      //process the result
186
      while (rs.next())
187
      {
188
        
189
        docId=rs.getString(1);//the result docId 
190
        //put the result into docIdInAccessTable vetor
191
        if (!docIdInAccessTable.contains(docId))// delete duplicate docid
192
        {
193
          docIdInAccessTable.add(docId);
194
        }
195
      }//while
196
      //close the pStmt
197
      pStmt.close();
198
    }//try
199
    catch (SQLException e)
200
    {
201
    	logMetacat.error("Error in getDocidListForDataPackage: "
202
                            +e.getMessage());
203
    }//catch
204
    //System.out.println("docid in access table");
205
    /*for (int i=0; i<docIdInAccessTable.size(); i++)
206
    {
207
         String str=(String)docIdInAccessTable.elementAt(i);
208
         System.out.println(str);
209
    }*/
210
    //for
211
  }// getDocIdInAccessTable()
212
  
213
  /**
214
   * associateDefaultValue to docid
215
   * This docid either couldn't find a mapped dataset or it self is a dataset
216
   * @param docId, the docid which will be associate default access value
217
   */
218
  private void associateDefaultValue(String docId)
219
                      throws SQLException, McdbException,Exception
220
  {
221
    PreparedStatement pStmt;
222
    
223
    String query=null;
224
  
225
    //the query stirng
226
    //we let accessfileid blank becuause we couldn't know access file
227
    query="INSERT INTO xml_access " + 
228
            "(docid, principal_name, permission, perm_type, perm_order)" +
229
             "VALUES (?,'public','4','allow','allowFirst')";
230
    
231
    try
232
    {
233
      pStmt=conn.prepareStatement(query);
234
      //bind value
235
      pStmt.setString(1, docId);
236
      //excute the query
237
      logMetacat.debug("running sql: " + pStmt.toString());
238
      pStmt.execute();
239
      pStmt.close();
240
    }//try
241
    catch (SQLException e)
242
    {
243
      System.out.println("Error in associateDefaultValue: "
244
                            +e.getMessage());
245
    }//catch
246
    
247
  }// associateDefaultValue
248
  /**
249
   * Get all docIds which don't have an entry in xml_access table
250
   */
251
  private void getDocIdWithoutAccessEntry()
252
                                 throws SQLException, McdbException,Exception
253
  {
254
    PreparedStatement pStmt=null;
255
    String docId;
256
    ResultSet rs=null;
257

    
258
   
259
    //the query stirng
260
    String query="SELECT docid from xml_documents";
261
    try
262
    {
263
      pStmt=conn.prepareStatement(query);
264
      //excute the query
265
      pStmt.execute();
266
      //get the result set
267
      rs=pStmt.getResultSet();
268
      //process the result
269
      while (rs.next())
270
      {
271
       
272
        docId=rs.getString(1);//the result docId 
273
        //System.out.println("docid in document talbe "+docId);
274
        //If this docId is not in the docIdInAccessTable list,
275
        //put the result into docIdInAccessTable vetor
276
        if (!docIdInAccessTable.contains(docId))
277
        {
278
           docIdWithoutAccessEntry.add(docId);
279
        }
280
     
281
      }//while
282
      //close the pStmt
283
      pStmt.close();
284
    }//try
285
    catch (SQLException e)
286
    {
287
      pStmt.close();
288
      logMetacat.error("Error in getDocidListForDataPackage: "
289
                            +e.getMessage());
290
    }//catch
291
    //System.out.println("docid without access entry:");
292
    /*for (int i=0; i<docIdWithoutAccessEntry.size(); i++)
293
    {
294
         String str=(String)docIdWithoutAccessEntry.elementAt(i);
295
         System.out.println(str);
296
    }*/
297
    //for
298
  }//getDocIdWithoutAccessEntry()  
299
  
300
  /**
301
   * Find dataset docid for these id which doesn't have an entry in access table
302
   * The access policy of dataset docid will apply the id which doesn't have an
303
   * an entry in access table.
304
   * docid and datasetid which will be stored in a hashtable.
305
   * docid as a key, datasetid as value
306
   */
307
  private void getDocIdMapDataSetId()
308
                              throws SQLException, McdbException,Exception
309
  {
310
    
311
    PreparedStatement pStmt=null;
312
    ResultSet rs=null;
313
    String docId=null;
314
    String dataSetId=null;
315
    //make sure there is some documents ids which doesn't has an access entry
316
    if ( docIdWithoutAccessEntry.isEmpty())
317
    {
318
      throw new 
319
          Exception("Every docid in xml_documents table has access policy");
320
    }
321
    String query="SELECT docid from xml_relation where subject= ? or object= ?";
322
    try
323
    {
324
      //find a dataset id for those id which doesn't have access entry
325
      for (int i=0;i<docIdWithoutAccessEntry.size();i++)
326
      {
327
        docId=(String)docIdWithoutAccessEntry.elementAt(i);
328
        pStmt=conn.prepareStatement(query);
329
        //bind the value to query
330
        pStmt.setString(1, docId);
331
        pStmt.setString(2, docId);
332
        //execute the query
333
        pStmt.execute();
334
        rs=pStmt.getResultSet();
335
        //process the result
336
        if (rs.next()) //There are some records for the id in docId fields
337
        {
338
          dataSetId=rs.getString(1);
339
          //System.out.println("dataset id: "+dataSetId);
340
          //put docid and dataset id into hashtable
341
          docIdMapDataSetId.put(docId, dataSetId);
342
        }
343
        else
344
        {
345
         //if not found a dataset id, associateput it into notFoundDataSetId
346
         //and associate with default value
347
          //System.out.println("the id couldn't find data set id: "+docId);
348
          associateDefaultValue(docId);
349
          notFoundDataSetId.add(docId);
350
        }
351
        pStmt.close();
352
      }//for
353
    }//try
354
    catch (SQLException e)
355
    {
356
      pStmt.close();
357
      System.out.println("Error ingetDocIdMapDataSetId: "
358
                            +e.getMessage());
359
    }
360
   
361
  }//getDocIdMapDataSetId()
362
  
363
  /**
364
   * Associate the access policy of dataset to the docid which the data set id
365
   * mapped
366
   */ 
367
   public void associateAccess()
368
                           throws SQLException, McdbException,Exception
369
   {
370
      String docId=null;
371
      String dataSetId=null;
372
      String accessFileId=null;
373
      String principal=null;
374
      int    permission=0;
375
      String permType=null;
376
      String permOrder=null;
377
      String beginTime=null;
378
      String endTime=null;
379
      int    ticketCount=-1;
380
      PreparedStatement pStmt=null;
381
      PreparedStatement insertStatement=null;
382
      ResultSet rs=null;
383
      String query=null;
384
      boolean hasRecord=false;
385
   
386
      //make sure there is some documents ids which doesn't has access entry
387
      if ( docIdWithoutAccessEntry.isEmpty())
388
      {
389
          throw new 
390
            Exception("Every docid in xml_documents table has access policy");
391
      }
392
      //every docid without access policy couldn't find a dataset to map
393
      //assign them default access policy value this aleady done in
394
      //getDocidMapDataSetId
395
      else if (docIdMapDataSetId.isEmpty())
396
      {
397
        
398
      }
399
      else
400
      {
401
        try
402
        {
403
          Enumeration docList = docIdMapDataSetId.keys();
404
          while (docList.hasMoreElements())
405
          {
406
            docId=(String)docList.nextElement();
407
            dataSetId=(String)docIdMapDataSetId.get(docId);
408
            query="select accessfileid, principal_name,permission,perm_type,"
409
                +"perm_order,begin_time,end_time,ticket_count from xml_access "
410
                +"where docid =?";
411
            pStmt=conn.prepareStatement(query);
412
            //bind the value to query
413
            pStmt.setString(1, dataSetId);
414
            //excute the query
415
            pStmt.execute();
416
            rs=pStmt.getResultSet();
417
            //every entry for data set id 
418
            hasRecord=rs.next();
419
            //couldn't find access entry for dataset the docid mapped
420
            //assing defualt value to this docid
421
            if (!hasRecord)
422
            {
423
              
424
              itsDataSetIdWithouAccessEntry.add(docId);
425
              associateDefaultValue(docId);
426
            }
427
            //find the dataset access entry, apply this entry to docid
428
            else
429
            {
430
              
431
              while (hasRecord)
432
              {
433
                //get datasetid's access policy and store them into variables
434
                accessFileId=rs.getString(1);
435
                //System.out.println("accessfileid: "+accessFileId);
436
                principal=rs.getString(2);
437
                //System.out.println("principal: "+principal);
438
                permission=rs.getInt(3);
439
                //System.out.println("permission: "+permission);
440
                permType=rs.getString(4);
441
                //System.out.println("permType: "+permType);
442
                permOrder=rs.getString(5);
443
                //System.out.println("permOrder: "+permOrder);
444
                beginTime=rs.getString(6);
445
                //System.out.println("beginTime: "+beginTime);
446
                endTime=rs.getString(7);
447
                //System.out.println("endTime: "+endTime);
448
                ticketCount=rs.getInt(8);
449
                //System.out.println("ticketCount: "+ticketCount);
450
            
451
                insertStatement = conn.prepareStatement(
452
                  "INSERT INTO xml_access " + 
453
                  "(docid, principal_name, permission, perm_type, perm_order,"+
454
                  "ticket_count, accessfileid) VALUES "+
455
                  "(?,?,?,?,?,?,?)");
456
                // Bind the values to the query
457
                insertStatement.setString(1, docId);
458
                insertStatement.setString(2, principal);
459
                insertStatement.setInt(3, permission);
460
                insertStatement.setString(4, permType);
461
                insertStatement.setString(5, permOrder);
462
                insertStatement.setString(7, accessFileId);
463
                if ( ticketCount > 0 ) 
464
                { 
465
                  insertStatement.setString(6, "" + ticketCount);
466
                } 
467
                else 
468
                {
469
                  insertStatement.setString(6, null);
470
                }
471
                logMetacat.debug("running sql: " + insertStatement.toString());
472
                insertStatement.execute();
473
                hasRecord=rs.next();
474
              }//while
475
              insertStatement.close();
476
            }//else
477
            
478
          }//while
479
          pStmt.close();
480
        }//try
481
        catch (SQLException e)
482
        {
483
          pStmt.close();
484
          insertStatement.close();
485
          logMetacat.error("Error in getDocidListForDataPackadge: "
486
                            +e.getMessage());
487
        }//catch
488
        
489
      }//else
490
      
491
  }//AccociateAccess
492
 
493
}//class
(8-8/69)