Index: lib/d1_common_java-0.5.0.jar
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: lib/d1_libclient_java-0.5.2.jar
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/edu/ucsb/nceas/metacat/dataone/CrudService.java
===================================================================
--- src/edu/ucsb/nceas/metacat/dataone/CrudService.java	(revision 5919)
+++ src/edu/ucsb/nceas/metacat/dataone/CrudService.java	(revision 5920)
@@ -30,20 +30,25 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.PrintWriter;
-import java.io.StringBufferInputStream;
-import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.sql.SQLException;
-import java.util.*;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.TimeZone;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.Vector;
 
-import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.log4j.Logger;
+import org.dataone.service.exceptions.BaseException;
 import org.dataone.service.exceptions.IdentifierNotUnique;
 import org.dataone.service.exceptions.InsufficientResources;
 import org.dataone.service.exceptions.InvalidRequest;
@@ -55,7 +60,20 @@
 import org.dataone.service.exceptions.ServiceFailure;
 import org.dataone.service.exceptions.UnsupportedType;
 import org.dataone.service.mn.MemberNodeCrud;
-import org.dataone.service.types.*;
+import org.dataone.service.types.AuthToken;
+import org.dataone.service.types.Checksum;
+import org.dataone.service.types.ChecksumAlgorithm;
+import org.dataone.service.types.DescribeResponse;
+import org.dataone.service.types.Event;
+import org.dataone.service.types.Identifier;
+import org.dataone.service.types.Log;
+import org.dataone.service.types.LogEntry;
+import org.dataone.service.types.NodeReference;
+import org.dataone.service.types.ObjectFormat;
+import org.dataone.service.types.ObjectList;
+import org.dataone.service.types.Principal;
+import org.dataone.service.types.SystemMetadata;
+import org.dataone.service.types.util.ServiceTypeUtil;
 import org.jibx.runtime.BindingDirectory;
 import org.jibx.runtime.IBindingFactory;
 import org.jibx.runtime.IMarshallingContext;
@@ -62,16 +80,8 @@
 import org.jibx.runtime.IUnmarshallingContext;
 import org.jibx.runtime.JiBXException;
 
-import org.dataone.service.types.Identifier;
-import org.dataone.service.types.util.ServiceTypeUtil;
-
-import com.gc.iotools.stream.is.InputStreamFromOutputStream;
-
 import edu.ucsb.nceas.metacat.AccessionNumber;
 import edu.ucsb.nceas.metacat.AccessionNumberException;
-import edu.ucsb.nceas.metacat.MetacatResultSet;
-import edu.ucsb.nceas.metacat.MetacatResultSet.Document;
-import edu.ucsb.nceas.metacat.DBQuery;
 import edu.ucsb.nceas.metacat.DocumentImpl;
 import edu.ucsb.nceas.metacat.EventLog;
 import edu.ucsb.nceas.metacat.IdentifierManager;
@@ -230,43 +240,50 @@
      * documents in the object store.  To work correctly, this should probably
      * be an adminstrative credential.
      */
-    public void generateMissingSystemMetadata(AuthToken token)
-    {
+    public void generateMissingSystemMetadata(AuthToken token) {
         IdentifierManager im = IdentifierManager.getInstance();
         //get the list of ids with no SM
-        List<String> l = im.getLocalIdsWithNoSystemMetadata();
-        for(int i=0; i<l.size(); i++)
-        { //for each id, add a system metadata doc
-            String localId = l.get(i);
-            System.out.println("Creating SystemMetadata for localId " + localId);
-            //get the document
-            try
-            {
-                //generate required system metadata fields from the document
-                SystemMetadata sm = createSystemMetadata(localId, token);
-                //insert the systemmetadata object
-                SessionData sessionData = getSessionData(token);
-                String smlocalid = insertSystemMetadata(sm, sessionData);
-                System.out.println("setting access on SM doc with localid " + smlocalid);
-                handler.setAccess(metacatUrl, sessionData.getUserName(), smlocalid,
-                        "public", "4", "allow", "allowFirst");
-                
-                String username = "public";
-                if(sessionData != null)
-                {
-                    username = sessionData.getUserName();
-                }
-                EventLog.getInstance().log(metacatUrl,
-                        username, localId, "generateMissingSystemMetadata");
+        List<String> idList = im.getLocalIdsWithNoSystemMetadata();
+        for (String localId : idList) { 
+            //for each id, add a system metadata doc
+            generateMissingSystemMetadata(token, localId);
+        }
+        logCrud.info("generateMissingSystemMetadata(token)");
+    }
+    
+    /**
+     * Generate SystemMetadata for a particular object with identifier localId.
+     * This is a utility method for migration of existing objects 
+     * to DataONE where SystemMetadata is required for all objects.
+     * 
+     * @param token an authtoken with appropriate permissions to read all 
+     *        documents in the object store.  To work correctly, this should
+     *        be an adminstrative credential.
+     * @param localId the identifier of the object to be processed
+     */
+    public void generateMissingSystemMetadata(AuthToken token, String localId) {
+        System.out.println("Creating SystemMetadata for localId " + localId);
+        try {
+            //generate required system metadata fields from the document
+            SystemMetadata sm = createSystemMetadata(localId, token);
+            
+            //insert the systemmetadata object
+            SessionData sessionData = getSessionData(token);
+            String smlocalid = insertSystemMetadata(sm, sessionData);
+            System.out.println("setting access on SM doc with localid " + smlocalid);
+            handler.setAccess(metacatUrl, sessionData.getUserName(), smlocalid, "public", "4", "allow", "allowFirst");
+
+            String username = "public";
+            if (sessionData != null) {
+                username = sessionData.getUserName();
             }
-            catch(Exception e)
-            {
-                e.printStackTrace();
-                System.out.println("Exception generating missing system metadata: " + e.getMessage());
-                logMetacat.error("Could not generate missing system metadata: " + e.getMessage());
-            }
+            EventLog.getInstance().log(metacatUrl, username, localId, "generateMissingSystemMetadata");
+        } catch (Exception e) { // TODO: Please don't catch Exception -- it masks bad things
+            e.printStackTrace();
+            System.out.println("Exception generating missing system metadata: " + e.getMessage());
+            logMetacat.error("Could not generate missing system metadata: " + e.getMessage());
         }
-        logCrud.info("generateMissingSystemMetadata");
+        logCrud.info("generateMissingSystemMetadata(token, localId)");
     }
     
     /**
@@ -1847,9 +1864,10 @@
      * return an MD5 checksum for the stream
      * @param is
      * @return
+     * @throws IOException 
+     * @throws NoSuchAlgorithmException 
      */
-    public static String checksum(InputStream is)
-        throws Exception
+    public static String checksum(InputStream is) throws NoSuchAlgorithmException, IOException
     {
         return checksum(is, "MD5");
     }
@@ -1856,9 +1874,10 @@
     
     /**
      * produce a checksum for item using the given algorithm
+     * @throws IOException 
+     * @throws NoSuchAlgorithmException 
      */
-    public static String checksum(InputStream is, String algorithm)
-      throws Exception
+    public static String checksum(InputStream is, String algorithm) throws NoSuchAlgorithmException, IOException
     {        
         return ServiceTypeUtil.checksum(is, ChecksumAlgorithm.convert(algorithm)).getValue();
     }
@@ -1904,23 +1923,29 @@
     
     /**
      * create system metadata with a specified id, doc and format
+     * @throws McdbDocNotFoundException 
+     * @throws SQLException
+     * @throws AccessionNumberException 
+     * @throws NumberFormatException 
+     * @throws IOException 
+     * @throws NoSuchAlgorithmException 
+     * @throws PropertyNotFoundException 
      */
-    private SystemMetadata createSystemMetadata(String localId, AuthToken token)
-      throws Exception
-    {
+    private SystemMetadata createSystemMetadata(String localId, AuthToken token) 
+        throws McdbDocNotFoundException, NumberFormatException, AccessionNumberException, 
+        SQLException, NoSuchAlgorithmException, IOException, PropertyNotFoundException, BaseException {
+        
         IdentifierManager im = IdentifierManager.getInstance();
         Hashtable<String, Object> docInfo = im.getDocumentInfo(localId);
-        
+
         //get the document text
         int rev = im.getLatestRevForLocalId(localId);
         Identifier identifier = new Identifier();
-        try
-        {
+        try {
             identifier.setValue(im.getGUID(localId, rev));
-        }
-        catch(McdbDocNotFoundException mcdbe)
-        { //we're creating a new SM doc for a doc that is not in the identifier table
-          //so we need to add it to
+        } catch (McdbDocNotFoundException mcdbe) { 
+            //we're creating a new SM doc for a doc that is not in the identifier table                                       
+            //so we need to add it to
             System.out.println("No guid in the identifier table.  adding it for " + localId);
             im.createMapping(localId, localId);
             System.out.println("mapping created for " + localId);
@@ -1927,30 +1952,26 @@
             AccessionNumber accNum = new AccessionNumber(localId, "NONE");
             identifier.setValue(im.getGUID(accNum.getDocid(), rev));
         }
-        
+
         System.out.println("creating system metadata for guid " + identifier.getValue());
         InputStream is = this.get(token, identifier);
+        SystemMetadata sm = new SystemMetadata();
         
-        SystemMetadata sm = new SystemMetadata();
         //set the id
         sm.setIdentifier(identifier);
-        
+
         //set the object format
-        String doctype = (String)docInfo.get("doctype");
-        ObjectFormat format = ObjectFormat.convert((String)docInfo.get("doctype"));
-        if(format == null)
-        {
-            if(doctype.trim().equals("BIN"))
-            {
+        String doctype = (String) docInfo.get("doctype");
+        ObjectFormat format = ObjectFormat.convert((String) docInfo.get("doctype"));
+        if (format == null) {
+            if (doctype.trim().equals("BIN")) {
                 format = ObjectFormat.OCTET_STREAM;
-            }
-            else
-            {
+            } else {
                 format = ObjectFormat.convert("text/plain");
             }
         }
         sm.setObjectFormat(format);
-        
+
         //create the checksum
         String checksumS = checksum(is);
         ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
@@ -1958,25 +1979,22 @@
         checksum.setValue(checksumS);
         checksum.setAlgorithm(ca);
         sm.setChecksum(checksum);
-        
+
         //set the size
         is = this.get(token, identifier);
         sm.setSize(sizeOfStream(is));
-        
+
         //submitter
         Principal p = new Principal();
-        p.setValue((String)docInfo.get("user_owner"));
+        p.setValue((String) docInfo.get("user_owner"));
         sm.setSubmitter(p);
         sm.setRightsHolder(p);
-        try
-        {
-            Date dateCreated = parseMetacatDate((String)docInfo.get("date_created"));
+        try {
+            Date dateCreated = parseMetacatDate((String) docInfo.get("date_created"));
             sm.setDateUploaded(dateCreated);
-            Date dateUpdated = parseMetacatDate((String)docInfo.get("date_updated"));
+            Date dateUpdated = parseMetacatDate((String) docInfo.get("date_updated"));
             sm.setDateSysMetadataModified(dateUpdated);
-        }
-        catch(Exception e)
-        {
+        } catch (Exception e) {
             System.out.println("POSSIBLE ERROR: couldn't parse a date: " + e.getMessage());
             Date dateCreated = new Date();
             sm.setDateUploaded(dateCreated);
@@ -1984,10 +2002,12 @@
             sm.setDateSysMetadataModified(dateUpdated);
         }
         NodeReference nr = new NodeReference();
-        //TODO: this should be set to be something more meaningful once the registry is up
-        nr.setValue("metacat");
+        nr.setValue(PropertyService.getProperty("dataone.memberNodeId"));
         sm.setOriginMemberNode(nr);
         sm.setAuthoritativeMemberNode(nr);
+        
+        // TODO: Need to set describes/describedBy
+        
         return sm;
     }
     
