made SimpleClientConfig externally configurable and added mechanism to prevent invali...
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimpleClient.java
index 8e8fba2..d13fe24 100644 (file)
@@ -25,6 +25,7 @@ import uk.ac.vamsas.client.Events;
 import uk.ac.vamsas.client.IClient;
 import uk.ac.vamsas.client.IClientDocument;
 import uk.ac.vamsas.client.IObjectUpdate;
+import uk.ac.vamsas.client.InvalidSessionDocumentException;
 import uk.ac.vamsas.client.InvalidSessionUrnException;
 import uk.ac.vamsas.client.SessionHandle;
 import uk.ac.vamsas.client.UserHandle;
@@ -94,31 +95,22 @@ public class SimpleClient implements IClient {
       
   }
   /**
-   * construct new session by importing objects from an existing vamsas document
+   * construct new SimpleClientsession by importing objects from an existing vamsas document
    * @param user
    * @param client
    * @param sess
    * @param importingArchive
    * @throws Exception IOExceptions for Session IO problems, and general Exception if importing document is invalid.
-   */
   protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess, File importingArchive) throws Exception {
     this(user, client, sess);
-    VamsasArchive sessdoc = _session.getVamsasDocument();
-    try {
-      VamsasArchiveReader odoc = new VamsasArchiveReader(importingArchive);
-      SimpleDocument sdoc = new SimpleDocument(makeVorbaIdFactory());
-      VamsasDocument doc = sdoc.getVamsasDocument(odoc);
-      sessdoc.putVamsasDocument(doc, sdoc.vorba);
-      sessdoc.closeArchive();
-      log.debug("Imported new vamsas data from "+importingArchive);
-    } catch (Exception e) {
-      sessdoc.cancelArchive();
-      // write a dummy iohandler
-      _session.slog.info("Exception when importing document data from "+importingArchive);
-      log.warn("While importing session data from existing archive in "+importingArchive, e);      
-      throw new Exception("Failed to import data from "+importingArchive, e);
+    if (log.isDebugEnabled())
+    {
+      log.debug("Attempting to overwrite session document with file: "+importingArchive);
     }
+  // TODO: write provenance entry for new session indicating the import.
+    
   }
+   */
   
   /*
    * (non-Javadoc)
@@ -248,8 +240,8 @@ public class SimpleClient implements IClient {
     _session.removeClient(this);
     //log.debug("Stopping EventGenerator..");
     //evgen.stopWatching();
-    SimpleClient.log.debug("EventGenerator halted.");
     this.cdocument = null;
+    SimpleClient.log.debug("EventGenerator halted.");
     log.debug("finalization Complete.");
   }
   
@@ -316,14 +308,21 @@ public class SimpleClient implements IClient {
     
     if (evgen.isDocumentWatchEnabled())
       throw new Error("Probable Client Error (did you remember to call SimpleClient.updateDocument(clientdoc) at the end of the document update handler?) - or Library Bug : Document watcher still enabled whilst ClientDocument instance exists.");
-    
-    if (!cdocument.isModified()) {
-      // client document is silently got rid of, with no session update events.
-      if (log.isDebugEnabled())
-        log.debug("updateDocument for "+session.getSessionUrn()+" with unmodified IClientDocument (skipping the write)");
+    if (cdocument.isInvalidModification())
+    {
+      log.info("Client has corrupted the vamsas document. It will not be written back to the session - sorry.");
+      // TODO: modify updateDocument signature: We should really raise an exception here to tell the client that it broke the datamodel.
     } else {
-      writeSessionDocument();
+      // actually try to write - if necessary.
+      if (!cdocument.isModified()) {
+        // client document is silently got rid of, with no session update events.
+        if (log.isDebugEnabled())
+        log.debug("updateDocument for "+session.getSessionUrn()+" with unmodified IClientDocument (skipping the write)");
+      } else {
+        writeSessionDocument();
+      }
     }
+    // release locks, reset and start to receive events again
     tidyAwaySessionDocumentState(); 
   }
   /**
@@ -589,5 +588,13 @@ public class SimpleClient implements IClient {
   protected Lock getClientLock() {
     return activeClientFilelock;
   }
+  SimpleClientConfig _config = null;
+  public SimpleClientConfig getSimpleClientConfig() {
+    if (_config==null)
+    {
+      _config = new SimpleClientConfig();
+    }
+    return _config;
+  }
 
 }