more fixes for clientdocument/simpleclient. Testign the vamsasArchive reader and...
[vamsas.git] / src / org / vamsas / client / simpleclient / ClientDocument.java
index f89f713..d360085 100644 (file)
@@ -68,6 +68,10 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
    * @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))
       return (Vobject) objrefs.get(id);
     log.debug("Returning null Vobject reference for id "+id.getId());
@@ -80,6 +84,10 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
    * @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]))
@@ -98,12 +106,23 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
    * @see org.vamsas.client.IClientDocument#getVamsasRoots()
    */
   public VAMSAS[] getVamsasRoots() {
+    if (doc==null) {
+      log.debug("Null document for getVamsasRoots(), returning null");
+      return null;
+    }
+    if (archive==null) {
+      // LATER: decide on read-only status of ClientDocument object
+      log.warn("getVamsasRoots() called on possibly read-only document.");
+    }
     if (_VamsasRoots!=null)
       return _VamsasRoots;
     VAMSAS[] roots = doc.getVAMSAS();
     if (roots == null) {
-      // Make a new one to return to client : TODO: Decide if this is correct
+      // Make a new one to return to client to get filled. 
       _VamsasRoots = new VAMSAS[] { new VAMSAS() };
+      // Do provenance now. just in case.
+      doc.getProvenance().addEntry(sclient.getProvenanceEntry("Created new document root [id="+_VamsasRoots[0].getId()+"]"));
+      doc.addVAMSAS(_VamsasRoots[0]);
     } else {
       _VamsasRoots = new VAMSAS[roots.length];
       for (int r=0;r<roots.length; r++)
@@ -244,6 +263,10 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
    * @see org.vamsas.IClientDocument.setVamsasRoots
    */
   public void setVamsasRoots(VAMSAS[] newroots) {
+    if (doc==null) {
+      log.debug("setVamsasRoots called on null document.");
+      return;
+    }
     VAMSAS[] newr;
     if (newroots==null) {
       log.debug("setVamsasRoots(null) - do nothing.");
@@ -271,6 +294,10 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
    * @see org.vamsas.client.IClientDocument#addVamsasRoot(org.vamsas.objects.core.VAMSAS)
    */
   public void addVamsasRoot(VAMSAS newroot) {
+    if (doc==null) {
+      log.debug("addVamsasRoots called on null document.");
+      return;
+    }
     VAMSAS[] newroots = _combineRoots(new VAMSAS[] {newroot}, _VamsasRoots, this);
     _VamsasRoots = newroots;  
   }
@@ -281,6 +308,14 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
    * @see org.vamsas.client.IClientDocument#registerObjects(org.vamsas.client.Vobject[])
    */
   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++)
@@ -299,6 +334,14 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
    * @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 (unregistered!=null) {
       VorbaId id = _registerObject(unregistered);
       log.debug("Registered object - total of "+objrefs.size()+" ids.");
@@ -315,6 +358,10 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
    * @see org.vamsas.client.IClientDocument#getClientAppdata()
    */
   public IClientAppdata getClientAppdata() {
+    if (doc==null) {
+      log.warn("getClientAppdata called on null document.");
+      return null;
+    }
     if (scappd==null) {
       log.debug("Creating new SimpleClientAppdata instance for "+sclient.getSessionHandle());
       scappd = new SimpleClientAppdata(this);
@@ -350,6 +397,15 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
   }
   protected boolean updateSessionDocument() throws java.io.IOException {
     boolean docupdate = true; // 'non-serious' problems below set this false
+    if (doc==null) {
+      log.warn("updateSessionDocument called on null document.");
+      throw new java.io.IOException("Document is closed.");
+    }
+    if (archive==null) {
+      log.warn("updateSessionDocument called document archive handler.");
+      throw new java.io.IOException("Document is closed.");
+    }
+    
     if (!isModified() && !scappd.isModified()) {
       log.debug("Document update not necessary. returning false.");
       return false;
@@ -426,7 +482,6 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
       log.debug("No remaining appdatas were transferred. (Correct?)");
     archive.closeArchive();
     log.debug("...successully finished and closed.");
-
     return docupdate; // no errors ?
   }
   /* (non-Javadoc)
@@ -438,7 +493,13 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
       scappd.finalize();
       scappd = null;
     }
-    // TODO local garbage collection
+    if (doc!=null) {
+      doc = null;
+    }
+    // disengage from client
+    if (sclient!=null)
+      sclient.cdocument = null;
+    sclient=null;
     
     super.finalize();
   }