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-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
20
 * '$Revision: 4080 $'
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
      pStmt.execute();
238
      pStmt.close();
239
    }//try
240
    catch (SQLException e)
241
    {
242
      System.out.println("Error in associateDefaultValue: "
243
                            +e.getMessage());
244
    }//catch
245
    
246
  }// associateDefaultValue
247
  /**
248
   * Get all docIds which don't have an entry in xml_access table
249
   */
250
  private void getDocIdWithoutAccessEntry()
251
                                 throws SQLException, McdbException,Exception
252
  {
253
    PreparedStatement pStmt=null;
254
    String docId;
255
    ResultSet rs=null;
256

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