Index: src/edu/ucsb/nceas/metacat/DocumentImpl.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 2701)
+++ src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 2702)
@@ -1911,14 +1911,33 @@
             if (action.equals("INSERT")) {
                 //AccessionNumber ac = new AccessionNumber();
                 //this.docid = ac.generate(docid, "INSERT");
-                
-                pstmt = connection
+                String sql = null;
+                if (catalogid != null )
+                {
+                	sql = "INSERT INTO xml_documents "
+                        + "(docid, rootnodeid, docname, doctype, user_owner, "
+                        + "user_updated, date_created, date_updated, "
+                        + "public_access, server_location, rev, catalog_id) "
+                        + "VALUES (?, ?, ?, ?, ?, ?, " + createDate + ", "
+                        + updateDate + ", ?, ?, ?, ?)";
+                }
+                else
+                {
+                	sql = "INSERT INTO xml_documents "
+                        + "(docid, rootnodeid, docname, doctype, user_owner, "
+                        + "user_updated, date_created, date_updated, "
+                        + "public_access, server_location, rev) "
+                        + "VALUES (?, ?, ?, ?, ?, ?, " + createDate + ", "
+                        + updateDate + ", ?, ?, ?)";
+                }
+                /*pstmt = connection
                         .prepareStatement("INSERT INTO xml_documents "
                         + "(docid, rootnodeid, docname, doctype, user_owner, "
                         + "user_updated, date_created, date_updated, "
                         + "public_access, catalog_id, server_location, rev) "
                         + "VALUES (?, ?, ?, ?, ?, ?, " + createDate + ", "
-                        + updateDate + ", ?, ?, ?, ?)");
+                        + updateDate + ", ?, ?, ?, ?)");*/
+                pstmt = connection.prepareStatement(sql);
                 // Increase dbconnection usage count
                 connection.increaseUsageCount(1);
                 
@@ -1942,10 +1961,15 @@
                  * pub.toUpperCase().equals("NO") || pub.equals("0") ) {
                  * pstmt.setInt(7, 0); }
                  */
-                pstmt.setInt(8, (new Integer(catalogid)).intValue());
-                pstmt.setInt(9, serverCode);
-                pstmt.setInt(10, rev);
+                pstmt.setInt(8, serverCode);
+                pstmt.setInt(9, rev);
+               
+                if (catalogid != null)
+                {
+                  pstmt.setInt(10, (new Integer(catalogid)).intValue());
+                }
                 
+                
             } else if (action.equals("UPDATE")) {
                 int thisrev = DBUtil.getLatestRevisionInDocumentTable(docid);
                 logMetacat.info("this revsion is: " + thisrev);
@@ -1983,7 +2007,29 @@
                 }
 
                 // Update the new document to reflect the new node tree
-                pstmt = connection
+                String updateSql = null;
+                if (catalogid != null)
+                {
+                	updateSql = "UPDATE xml_documents "
+                        + "SET rootnodeid = ?, docname = ?, doctype = ?, "
+                        + "user_updated = ?, date_updated = "
+                        + updateDate
+                        + ", "
+                        + "server_location = ?, rev = ?, public_access = ?, "
+                        + "catalog_id = ? "
+                        + "WHERE docid = ?";
+                }
+                else
+                {
+                	updateSql = "UPDATE xml_documents "
+                        + "SET rootnodeid = ?, docname = ?, doctype = ?, "
+                        + "user_updated = ?, date_updated = "
+                        + updateDate
+                        + ", "
+                        + "server_location = ?, rev = ?, public_access = ? "
+                        + "WHERE docid = ?";
+                }
+                /*pstmt = connection
                         .prepareStatement("UPDATE xml_documents "
                         + "SET rootnodeid = ?, docname = ?, doctype = ?, "
                         + "user_updated = ?, date_updated = "
@@ -1991,8 +2037,9 @@
                         + ", "
                         + "server_location = ?, rev = ?, public_access = ?, "
                         + "catalog_id = ? "
-                        + "WHERE docid = ?");
+                        + "WHERE docid = ?");*/
                 // Increase dbconnection usage count
+                pstmt = connection.prepareStatement(updateSql);
                 connection.increaseUsageCount(1);
                 // Bind the values to the query
                 pstmt.setLong(1, rootnodeid);
@@ -2008,8 +2055,15 @@
                  * .setInt(7, 1); } else if ( pub.toUpperCase().equals("NO") ||
                  * pub.equals("0") ) { pstmt.setInt(7, 0); }
                  */
-                pstmt.setInt(8, (new Integer(catalogid)).intValue());
-                pstmt.setString(9, this.docid);
+                if (catalogid != null)
+                {
+                  pstmt.setInt(8, (new Integer(catalogid)).intValue());
+                  pstmt.setString(9, this.docid);
+                }
+                else
+                {
+                  pstmt.setString(8, this.docid);
+                }
 
             } else {
                 System.err.println("Action not supported: " + action);
