Index: src/edu/ucsb/nceas/metacat/CleanupAccessTable.java
===================================================================
--- src/edu/ucsb/nceas/metacat/CleanupAccessTable.java	(revision 1380)
+++ src/edu/ucsb/nceas/metacat/CleanupAccessTable.java	(nonexistent)
@@ -1,1226 +0,0 @@
-/**
- *  '$RCSfile$'
- *  Copyright: 2000 Regents of the University of California and the
- *              National Center for Ecological Analysis and Synthesis
- *    Authors: @tao@
- *    Release: @release@
- *
- *   '$Author$'
- *     '$Date$'
- * '$Revision$'
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-package edu.ucsb.nceas.metacat;
-
-import edu.ucsb.nceas.morpho.datapackage.PackageUtil;
-import edu.ucsb.nceas.morpho.datapackage.Triple;
-import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
-
-import java.io.FileOutputStream;
-import java.io.PrintWriter;
-import java .io.StringReader;
-import java.sql.ResultSet;
-import java.sql.PreparedStatement;
-import java.sql.SQLException;
-import java.util.Vector;
-
-import org.apache.xerces.parsers.DOMParser;
-import org.apache.xpath.XPathAPI;
-import org.w3c.dom.Attr;
-import org.w3c.dom.NamedNodeMap;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.Document;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.DocumentType;
-import org.xml.sax.InputSource;
-
-
-
-/**
- * This class is used to clean up access table in knb. It will delete any access
- * control rules which are not in access xml doc (someone add them into table
- * directly). It will update data set doc according new triples in access table.
- */
-
-public class CleanupAccessTable
-{
-  private static final String TRIPLESTAG = "//triple";
-  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 = ".";
-  private static final String LOGFILENAME = "log";
-  private static final String ALLOW = "allow";
-  private static final String ALLOWPATH ="//allow";
-  private static final String DENY = "deny";
-  private static final String DENYPATH = "//deny";
-  /**
-   * A method to add a new triple to a data set doc and update data set doc
-   * in metacat db
-   *
-   * @param accessionNumber  the accession number of the data set doc
-   * @param newTripleVector  the vector containing new triples need to be added 
-   *                      into the data set doc
-   */
-   public static void addNewTripleIntoDataset(String accessionNumber, 
-                                             Vector newTripleVector)
-  {
-    String docidWithoutVersion = null;
-    int rev = 0;
-    // check parameter
-    if (newTripleVector.size()==0 || accessionNumber == null || 
-        accessionNumber.equals(""))
-    {
-      return;
-    }
-    
-    DocumentImpl doc = null;
-    try
-    {
-      docidWithoutVersion = MetaCatUtil.getDocIdFromString(accessionNumber); 
-      rev = MetaCatUtil.getVersionFromString(accessionNumber);
-      doc = new DocumentImpl(accessionNumber);
-    }
-    catch (Exception e)
-    {
-      MetaCatUtil.debugMessage("error in create an document: "
-                              + e.getMessage(), 30);
-      return;
-    }
-    String xmlDoc = doc.toString();
-    
-    // create new triple collection
-    TripleCollection newTripleForDataSet = new TripleCollection();
-    for (int i = 0; i<newTripleVector.size(); i++)
-    {
-      //Add new tripe
-      Triple triple = (Triple)newTripleVector.elementAt(i);
-      MetaCatUtil.debugMessage("New triple: "+triple.toString(), 30);
-      newTripleForDataSet.addTriple(triple);
-    }
-    xmlDoc = addTriplesToTriplesString(newTripleForDataSet, xmlDoc, TRIPLESTAG);
-   
-    // Update accessNumber
-    Vector oldAccessNumberVector = new Vector();
-    Vector newAccessNumberVector = new Vector();
-    oldAccessNumberVector.addElement(accessionNumber);
-    //increase rev number
-    rev++;
-    accessionNumber = docidWithoutVersion + "." + rev;
-    newAccessNumberVector.addElement(accessionNumber);
-    xmlDoc = incRevInTriples(xmlDoc, oldAccessNumberVector, 
-                             newAccessNumberVector, TRIPLESTAG);
-    
-  
-    // 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);
-      MetaCatUtil.debugMessage("update data set file: "+ accessionNumber, 30);
-      writeToLogFile("Update data set file: " + accessionNumber);
-      writeToLogFile("New triples added are: "+ newTripleVector.toString());
-      writeToLogFile("\n");
-    }
-    catch (Exception e)
-    {
-      MetaCatUtil.debugMessage("error in addNewTripleIntoDataset: "+
-                                e.getMessage(), 30);
-    }
-    finally
-    {
-      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
-    }
-    
-  }
-  
-    /**
-   * method to add a collection of triples to a triples string.  this method
-   * searches for any triples already in the string and appends the new
-   * ones after the existing ones.  
-   * @param triples the collection of triples to add
-   * @param dataPackageString the xml document of data set
-   * @param triplesTag the trip tag in xml docment
-   */
-  private static String addTriplesToTriplesString(TripleCollection triples,
-                                               String dataPackageString, 
-                                               String triplesTag)
-  {
-    //String triplesTag = morpho.getConfiguration().get("triplesTag", 0);
-    Document doc = null;
-    DOMParser parser = new DOMParser();
-    InputSource in;
-    StringReader sr;
-    try
-    { //parse the wizard created file with existing triples
-      sr = new StringReader(dataPackageString);
-      in = new InputSource(sr);
-    }
-    catch(Exception fnf)
-    {
-      fnf.printStackTrace();
-      return null;
-    }
-    try
-    {
-      parser.parse(in);
-      sr.close();
-    }
-    catch(Exception e1)
-    {
-      System.err.println(e1.toString());
-    }
-    doc = parser.getDocument();
-    //get the DOM rep of the document with existing triples
-    NodeList tripleNodeList = triples.getNodeList();
-    NodeList docTriplesNodeList = null;
-    
-    try
-    {
-      //find where the triples go in the file
-      docTriplesNodeList = XPathAPI.selectNodeList(doc, triplesTag);
-    }
-    catch(Exception se)
-    {
-      System.err.println(se.toString());
-    }
-    
-    Node docNode = doc.getDocumentElement();
-    for(int j=0; j<tripleNodeList.getLength(); j++)
-    { //add the triples to the appropriate position in the file
-      Node n = doc.importNode(tripleNodeList.item(j), true);
-      int end = docTriplesNodeList.getLength() - 1;
-      Node triplesNode = docTriplesNodeList.item(end);
-      Node parent = triplesNode.getParentNode();
-      if(triplesNode.getNextSibling() == null)
-      {
-        parent.appendChild(n);
-      }
-      else
-      {
-        parent.insertBefore(n, triplesNode.getNextSibling());
-      }
-    }
-    
-    String docString = PackageUtil.printDoctype(doc);
-    docString += PackageUtil.print(doc.getDocumentElement());
-    return docString;
-  }
-  
-  
-  
-  private static String incRevInTriples(String xml, Vector oldid, 
-                                        Vector newid, String triplePath)
-  {
-    MetaCatUtil.debugMessage("oldid: " + oldid.toString() + 
-                             " newid: " + newid.toString(), 30);
-    Document doc = null;
-    DOMParser parser = new DOMParser();
-    InputSource in;
-    StringReader sr;
-     try
-    { //parse the wizard created file with existing triples
-      sr = new StringReader(xml);
-      in = new InputSource(sr);
-    }
-    catch(Exception fnf)
-    {
-      fnf.printStackTrace();
-      return null;
-    }
-    try
-    {
-      parser.parse(in);
-      sr.close();
-    }
-    catch(Exception e1)
-    {
-      System.err.println(e1.toString());
-    }
-    
-    doc = parser.getDocument();
-    NodeList tripleList = null;
-    try
-    {
-      //find where the triples go in the file
-      tripleList = XPathAPI.selectNodeList(doc, triplePath);
-    }
-    catch(Exception se)
-    {
-      System.err.println("incRevInTriples() : parse threw: " + 
-                         se.toString());
-    }
-    
-    for(int i=0; i<tripleList.getLength(); i++)
-    {
-      Node triple = tripleList.item(i);
-      NodeList children = triple.getChildNodes();
-      String sub = null;
-      String rel = null;
-      String obj = null;
-      if(children.getLength() > 2)
-      {
-        for(int j=0; j<children.getLength(); j++)
-        {
-          Node childNode = children.item(j);
-          String nodename = childNode.getNodeName().trim().toUpperCase();
-          if(nodename.equals("SUBJECT") || nodename.equals("OBJECT"))
-          {
-            String nodeval;
-            try
-            {
-              nodeval = childNode.getFirstChild().getNodeValue().trim();
-            }
-            catch(NullPointerException npe)
-            {
-              continue;
-            }
-            
-            if(oldid.contains(nodeval.trim()))
-            {
-              String newidS = "";
-              for(int k=0; k<newid.size(); k++)
-              {
-                newidS = (String)newid.elementAt(k);
-                if(nodeval.trim().equals(oldid.elementAt(k)))
-                {
-                  break;
-                }
-              }
-              MetaCatUtil.debugMessage("replacing: " + nodeval + 
-                                       " with " + newidS, 30);
-              childNode.getFirstChild().setNodeValue(newidS);
-            }
-          }
-        }
-      }
-    }
-    
-    //increase the identifier
-    NodeList identiferList = null; 
-    try
-    {
-      //find where the triples go in the file
-      identiferList = XPathAPI.selectNodeList(doc, IDENTIFIER);
-    }
-    catch(Exception se)
-    {
-      System.err.println("incRevInTriples() : parse threw: " + 
-                         se.toString());
-    }
-    
-    for(int i=0; i<identiferList.getLength(); i++)
-    {
-      Node identifierNode = identiferList.item(i);
-      String nodeName = identifierNode.getNodeName().trim().toUpperCase();
-      if (nodeName.equals("IDENTIFIER"))
-      {
-        String nodeValue = null;
-        try
-        {
-           nodeValue = identifierNode.getFirstChild().getNodeValue().trim();
-        }
-        catch(NullPointerException npe)
-        {
-          continue;
-        }
-        
-        if(oldid.contains(nodeValue.trim()))
-        {
-           String newidS = "";
-            for(int k=0; k<newid.size(); k++)
-            {
-               newidS = (String)newid.elementAt(k);
-               if(nodeValue.trim().equals(oldid.elementAt(k)))
-               {
-                  break;
-               }
-            }
-            MetaCatUtil.debugMessage("replacing: " + nodeValue + 
-                                      " with " + newidS, 30);
-            identifierNode.getFirstChild().setNodeValue(newidS);
-         }
-      
-     
-      }
-    }
-    
-    return PackageUtil.printDoctype(doc) + 
-           PackageUtil.print(doc.getDocumentElement());
-  }
-  
- 
-  /*
-   *Give a docid to find a package accesssionNumber for it.
-   */
-   private static String getDataPackageAccessionNumber(String docId)
-  {
-    String accessNumber = null;
-    String packageIdWithoutVersion = null;
-    String rev = null;
-    PreparedStatement pStmt = null;
-    ResultSet rs=null;
-    String query="SELECT docId from xml_relation where subject = ?";
-    DBConnection dbConn = null;
-    int serialNumber = -1;
-    try
-    {
-      dbConn=DBConnectionPool.
-                  getDBConnection("DBQuery.getDataPackageId");
-      serialNumber=dbConn.getCheckOutSerialNumber();
-      pStmt=dbConn.prepareStatement(query);
-      //bind the value to query
-      pStmt.setString(1, docId);
-      //execute the query
-      pStmt.execute();
-      rs=pStmt.getResultSet();
-      //process the result
-      if (rs.next()) //There are some records for the id in docId fields
-      {
-        packageIdWithoutVersion=rs.getString(1);//get data package id
-      }
-      pStmt.close();
-      rev = getRevNumber(packageIdWithoutVersion);
-      accessNumber = packageIdWithoutVersion+"."+rev;
-      MetaCatUtil.debugMessage("DataPackageId: "+accessNumber, 50);
-    }//try
-    catch (SQLException e)
-    {
-      MetaCatUtil.debugMessage("Error in isDataPackageId: "
-                            +e.getMessage(), 30);
-    }
-    finally
-    {
-      try
-      {
-        pStmt.close();
-      }//try
-      catch (SQLException ee)
-      {
-        MetaCatUtil.debugMessage("Error in isDataPackageId: "
-                                                        + ee.getMessage(), 30);
-      }//catch
-      finally
-      {
-        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
-      }//finally
-    }//finally
-    return accessNumber;
-  }//isDataPackageId()
-  
-  /**
-   * Get a dev number from a given docid(without dev)
-   */
-  public static String getRevNumber(String docId) 
-  {
-    DBConnection dbConn = null;
-    int serialNumber = -1;
-    String query = "SELECT rev from xml_documents where docid = ?";
-    PreparedStatement pStmt = null;
-    ResultSet rs = null;
-    String rev = null;
-    try
-    {
-      dbConn=DBConnectionPool.
-                  getDBConnection("DBQuery.getDataPackageId");
-      serialNumber=dbConn.getCheckOutSerialNumber();
-      pStmt=dbConn.prepareStatement(query);
-      //bind the value to query
-      pStmt.setString(1, docId);
-      //execute the query
-      pStmt.execute();
-      rs=pStmt.getResultSet();
-      //process the result
-      if (rs.next()) //There are some records for the id in docId fields
-      {
-        rev=rs.getString(1);//get data package id
-      }
-      pStmt.close();
-    }
-    catch (SQLException e)
-    {
-      MetaCatUtil.debugMessage("the error in getRevNumber: "+
-                                e.getMessage(), 30);
-    }
-    finally
-    {
-      try
-      {
-        pStmt.close();
-      }
-      catch (SQLException ee)
-      {
-        MetaCatUtil.debugMessage("the error in getRevNumber2: "+
-                                  ee.getMessage(), 30);
-      }
-      finally
-      {
-        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
-        return rev;
-      }
-    }
-   
-  }
-  /**
-   * Delete a access control rule which is not in access xml document 
-   */
-  public static void deleteAccessRule(String aclDocid, String accessRule)
-  {
-    String principal  = null;
-    String allowType  = null;
-    String permission = null;
-    String allowOrder = null;
-    String [] parseAccessRuleArray = null;
-     DBConnection dbConn = null;
-    int serialNumber = -1;
-    String query = "delete from xml_access where accessfileid = ? " +
-                   "and principal_name = ? and perm_type = ? "+
-                   "and permission = ? and perm_order = ?";
-    PreparedStatement pStmt = null;
-    String rev = null;
-    // parse the access rule 
-    parseAccessRuleArray = parseAccessRule(accessRule);
-    if ( parseAccessRuleArray == null)
-    {
-      MetaCatUtil.debugMessage("couldn't parse docid: "+aclDocid, 30);
-      return;
-    }
-    else
-    {
-      principal  = parseAccessRuleArray[0];
-      allowType  = parseAccessRuleArray[1];
-      permission = parseAccessRuleArray[2];
-      allowOrder = parseAccessRuleArray[3];
-      
-    }
-    
-    try
-    {
-      dbConn=DBConnectionPool.
-                  getDBConnection("DBQuery.getDataPackageId");
-      serialNumber=dbConn.getCheckOutSerialNumber();
-      pStmt=dbConn.prepareStatement(query);
-      //bind the value to query
-      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();
-      MetaCatUtil.debugMessage("delete rules: "+accessRule+" for "
-                               +aclDocid+" in table", 30);
-      writeToLogFile("Delete rules: "+accessRule+" for "
-                     +aclDocid+" in xml_access table");
-    }
-    catch (SQLException e)
-    {
-      MetaCatUtil.debugMessage("the error in deleteAccessRule(): "+
-                                e.getMessage(), 30);
-    }
-    finally
-    {
-      try
-      {
-        pStmt.close();
-      }
-      catch (SQLException ee)
-      {
-        MetaCatUtil.debugMessage("the error in getRevNumber2: "+
-                                  ee.getMessage(), 30);
-      }
-      finally
-      {
-        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
-       
-      }
-    }
-  }
-  
-  /*
-   * Add a access control rule which is not in access xml document 
-   */
-  public static void addAccessRules(String aclDocid, Vector accessRules)
-                                    throws Exception
-  {
-    String principal  = null;
-    String allowType  = null;
-    String permission = null;
-    String allowOrder = null;
-    String revOfAcl = null;
-    String accessionNumberOfAcl = null;
-    String newAccessionNumerOfAcl = null;
-    String datasetDocId = null;
-    String accessionNumberOfDataset = null;
-    String newAccessionNumberOfDataset = null;
-    int     revOfDataset = 0;
-    Vector xmlRuleVector = new Vector();
-    Vector oldAccessionNumberVector = new Vector();
-    Vector newAccessionNumberVector = new Vector();
-    
-    //checking parameter
-    if (aclDocid==null|| accessRules==null || accessRules.size() == 0)
-    {
-      return;
-    }
-    
-    // get xmlString for acl document
-    String oldxmlStringofAcl = getXMLStringFromDocId(aclDocid);
-    
-    
-    for(int i = 0; i<accessRules.size(); i++)
-    {
-      String oneRule = (String)accessRules.elementAt(i);
-      // parse the access rule 
-      String [] parseAccessRuleArray = parseAccessRule(oneRule);
-      if ( parseAccessRuleArray == null)
-      {
-        MetaCatUtil.debugMessage("couldn't parse docid: "+aclDocid, 30);
-        continue;
-      }
-      else
-      {
-        principal  = parseAccessRuleArray[0];
-        allowType  = parseAccessRuleArray[1];
-        permission = parseAccessRuleArray[2];
-        allowOrder = parseAccessRuleArray[3];
-        //only for the allowtype is allow or deny
-        if (allowType.equals(ALLOW) || allowType.equals(DENY))
-        {
-          try
-          {
-            String xmlRule = ruleToXML(allowType, principal, permission);
-            xmlRuleVector.addElement(xmlRule);
-          }
-          catch(Exception e)
-          {
-            MetaCatUtil.debugMessage("error in ruletoxml: "+e.getMessage(), 30);
-            continue;
-          }
-        }//if
-        
-      }//else
-    }//for
-    // create new rules xml document
-    String patchRuleXmlAdded    = createXMLStringForAllAddingRules(xmlRuleVector);
-    String newACLXmlString = updateACLXml(patchRuleXmlAdded, oldxmlStringofAcl);
-    MetaCatUtil.debugMessage("new acl xml: "+newACLXmlString, 30);
-    // update acl file and write back to db
-    revOfAcl = getRevNumber(aclDocid);
-    accessionNumberOfAcl = aclDocid+DOT+revOfAcl;
-    oldAccessionNumberVector.addElement(accessionNumberOfAcl);
-    MetaCatUtil.debugMessage("old accession number of acl: "+
-                              accessionNumberOfAcl, 30);
-    revOfAcl = (new Integer((new Integer(revOfAcl)).intValue()+1)).toString();
-    newAccessionNumerOfAcl = aclDocid+DOT+revOfAcl;
-    newAccessionNumberVector.addElement(newAccessionNumerOfAcl);
-    MetaCatUtil.debugMessage("new accession number of acl: "+
-                              newAccessionNumerOfAcl, 30);
-    StringReader xmlAcl = new StringReader(newACLXmlString); 
-    String pub = "no";
-    StringReader dtd = null;
-    String doAction = "UPDATE";
-    boolean validate = true;
-    DBConnection dbConn = null;
-    int serialNumber = -1;
-    try
-    {
-      dbConn=DBConnectionPool.
-                  getDBConnection("CleanupAccessTable.addAccessRules");
-      serialNumber=dbConn.getCheckOutSerialNumber();
-      DocumentImpl.write(dbConn, xmlAcl, pub, dtd, doAction,
-                       newAccessionNumerOfAcl, null, null, validate);
-     
-      writeToLogFile("updated acl file: " + newAccessionNumerOfAcl);
-      writeToLogFile("New rules added are: "+ patchRuleXmlAdded.toString());
-    }
-    finally
-    {
-      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
-    }
-    
-    // we need to inrease accessnumber in dataset and write it back to db
-    accessionNumberOfDataset = getDataPackageAccessionNumber(aclDocid);
-    oldAccessionNumberVector.addElement(accessionNumberOfDataset);
-    DocumentImpl doc = new DocumentImpl(accessionNumberOfDataset);
-    String xmlOfDataset = doc.toString();
-    MetaCatUtil.debugMessage("old access of dataset: "+
-                              accessionNumberOfDataset, 30);
-    datasetDocId = MetaCatUtil.getDocIdFromString(accessionNumberOfDataset);
-    revOfDataset = MetaCatUtil.getVersionFromString(accessionNumberOfDataset);
-    revOfDataset++;
-    newAccessionNumberOfDataset = datasetDocId+DOT+revOfDataset;
-    newAccessionNumberVector.addElement(newAccessionNumberOfDataset);
-    MetaCatUtil.debugMessage("new accessionNumber of dataset: "+
-                              newAccessionNumberOfDataset, 30);
-    String newDatasetString = incRevInTriples(xmlOfDataset, 
-                                              oldAccessionNumberVector,
-                                              newAccessionNumberVector,
-                                              TRIPLESTAG);
-    StringReader xmlDataset = new StringReader(newDatasetString);
-    MetaCatUtil.debugMessage("new dataset xml: "+newDatasetString, 30);
-    try
-    {
-      dbConn=DBConnectionPool.
-                  getDBConnection("CleanupAccessTable.addAccessRules");
-      serialNumber=dbConn.getCheckOutSerialNumber();
-      DocumentImpl.write(dbConn, xmlDataset, pub, dtd, doAction,
-                         newAccessionNumberOfDataset, null, null, validate);
-     
-      writeToLogFile("Update data set file: " + newAccessionNumberOfDataset);
-      writeToLogFile("old accessionNumber: " + 
-                      oldAccessionNumberVector.toString() +
-                      " were replaced by: " + 
-                      newAccessionNumberVector.toString());
-      writeToLogFile("\n");
-    }
-    finally
-    {
-      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
-    }
-    
-    
-  }
-  
-  /*
-   * Method to add rule(xml format ) to the orginal acl xml
-   */
-  private static String updateACLXml(String patchXML, String originalXML)
-  {
-    Document doc = getDOMDocumentFromString(originalXML);
-    Document docPatch = getDOMDocumentFromString(patchXML);
-    NodeList allowInPatch = getNodeList(docPatch, ALLOWPATH);
-    
-    NodeList denyInPatch   = getNodeList(docPatch, DENYPATH);
-   
-    NodeList allowInOrigin= getNodeList(doc, ALLOWPATH);
-   
-    NodeList denyInorigin = getNodeList(doc,DENYPATH);
-   
-    
-    
-    //for allow rule
-    
-    for(int j=0; j<allowInPatch.getLength(); j++)
-    { 
-      int end = allowInOrigin.getLength() - 1;
-      Node allowNode = allowInOrigin.item(end);
-      Node parent = allowNode.getParentNode();
-      //add the triples to the appropriate position in the file
-      Node n = doc.importNode(allowInPatch.item(j), true);
-      if(allowNode.getNextSibling() == null)
-      {
-        parent.appendChild(n);
-      }
-      else
-      {
-        parent.insertBefore(n, allowNode.getNextSibling());
-      }
-    }
-   
-    // for deny rule
-    for (int i=0; i<denyInPatch.getLength(); i++)
-    {
-      int endOfDeny = denyInorigin.getLength()-1;
-      if (endOfDeny != -1)
-      {
-        // already has deny rule in access document
-        Node denyNode = denyInorigin.item(endOfDeny);
-        Node parentOfDeny = denyNode.getParentNode();
-        Node n = doc.importNode(denyInPatch.item(i), true);
-        if (denyNode.getNextSibling() == null)
-        {
-          parentOfDeny.appendChild(n);
-        }
-        else
-        {
-          parentOfDeny.insertBefore(n, denyNode.getNextSibling());
-        }
-      }
-      else
-      {
-        // no deny rule in access file, just append the deny rule to allow rule
-        int end = allowInOrigin.getLength() - 1;
-        Node allowNode = allowInOrigin.item(end);
-        Node parent = allowNode.getParentNode();
-        //add the triples to the appropriate position in the file
-        Node n = doc.importNode(denyInPatch.item(i), true);
-        parent.appendChild(n);
-      
-      }
-    }
-    String docString = PackageUtil.printDoctype(doc);
-    docString += PackageUtil.print(doc.getDocumentElement());
-    return docString;
-  }
-  
-  /*
-   * Transfer a rule to String
-   */
-  private static String ruleToXML(String allowType, String principal, 
-                                  String permission) throws Exception
-  {
-     Vector permissionVector = transferPermission(permission);
-     StringBuffer sb = new StringBuffer();
-     sb.append("<");
-     sb.append(allowType);
-     sb.append(">");
-     sb.append("<principal>").append(principal).append("</principal>");
-     // add permission
-     for ( int i = 0; i < permissionVector.size(); i++)
-     {
-        String permissionString = (String)permissionVector.elementAt(i);
-        sb.append("<permission>").append(permissionString).
-                                  append("</permission>");
-     }
-     sb.append("</");
-     sb.append(allowType);
-     sb.append(">");
-     return sb.toString();
-  }
-  
-  /*
-   * Method tansfer permission form number(4) to string (read)
-   */
-  private static Vector transferPermission(String permission) throws Exception
-  {
-    Vector result = new Vector();
-    String read = "read";
-    String write = "write";
-    String all = "all";
-    int permissionInInt = (new Integer(permission)).intValue();
-    switch (permissionInInt)
-    {
-      case 2:
-        result.addElement(write);
-        break;
-      case 4:
-        result.addElement(read);
-        break;
-      case 6:
-        result.addElement(write);
-        result.addElement(read);
-        break;
-      case 7:
-        result.addElement(all);
-        break;
-    }//switch
-    return result; 
-  }
-   
-   
-  /*
-   * A method create a complete xml string for given a vector
-   */
-  private static String createXMLStringForAllAddingRules(Vector rules)
-  {
-    String root = "rule";
-    StringBuffer sb = new StringBuffer();
-    sb.append("<?xml version=\"1.0\"?>");
-    sb.append("<" + root + ">");
-    
-    for(int i=0; i<rules.size(); i++)
-    {
-      sb.append((String)rules.elementAt(i));
-    }
-    sb.append("</" + root + ">");
-    MetaCatUtil.debugMessage("new xml for adding rules: "+sb.toString(), 30);
-    return sb.toString();
-    
-  }
- 
-  /*
-   * A method to get nodelist for given xmlString and path
-   */
-  private static NodeList getNodeList(Document doc, String path)
-  {
-     NodeList list = null;
-     try
-    {
-      //find where the triples go in the file
-      list = XPathAPI.selectNodeList(doc, path);
-    }
-    catch(Exception se)
-    {
-      System.err.println(se.toString());
-    }
-    return list;
-  }
-  
-  /*
-   * Method to get xml string for given docid 
-   */
-  private static String getXMLStringFromDocId(String docId)
-  {
-    DocumentImpl doc = null;
-    String docidWithoutVersion = null;
-    String rev = null;
-    String accessionNumber = null;
-    String xmlDoc = null;
-    if (docId == null || docId.equals(""))
-    {
-      return null;
-    }
-    
-    try
-    {
-      docidWithoutVersion = MetaCatUtil.getDocIdFromString(docId); 
-      rev = getRevNumber(docidWithoutVersion);
-      accessionNumber = docidWithoutVersion+DOT+rev;
-      doc = new DocumentImpl(accessionNumber);
-    }
-    catch (Exception e)
-    {
-      MetaCatUtil.debugMessage("error in create an document: "
-                              + e.getMessage(), 30);
-      return null;
-    }
-    xmlDoc = doc.toString();
-    return xmlDoc;
-  }
-  
- 
-  
-  /*
-   * method to get a dom docoument by given a xml string
-   */ 
-  private static Document getDOMDocumentFromString(String xml)
-  {
-    Document doc = null;
-    DOMParser parser = new DOMParser();
-    InputSource in = null;
-    StringReader sr = null;
-    String xmlString = null;
-    //checking parameter
-    if (xml==null || xml.equals(""))
-    {
-      return null;
-    }
-       
-    try
-    { //parse the wizard created file with existing triples
-      sr = new StringReader(xml);
-      in = new InputSource(sr);
-    }
-    catch(Exception fnf)
-    {
-      fnf.printStackTrace();
-      return null;
-    }
-    try
-    {
-      parser.parse(in);
-      sr.close();
-    }
-    catch(Exception e1)
-    {
-      System.err.println(e1.toString());
-      return null;
-    }
-    doc = parser.getDocument();
-    return doc;
-  }
-  
-  /*
-   * parse the string and get a array cotains principle name(index 0),
-   * allow type (index1), permission (index2), and allow order(index, 4)
-   */
-  private static String[] parseAccessRule(String accessRule)
-  {
-    String principal  = null;
-    String allowType  = null;
-    String permission = null;
-    String allowOrder = null;
-    String [] result  = new String[4];
-    int count = 0;
-    int index = 0;
-    char delimiter = DELIMITER.charAt(0);
-    
-    if (accessRule== null)
-    {
-      return result;
-    }
-    for (int i=0; i<accessRule.length(); i++)
-    {
-      
-      char charInString = accessRule.charAt(i);
-      // find a symbol !
-      if (charInString == delimiter)
-      {
-        count ++;
-      }
-      else
-      {
-        continue;
-      }
-      
-      switch (count)
-      {
-        case 1:
-        {
-          // get principal
-          principal = accessRule.substring(0, i);
-          // remeber the index
-          index = i;
-          break;
-        }
-        case 2:
-        {
-          allowType = accessRule.substring(index+1, i);
-          index = i;
-          break;
-        }
-        case 3:
-        {
-          permission = accessRule.substring(index+1, i);
-          index = i;
-          allowOrder = accessRule.substring(index+1, accessRule.length());
-          break;
-        }
-      }//switch
-    }//for
-    
-    // if the string exactly has 3 !, add it to array
-    if ( count == 3)
-    {
-      result[0] = principal;
-      result[1] = allowType;
-      result[2] = permission;
-      result[3] = allowOrder;
-    }//if
-    return result;
-  }
-  
-  /**
-   * A method get acl docid list from xml_access table
-   */
-  public static Vector readAccessDocIdFromDB()
-  {
-    Vector docidList = new Vector();
-    String docid = null;
-    DBConnection conn = null;
-    int serialNumber = -1;
-    PreparedStatement pstmt = null;
-    try
-    {
-      //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) 
-      {
-        docid  = rs.getString(1);
-        // if hashtable doesn't has the rule add to hashtable
-        if (docid !=null && !docidList.contains(docid))
-        {
-          docidList.addElement(docid);
-          
-        }
-        hasRows = rs.next();
-        // reset value
-        docid = null;
-        
-      }//while
-  
-    }
-    catch (SQLException e)
-    {
-      MetaCatUtil.debugMessage("error in readAccessDocIdFromDB: "+
-                              e.getMessage(), 30);
-    }
-    finally
-    {
-      try
-      {
-        pstmt.close();
-      }
-      catch (SQLException ee)
-      {
-        MetaCatUtil.debugMessage("error in readAccessDocIdFromDB: "+
-                                 ee.getMessage(), 30);
-      }
-      finally
-      {
-        //retrun DBConnection
-        DBConnectionPool.returnDBConnection(conn,serialNumber);
-        return docidList;
-      }
-    }
- 
-  }
-  
-  /**
-   * Method to delete rules which is not in xml access file
-   */
-  public static void addAccessRulesToDocument(String aclDocId, 
-                                              Vector rulesFromDB,
-                                              Vector rulesFromDocument) 
-  {
-    
-    try
-    {
-      Vector rulesAdded = new Vector();
-      //check for every rules
-      for (int j=0; j< rulesFromDB.size(); j++)
-      {
-        String oneRule = (String)rulesFromDB.elementAt(j);
-        // if onerules get from db not in xml_access, delete it
-        if (!rulesFromDocument.contains(oneRule))
-        {
-          MetaCatUtil.debugMessage("one rule: "+oneRule, 30);
-          rulesAdded.addElement(oneRule);
-        }//if
-        oneRule = null;
-      }//for
-      addAccessRules(aclDocId, rulesAdded);
-    }
-    catch (Exception e)
-    {
-      MetaCatUtil.debugMessage("error delete addAccessRulesToDocument(): "+
-                                e.getMessage(), 30);
-    } 
-  }
-  
-  /**
-   * Method to updata data set files
-   */
-  public static void updateDataSetFilesForNewTriples(String aclDocId,
-                                                     Vector docidFromAccessDB, 
-                                                     Vector docidFromRelationDB) 
-  {
-    String aclAccessionNumber = null;
-    String revForAcl = null;
-    String packagAccessionNumber = null;
-    //checking for every acl id
-    
-    try
-    {
-      //Get package and acl access number
-      packagAccessionNumber = getDataPackageAccessionNumber(aclDocId);
-      revForAcl = getRevNumber(aclDocId);
-      aclAccessionNumber = aclDocId+DOT+revForAcl;
-      //check for every docid in relation ship
-      Vector newTripleVector = new Vector();
-      for (int j=0; j<docidFromAccessDB.size(); j++)
-      {
-        String objectId = (String)docidFromAccessDB.elementAt(j);
-        String objectAccessionNumber = null;
-        String revForObject = null;
-        // new triple
-        if (!docidFromRelationDB.contains(objectId))
-        {
-          revForObject = getRevNumber(objectId);
-          objectAccessionNumber = objectId+DOT+revForObject;
-          Triple newTriple = new Triple(aclAccessionNumber, RELATIONSHIP, 
-                                        objectAccessionNumber);
-          newTripleVector.add(newTriple);
-          MetaCatUtil.debugMessage("find a new triple"+
-                                      newTriple.toString(), 30);
-          
-        }//if
-      }//for
-      // add new trip into data set
-      addNewTripleIntoDataset(packagAccessionNumber, newTripleVector);
-     
-    }
-    catch (Exception e)
-    {
-        MetaCatUtil.debugMessage("error in updataDatasetFiles(): "+
-                                  e.getMessage(), 30);
-    }
-      
-  
-  }
-  
-  /*
-   * Method to write information into log file
-   */
-  private static void writeToLogFile(String message)
-  {
-    try
-    {
-      FileOutputStream fos = new FileOutputStream(LOGFILENAME, true);
-      PrintWriter pw = new PrintWriter(fos);
-      pw.println(message);
-      pw.flush();
-    }
-    catch(Exception e)
-    {
-      MetaCatUtil.debugMessage("error writing to log: " + e.getMessage(), 30);
-     
-    }
-  }
-  
-  public static void main(String[] agus)
-  {
-    
-    try
-    {
-      DBConnectionPool pool = DBConnectionPool.getInstance();
-      Vector aclVector = readAccessDocIdFromDB();
-      for (int i = 0; i < aclVector.size(); i++)
-      {
-        try
-        {
-          String aclDocId = (String)aclVector.elementAt(i);
-          AccessRulesFromDocument xmlDocument = new 
-                                            AccessRulesFromDocument(aclDocId);
-          AccessRulesFromDB db = new AccessRulesFromDB(aclDocId);
-          Vector ruleFromDocument = xmlDocument.getAccessRuleVector();
-          Vector ruleFromDB = db.getAccessRuleVector();
-          Vector docIdFromRelation = xmlDocument.getACLObjects();
-          Vector docIdFromAccess = db.getDocidAccessRuleApplied();
-          // update rules in acl and triple in dataset file
-          addAccessRulesToDocument(aclDocId, ruleFromDB, ruleFromDocument);
-          //update triplse in dataset file
-          updateDataSetFilesForNewTriples(aclDocId, docIdFromAccess, 
-                                          docIdFromRelation);
-        }
-        catch (Exception e)
-        {
-           MetaCatUtil.debugMessage("exception in main is: " +
-                                    e.getMessage(), 30);
-        }
-        
-      }
-    }
-    catch(Exception ee)
-    {
-      MetaCatUtil.debugMessage("exception is: "+ee.getMessage(), 30);
-    }
-  }
-  
-}//cleanupAccessTable

Property changes on: src/edu/ucsb/nceas/metacat/CleanupAccessTable.java
___________________________________________________________________
Deleted: svn:eol-style
## -1 +0,0 ##
-native
\ No newline at end of property
Deleted: svn:executable
## -1 +0,0 ##
-*
\ No newline at end of property
Deleted: svn:keywords
## -1 +0,0 ##
-Author Date Id Revision
\ No newline at end of property
