better time delay in command execution.
[vamsas.git] / src / org / vamsas / client / SimpleClient.java
index 1f83b6f..e70ffec 100644 (file)
@@ -37,9 +37,10 @@ public class SimpleClient implements IClient {
   SessionHandle session = null;
 
   ClientHandle client = null;
+  
   /*
    * (non-Javadoc)
-   * 
+   * TODO: check that build substitution variables are correct
    * @see org.vamsas.client.IClient#getAbout()
    */
   public String getAbout() {
@@ -83,7 +84,9 @@ public class SimpleClient implements IClient {
   }
 
   private Hashtable handlers = initHandlers();
+
   private Vector listeners = new Vector();
+
   /**
    * make all the PropertyChangeSupport objects for the
    * events described in org.vamsas.client.Event
@@ -98,6 +101,7 @@ public class SimpleClient implements IClient {
     }
     return events;
   }
+
   /*
    * (non-Javadoc)
    * 
@@ -106,10 +110,10 @@ public class SimpleClient implements IClient {
   public void addDocumentUpdateHandler(PropertyChangeListener evt) {
     if (handlers.containsKey(Events.DOCUMENT_UPDATE)) {
       Object handler;
-      ((PropertyChangeSupport) (handler
-          = handlers.get(Events.DOCUMENT_UPDATE))).addPropertyChangeListener(evt);
+      ((PropertyChangeSupport) (handler = handlers.get(Events.DOCUMENT_UPDATE)))
+          .addPropertyChangeListener(evt);
       listeners.add(handler);
-      listeners.add((Object) evt);      
+      listeners.add((Object) evt);
     }
   }
 
@@ -119,33 +123,44 @@ public class SimpleClient implements IClient {
    * @see org.vamsas.client.IClient#finalizeClient()
    */
   public void finalizeClient() {
-    // TODO: raise events
+    // TODO: determine if this is last client in session
+    // TODO: raise events like : ((lst_client && document.request.to.close), (client_finalization), (
+    
+    // if (handlers.containsKey(Events.))
+    // if (handlers.containsKey(Events.CLIENT_FINALIZATION))
     // deregister listeners.
     // mark this instance as finalized
   }
-  
+
   /**
    * writes the VamsasDocument to the given stream.
+   * TODO: ensure that (at least) default provenance entries are written for objects.
    * @param outstream
    * @param doc
    * @throws IOException
    * @throws MarshalException
    * @throws ValidationException
    */
-  private static void setVamsasDocument(Writer outstream, VamsasDocument doc) throws IOException, MarshalException, ValidationException {
-    Marshaller marshaller = new Marshaller(outstream);  
+  private static void setVamsasDocument(Writer outstream, VamsasDocument doc)
+      throws IOException, MarshalException, ValidationException {
+    Marshaller marshaller = new Marshaller(outstream);
     marshaller.marshal(doc);
   }
-  
+
   /**
    * Unmarshals a vamsasDocument object from a stream, registers
-   * any VorbaIds, and completes the org.vamsas.client.object housekeeping fields.
-   * 
+   * unregistered objects, records existing VorbaIds, and completes 
+   * the org.vamsas.client.object housekeeping fields.
+   * For a valid unmarshalling, the array of returned objects also includes
+   * a <return>sync</return> parameter which is true if new VorbaIds
+   * were created. If sync is false, then the caller should ensure that the
+   * vamsasDocument is written back to disk to propagate the new VorbaIds.
+   *  TODO: ensure that provenance is correct for newly registered objects
    * @param instream - the XML input stream 
    * @param factory - the SimpleClient's properly configured VorbaId factory to make new references.
-   * @return null or {(Object) VamsasDocument object, (Object) Hashtable of object references)
+   * @return null or {(Object) VamsasDocument object, (Object) Hashtable of object references, (Object) Boolean(sync) }
    */
-  private static Object[] getVamsasDocument(Reader instream,
+private static Object[] getVamsasDocument(Reader instream,
       IVorbaIdFactory factory) {
     Unmarshaller unmarshaller = new Unmarshaller(instream);
     unmarshaller.setIDResolver(new IDResolver() {
@@ -156,8 +171,10 @@ public class SimpleClient implements IClient {
       }
     });
     Hashtable refbase = new Hashtable();
+    Vector unrefed = new Vector();
     final Hashtable objrefs = refbase;
     final IVorbaIdFactory vorbafactory = factory;
+    final Vector unrefedObj =  unrefed;
     unmarshaller.setUnmarshalListener(new UnmarshalListener() {
 
       /*
@@ -197,25 +214,36 @@ public class SimpleClient implements IClient {
           nobj.set__stored_in_document(true);
           Field fd = null;
           try {
-            // look for the id field (should be an NCName string)
-            fd = nobj.getClass().getField("id");
-            String idstring;
-            if (fd.getType().getClass().equals("astring".getClass())) {
+            if (nobj.isRegisterable()) {
+              // look for the id field (should be an NCName string)
+              nobj.__vorba = vorbafactory;
+              fd = nobj.getClass().getField("id");
+              String idstring;
               if (fd.get(nobj) != null) {
                 idstring = (String) fd.get(nobj);
-                if (idstring.length() > 0)
+                if (idstring.length() > 0) {
                   if (!objrefs.containsKey(idstring)) {
                     objrefs.put(idstring, nobj);
                     nobj.setVorbaId(VorbaId.newId(idstring));
-                    nobj.__vorba = vorbafactory;
+                  } else {
+                    System.err.println("Serious problem : duplicate id '"+idstring+"' found! expect badness.");
+                    // TODO: HANDLE duplicate XML ids correctly
                   }
+                } else {
+                  // add to list of objects without a valid vorbaId
+                  unrefedObj.add(nobj);
+                }
+              } else {
+                // add to list of objects without a valid vorbaId
+                unrefedObj.add(nobj);
               }
-            }
+              
             nobj.doHash();
+          }
           } catch (Exception e) {
             return;
-          }
-          ;
+          };
+          
         }
       }
 
@@ -224,8 +252,23 @@ public class SimpleClient implements IClient {
     try {
       while (instream.ready()) {
         Object obj = unmarshaller.unmarshal(instream);
+        boolean sync=true;
         if (obj instanceof VamsasDocument) {
-          return new Object[] { obj, objrefs};
+          if (unrefed.size()>0) {
+            sync=false; // document is out of sync - ids have been created.
+            java.util.Iterator newobj = unrefed.listIterator();
+            while (newobj.hasNext()) {
+              object o = (object) newobj.next();
+              // forces registration and id field update.
+              VorbaId id = o.getVorbaId();
+              if (!objrefs.containsKey(id)) {
+                objrefs.put(id.id, o);
+              } else {
+                throw new Error("Serious! Duplicate reference made by vorbaIdFactory!");
+              }
+            }
+          }
+          return new Object[] { obj, objrefs, new Boolean(sync)};
         }
       }
     } catch (MarshalException e) {
@@ -240,12 +283,13 @@ public class SimpleClient implements IClient {
     }
     return null;
   }
-/**
- * extract data appropriate for client, session and user
- * from vamsas document.
- * @return application's byte array
- */
+  /**
+   * extract data appropriate for client, session and user
+   * from vamsas document.
+   * @return application's byte array
+   */
   private byte[] getApplicationData() {
+    // TODO: extract correct byte object from Jar and return it to application.
     return null;
   }
 
@@ -256,8 +300,8 @@ public class SimpleClient implements IClient {
    */
   public IClientDocument getClientDocument() {
     Object[] vdoc;//  TODO: = getVamsasDocument(new Reader());
-    ClientDocument cdoc = new ClientDocument(getApplicationData(),
-        ((VamsasDocument) vdoc[0]).getVAMSAS(), (Hashtable) vdoc[1], this);
+    // ClientDocument cdoc = new ClientDocument(getApplicationData(),
+        // ((VamsasDocument) vdoc[0]).getVAMSAS(), (Hashtable) vdoc[1], this);
     // 
     return null;
   }
@@ -269,7 +313,7 @@ public class SimpleClient implements IClient {
    */
   public void updateDocument(IClientDocument newdoc) {
     // TODO Auto-generated method stub
-
+    // 
   }
 
   /*
@@ -291,13 +335,29 @@ public class SimpleClient implements IClient {
   public void addVorbaEventHandler(String EventChain, PropertyChangeListener evt) {
     if (handlers.containsKey(EventChain)) {
       Object handler;
-      ((PropertyChangeSupport) (handler
-          = handlers.get(EventChain))).addPropertyChangeListener(evt);
+      ((PropertyChangeSupport) (handler = handlers.get(EventChain)))
+          .addPropertyChangeListener(evt);
       listeners.add(handler);
-      listeners.add((Object) evt);      
+      listeners.add((Object) evt);
     }
   }
 
+  /* (non-Javadoc)
+   * @see org.vamsas.client.IClient#pollUpdate()
+   */
+  public void pollUpdate() {
+    // TODO wake up UpdateWatcher thread to check for updates.
+
+  }
+
   public static void main(String[] args) {
   }
+
+  /* (non-Javadoc)
+   * @see org.vamsas.client.IClient#joinSession()
+   */
+  public void joinSession() throws Exception {
+    // TODO Auto-generated method stub
+    
+  }
 }