reprioritising TODOs and LATERs and fixing documentation typos.
[vamsas.git] / src / org / vamsas / client / simpleclient / ClientDocument.java
index 05fe494..a6bb78c 100644 (file)
@@ -4,54 +4,79 @@
 package org.vamsas.client.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.IClient;
+import org.vamsas.client.IClientAppdata;
 import org.vamsas.client.IClientDocument;
 import org.vamsas.client.VorbaId;
 import org.vamsas.client.object;
+import org.vamsas.objects.core.AppData;
+import org.vamsas.objects.core.ApplicationData;
+import org.vamsas.objects.core.User;
 import org.vamsas.objects.core.VAMSAS;
 import org.vamsas.objects.core.VamsasDocument;
+import org.vamsas.objects.utils.AppDataReference;
 
 /**
  * @author jimp Contains a collection of vamsas objects and reference to a
  *         specified ClientHandle's information.
  */
-public class ClientDocument implements IClientDocument {
+public class ClientDocument extends org.vamsas.client.ClientDocument implements IClientDocument {
+  private static Log log = LogFactory.getLog(ClientDocument.class);
+  private VamsasDocument doc;
   protected IClient vorba;
-  protected org.vamsas.objects.core.VAMSAS[] roots;
-  protected byte[] appData;
-
+  protected ApplicationData appsglobal=null;
+  protected User usersdata=null;
+  protected byte[] appData=null;
+  protected VamsasArchive archive = null;
   /**
-   * collection of org.vamsas.client.object references
-   */
-  protected Hashtable vamsasObjects;
-  
-  /*
-   * (non-Javadoc)
-   * 
-   * @see org.vamsas.client.IClientDocument#getApplicationData()
+   *
+   *  prepare Application-side dataset from the vamsas Document archive
+   * @param doc - the dataset
+   * @param docHandler - the sessionFile IO handler
+   * @param Factory - the source of current and new vorbaIds
+   * @param vorba - the client instance
    */
-
+  protected ClientDocument(VamsasDocument doc, VamsasArchive docHandler, IdFactory Factory, IClient vorba) {
+    super(Factory.getVorbaIdHash(), Factory);
+    
+    /**
+     * prepare Application-side dataset from the vamsas Document archive
+     */
+    this.vorba = vorba;
+    archive = docHandler;
+    this.doc = doc;
+    extractAppData();
+  }
   /**
+   * gets appropriate app data for the application, if it exists in this dataset
    * 
-   * @param data
-   * @param doc
-   * @param objects
-   * @param vorba
    */
-  protected ClientDocument(byte[] data, VAMSAS[] roots, Hashtable objects, IClient vorba) {
-    // TODO Auto-generated constructor stub
-    appData = data;
-    this.roots =roots;
-    vamsasObjects = objects;
-    this.vorba = vorba;
-  }
-
-  public byte[] getApplicationData() {
-    // Look up client byte stash using client and user handle
-    return appData;
+  private void extractAppData() {
+    Vector apldataset = AppDataReference.getUserandApplicationsData(
+        doc, vorbafactory.getUserHandle(), vorbafactory.getClientHandle());
+    if (apldataset!=null) {
+      if (apldataset.size()>0) {
+        AppData clientdat = (AppData) apldataset.get(0);
+        if (clientdat instanceof ApplicationData) {
+          appsglobal = (ApplicationData) clientdat;
+          if (apldataset.size()>1) {
+            clientdat = (AppData) apldataset.get(1);
+            if (clientdat instanceof User)
+              usersdata = (User) clientdat;
+            if (apldataset.size()>2)
+              log.info("Ignoring additional ("+(apldataset.size()-2)+") AppDatas returned by document appdata query.");
+          } 
+        } else {
+          log.warn("Unexpected entry in AppDataReference query: id="+clientdat.getVorbaId()+" type="+clientdat.getClass().getName());
+        }
+      }
+    }
   }
-
+  
   /*
    * (non-Javadoc)
    * 
@@ -62,7 +87,7 @@ public class ClientDocument implements IClientDocument {
     // retrieve object and return
     return null;
   }
-
+  
   /*
    * (non-Javadoc)
    * 
@@ -72,19 +97,19 @@ public class ClientDocument implements IClientDocument {
     // TODO: getObject in bulk
     return null;
   }
-
+  
   /*
    * (non-Javadoc)
    * 
    * @see org.vamsas.client.IClientDocument#getVamsasRoots()
    */
   public VAMSAS[] getVamsasRoots() {
-    if (roots == null)
+    if (doc.getVAMSAS() == null)
       // Make a new one to return to client : TODO: Decide if this is correct
       return new VAMSAS[] { new VAMSAS() };
-    return roots;
+    return doc.getVAMSAS();
   }
-
+  
   /**
    * update the document with new roots.
    */
@@ -96,13 +121,13 @@ public class ClientDocument implements IClientDocument {
       // if they have ? we overwrite them with their new version, ensuring that
       // provenance is updated.
       // if they haven't ? do nothing ?
-
+      
       for (int i = 0, k = newroots.length; i < k; i++) {
         if (newroots[i].isRegistered()) {
           // easy - just check if anything has changed and do provenance
           object oldversion = getObject(newroots[i].getVorbaId());
           if (oldversion instanceof VAMSAS) {
-            // TODO: appropriate merging behaviour when two clients have
+            // LATER: appropriate merging behaviour when two clients have
             // modified the same registered object independently
             if (newroots[i].get__last_hash() != newroots[i].hashCode()) {
               // client has modified this object since last retrieval.
@@ -115,8 +140,8 @@ public class ClientDocument implements IClientDocument {
           } else {
             throw new Error(
                 "SimpleClient error when using setVamsasRoots : The vorbaId for object "
-                    + i
-                    + " does not refer to an object of type VAMSAS in the current document!");
+                + i
+                + " does not refer to an object of type VAMSAS in the current document!");
           }
         } else {
           if (!newroots[i].is__stored_in_document()) {
@@ -136,7 +161,7 @@ public class ClientDocument implements IClientDocument {
       }
     }
   }
-
+  
   /* (non-Javadoc)
    * @see org.vamsas.client.IClientDocument#addVamsasRoot(org.vamsas.objects.core.VAMSAS)
    */
@@ -144,17 +169,7 @@ public class ClientDocument implements IClientDocument {
     // TODO Auto-generated method stub
     
   }
-
-/*
-   * (non-Javadoc)
-   * 
-   * @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.object)
-   */
-public VorbaId registerObject(object unregistered) {
-    if (!unregistered.isRegistered())
-      unregistered.setVorbaId(unregistered.__vorba.makeVorbaId());
-    return unregistered.getVorbaId();
-  }
+  
   /*
    * (non-Javadoc)
    * 
@@ -169,13 +184,19 @@ public VorbaId registerObject(object unregistered) {
     }
     return null;
   }
-
-  /*
-   * (non-Javadoc)
-   * 
-   * @see org.vamsas.client.IClientDocument#setApplicationData(byte[])
+  
+  /* (non-Javadoc)
+   * @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.object)
+   */
+  public VorbaId registerObject(object unregistered) {
+    // TODO: add provenance stuff to newly registered object
+    return _registerObject(unregistered);
+  }
+  /* (non-Javadoc)
+   * @see org.vamsas.client.IClientDocument#getClientAppdata()
    */
-  public void setApplicationData(byte[] newData) {
-    appData = newData;
+  public IClientAppdata getClientAppdata() {
+    // TODO Auto-generated method stub
+    return null;
   }
 }