[metacat-cvs] metacat/src/edu/ucsb/nceas/metacat DocumentImpl.java

Jing Tao tao at ecoinformatics.org
Wed Nov 16 13:07:52 PST 2005


tao         05/11/16 13:07:52

  Modified:    src/edu/ucsb/nceas/metacat DocumentImpl.java
  Log:
  Fixed the bug that replicate data file failed that the documents records wouldn't be deleted.
  
  Revision  Changes    Path
  1.176     +141 -87   metacat/src/edu/ucsb/nceas/metacat/DocumentImpl.java
  
  Index: DocumentImpl.java
  ===================================================================
  RCS file: /cvs/metacat/src/edu/ucsb/nceas/metacat/DocumentImpl.java,v
  retrieving revision 1.175
  retrieving revision 1.176
  diff -u -r1.175 -r1.176
  --- DocumentImpl.java	11 Nov 2005 18:01:47 -0000	1.175
  +++ DocumentImpl.java	16 Nov 2005 21:07:52 -0000	1.176
  @@ -6,9 +6,9 @@
    *    Authors: Matt Jones
    *    Release: @release@
    *
  - *   '$Author: sgarg $'
  - *     '$Date: 2005/11/11 18:01:47 $'
  - * '$Revision: 1.175 $'
  + *   '$Author: tao $'
  + *     '$Date: 2005/11/16 21:07:52 $'
  + * '$Revision: 1.176 $'
    *
    * 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
  @@ -296,26 +296,49 @@
               String accnum, String user, String[] groups, int serverCode)
               throws SQLException, AccessionNumberException, Exception
       {
  -
  -        String action = null;
  -        String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
  -        int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
  -        action = checkRevInXMLDocuments(accnum, userSpecifyRev);
  -        if (action.equals("UPDATE"))
  +        DBConnection conn = null;
  +        int serialNumber = -1;
  +        try
           {
  -            //archive the old entry
  -            if (!hasWritePermission(user, groups, accnum)) { throw new Exception(
  -                    "User " + user
  -                    + " does not have permission to update the document"
  -                    + accnum); }
  -            archiveDocRevision(docIdWithoutRev, user);  
  +            conn = DBConnectionPool
  +                .getDBConnection("DocumentImpl.registerDocumentInreplication");
  +            serialNumber = conn.getCheckOutSerialNumber();
  +            conn.setAutoCommit(false);
  +            String action = null;
  +            String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
  +            int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
  +            action = checkRevInXMLDocuments(docIdWithoutRev, userSpecifyRev);
  +            logMetacat.warn("after check rev, the action is "+action);
  +            if (action.equals("UPDATE"))
  +            {
  +                //archive the old entry
  +                if (!hasWritePermission(user, groups, accnum)) { throw new Exception(
  +                        "User " + user
  +                        + " does not have permission to update the document"
  +                        + accnum); }
  +                archiveDocRevision(docIdWithoutRev, user, conn);  
  +            }
  +            
  +            String rev = Integer.toString(userSpecifyRev);
  +            modifyRecordsInGivenTable(DOCUMENTTABLE, action,docIdWithoutRev, doctype, docname,
  +                        user, rev, serverCode, null, null, conn);
  +                        // null and null is createdate and updatedate
  +                        // null will create current time
  +            conn.commit();
  +            conn.setAutoCommit(true);
  +        }
  +        catch(Exception e)
  +        {
  +            conn.rollback();
  +            conn.setAutoCommit(true);
  +            throw e;
  +        }
  +        finally
  +        {
  +            //check in DBConnection
  +            DBConnectionPool.returnDBConnection(conn, serialNumber);
           }
           
  -        String rev = Integer.toString(userSpecifyRev);
  -        modifyRecordsInGivenTable(DOCUMENTTABLE, action,docIdWithoutRev, doctype, docname,
  -                    user, rev, serverCode, null, null);
  -                    // null and null is createdate and updatedate
  -                    // null will create current time
       }
   
       /**
  @@ -352,32 +375,54 @@
               String tableName, String createDate, String updateDate)
               throws SQLException, AccessionNumberException, Exception
       {
  -       
  -        String action = null;
  -        String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
  -        int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
  -        if (tableName.equals(DOCUMENTTABLE))
  +        DBConnection conn = null;
  +        int serialNumber = -1;
  +        try
           {
  -            action = checkRevInXMLDocuments(accnum, userSpecifyRev);
  -            if (action.equals("UPDATE"))
  +            //check out DBConnection
  +            conn = DBConnectionPool
  +                    .getDBConnection("DocumentImpl.registerDocumentInreplication");
  +            serialNumber = conn.getCheckOutSerialNumber();
  +            conn.setAutoCommit(false);
  +            String action = null;
  +            String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
  +            int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
  +            if (tableName.equals(DOCUMENTTABLE))
  +            {
  +                action = checkRevInXMLDocuments(docIdWithoutRev, userSpecifyRev);
  +                if (action.equals("UPDATE"))
  +                {
  +                        //archive the old entry
  +                        archiveDocRevision(docIdWithoutRev, user, conn);  
  +                }
  +            }
  +            else if (tableName.equals(REVISIONTABLE))
  +            {
  +                action = checkXMLRevisionTable(docIdWithoutRev, userSpecifyRev);
  +            }
  +            else
               {
  -                    //archive the old entry
  -                    archiveDocRevision(docIdWithoutRev, user);  
  +                throw new Exception("Couldn't handle this table name "+tableName);
               }
  +           
  +            String rev = Integer.toString(userSpecifyRev);
  +            modifyRecordsInGivenTable(tableName, action,docIdWithoutRev, doctype, docname,
  +                        user, rev, serverCode, createDate, updateDate, conn);
  +            conn.commit();
  +            conn.setAutoCommit(true);
           }
  -        else if (tableName.equals(REVISIONTABLE))
  +        catch(Exception e)
           {
  -            action = checkXMLRevisionTable(accnum, userSpecifyRev);
  +            conn.rollback();
  +            conn.setAutoCommit(true);
  +            throw e;
           }
  -        else
  +        finally
           {
  -            throw new Exception("Couldn't handle this table name "+tableName);
  +            //check in DBConnection
  +            DBConnectionPool.returnDBConnection(conn, serialNumber);
           }
          
  -        String rev = Integer.toString(userSpecifyRev);
  -        modifyRecordsInGivenTable(tableName, action,docIdWithoutRev, doctype, docname,
  -                    user, rev, serverCode, createDate, updateDate);
  -       
       }
       
      /*
  @@ -385,11 +430,11 @@
       */
      private static void modifyRecordsInGivenTable(String tableName, String action,
                          String docid, String doctype, String docname, String user,
  -                       String rev, int serverCode, String createDate, String updateDate) throws Exception
  +                       String rev, int serverCode, String createDate, String updateDate,
  +                       DBConnection dbconn) throws Exception
      {
  -      DBConnection dbconn = null;
  +      
         PreparedStatement pstmt = null;
  -      int serialNumber = -1;
         int revision = (new Integer(rev)).intValue();
         String sqlDateString = dbAdapter.getDateTimeFunction();
         if (createDate == null)
  @@ -411,11 +456,10 @@
         }
         try
         {
  -        dbconn = DBConnectionPool.getDBConnection(
  -          "DocumentImpl.registerDocumentInReplication");
  -        serialNumber = dbconn.getCheckOutSerialNumber();
  +        
           StringBuffer sql = new StringBuffer();
           if (action != null && action.equals("INSERT")) {
  +            
               sql.append("insert into ");
               sql.append(tableName);
               sql.append(" (docid, docname, " +
  @@ -434,6 +478,7 @@
               sql.append(updateDate).append(",");
               sql.append("'0')");
           } else if (action != null && action.equals("UPDATE")) {
  +            
               sql.append("update xml_documents set docname ='");
               sql.append(docname).append("', ");
               sql.append("user_updated='");
  @@ -451,15 +496,19 @@
           pstmt.execute();
           pstmt.close();
          
  -    } finally {
  -        try {
  -            if(pstmt != null){
  -                pstmt.close();
  -            }
  -        } finally {
  -            DBConnectionPool.returnDBConnection(dbconn, serialNumber);
  -        }
  -    }     
  +    }
  +    catch(Exception e) 
  +    {
  +     
  +        throw e;
  +    }
  +    finally 
  +    {
  +       if(pstmt != null)
  +       {
  +         pstmt.close();
  +       }
  +     }     
      }
       /**
        * This method will register a data file entry in xml_documents and save a
  @@ -505,30 +554,41 @@
           // Get server code again
           serverCode = getServerCode(docHomeServer);
   
  -        //register data file into xml_documents table
  -        registerDocumentInReplication(docname, doctype, accnum, user,
  -                serverCode, tableName, createDate, updateDate);
  +        
           //write inputstream into file system.
           File dataDirectory = new File(filePath);
  -        File newFile = new File(dataDirectory, accnum);
  -
  -        // create a buffered byte output stream
  -        // that uses a default-sized output buffer
  -        FileOutputStream fos = new FileOutputStream(newFile);
  -        BufferedOutputStream outPut = new BufferedOutputStream(fos);
  -
  -        BufferedInputStream bis = null;
  -        bis = new BufferedInputStream(input);
  -        byte[] buf = new byte[4 * 1024]; // 4K buffer
  -        int b = bis.read(buf);
  -
  -        while (b != -1) {
  -            outPut.write(buf, 0, b);
  -            b = bis.read(buf);
  -        }
  -        bis.close();
  -        outPut.close();
  -        fos.close();
  +        File newFile = null;
  +        try
  +        {
  +            newFile = new File(dataDirectory, accnum);
  +    
  +            // create a buffered byte output stream
  +            // that uses a default-sized output buffer
  +            FileOutputStream fos = new FileOutputStream(newFile);
  +            BufferedOutputStream outPut = new BufferedOutputStream(fos);
  +    
  +            BufferedInputStream bis = null;
  +            bis = new BufferedInputStream(input);
  +            byte[] buf = new byte[4 * 1024]; // 4K buffer
  +            int b = bis.read(buf);
  +    
  +            while (b != -1) {
  +                outPut.write(buf, 0, b);
  +                b = bis.read(buf);
  +            }
  +            bis.close();
  +            outPut.close();
  +            fos.close();
  +            
  +            //register data file into xml_documents table
  +            registerDocumentInReplication(docname, doctype, accnum, user,
  +                    serverCode, tableName, createDate, updateDate);
  +        }
  +        catch (Exception ee)
  +        {
  +            newFile.delete();
  +            throw ee;
  +        }
           
           // Force replicate data file
           if (!timedReplication)
  @@ -546,10 +606,11 @@
        private static String checkRevInXMLDocuments(String docid, int userSpecifyRev) throws Exception
        {
           String action = null;
  -        logMetacat.info("The user specifyRev: " + userSpecifyRev);
  +        logMetacat.warn("The docid without rev is "+docid);
  +        logMetacat.warn("The user specifyRev: " + userSpecifyRev);
           // Revision for this docid in current database
           int revInDataBase =DBUtil.getLatestRevisionInDocumentTable(docid);
  -        logMetacat.info("The rev in data base: " + revInDataBase);
  +        logMetacat.warn("The rev in data base: " + revInDataBase);
           // String to store the revision
           String rev = null;
   
  @@ -2412,12 +2473,13 @@
           logMetacat.info("user in replication" + user);
           // Docid without revision
           String docid = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
  +        logMetacat.warn("The docid without rev is " + docid);
           // Revision specified by user (int)
           int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
  -        logMetacat.info("The user specifyRev: " + userSpecifyRev);
  +        logMetacat.warn("The user specifyRev: " + userSpecifyRev);
           // Revision for this docid in current database
           int revInDataBase = DBUtil.getLatestRevisionInDocumentTable(docid);
  -        logMetacat.info("The rev in data base: " + revInDataBase);
  +        logMetacat.warn("The rev in data base: " + revInDataBase);
           // String to store the revision
           String rev = (new Integer(userSpecifyRev)).toString();
   
  @@ -3147,11 +3209,9 @@
       }
   
       /** Save a document entry in the xml_revisions table */
  -    private static void archiveDocRevision(String docid, String user) throws Exception
  +    private static void archiveDocRevision(String docid, String user, DBConnection conn) throws Exception
       {
           String sysdate = dbAdapter.getDateTimeFunction();
  -        DBConnection conn = null;
  -        int serialNumber = -1;
           PreparedStatement pstmt = null;
   
           // create a record in xml_revisions table
  @@ -3159,9 +3219,6 @@
   
           try {
               //check out DBConnection
  -            conn = DBConnectionPool
  -                    .getDBConnection("DocumentImpl.archiveDocRevision");
  -            serialNumber = conn.getCheckOutSerialNumber();
               pstmt = conn.prepareStatement("INSERT INTO xml_revisions "
                       + "(docid, rootnodeid, docname, doctype, "
                       + "user_owner, user_updated, date_created, date_updated, "
  @@ -3190,10 +3247,7 @@
                           "Error in DocumnetImpl.archiveDocRevision: "
                                   + ee.getMessage());
                   throw ee;
  -            } finally {
  -                //check in DBConnection
  -                DBConnectionPool.returnDBConnection(conn, serialNumber);
  -            }
  +            } 
           }
       }
   
  
  
  


More information about the Metacat-cvs mailing list