Index: src/edu/ucsb/nceas/metacat/DocumentImpl.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 1574)
+++ src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 1575)
@@ -1675,9 +1675,9 @@
           conn.setAutoCommit(true);
           throw e;
         }
-                
+        // run write into relational db and access db       
+        RunRelationHandlerAndAccessHandler(accnum, user, groups, serverCode);
         
-        
         // Force replication the docid
         ForceReplicationHandler frh = new ForceReplicationHandler
                                                           (accnum, true, null);
@@ -1737,6 +1737,9 @@
       throw e;
     }
     
+    // run write into relational db and access db       
+    RunRelationHandlerAndAccessHandler(accnum, user, groups, serverCode);
+    
     // Force replicate out the new document to each server in our server list.
     // Start the thread to replicate this new document out to the other servers
     // true mean it is xml document
@@ -1852,6 +1855,8 @@
       throw e;
     }
     
+    // run write into relational db and access db       
+    RunRelationHandlerAndAccessHandler(accnum, user, groups, serverCode);
     //Force replication to other server
     ForceReplicationHandler forceReplication = new ForceReplicationHandler
                                   (accnum, action, true, notifyServer);
@@ -1859,8 +1864,138 @@
 
     return(accnum);
   }
-
   
+  /* Running write record to xml_relation and xml_access*/
+  private static void RunRelationHandlerAndAccessHandler(String accnumber, 
+                                                  String userName, 
+                                                  String[]group, int servercode) 
+                                                   throws Exception
+  {
+    DBConnection dbconn = null;
+    int serialNumber = -1;
+    PreparedStatement pstmt =null;
+    String documenttype = getDocTypeFromDBForCurrentDocument(accnumber);
+    try
+    {
+      String packagedoctype = MetaCatUtil.getOption("packagedoctype");
+      Vector packagedoctypes = new Vector();
+      packagedoctypes = MetaCatUtil.getOptionList(packagedoctype);
+      String docIdWithoutRev = MetaCatUtil.getDocIdFromString(accnumber);
+      if (documenttype != null && packagedoctypes.contains(documenttype) )
+      {
+        dbconn=DBConnectionPool.
+           getDBConnection("DocumentImpl.RunRelationHandelerAndAccessHandeler");
+        serialNumber=dbconn.getCheckOutSerialNumber();
+        dbconn.setAutoCommit(false);
+        // write the package info to xml_relation table
+        RelationHandler rth = new RelationHandler(docIdWithoutRev, dbconn);
+        dbconn.commit();
+        // from the relations get the access file id for that package
+        String aclid = rth.getAccessFileID(docIdWithoutRev);
+        // if there are access file, write ACL for that package
+        if ( aclid != null ) 
+        {
+          runAccessControlList(dbconn, aclid, userName, group, servercode);
+        }
+        dbconn.commit();
+        dbconn.setAutoCommit(true);
+      }
+        // if it is an access file
+      else if ( documenttype != null && MetaCatUtil.getOptionList(
+                 MetaCatUtil.getOption("accessdoctype")).contains(documenttype))
+      {
+        dbconn=DBConnectionPool.
+           getDBConnection("DocumentImpl.RunRelationHandelerAndAccessHandeler");
+        serialNumber=dbconn.getCheckOutSerialNumber();
+        dbconn.setAutoCommit(false);
+        // write ACL for the package
+        runAccessControlList(dbconn, docIdWithoutRev, 
+                             userName, group, servercode);
+        dbconn.commit();
+        dbconn.setAutoCommit(true);
+        
+      }
+      
+    } 
+    catch (Exception e) 
+    {
+      if( dbconn != null)
+      {
+        dbconn.rollback();
+        dbconn.setAutoCommit(true);
+      }
+      MetaCatUtil.debugMessage("Error in RunRelationHandlerAndAccessHandler " +
+                                e.getMessage(), 30);
+      throw e;
+    }
+    finally
+    {
+      if (dbconn != null)
+      {
+        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
+      }
+    }//
+  }
+  
+  // It runs in xmlIndex thread. It writes ACL for a package.
+  private static void runAccessControlList (DBConnection conn, String aclid, 
+                                    String users, String[]group, int servercode)
+                                                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, users, group, servercode);
+   
+  }
+  
+  /* Method get document type from db*/
+  private static String getDocTypeFromDBForCurrentDocument(String accnumber)
+                                                  throws SQLException
+  {
+    String docoumentType = null;
+    String docid = null;
+    PreparedStatement pstate = null;
+    ResultSet rs = null;
+    String sql = "SELECT doctype FROM xml_documents where docid = ?";
+    DBConnection dbConnection = null;
+    int serialNumber = -1;
+    try
+    {
+      //get rid of revision number
+      docid = MetaCatUtil.getDocIdFromString(accnumber);
+      dbConnection=DBConnectionPool.
+           getDBConnection("DocumentImpl.getDocTypeFromDBForCurrentDoc");
+      serialNumber=dbConnection.getCheckOutSerialNumber();
+      pstate = dbConnection.prepareStatement(sql);
+      //bind variable
+      pstate.setString(1, docid);
+      //excute query
+      pstate.execute();
+      //handle resultset
+      rs = pstate.getResultSet();
+      if (rs.next())
+      {
+        docoumentType = rs.getString(1);
+      }
+      rs.close();
+      pstate.close();
+    }//try
+    catch (SQLException e)
+    {
+      MetaCatUtil.debugMessage("error in DocumentImpl."+
+                      "getDocTypeFromDBForCurrentDocument "+e.getMessage(), 30);
+      throw e;
+    }//catch
+    finally
+    {
+      pstate.close();
+      DBConnectionPool.returnDBConnection(dbConnection, serialNumber);
+    }//
+    MetaCatUtil.debugMessage("The current doctype from db is: "+
+                              docoumentType, 35);
+    return docoumentType;
+  }
   /**
    * Delete an XML file from the database (actually, just make it a revision
    * in the xml_revisions table)
