renamed base class for all vamsas document objects (now org.vamsas.client.Vobject)
[vamsas.git] / src / org / vamsas / test / simpleclient / ArchiveWriter.java
index 7f45d5c..1a9bae9 100644 (file)
@@ -7,6 +7,7 @@ import java.io.PrintWriter;
 import java.text.DateFormat;
 import java.util.Date;
 import java.util.Hashtable;
+import java.util.Vector;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -17,34 +18,17 @@ import org.vamsas.objects.core.Entry;
 import org.vamsas.objects.core.Provenance;
 import org.vamsas.objects.core.VAMSAS;
 import org.vamsas.objects.core.VamsasDocument;
+import org.vamsas.objects.utils.ProvenanceStuff;
 
 public class ArchiveWriter {
   
   /**
    * Test program for writing archive files.
+   * form is ArchiveWriter new/modified argive command list
    */
   
-  static Log log = LogFactory.getLog("org.vamsas.test.simpleclient.ArchiveWriter");
-  
-  /**
-   * @param action
-   *          text for action entry
-   * @return new Provenance entry for ArchiveWriter created docs.
-   */
-  public static Entry newProvenanceEntry(String user, String action) { 
-    log.debug("Adding ProvenanceEntry("+user+","+action+")");
-    Entry e = new Entry();
-    e.setAction(action);
-    e.setUser(user);
-    e.setDate(new org.exolab.castor.types.Date(new Date()));
-    return e;
-  }
-  
-  public static Provenance newProvenance() {
-    Provenance list = new Provenance();
-    list.addEntry(newProvenanceEntry("ArchiveWriter", "Created new Vamsas Document"));
-    return list;
-  }
+  static Log log = LogFactory.getLog(ArchiveWriter.class);
+    
   private static void mergeVecs(Object[] destvec, Object[] svec1, Object[] svec2) {
     int i;
     for (i=0; i<svec1.length; i++)
@@ -62,7 +46,7 @@ public class ArchiveWriter {
       if (!ht.containsKey(appdatas[i].getUrn())) {
         Hashtable aphash = new Hashtable();
         ht.put(appdatas[i].getUrn(), aphash);
-        aphash.put(appdatas[i], appdatas[i].getAppDataChoice().getDataReference());
+        aphash.put(appdatas[i], appdatas[i].getDataReference());
       } else {
         // ensure urns and references are unique
         
@@ -72,6 +56,13 @@ public class ArchiveWriter {
     }
     return ht;
   }
+  /**
+   * safely copies an appData from one archive to another.
+   * @param darc destination archive
+   * @param dest destination document Vobject
+   * @param sarc source archive reader
+   * @param entry application data to be copied from source archive
+   */
   public static void addAppDataEntry(VamsasArchive darc, VamsasDocument dest,  VamsasArchiveReader sarc, ApplicationData entry) {
     
     // check uniqueness of entry.urn amongst dest.ApplicationData[].urn
@@ -94,14 +85,26 @@ public class ArchiveWriter {
       // 
     }
   }
+  /**
+   * Copy new datasets and appdatas from one vamsas document to another.
+   * @param darc
+   * @param dest
+   * @param sarc
+   * @param source
+   * @return true if merge was successful.
+   */
   public static boolean mergeDocs(VamsasArchive darc, VamsasDocument dest,  VamsasArchiveReader sarc, VamsasDocument source) {
     log.debug("mergeDocs entered.");
     // search for appDatas in cdoc
     VAMSAS[] newr = new VAMSAS[dest.getVAMSASCount()+source.getVAMSASCount()];
     mergeVecs(newr, dest.getVAMSAS(), source.getVAMSAS());
     dest.setVAMSAS(newr);
-    // TODO: should verify that all ids really are unique in newly merged document. If not then what ?
-    
+    /** TODO: LATER: should verify that all ids really are unique in newly merged document. If not then what ?
+     *  investigate possibility of having an id translation between appDatas and the core document - 
+     *  the mapping is stored when an external application performs a merge, but when the owning 
+     *  Application accesses the Vobject, the vorba_id is updated to the new one when it writes its 
+     *  references in to its appdata again
+     */
     if (source.getApplicationDataCount()>0) {
       ApplicationData[] newdat = new ApplicationData[source.getApplicationDataCount()+dest.getApplicationDataCount()];
       ApplicationData[] sappd = source.getApplicationData();
@@ -115,8 +118,19 @@ public class ArchiveWriter {
     return true; // success    
   }
   
+  private static CommandProcessor cproc;
+  static {
+    cproc.addCommand("new", 0, "no args");
+    cproc.addCommand("add", 1, "Need another vamsas document archive filename as argument.");    
+    cproc.addCommand("repair", 0, "no args");
+    cproc.addCommand("list", 0, "no args");    
+    cproc.addCommand("monitor", 0, "no args");    
+  }
   
   public static void main(String argv[]) {
+    /**
+     * TODO: switches for setting user identities for writing to vamsas document
+     */ 
     if (argv.length<1) {
       log.fatal("Usage : <archive to create> [(commands)]");
       return;
@@ -128,29 +142,31 @@ public class ArchiveWriter {
       VamsasArchive varc = new VamsasArchive(newarch, true);
       VamsasDocument docroot;
       docroot = new VamsasDocument();
-      docroot.setProvenance(newProvenance());
+      docroot.setProvenance(ProvenanceStuff.newProvenance("ArchiveWriter", "Created new Vamsas Document"));
       while (++argpos<argv.length) {
         File archive = new File(argv[argpos]);
+        InputStream istream;
         if (archive.exists()) {
           VamsasArchiveReader vdoc = new VamsasArchiveReader(archive);
           if (vdoc.isValid()) {
-            InputStream istream = vdoc.getVamsasDocumentStream(); 
+            istream = vdoc.getVamsasDocumentStream(); 
             if (istream!=null) {
               VamsasDocument cdocroot = VamsasDocument.unmarshal(new InputStreamReader(istream));
               if (cdocroot!=null) 
                 mergeDocs(varc, docroot, vdoc, cdocroot);
-            } else {
+            } else 
+              log.warn("Unexpectedly null document stream from existing document "+archive);
+          } else {
+              // updating an oldformat stream ?
               if ((istream = vdoc.getVamsasXmlStream())!=null) {
                 // make a new vamsas document from the vamsas.xml entry
-                VAMSAS root = VAMSAS.unmarshal(new InputStreamReader(istream));
-                docroot.getProvenance().addEntry(newProvenanceEntry("user", "added vamsas.xml from "+argv[argpos-1]));
+                VAMSAS root = VAMSAS.unmarshal(new InputStreamReader(istream)); // TODO: verify only one VAMSAS element per vamsas.xml entry.
+                docroot.getProvenance().addEntry(ProvenanceStuff.newProvenanceEntry("user", "added vamsas.xml from "+argv[argpos-1]));
                 docroot.addVAMSAS(root);
               }
-            }
-            
           }
-          // Write a dummy vamsas document
-        
+        } else {
+          // Begin a new vamsas document
           PrintWriter docwriter = varc.getDocumentOutputStream();
         }
       }