Index: src/edu/ucsb/nceas/metacat/DBSAXHandler.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DBSAXHandler.java	(revision 818)
+++ src/edu/ucsb/nceas/metacat/DBSAXHandler.java	(revision 819)
@@ -214,7 +214,7 @@
 
   }
   
-  // The run method of xmlIndex thread. It writes XML Index for the document.
+  /* The run method of xmlIndex thread. It writes XML Index for the document. */
   public void run () {
     DBSAXNode currNode = null;
     DBSAXNode prevNode = null;
@@ -238,34 +238,25 @@
     
       dbconn.commit();
          
-      //if this is a package file then write the package info to 
-      //the xml_relation table. RelationHandler checks to see
-      //if it is a package file so you don't have to do it here.
+      //if this is a package file
       if ( doctype.equals(util.getOption("packagedoctype")) )
       {
-        //DocumentImpl xmldoc = new DocumentImpl(dbconn, docid);
+        // write the package info to xml_relation table
         RelationHandler rth = new RelationHandler(docid, dbconn);
-      } 
-      else if ( doctype.equals(util.getOption("accessdoctype")) ) 
-      {
-        DocumentImpl xmldoc = new DocumentImpl(dbconn, docid);
-        String xml = xmldoc.toString();
-        try {
-          AccessControlList aclobj = 
-          new AccessControlList(dbconn, docid, new StringReader(xml),
-                                user, groups, serverCode);
-          dbconn.commit();
-        } catch (SAXException e) {
-          try {
-            dbconn.rollback();
-            dbconn.close();
-          } catch (SQLException sqle) {}
-          System.out.println("Error writing ACL in DBSaxHandler.run " + 
-                             e.getMessage()); 
+        // from the relations get the access file id for that package
+        String aclid = rth.getAccessFileID(docid);
+        // if there are access file, write ACL for that package
+        if ( aclid != null ) {
+          runAccessControlList(dbconn, aclid);
         }
       }
+      // if it is an access file
+      else if ( doctype.equals(util.getOption("accessdoctype")) )
+      {
+        // write ACL for the package
+        runAccessControlList(dbconn, docid);
+      }
       
-      
       dbconn.close();
 
     } catch (Exception e) {
@@ -273,11 +264,24 @@
         dbconn.rollback();
         dbconn.close();
       } catch (SQLException sqle) {}
-      System.out.println("Error writing XML Index in DBSaxHandler.run " + 
-                         e.getMessage()); 
+      System.out.println("Error in DBSAXHandler.run " + e.getMessage());
+      e.printStackTrace();
     }      
   }
+  
+  // It runs in xmlIndex thread. It writes ACL for a package.
+  private void runAccessControlList (Connection conn, String aclid)
+                                                throws Exception
+  {
+    // read the access file from xml_nodes
+    // parse the access file and store the access info into xml_access
+    AccessControlList aclobj = 
+    new AccessControlList(conn, aclid, //new StringReader(xml),
+                          user, groups, serverCode);
+    conn.commit();
+  }
 
+
   /** SAX Handler that is called for each XML text node */
   public void characters(char[] cbuf, int start, int len) throws SAXException {
      MetaCatUtil.debugMessage("CHARACTERS");
Index: src/edu/ucsb/nceas/metacat/RelationHandler.java
===================================================================
--- src/edu/ucsb/nceas/metacat/RelationHandler.java	(revision 818)
+++ src/edu/ucsb/nceas/metacat/RelationHandler.java	(revision 819)
@@ -38,6 +38,7 @@
    * @param docid the ID of the XML document to index.
    */
   public RelationHandler(String docid, Connection conn)
+              throws McdbException, SQLException, AccessionNumberException
   {
     this.conn = conn;
     this.docid = docid;
@@ -47,130 +48,86 @@
   /**
    * insert the relations specified in the triples into xml_relation table
    */ 
-  public void putRelations() {
-    
+  private void putRelations() 
+              throws McdbException, SQLException, AccessionNumberException
+  {
+    String packagetype = null;
+    String subject = null;
+    String subDoctype = null;
+    String relationship = null;
+    String object = null;
+    String objDoctype = null;
+    PreparedStatement pstmt = null; // to get the relations from xml_nodes
+    PreparedStatement tstmt = null; // to insert each relation into xml_relation
     MetaCatUtil.debugMessage("Running relation handler!");
 
-    // deletes all of the relations with a docid of @docid.
-    //pseudo-code algorithm
-    //for each new relation r in xml_nodes
-    //  put r into xml_relation
-    //  compare r to each relation already in xml_relation
-    //  if r isrelatedto a row in xml_relation
-    //    add a new row to xml_relation that represents this new relation
-    try {
+    /* 
+     * PSEUDO-CODE ALGORITHM:
+     * deletes all of the relations with a docid of @docid.
+     * for each new relation r in xml_nodes
+     *   put r into xml_relation
+     */
 
-      DocumentImpl xmldoc = new DocumentImpl(conn,docid,false);
-      //DocumentImpl xmldoc = new DocumentImpl(conn);
-      //xmldoc.getDocumentInfo(docid);
-      String packagetype = xmldoc.getDoctype();
+    //DocumentImpl xmldoc = new DocumentImpl(conn,docid,false);
+    packagetype = (new DocumentImpl(conn,docid,false)).getDoctype();
 
-      // first delete the relations for this package document if any
-      deleteRelations(docid);
-      // put the new relations
-      PreparedStatement pstmt = conn.prepareStatement(
-                                QuerySpecification.printPackageSQL(docid));
-      pstmt.execute();
-      
-      //get the new relations out of xml_nodes
-      ResultSet rs = pstmt.getResultSet();
-      boolean hasmorerows = rs.next();
-      while(hasmorerows) {
-        String subject = rs.getString(1);
-        String relationship = rs.getString(2);
-        String object = rs.getString(3);
+    // first delete the relations for this package document if any
+    deleteRelations(docid);
 
-        String subjectDoctype = null;
-        String paramDocid = null;
-        URL subjectMurl = null;
+    // to put the new relations get them out of xml_nodes
+    pstmt = conn.prepareStatement(QuerySpecification.printPackageSQL(docid));
+    pstmt.execute();
+    ResultSet rs = pstmt.getResultSet();
+    boolean hasmorerows = rs.next();
+    if (hasmorerows) {
+      tstmt = conn.prepareStatement("INSERT INTO xml_relation (" +
+                                    "docid,packagetype,subject,subdoctype," +
+                                    "relationship, object, objdoctype) " + 
+                                    "VALUES (?, ?, ?, ?, ?, ?, ?)");
+    }
+    while(hasmorerows) {
+      subject = rs.getString(1);
+      relationship = rs.getString(2);
+      object = rs.getString(3);
 
-        //get the current relations information
-        String subDocid = null;
-        String objDocid = null;
-        String subDoctype = null;
-        String objDoctype = null;
+      // cut out the revision number for subject and object
+      subject = (new DocumentIdentifier(subject)).getIdentifier();
+      object = (new DocumentIdentifier(object)).getIdentifier();
+
+      //subDoctype and objDoctype are N/A
+      subDoctype = null;
+      objDoctype = null;
+
+      //put the new relation into xml_relation
+      tstmt.setString(1, docid);
+      tstmt.setString(2, packagetype);
+      tstmt.setString(3, subject);
+      tstmt.setString(4, subDoctype);
+      tstmt.setString(5, relationship);
+      tstmt.setString(6, object);
+      tstmt.setString(7, objDoctype);
+      tstmt.execute(); 
         
-        try {
-          URL subMurl = new URL(subject);
-          if(subMurl.getQuery() != null) {
-            Hashtable subMurlParams = util.parseQuery(subMurl.getQuery());
-            subDocid = (String)subMurlParams.get("docid");
-            if(subMurl.getProtocol().equals("metacat")) {
-              DocumentImpl subDoc = new DocumentImpl(conn, subDocid);
-              subDoctype = subDoc.getDoctype();
-            }
-          } else {
-            subDocid = subject;
-          }
-        } catch(MalformedURLException murle) { 
-          //assume this is just a docid not a url
-          subDocid = subject;
-        }
-        
-        try {
-          URL objMurl = new URL(object); 
-          if(objMurl.getQuery() != null) {
-            Hashtable objMurlParams = util.parseQuery(objMurl.getQuery());
-            objDocid = (String)objMurlParams.get("docid");
-            if(objMurl.getProtocol().equals("metacat")) {
-              DocumentImpl objDoc = new DocumentImpl(conn, objDocid);
-              objDoctype = objDoc.getDoctype();
-            }
-          } else {
-            objDocid = object;
-          }
-        } catch(MalformedURLException murle) {
-          //assume this is just a docid
-          objDocid = object;
-        }
-        
-        
-        //now that the comparisons are done, the new relation can be put
-        //into xml_relation
-        StringBuffer insertStmt = new StringBuffer();
-        insertStmt.append("insert into xml_relation (docid, packagetype, ");
-        insertStmt.append("subject, subdoctype, ");
-        insertStmt.append("relationship, object, objdoctype) values ('");
-        insertStmt.append(docid).append("', '");
-        insertStmt.append(packagetype).append("', '");
-        insertStmt.append(subject).append("', '");
-        insertStmt.append(subDoctype).append("', '");
-        insertStmt.append(relationship).append("', '");
-        insertStmt.append(object).append("', '");
-        insertStmt.append(objDoctype).append("')");
-        
-        pstmt = conn.prepareStatement(insertStmt.toString());
-        pstmt.execute(); 
-        
-        hasmorerows = rs.next();
-      }
+      hasmorerows = rs.next();
+    }
       
-      pstmt.close();
-      conn.commit();
-
-    } catch(Exception e) { 
-      MetaCatUtil.debugMessage("Error in RelationHandler.run(): " + 
-                               e.getMessage());
-      System.out.println("Error in RelationHandler.run(): " + 
-                               e.getMessage());
-      try { 
-        conn.rollback();
-      } catch (SQLException sqle) {
-        System.out.println("Error in RelationHandler.run(): " + 
-                           sqle.getMessage());
-      }
+    if ( tstmt != null ) {
+      tstmt.close();
     }
+    pstmt.close();
+    conn.commit();
   }
   
   /**
    * Deletes all of the relations with a docid of 'docid'.
-   * @param docid the docid to delete.
+   * @param docid the docid of the package which relations to delete.
    */
   public void deleteRelations(String docid) throws SQLException
   {
     try {
-      PreparedStatement pstmt = conn.prepareStatement("delete from " +
-                             "xml_relation where docid like '" + docid + "'");
+      PreparedStatement pstmt = conn.prepareStatement(
+                                "DELETE FROM xml_relation " +
+                                "WHERE docid = '" + docid + "'");
       pstmt.execute();
       pstmt.close();
     } catch(SQLException e) {
@@ -181,4 +138,32 @@
       throw e;
     }
   }
+
+  /**
+   * Get the access file id for a package
+   * @param docid the document identifier of the package
+   * @return the document identifier of the access file for that package
+   */
+  public String getAccessFileID(String docid) throws SQLException
+  {
+    String aclid = null;
+    PreparedStatement pstmt = 
+      conn.prepareStatement("SELECT docid FROM xml_documents " +
+                            "WHERE docid in " +
+                            "(SELECT subject FROM xml_relation " +
+                            "WHERE docid = ?) " +
+                            "AND doctype = ?");
+    pstmt.setString(1, docid);
+    pstmt.setString(2, MetaCatUtil.getOption("accessdoctype"));
+    pstmt.execute();
+    ResultSet rs = pstmt.getResultSet();
+    boolean hasRow = rs.next();
+    if (hasRow) {
+      aclid = rs.getString(1);
+    }
+    pstmt.close();
+    
+    return aclid;
+  }
+
 }
Index: src/edu/ucsb/nceas/metacat/AccessControlList.java
===================================================================
--- src/edu/ucsb/nceas/metacat/AccessControlList.java	(revision 818)
+++ src/edu/ucsb/nceas/metacat/AccessControlList.java	(revision 819)
@@ -47,8 +47,6 @@
 import org.xml.sax.helpers.XMLReaderFactory;
 import org.xml.sax.helpers.DefaultHandler;
 
-//import edu.ucsb.nceas.dbadapter.AbstractDatabase;
-
 /** 
  * A Class that loads eml-access.xml file containing ACL for a metadata
  * document into relational DB. It extends DefaultHandler class to handle
@@ -116,10 +114,9 @@
    * @param serverCode the serverid from xml_replication on which this document
    *        resides.
    */
-  public AccessControlList(Connection conn, String aclid, Reader acl,
+  public AccessControlList(Connection conn, String aclid, //Reader acl,
                            String user, String[] groups, int serverCode)
-                  throws SAXException, IOException, ClassNotFoundException,
-                         Exception
+                  throws SAXException, IOException, McdbException
   {
     String parserName = MetaCatUtil.getOption("saxparser");
     this.server = MetaCatUtil.getOption("server");
@@ -133,8 +130,6 @@
     this.user = user;
     this.groups = groups;
     this.aclid = aclid;
-    DocumentImpl aclinfo = new DocumentImpl(conn,aclid,false);
-    this.rev = aclinfo.getRev();
     this.resourceURL = new Vector();
     this.resourceID = new Vector();
     this.principal = new Vector();
@@ -143,31 +138,37 @@
   //  this.publicAcc = null;
     this.serverCode = serverCode;
     
-    // Initialize the parser and read the queryspec
+    // read the access file from db connection
+    DocumentImpl acldoc = new DocumentImpl(conn, aclid);
+    String acl = acldoc.toString();
+    this.rev = acldoc.getRev();
+
+    // Initialize the parse
     XMLReader parser = initializeParser();
-    parser.parse(new InputSource(acl));
-
+    // parse the access file and write the info to xml_access
+    parser.parse(new InputSource(new StringReader(acl)));
+    
   }
 
-  /**
-   * Construct an instance of the AccessControlList class.
-   * It parses eml-access file and loads acl data into db connection.
-   * It is used from command line execution.
-   *
-   * @param conn the JDBC connection where acl data are loaded
-   * @param docid the Accession# of the document with the acl data
-   * @param aclfilename the name of acl file containing acl data
-   * @param user the user connected to MetaCat servlet and owns the document
-   * @param groups the groups to which user belongs
-   */
-  public AccessControlList( Connection conn, String aclid, String aclfilename,
-                           String user, String[] groups )
-                  throws SAXException, IOException, ClassNotFoundException,
-                         Exception
-  {
-    this(conn, aclid, new FileReader(new File(aclfilename).toString()), 
-         user, groups, 1);
-  }
+// NOT USED
+//  /**
+//   * Construct an instance of the AccessControlList class.
+//   * It parses eml-access file and loads acl data into db connection.
+//   * It is used from command line execution.
+//   *
+//   * @param conn the JDBC connection where acl data are loaded
+//   * @param docid the Accession# of the document with the acl data
+//   * @param aclfilename the name of acl file containing acl data
+//   * @param user the user connected to MetaCat servlet and owns the document
+//   * @param groups the groups to which user belongs
+//   */
+//  public AccessControlList( Connection conn, String aclid, String aclfilename,
+//                           String user, String[] groups )
+//                  throws SAXException, IOException, McdbException
+//  {
+//    this(conn, aclid, new FileReader(new File(aclfilename).toString()), 
+//         user, groups, 1);
+//  }
   
   /* Set up the SAX parser for reading the XML serialized ACL */
   private XMLReader initializeParser() throws SAXException 
@@ -203,13 +204,11 @@
     //delete all previously submitted permissions @ relations
     //this happens only on UPDATE of the access file
     try {
-      this.aclObjects = getACLObjects(aclid + sep + rev);
+      this.aclObjects = getACLObjects(aclid);
 
+      //delete all permissions for resources related to @aclid if any
       if ( aclid != null ) {
-        //delete all permissions for resources related to @aclid if any
         deletePermissionsForRelatedResources(aclid);
-      //  //then delete all relations with docid of @aclid if any
-      //  deleteRelations(aclid);
       }
     } catch (SQLException sqle) {
       throw new SAXException(sqle);
