show vamsasId for aligments.
[vamsas.git] / src / org / vamsas / test / simpleclient / ClientDoc.java
index d5b0d42..da05e00 100644 (file)
@@ -1,9 +1,12 @@
 package org.vamsas.test.simpleclient;
 
+import java.util.Hashtable;
 import java.util.Vector;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.vamsas.client.IClientAppdata;
+import org.vamsas.client.Vobject;
 import org.vamsas.client.VorbaId;
 import org.vamsas.client.simpleclient.ClientDocument;
 import org.vamsas.client.simpleclient.VamsasArchive;
@@ -14,7 +17,13 @@ import org.vamsas.objects.core.VamsasDocument;
 import org.vamsas.objects.utils.ProvenanceStuff;
 
 // simple holder to pass to client.
-public class ClientDoc {
+public class ClientDoc extends org.vamsas.client.ClientDocument {
+  /* (non-Javadoc)
+   * @see org.vamsas.client.IClientDocument#getClientAppdata()
+   */
+  public IClientAppdata getClientAppdata() {
+    throw new Error("Appdata access is not implemented in the test.simpleclient.ClientDoc instance."); // TODO Auto-generated method stub
+  }
   protected boolean isModified=false;
   private Log log = LogFactory.getLog(ClientDoc.class);
   protected VamsasDocument doc;
@@ -22,19 +31,23 @@ public class ClientDoc {
   protected VamsasArchive iohandler=null;
   protected VamsasArchiveReader reader=null;
   private String user=null;
+  private String app=null;
   
   /**
    * @param doc
    * @param iohandler
    * @param reader
+   * @param app
    * @param user
    */
-  public ClientDoc(VamsasDocument doc, VamsasArchive iohandler, VamsasArchiveReader reader, String user) {
-    super();
+  public ClientDoc(VamsasDocument doc, VamsasArchive iohandler, VamsasArchiveReader reader, String app, String user, Hashtable objrefs) {
+    super(objrefs, (iohandler!=null) ? iohandler.getVorba() : null);
     this.doc = doc;
     this.iohandler = iohandler;
     this.reader = reader;
+    this.app = app;
     this.user = user;
+    this.objrefs = objrefs;
     _VamsasRoots = doc.getVAMSAS();
   }
   // AppDataOutputStream appd;
@@ -44,7 +57,7 @@ public class ClientDoc {
    */
   protected Entry getProvenanceEntry(String action) {
     // VAMSAS: modify schema to allow referencing of user field (plus other issues, ClientUrn field, machine readable action, input parameters, additional data generated notes
-    Entry prov = ProvenanceStuff.newProvenanceEntry(user, action);
+    Entry prov = ProvenanceStuff.newProvenanceEntry(app, user, action);
     return prov;
   }
   public VAMSAS[] getVamsasRoots() {
@@ -203,14 +216,140 @@ public class ClientDoc {
   public VamsasArchiveReader getReader() {
     return reader;
   }
+  private void _finalize() {
+    log.debug("finalizing clientDoc");
+    if (doc!=null) {
+      doc = null;
+    }
+    if (_VamsasRoots!=null) {
+      for (int i=0; i<_VamsasRoots.length; i++)
+        _VamsasRoots[i]=null;
+      _VamsasRoots=null;
+       
+    }
+      
+    if (reader!=null) {
+      log.debug("Closing and removing reader reference");
+      reader.close();
+      reader=null;
+    }
+    if (iohandler!=null) {
+      log.debug("Removing ioHandler reference.");
+      iohandler.cancelArchive();
+      iohandler=null;
+    }
+  }
   protected void finalize() throws Throwable {
+    _finalize();
     super.finalize();
-    //if (reader!=null)
-    //  reader.close();
-    //reader=null;
-//    if (iohandler!=null) {
-//      iohandler.cancelArchive(); // otherwise the original may be overwritten.
-//    }
+  }
+  private java.util.Hashtable objrefs=null;
+  
+  public VorbaId[] registerObjects(Vobject[] unregistered) {
+    if (doc==null) {
+      log.warn("registerObjects[] called on null document.");
+      return null;
+    }
+    if (objrefs==null) {
+      log.warn("registerObjects[] called for null objrefs hasharray.");
+      return null;
+    }
+    if (unregistered!=null) {
+      VorbaId ids[] = new VorbaId[unregistered.length];
+      for (int i=0,k=unregistered.length; i<k; i++)
+        if (unregistered[i]!=null) {
+          log.warn("Null Vobject passed to registerObject[] at position "+i);
+          return null;
+        } else {
+          ids[i]=registerObject(unregistered[i]);
+        }
+      log.debug("Registered "+unregistered.length+" objects - total of "+objrefs.size()+" ids.");
+      return ids;
+    }
+    return null;
+  }
+  /* (non-Javadoc)
+   * @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.Vobject)
+   */
+  public VorbaId registerObject(Vobject unregistered) {
+    if (doc==null) {
+      log.warn("registerObjects called on null document.");
+      return null;
+    }
+    if (objrefs==null) {
+      log.warn("registerObjects called for null objrefs hasharray.");
+      return null;
+    }
+    if (iohandler==null) {
+      log.warn("registerObjects called for read only document.");
+      return null;
+    }
+    
+    if (unregistered!=null) {
+      VorbaId id = this._registerObject(unregistered);
+      log.debug("Registered object - total of "+objrefs.size()+" ids.");
+      return id;
+    }
+    log.warn("Null Vobject passed to registerObject.");
+    return null;
+  }
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.vamsas.client.IClientDocument#getObject(org.vamsas.client.VorbaId)
+   */
+  public Vobject getObject(VorbaId id) {
+    if (objrefs==null) {
+      log.debug("getObject called on null objrefs list.");
+      return null;
+    }
+    if (objrefs.containsKey(id.getId()))
+      return (Vobject) objrefs.get(id.getId());
+    log.debug("Returning null Vobject reference for id "+id.getId());
+    return null;
+  }
+  
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.vamsas.client.IClientDocument#getObjects(org.vamsas.client.VorbaId[])
+   */
+  public Vobject[] getObjects(VorbaId[] ids) {
+    if (objrefs==null) {
+      log.debug("getObject[]  called on null objrefs list.");
+      return null;
+    }
+    Vobject[] vo = new Vobject[ids.length];
+    for (int i=0,j=ids.length; i<j;i++) 
+      if (objrefs.containsKey(ids[i]))
+        vo[i] = (Vobject) objrefs.get(ids[i]);
+      else
+        log.debug("Returning null Vobject reference for id "+ids[i].getId());
+    return vo;
+  }
+  protected void updateDocumentRoots() {
+    if (doc==null) {
+      log.error("updateDocumentRoots called on null document. Probably an implementation error.");
+      return;
+    }
+    if (isModified) {
+      if (_VamsasRoots!=null) {
+        doc.setVAMSAS(_VamsasRoots);
+        _VamsasRoots=null;
+      }
+    }
+  }
+  /**
+   * tell vamsas client to close the document and reset the object. Once closed, nothing can be done with the object.
+   *
+   */
+  public void closeDoc() {
+    if (doc!=null) {
+      log.debug("Closing open document.");
+      _finalize();
+    } else {
+      log.warn("Ignoring closeDoc on invalid document.");
+    }
   }
   
 }
\ No newline at end of file