Project

General

Profile

Revision 1325

Added by Jing Tao over 18 years ago

Add two new methods to update triples in data set file and delete some access rules which doesn't exist in xml_access document. Change the main method, it will read access file form xml_access table rather than getting from argument.

View differences:

src/edu/ucsb/nceas/metacat/CleanupAccessTable.java
69 69
  private static final String IDENTIFIER = "//identifier";
70 70
  private static final String RELATIONSHIP = "provides access control rules for";
71 71
  public static final String DELIMITER = "!";
72
  private static final String DOT = ".";
72 73
  
73 74
  public static void addNewTripleIntoDataset(String accessionNumber, 
74 75
                                             Vector newTripleVector)
......
96 97
      return;
97 98
    }
98 99
    String xmlDoc = doc.toString();
99
    System.out.println("orginal xml file: "+xmlDoc);
100
    //System.out.println("orginal xml file: "+xmlDoc);
100 101
    // create new triple collection
101 102
    TripleCollection newTripleForDataSet = new TripleCollection();
102 103
    for (int i = 0; i<newTripleVector.size(); i++)
103 104
    {
104 105
      //Add new tripe
105 106
      Triple triple = (Triple)newTripleVector.elementAt(i);
107
      System.out.println("New triple: "+triple.toString());
106 108
      newTripleForDataSet.addTriple(triple);
107 109
    }
108 110
    xmlDoc = addTriplesToTriplesString(newTripleForDataSet, xmlDoc, TRIPLESTAG);
109
    System.out.println("add triple xml file: "+xmlDoc);
111
   
110 112
    // Update accessNumber
111 113
    Vector oldAccessNumberVector = new Vector();
112 114
    Vector newAccessNumberVector = new Vector();
......
118 120
    xmlDoc = incRevInTriples(xmlDoc, oldAccessNumberVector, 
119 121
                             newAccessNumberVector, TRIPLESTAG);
120 122
    
121
    System.out.println("increate rev xml file: "+xmlDoc);
123
  
124
    // update database
125
    StringReader xml = new StringReader(xmlDoc);
126
    String pub = "no";
127
    StringReader dtd = null;
128
    String doAction = "UPDATE";
129
    boolean validate = true;
130
    DBConnection dbConn = null;
131
    int serialNumber = -1;
132
    try
133
    {
134
      dbConn=DBConnectionPool.
135
                  getDBConnection("DBQuery.getDataPackageId");
136
      serialNumber=dbConn.getCheckOutSerialNumber();
137
      DocumentImpl.write(dbConn, xml, pub, dtd, doAction,
138
                       accessionNumber, null, null, validate);
139
      System.out.println("update data set file: "+ accessionNumber);
140
    }
141
    catch (Exception e)
142
    {
143
      System.out.println("error in addNewTripleIntoDataset: "+e.getMessage());
144
    }
145
    finally
146
    {
147
      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
148
    }
122 149
    
123 150
  }
124 151
  
......
367 394
      pStmt.close();
368 395
      rev = getRevNumber(packageIdWithoutVersion);
369 396
      accessNumber = packageIdWithoutVersion+"."+rev;
370
      MetaCatUtil.debugMessage("DataPackageId: "+accessNumber, 20);
397
      MetaCatUtil.debugMessage("DataPackageId: "+accessNumber, 50);
371 398
    }//try
372 399
    catch (SQLException e)
373 400
    {
......
457 484
     DBConnection dbConn = null;
458 485
    int serialNumber = -1;
459 486
    String query = "delete from xml_access where accessfileid = ? " +
460
                   "and principal_name = ? and permission_type = ? "+
461
                   "and permission = ? and permission_order = ?";
487
                   "and principal_name = ? and perm_type = ? "+
488
                   "and permission = ? and perm_order = ?";
462 489
    PreparedStatement pStmt = null;
463 490
    String rev = null;
464 491
    // parse the access rule 
......
471 498
    else
472 499
    {
473 500
      principal  = parseAccessRuleArray[0];
474
      System.out.println("principal: "+principal);
475 501
      allowType  = parseAccessRuleArray[1];
476
      System.out.println("allowType: "+allowType);
477 502
      permission = parseAccessRuleArray[2];
478
      System.out.println("permission: "+permission);
479 503
      allowOrder = parseAccessRuleArray[3];
480
      System.out.println("allowOrder: "+allowOrder);
504
      
481 505
    }
482 506
    
483
    /*try
507
    try
484 508
    {
485 509
      dbConn=DBConnectionPool.
486 510
                  getDBConnection("DBQuery.getDataPackageId");
487 511
      serialNumber=dbConn.getCheckOutSerialNumber();
488
      //pStmt=dbConn.prepareStatement(query);
512
      pStmt=dbConn.prepareStatement(query);
489 513
      //bind the value to query
490
      //pStmt.setString(1, docId);
514
      pStmt.setString(1, aclDocid);
515
      pStmt.setString(2, principal);
516
      pStmt.setString(3, allowType);
517
      pStmt.setInt(4, (new Integer(permission)).intValue());
518
      pStmt.setString(5, allowOrder);
491 519
      //execute the query
492
      //pStmt.execute();
493
      //pStmt.close();
520
      pStmt.execute();
521
      pStmt.close();
522
      System.out.println("delete rules: "+accessRule+" for "
523
                          +aclDocid+" in table");
494 524
    }
495 525
    catch (SQLException e)
496 526
    {
497
      System.out.println("the error in getRevNumber: "+e.getMessage());
527
      System.out.println("the error in deleteAccessRule(): "+e.getMessage());
498 528
    }
499 529
    finally
500 530
    {
501 531
      try
502 532
      {
503
        //pStmt.close();
533
        pStmt.close();
504 534
      }
505 535
      catch (SQLException ee)
506 536
      {
......
511 541
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
512 542
       
513 543
      }
514
    }*/
544
    }
515 545
  }
516 546
  
517 547
  /*
......
584 614
    return result;
585 615
  }
586 616
  
587
  public static void main(String[] agus)
617
  /**
618
   * A method get acl docid list from xml_access table
619
   */
620
  public static Vector readAccessDocIdFromDB()
588 621
  {
589
    if(agus.length == 0)
590
    {
591
      System.out.println("you should specify a docid!");
592
      return;
593
    }
594
    String docID = agus[0];
595
    Vector newTripleVector = new Vector();
596
    
622
    Vector docidList = new Vector();
623
    String docid = null;
624
    DBConnection conn = null;
625
    int serialNumber = -1;
626
    PreparedStatement pstmt = null;
597 627
    try
598 628
    {
599
      DBConnectionPool pool = DBConnectionPool.getInstance();
600
      String packagAccessionNumber=getDataPackageAccessionNumber(docID);
601
      AccessRulesFromDocument xmlDocument = new AccessRulesFromDocument(docID);
602
      Vector rules = xmlDocument.getAccessRuleVector();
603
      Vector docid = xmlDocument.getACLObjects();
604
      AccessRulesFromDB db = new AccessRulesFromDB(docID);
605
      Vector rulesFromDB = db.getAccessRuleVector();
606
      Vector docidFromDB = db.getDocidAccessRuleApplied();
607
      for (int i = 0; i<rulesFromDB.size(); i++)
629
      //get connection from DBConnectionPool
630
      conn = DBConnectionPool.
631
              getDBConnection("CleanupAccessTable.readAccessDocIdFromDB");
632
      serialNumber=conn.getCheckOutSerialNumber();
633
      
634
      // delete all acl records for resources related to @aclid if any
635
      pstmt = conn.prepareStatement("SELECT accessfileid FROM xml_access");
636
      pstmt.execute();
637
      ResultSet rs = pstmt.getResultSet();
638
      boolean hasRows = rs.next();
639
      int i = 0;
640
      while (hasRows) 
608 641
      {
609
        System.out.println("rule: "+(String)rulesFromDB.elementAt(i));
610
        if (!rules.contains(rulesFromDB.elementAt(i)))
642
        docid  = rs.getString(1);
643
        // if hashtable doesn't has the rule add to hashtable
644
        if (docid !=null && !docidList.contains(docid))
611 645
        {
612
          System.out.println("find a new rule!!!");
613
          deleteAccessRule(docID, (String)rulesFromDB.elementAt(i));
646
          docidList.addElement(docid);
647
          
614 648
        }
649
        hasRows = rs.next();
650
        // reset value
651
        docid = null;
652
        
653
      }//while
654
  
655
    }
656
    catch (SQLException e)
657
    {
658
      System.out.println("error in readAccessDocIdFromDB: "+e.getMessage());
659
    }
660
    finally
661
    {
662
      try
663
      {
664
        pstmt.close();
615 665
      }
616
      for (int i = 0; i<docidFromDB.size(); i++)
666
      catch (SQLException ee)
617 667
      {
618
        String objectId = (String)docidFromDB.elementAt(i);
619
        System.out.println("docid: "+objectId);
620
        if (!docid.contains(objectId))
668
        System.out.println("error in readAccessDocIdFromDB: "+ee.getMessage());
669
      }
670
      finally
671
      {
672
        //retrun DBConnection
673
        DBConnectionPool.returnDBConnection(conn,serialNumber);
674
        return docidList;
675
      }
676
    }
677
 
678
  }
679
  
680
  /**
681
   * Method to delete rules which is not in xml access file
682
   */
683
  public static void deleteRules(Vector aclDocList) 
684
  {
685
    String aclDocId = null;
686
    //checking for every acl id
687
    for (int i= 0; i<aclDocList.size(); i++)
688
    {
689
      try
690
      {
691
        aclDocId = (String)aclDocList.elementAt(i);
692
        AccessRulesFromDocument xmlDocument = new 
693
                                            AccessRulesFromDocument(aclDocId);
694
        Vector rules = xmlDocument.getAccessRuleVector();
695
        AccessRulesFromDB db = new AccessRulesFromDB(aclDocId);
696
        Vector rulesFromDB = db.getAccessRuleVector();
697
        //check for every rules
698
        for (int j=0; j< rulesFromDB.size(); j++)
621 699
        {
622
          String revs = getRevNumber(objectId);
623
          objectId = objectId+"."+revs;
624
          revs = getRevNumber(docID);
625
          docID = docID+"."+revs;
626
          Triple newTriple = new Triple(docID, RELATIONSHIP, objectId);
627
          newTripleVector.add(newTriple);
628
          System.out.println("find a new triple");
700
          String oneRule = (String)rulesFromDB.elementAt(j);
701
          // if onerules get from db not in xml_access, delete it
702
          if (!rules.contains(oneRule))
703
          {
704
            System.out.println("one rule: "+oneRule);
705
            deleteAccessRule(aclDocId, oneRule);
706
          }//if
707
          oneRule = null;
708
        }//for
709
        aclDocId = null;
710
      }
711
      catch (Exception e)
712
      {
713
        System.out.println("error delete rules: "+e.getMessage());
714
      } 
715
    }//for
716
  }
717
  
718
  /**
719
   * Method to updata data set files
720
   */
721
  public static void updateDataSetFiles(Vector aclDocList) 
722
  {
723
    String aclDocId = null;
724
    String aclAccessionNumber = null;
725
    String revForAcl = null;
726
    String packagAccessionNumber = null;
727
    //checking for every acl id
728
    for (int i= 0; i<aclDocList.size(); i++)
729
    {
730
      try
731
      {
732
        aclDocId = (String)aclDocList.elementAt(i);
733
        packagAccessionNumber = getDataPackageAccessionNumber(aclDocId);
734
        AccessRulesFromDocument xmlDocument = new 
735
                                            AccessRulesFromDocument(aclDocId);
736
        Vector docid = xmlDocument.getACLObjects();
737
        AccessRulesFromDB db = new AccessRulesFromDB(aclDocId);
738
        Vector docidFromDB = db.getDocidAccessRuleApplied();
739
        revForAcl = getRevNumber(aclDocId);
740
        aclAccessionNumber = aclDocId+DOT+revForAcl;
741
        //check for every docid in relation ship
742
        Vector newTripleVector = new Vector();
743
        for (int j=0; j<docidFromDB.size(); j++)
744
        {
745
          String objectId = (String)docidFromDB.elementAt(j);
746
          String objectAccessionNumber = null;
747
          String revForObject = null;
748
          // new triple
749
          if (!docid.contains(objectId))
750
          {
751
            revForObject = getRevNumber(objectId);
752
            objectAccessionNumber = objectId+DOT+revForObject;
753
            Triple newTriple = new Triple(aclAccessionNumber, RELATIONSHIP, 
754
                                        objectAccessionNumber);
755
            newTripleVector.add(newTriple);
756
            System.out.println("find a new triple");
629 757
          
630
        }
758
          }//if
759
        }//for
760
        // add new trip into data set
761
        addNewTripleIntoDataset(packagAccessionNumber, newTripleVector);
762
        // reset value
763
        aclDocId = null;
764
        aclAccessionNumber = null;
765
        revForAcl = null;
766
        packagAccessionNumber = null;
631 767
      }
632
      // add new triple vector to xml
633
      addNewTripleIntoDataset(packagAccessionNumber, newTripleVector);
768
      catch (Exception e)
769
      {
770
        System.out.println("error in updataDatasetFiles(): "+e.getMessage());
771
      }
772
      
773
    }//for
774
  }
775
  
776
  public static void main(String[] agus)
777
  {
778
    
779
    try
780
    {
781
      DBConnectionPool pool = DBConnectionPool.getInstance();
782
      Vector aclVector = readAccessDocIdFromDB();
783
      deleteRules(aclVector);
784
      Vector aclVector2 = readAccessDocIdFromDB();
785
      updateDataSetFiles(aclVector2);
634 786
    }
635 787
    catch(Exception e)
636 788
    {

Also available in: Unified diff