Index: src/edu/ucsb/nceas/metacat/MetacatReplication.java
===================================================================
--- src/edu/ucsb/nceas/metacat/MetacatReplication.java	(revision 1030)
+++ src/edu/ucsb/nceas/metacat/MetacatReplication.java	(revision 1031)
@@ -514,7 +514,7 @@
       String docType = (String)docinfoHash.get("doctype");
      
       
-      conn = util.openDBConnection();
+      conn = util.getConnection();
       
       //if action is delete, we don't delete the data file. Just archieve
       //the xml_documents
@@ -522,83 +522,23 @@
       {
         DocumentImpl.delete(conn,docid,user,null);
       }
-      else if (dbaction.equals("insert"))
+      //To data file insert or update is same
+      else if (dbaction.equals("insert")||dbaction.equals("update"))
       {
-        //register data information into xml_documents table
-        DocumentImpl.registerDocument(docName,docType,docid,user,serverCode);
-      
         //Get data file and store it into local file system.
         // sending back readdata request to server
         URL url = new URL("https://" + server + "?server="
                 +util.getLocalReplicationServerName()
                 +"&action=readdata&docid=" + docid);
-        //Create file to store the data.
-        String datafilepath = util.getOption("datafilepath");
-        File dataDirectory = new File(datafilepath);
-        //dataDirectory.mkdirs();
-        File newFile = new File(dataDirectory, docid); 
-       
-        // 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(url.openStream());
-        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();
-      
+        String datafilePath = util.getOption("datafilepath");
+        //register data file into xml_documents table and wite data file
+        //into file system
+        DocumentImpl.writeDataFile(url.openStream(), datafilePath, docName,
+                              docType, docid, user,serverCode);
      }
-     else if (dbaction.equals("update"))
-     {
-        //archieve old entry
-        DocumentImpl.unRegisterDocument(conn,docid,user,null);
-        //register new one
-        DocumentImpl.registerDocument(docName,docType,docid,user,serverCode);
-        // sending back readdata request to server
-        URL url = new URL("https://" + server + "?server="
-                +util.getLocalReplicationServerName()
-                +"&action=readdata&docid=" + docid);
-        //Create file to store the data.
-        String datafilepath = util.getOption("datafilepath");
-        File dataDirectory = new File(datafilepath);
-        //dataDirectory.mkdirs();
-        File newFile = new File(dataDirectory, docid);
-        
-        // 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(url.openStream());
-        byte[] buf = new byte[4 * 1024]; // 4K buffer
-        int b = bis.read(buf);
-        System.out.println("before the while loop");
-        while (b != -1) 
-        {
-          outPut.write(buf, 0, b);
-          b = bis.read(buf);
-        }
-        bis.close();
-	      outPut.close();
-	      fos.close();
-        
-      
-    }
-    conn.close();
     
+    util.returnConnection(conn);
+    
     MetacatReplication.replLog("document " + docid + " added to DB with " +
                                  "action " + dbaction);
     } catch(Exception e) 
