fixed the User element and the form of the ApplicationData dataType.xsd definition.
[vamsas.git] / src / org / vamsas / client / simpleclient / VamsasArchive.java
index fab9ef2..39681ad 100644 (file)
@@ -7,6 +7,8 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.util.Hashtable;
 import java.util.jar.JarEntry;
@@ -14,6 +16,12 @@ import java.util.jar.JarOutputStream;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.vamsas.client.ClientHandle;
+import org.vamsas.client.IVorbaIdFactory;
+import org.vamsas.client.SessionHandle;
+import org.vamsas.client.UserHandle;
+import org.vamsas.client.VorbaIdFactory;
+import org.vamsas.client.VorbaXmlBinder;
 import org.vamsas.client.object;
 import org.vamsas.objects.core.ApplicationData;
 import org.vamsas.objects.core.VAMSAS;
@@ -97,7 +105,7 @@ public class VamsasArchive {
       this.archive = archive; // archive is written in place.
       virginArchive = true;
     }
-    this.openArchive();
+    this.openArchive(); // open archive
   }
   /**
    * name of backup of existing archive that has been updated/overwritten.
@@ -118,7 +126,7 @@ public class VamsasArchive {
       }
     }
   }
-
+  
   /**
    * called after archive is written to put file in its final place
    * TODO: FINISH ?? original should have sessionFile, and archive should also have sessionFile
@@ -126,22 +134,22 @@ public class VamsasArchive {
   private void updateOriginal() {
     if (!virginArchive) {
       // make sure original document really is backed up and then overwrite it.
-        if (odoc!=null) {
-          // try to shut the odoc reader.
-          odoc.close();
-          odoc = null;
-        }
-        // Make a backup if it isn't done already
-        if (originalBackup==null)
-          makeBackup();
-        try {
-          // copy new Archive data that was writen to a temporary file
-          odoclock.updateFrom(null, rchive);
-        }
-        catch (IOException e) {
-          log.error("Problem updating archive from temporary file! - backup in '"
-              +backupFile().getAbsolutePath()+"'",e);
-        }
+      if (odoc!=null) {
+        // try to shut the odoc reader.
+        odoc.close();
+        odoc = null;
+      }
+      // Make a backup if it isn't done already
+      if (originalBackup==null)
+        makeBackup();
+      try {
+        // copy new Archive data that was writen to a temporary file
+        odoclock.updateFrom(null, rchive);
+      }
+      catch (IOException e) {
+        log.error("Problem updating archive from temporary file! - backup in '"
+            +backupFile().getAbsolutePath()+"'",e);
+      }
     } else {
       // don't need to do anything.
     }
@@ -155,7 +163,6 @@ public class VamsasArchive {
     if (!virginArchive) {
       makeBackup();
       return ((original!=null) ? originalBackup : null);
-      
     }
     return null;
   }
@@ -174,7 +181,7 @@ public class VamsasArchive {
    */
   protected boolean isDocumentWritten() {
     if (newarchive==null)
-      log.warn("isDocumentWritten called for unopened archive.");
+      log.warn("isDocumentWritten() called for unopened archive.");
     if (entries!=null) {
       if (entries.containsKey(getDocumentJarEntry()))
         return true;
@@ -266,7 +273,7 @@ public class VamsasArchive {
    * @throws IOException
    */
   public AppDataOutputStream getAppDataStream(String appdataReference) throws IOException {
-    if (newarchive!=null)
+    if (newarchive==null)
       openArchive();
     if (addValidEntry(appdataReference)) {
       return new AppDataOutputStream(newarchive);
@@ -276,7 +283,7 @@ public class VamsasArchive {
   
   /**
    * Stops any current write to archive, and reverts to the backup if it exists.
-   * All existing locks on the original will be released.
+   * All existing locks on the original will be released. All backup files are removed.
    */
   public boolean cancelArchive() {
     if (newarchive!=null) {
@@ -306,15 +313,11 @@ public class VamsasArchive {
             return false;
           }
         }
-        // original is untouched
-        // just delete temp files
-        
-        
       }
     } else {
       log.info("cancelArchive called before archive("+original.getAbsolutePath()+") has been opened!");
     }
-    closeAndReset();
+    closeAndReset(); // tidy up and release locks.
     return true;
   }
   
@@ -420,6 +423,8 @@ public class VamsasArchive {
   /**
    * Tidies up and closes archive, removing any backups that were created.
    * NOTE: It is up to the caller to delete the original archive backup obtained from backupFile()
+   * TODO: ensure all extant AppDataReference jar entries are transferred to new Jar
+   * TODO: provide convenient mechanism for generating new unique AppDataReferences and adding them to the document
    */
   public void closeArchive() throws IOException {
     if (newarchive!=null) {
@@ -487,6 +492,22 @@ public class VamsasArchive {
     }
     return null;
   }
+  protected VorbaIdFactory vorba = null;
+  
+  /**
+   * @return Returns the current VorbaIdFactory for the archive.
+   */
+  public VorbaIdFactory getVorba() {
+    return vorba;
+  }
+
+  /**
+   * @param vorba the VorbaIdFactory to use for accessing vamsas objects.
+   */
+  public void setVorba(VorbaIdFactory vorba) {
+    this.vorba = vorba;
+  }
+
   /**
    * Access and return current vamsas Document, if it exists, or create a new one 
    * (without affecting VamsasArchive object state - so is NOT THREAD SAFE)
@@ -501,7 +522,7 @@ public class VamsasArchive {
    */
   public VamsasDocument getVamsasDocument() throws IOException, 
   org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
-    VamsasDocument doc = getOriginalVamsasDocument(this);
+    VamsasDocument doc = getOriginalVamsasDocument(this, getVorba());
     if (doc!=null)
       return doc;
     // Create a new document and return it
@@ -524,23 +545,64 @@ public class VamsasArchive {
    */
   public static VamsasDocument getOriginalVamsasDocument(VamsasArchive ths) throws IOException, 
   org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
+    return VamsasArchive.getOriginalVamsasDocument(ths, null);
+  }
+  private VorbaIdFactory makeDefaultFactory(VorbaIdFactory vorba) {
+    if (vorba==null) {
+      vorba = getVorba();
+      if (vorba==null) {
+        vorba = IdFactory.getDummyFactory("simpleclient.VamsasArchive");
+        setVorba(vorba); // save for later use
+      }
+    }
+    return vorba;
+  }
+  /**
+   * Uses VorbaXmlBinder to retrieve the VamsasDocument from the original archive referred to by ths
+   * @param ths
+   * @param vorba
+   * @return
+   * @throws IOException
+   * @throws org.exolab.castor.xml.MarshalException
+   * @throws org.exolab.castor.xml.ValidationException
+   */
+  public static VamsasDocument getOriginalVamsasDocument(VamsasArchive ths, VorbaIdFactory vorba) throws IOException, 
+  org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
     VamsasArchiveReader oReader = ths.getOriginalArchiveReader();
     if (oReader!=null) {
+      // check the factory
+      vorba = ths.makeDefaultFactory(vorba);
       if (oReader.isValid()) {
         InputStreamReader vdoc = new InputStreamReader(oReader.getVamsasDocumentStream());
-        VamsasDocument doc = VamsasDocument.unmarshal(vdoc);
-        if (doc!=null) 
-          return doc;
-      } else {
+        Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vdoc, vorba, new VamsasDocument());
+        if (unmarsh==null)
+          log.fatal("Couldn't unmarshall document!");
+        
+        object vobjs = (object) unmarsh[0];
+        if (vobjs!=null) { 
+          VamsasDocument doc=(VamsasDocument) vobjs;
+          if (doc!=null)
+            return doc;
+        }
+        log.debug("Found no VamsasDocument object in properly formatted Vamsas Archive.");
+      } else {        
         // deprecated data handler
         InputStream vxmlis = oReader.getVamsasXmlStream();
         if (vxmlis!=null) { // Might be an old vamsas file.
           BufferedInputStream ixml = new BufferedInputStream(oReader.getVamsasXmlStream());
           InputStreamReader vxml = new InputStreamReader(ixml);
-          VAMSAS root[] = new VAMSAS[1];
-          root[0] = VAMSAS.unmarshal(vxml);
-          if (root[0]!=null) {
-            log.debug("Reading old format vamsas.xml into a dummy document.");
+          Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vxml, vorba, new VAMSAS());
+          
+          if (unmarsh==null)
+            log.fatal("Couldn't unmarshall document!");
+
+          VAMSAS root[]= new VAMSAS[] { null};
+          root[0] = (VAMSAS) unmarsh[0]; 
+          
+          if (root[0]==null) {
+            log.debug("Found no VAMSAS object in VamsasXML stream.");
+          } else {
+            log.debug("Making new VamsasDocument from VamsasXML stream.");
             VamsasDocument doc = DocumentStuff.newVamsasDocument(root, 
                 ProvenanceStuff.newProvenance(
                     "org.vamsas.simpleclient.VamsasArchive", // TODO: VAMSAS: decide on 'system' operations provenance form
@@ -553,7 +615,13 @@ public class VamsasArchive {
           }
         }
       }
-    } // otherwise - there was no valid original document to read.
+    }
+    // otherwise - there was no valid original document to read.
     return null;    
   }
+  public void putVamsasDocument(VamsasDocument doc) throws IOException, 
+  org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
+    VorbaIdFactory vorba = makeDefaultFactory(getVorba());
+    VorbaXmlBinder.putVamsasDocument(getDocumentOutputStream(), vorba, doc);
+  }
 }