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

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