Index: src/edu/ucsb/nceas/metacat/CleanupAccessTable.java
===================================================================
--- src/edu/ucsb/nceas/metacat/CleanupAccessTable.java	(revision 1324)
+++ src/edu/ucsb/nceas/metacat/CleanupAccessTable.java	(revision 1325)
@@ -69,6 +69,7 @@
   private static final String IDENTIFIER = "//identifier";
   private static final String RELATIONSHIP = "provides access control rules for";
   public static final String DELIMITER = "!";
+  private static final String DOT = ".";
   
   public static void addNewTripleIntoDataset(String accessionNumber, 
                                              Vector newTripleVector)
@@ -96,7 +97,7 @@
       return;
     }
     String xmlDoc = doc.toString();
-    System.out.println("orginal xml file: "+xmlDoc);
+    //System.out.println("orginal xml file: "+xmlDoc);
     // create new triple collection
     TripleCollection newTripleForDataSet = new TripleCollection();
     for (int i = 0; i<newTripleVector.size(); i++)
@@ -103,10 +104,11 @@
     {
       //Add new tripe
       Triple triple = (Triple)newTripleVector.elementAt(i);
+      System.out.println("New triple: "+triple.toString());
       newTripleForDataSet.addTriple(triple);
     }
     xmlDoc = addTriplesToTriplesString(newTripleForDataSet, xmlDoc, TRIPLESTAG);
-    System.out.println("add triple xml file: "+xmlDoc);
+   
     // Update accessNumber
     Vector oldAccessNumberVector = new Vector();
     Vector newAccessNumberVector = new Vector();
@@ -118,7 +120,32 @@
     xmlDoc = incRevInTriples(xmlDoc, oldAccessNumberVector, 
                              newAccessNumberVector, TRIPLESTAG);
     
-    System.out.println("increate rev xml file: "+xmlDoc);
+  
+    // update database
+    StringReader xml = new StringReader(xmlDoc);
+    String pub = "no";
+    StringReader dtd = null;
+    String doAction = "UPDATE";
+    boolean validate = true;
+    DBConnection dbConn = null;
+    int serialNumber = -1;
+    try
+    {
+      dbConn=DBConnectionPool.
+                  getDBConnection("DBQuery.getDataPackageId");
+      serialNumber=dbConn.getCheckOutSerialNumber();
+      DocumentImpl.write(dbConn, xml, pub, dtd, doAction,
+                       accessionNumber, null, null, validate);
+      System.out.println("update data set file: "+ accessionNumber);
+    }
+    catch (Exception e)
+    {
+      System.out.println("error in addNewTripleIntoDataset: "+e.getMessage());
+    }
+    finally
+    {
+      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
+    }
     
   }
   
@@ -367,7 +394,7 @@
       pStmt.close();
       rev = getRevNumber(packageIdWithoutVersion);
       accessNumber = packageIdWithoutVersion+"."+rev;
-      MetaCatUtil.debugMessage("DataPackageId: "+accessNumber, 20);
+      MetaCatUtil.debugMessage("DataPackageId: "+accessNumber, 50);
     }//try
     catch (SQLException e)
     {
@@ -457,8 +484,8 @@
      DBConnection dbConn = null;
     int serialNumber = -1;
     String query = "delete from xml_access where accessfileid = ? " +
-                   "and principal_name = ? and permission_type = ? "+
-                   "and permission = ? and permission_order = ?";
+                   "and principal_name = ? and perm_type = ? "+
+                   "and permission = ? and perm_order = ?";
     PreparedStatement pStmt = null;
     String rev = null;
     // parse the access rule 
@@ -471,36 +498,39 @@
     else
     {
       principal  = parseAccessRuleArray[0];
-      System.out.println("principal: "+principal);
       allowType  = parseAccessRuleArray[1];
-      System.out.println("allowType: "+allowType);
       permission = parseAccessRuleArray[2];
-      System.out.println("permission: "+permission);
       allowOrder = parseAccessRuleArray[3];
-      System.out.println("allowOrder: "+allowOrder);
+      
     }
     
-    /*try
+    try
     {
       dbConn=DBConnectionPool.
                   getDBConnection("DBQuery.getDataPackageId");
       serialNumber=dbConn.getCheckOutSerialNumber();
-      //pStmt=dbConn.prepareStatement(query);
+      pStmt=dbConn.prepareStatement(query);
       //bind the value to query
-      //pStmt.setString(1, docId);
+      pStmt.setString(1, aclDocid);
+      pStmt.setString(2, principal);
+      pStmt.setString(3, allowType);
+      pStmt.setInt(4, (new Integer(permission)).intValue());
+      pStmt.setString(5, allowOrder);
       //execute the query
-      //pStmt.execute();
-      //pStmt.close();
+      pStmt.execute();
+      pStmt.close();
+      System.out.println("delete rules: "+accessRule+" for "
+                          +aclDocid+" in table");
     }
     catch (SQLException e)
     {
-      System.out.println("the error in getRevNumber: "+e.getMessage());
+      System.out.println("the error in deleteAccessRule(): "+e.getMessage());
     }
     finally
     {
       try
       {
-        //pStmt.close();
+        pStmt.close();
       }
       catch (SQLException ee)
       {
@@ -511,7 +541,7 @@
         DBConnectionPool.returnDBConnection(dbConn, serialNumber);
        
       }
-    }*/
+    }
   }
   
   /*
@@ -584,53 +614,175 @@
     return result;
   }
   
-  public static void main(String[] agus)
+  /**
+   * A method get acl docid list from xml_access table
+   */
+  public static Vector readAccessDocIdFromDB()
   {
-    if(agus.length == 0)
-    {
-      System.out.println("you should specify a docid!");
-      return;
-    }
-    String docID = agus[0];
-    Vector newTripleVector = new Vector();
-    
+    Vector docidList = new Vector();
+    String docid = null;
+    DBConnection conn = null;
+    int serialNumber = -1;
+    PreparedStatement pstmt = null;
     try
     {
-      DBConnectionPool pool = DBConnectionPool.getInstance();
-      String packagAccessionNumber=getDataPackageAccessionNumber(docID);
-      AccessRulesFromDocument xmlDocument = new AccessRulesFromDocument(docID);
-      Vector rules = xmlDocument.getAccessRuleVector();
-      Vector docid = xmlDocument.getACLObjects();
-      AccessRulesFromDB db = new AccessRulesFromDB(docID);
-      Vector rulesFromDB = db.getAccessRuleVector();
-      Vector docidFromDB = db.getDocidAccessRuleApplied();
-      for (int i = 0; i<rulesFromDB.size(); i++)
+      //get connection from DBConnectionPool
+      conn = DBConnectionPool.
+              getDBConnection("CleanupAccessTable.readAccessDocIdFromDB");
+      serialNumber=conn.getCheckOutSerialNumber();
+      
+      // delete all acl records for resources related to @aclid if any
+      pstmt = conn.prepareStatement("SELECT accessfileid FROM xml_access");
+      pstmt.execute();
+      ResultSet rs = pstmt.getResultSet();
+      boolean hasRows = rs.next();
+      int i = 0;
+      while (hasRows) 
       {
-        System.out.println("rule: "+(String)rulesFromDB.elementAt(i));
-        if (!rules.contains(rulesFromDB.elementAt(i)))
+        docid  = rs.getString(1);
+        // if hashtable doesn't has the rule add to hashtable
+        if (docid !=null && !docidList.contains(docid))
         {
-          System.out.println("find a new rule!!!");
-          deleteAccessRule(docID, (String)rulesFromDB.elementAt(i));
+          docidList.addElement(docid);
+          
         }
+        hasRows = rs.next();
+        // reset value
+        docid = null;
+        
+      }//while
+  
+    }
+    catch (SQLException e)
+    {
+      System.out.println("error in readAccessDocIdFromDB: "+e.getMessage());
+    }
+    finally
+    {
+      try
+      {
+        pstmt.close();
       }
-      for (int i = 0; i<docidFromDB.size(); i++)
+      catch (SQLException ee)
       {
-        String objectId = (String)docidFromDB.elementAt(i);
-        System.out.println("docid: "+objectId);
-        if (!docid.contains(objectId))
+        System.out.println("error in readAccessDocIdFromDB: "+ee.getMessage());
+      }
+      finally
+      {
+        //retrun DBConnection
+        DBConnectionPool.returnDBConnection(conn,serialNumber);
+        return docidList;
+      }
+    }
+ 
+  }
+  
+  /**
+   * Method to delete rules which is not in xml access file
+   */
+  public static void deleteRules(Vector aclDocList) 
+  {
+    String aclDocId = null;
+    //checking for every acl id
+    for (int i= 0; i<aclDocList.size(); i++)
+    {
+      try
+      {
+        aclDocId = (String)aclDocList.elementAt(i);
+        AccessRulesFromDocument xmlDocument = new 
+                                            AccessRulesFromDocument(aclDocId);
+        Vector rules = xmlDocument.getAccessRuleVector();
+        AccessRulesFromDB db = new AccessRulesFromDB(aclDocId);
+        Vector rulesFromDB = db.getAccessRuleVector();
+        //check for every rules
+        for (int j=0; j< rulesFromDB.size(); j++)
         {
-          String revs = getRevNumber(objectId);
-          objectId = objectId+"."+revs;
-          revs = getRevNumber(docID);
-          docID = docID+"."+revs;
-          Triple newTriple = new Triple(docID, RELATIONSHIP, objectId);
-          newTripleVector.add(newTriple);
-          System.out.println("find a new triple");
+          String oneRule = (String)rulesFromDB.elementAt(j);
+          // if onerules get from db not in xml_access, delete it
+          if (!rules.contains(oneRule))
+          {
+            System.out.println("one rule: "+oneRule);
+            deleteAccessRule(aclDocId, oneRule);
+          }//if
+          oneRule = null;
+        }//for
+        aclDocId = null;
+      }
+      catch (Exception e)
+      {
+        System.out.println("error delete rules: "+e.getMessage());
+      } 
+    }//for
+  }
+  
+  /**
+   * Method to updata data set files
+   */
+  public static void updateDataSetFiles(Vector aclDocList) 
+  {
+    String aclDocId = null;
+    String aclAccessionNumber = null;
+    String revForAcl = null;
+    String packagAccessionNumber = null;
+    //checking for every acl id
+    for (int i= 0; i<aclDocList.size(); i++)
+    {
+      try
+      {
+        aclDocId = (String)aclDocList.elementAt(i);
+        packagAccessionNumber = getDataPackageAccessionNumber(aclDocId);
+        AccessRulesFromDocument xmlDocument = new 
+                                            AccessRulesFromDocument(aclDocId);
+        Vector docid = xmlDocument.getACLObjects();
+        AccessRulesFromDB db = new AccessRulesFromDB(aclDocId);
+        Vector docidFromDB = db.getDocidAccessRuleApplied();
+        revForAcl = getRevNumber(aclDocId);
+        aclAccessionNumber = aclDocId+DOT+revForAcl;
+        //check for every docid in relation ship
+        Vector newTripleVector = new Vector();
+        for (int j=0; j<docidFromDB.size(); j++)
+        {
+          String objectId = (String)docidFromDB.elementAt(j);
+          String objectAccessionNumber = null;
+          String revForObject = null;
+          // new triple
+          if (!docid.contains(objectId))
+          {
+            revForObject = getRevNumber(objectId);
+            objectAccessionNumber = objectId+DOT+revForObject;
+            Triple newTriple = new Triple(aclAccessionNumber, RELATIONSHIP, 
+                                        objectAccessionNumber);
+            newTripleVector.add(newTriple);
+            System.out.println("find a new triple");
           
-        }
+          }//if
+        }//for
+        // add new trip into data set
+        addNewTripleIntoDataset(packagAccessionNumber, newTripleVector);
+        // reset value
+        aclDocId = null;
+        aclAccessionNumber = null;
+        revForAcl = null;
+        packagAccessionNumber = null;
       }
-      // add new triple vector to xml
-      addNewTripleIntoDataset(packagAccessionNumber, newTripleVector);
+      catch (Exception e)
+      {
+        System.out.println("error in updataDatasetFiles(): "+e.getMessage());
+      }
+      
+    }//for
+  }
+  
+  public static void main(String[] agus)
+  {
+    
+    try
+    {
+      DBConnectionPool pool = DBConnectionPool.getInstance();
+      Vector aclVector = readAccessDocIdFromDB();
+      deleteRules(aclVector);
+      Vector aclVector2 = readAccessDocIdFromDB();
+      updateDataSetFiles(aclVector2);
     }
     catch(Exception e)
     {
