SimpleClientAppdata implemented but untested.
[vamsas.git] / src / org / vamsas / client / simpleclient / ClientDocument.java
index a6bb78c..dd1d695 100644 (file)
@@ -12,7 +12,7 @@ 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.client.Vobject;
 import org.vamsas.objects.core.AppData;
 import org.vamsas.objects.core.ApplicationData;
 import org.vamsas.objects.core.User;
@@ -27,10 +27,7 @@ import org.vamsas.objects.utils.AppDataReference;
 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 ApplicationData appsglobal=null;
-  protected User usersdata=null;
-  protected byte[] appData=null;
+  protected SimpleClient sclient;
   protected VamsasArchive archive = null;
   /**
    *
@@ -38,43 +35,17 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
    * @param doc - the dataset
    * @param docHandler - the sessionFile IO handler
    * @param Factory - the source of current and new vorbaIds
-   * @param vorba - the client instance
+   * @param sclient - the simpleclient instance
    */
-  protected ClientDocument(VamsasDocument doc, VamsasArchive docHandler, IdFactory Factory, IClient vorba) {
+  protected ClientDocument(VamsasDocument doc, VamsasArchive docHandler, IdFactory Factory, SimpleClient vorba) {
     super(Factory.getVorbaIdHash(), Factory);
     
     /**
      * prepare Application-side dataset from the vamsas Document archive
      */
-    this.vorba = vorba;
+    this.sclient = vorba;
     archive = docHandler;
     this.doc = doc;
-    extractAppData();
-  }
-  /**
-   * gets appropriate app data for the application, if it exists in this dataset
-   * 
-   */
-  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());
-        }
-      }
-    }
   }
   
   /*
@@ -82,9 +53,9 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
    * 
    * @see org.vamsas.client.IClientDocument#getObject(org.vamsas.client.VorbaId)
    */
-  public object getObject(VorbaId id) {
-    // TODO: look up id in document object
-    // retrieve object and return
+  public Vobject getObject(VorbaId id) {
+    // TODO: look up id in document Vobject
+    // retrieve Vobject and return
     return null;
   }
   
@@ -93,7 +64,7 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
    * 
    * @see org.vamsas.client.IClientDocument#getObjects(org.vamsas.client.VorbaId[])
    */
-  public object[] getObjects(VorbaId[] ids) {
+  public Vobject[] getObjects(VorbaId[] ids) {
     // TODO: getObject in bulk
     return null;
   }
@@ -125,36 +96,36 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
       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());
+          Vobject oldversion = getObject(newroots[i].getVorbaId());
           if (oldversion instanceof VAMSAS) {
             // LATER: appropriate merging behaviour when two clients have
-            // modified the same registered object independently
+            // modified the same registered Vobject independently
             if (newroots[i].get__last_hash() != newroots[i].hashCode()) {
-              // client has modified this object since last retrieval.
+              // client has modified this Vobject since last retrieval.
               if (newroots[i].get__last_hash() != oldversion.get__last_hash()) {
-                // object has been modified by another client since this
+                // Vobject has been modified by another client since this
                 // client's
                 // last access to document.
               }
             }
           } else {
             throw new Error(
-                "SimpleClient error when using setVamsasRoots : The vorbaId for object "
+                "SimpleClient error when using setVamsasRoots : The vorbaId for Vobject "
                 + i
-                + " does not refer to an object of type VAMSAS in the current document!");
+                + " does not refer to an Vobject of type VAMSAS in the current document!");
           }
         } else {
           if (!newroots[i].is__stored_in_document()) {
-            // check if object is modified
+            // check if Vobject is modified
             if (newroots[i].get__last_hash() != newroots[i].hashCode()) {
-              // it is - so we add newroots[i] as a new object, with updated
+              // it is - so we add newroots[i] as a new Vobject, with updated
               // provenance.
             } else {
               // do nothing
               newroots[i] = null;
             }
           } else {
-            // just add newroots[i] as a new object in the document
+            // just add newroots[i] as a new Vobject in the document
             // - with appropriate provenance.
           }
         }
@@ -173,9 +144,9 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
   /*
    * (non-Javadoc)
    * 
-   * @see org.vamsas.client.IClientDocument#registerObjects(org.vamsas.client.object[])
+   * @see org.vamsas.client.IClientDocument#registerObjects(org.vamsas.client.Vobject[])
    */
-  public VorbaId[] registerObjects(object[] unregistered) {
+  public VorbaId[] registerObjects(Vobject[] unregistered) {
     if (unregistered!=null) {
       VorbaId ids[] = new VorbaId[unregistered.length];
       for (int i=0,k=unregistered.length; i<k; i++)
@@ -186,17 +157,60 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements
   }
   
   /* (non-Javadoc)
-   * @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.object)
+   * @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.Vobject)
    */
-  public VorbaId registerObject(object unregistered) {
-    // TODO: add provenance stuff to newly registered object
+  public VorbaId registerObject(Vobject unregistered) {
+    // TODO: add provenance stuff to newly registered Vobject
     return _registerObject(unregistered);
   }
+  /**
+   * IClientAppdata instance - if it exists.
+   */
+  SimpleClientAppdata scappd = null;
   /* (non-Javadoc)
    * @see org.vamsas.client.IClientDocument#getClientAppdata()
    */
   public IClientAppdata getClientAppdata() {
-    // TODO Auto-generated method stub
+    if (scappd==null) {
+      log.debug("Creating new SimpleClientAppdata instance for "+sclient.getSessionHandle());
+      scappd = new SimpleClientAppdata(this);
+      if (scappd==null) {
+        // LATER: may not need this as a warning message.
+        log.warn("Null appdata object for "+sclient.getSessionHandle());
+      } else {
+        log.debug("Created SimpleClientAppdata successfully.");
+      }
+    } else {
+      log.debug("Returning existing SimpleClientAppdata reference.");
+    }
+    return scappd;
+  }
+  /* (non-Javadoc)
+   * @see java.lang.Object#finalize()
+   */
+  protected void finalize() throws Throwable {
+    log.debug("Garbage collecting on ClientDocument instance.");
+    if (scappd!=null) {
+      scappd.finalize();
+      scappd = null;
+    }
+    
+    super.finalize();
+  }
+  /**
+   * access the vamsas document
+   * @return the session's vamsas document
+   */
+  protected VamsasDocument getVamsasDocument() {
+    // TODO: IMPLEMENT
+    return null;
+  }
+  /**
+   * returns the read-only IO interface for the vamsas document Jar file
+   * @return
+   */
+  protected VamsasArchiveReader getVamsasArchiveReader() {
+    // TODO: IMPLEMENT getVamsasArchiveReader
     return null;
   }
 }