made SimpleClientConfig externally configurable and added mechanism to prevent invali...
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / ClientDocument.java
index ef0a7d5..b9e918e 100644 (file)
@@ -103,6 +103,11 @@ public class ClientDocument extends uk.ac.vamsas.client.ClientDocument implement
    * internal reference to single copy of Document Roots array
    */
   private VAMSAS[] _VamsasRoots=null;
+  /**
+   * set if the client has corrupted the Vamsas Document structure somehow.
+   * if this is set the document will never be written back to the session unless the corruption is fixed.
+   */
+  private boolean invalidModification=false;
 
   protected void updateDocumentRoots() {
     if (doc==null) {
@@ -164,33 +169,56 @@ public class ClientDocument extends uk.ac.vamsas.client.ClientDocument implement
  * verify that newr version is really an intact version of the 
  * @param newVersion (may be modified)
  * @param oldVersion 
+ * @param modflag 
  * @return true if newVersion is a valid root that preserves original references
  */
-  private boolean isValidUpdate(VAMSAS newVersion, final VAMSAS oldVersion) {
+  private boolean isValidUpdate(VAMSAS newVersion, final VAMSAS oldVersion, ClientDocument modflag) {
     // ideal - this cascades down the two structures, ensuring that all ID'd objects in one are present in the other.
     if (oldVersion==newVersion) {
       // may be a virgin root element.
       if (!newVersion.isRegistered())
-        _registerObject(newVersion); // TODO: check - this call hasn't been tested.
-      // Should retrieve original version and compare - unless local hashes can be used to determine if resultSet has been truncated.
+      {
+        _registerObject(newVersion); // TODO: check - this call hasn't been tested. (seems to work now)
+        modflag.isModified=true;
+      }
+      // TODO: Should attempt to repair document if client app has deleted/broken bits of it
+      if (oldVersion.is__stored_in_document()) {
+        // retrieve compare hashCodes to detect update.
+        if (oldVersion.get__last_hash()!=oldVersion.hashCode())
+        {
+          log.debug("Modified hashcode for vamsas root "+oldVersion.getVorbaId());
+          modflag.isModified = true;
+        } else {
+          log.debug("Unmodified vamsas root "+oldVersion.getVorbaId());
+        }
+      }
       // just do internal validation for moment.
       try {
-        if (SimpleClientConfig.validateUpdatedRoots())
+        if (getSimpleClientConfig().validateUpdatedRoots())
+        {
           newVersion.validate();
+        }
         return true;
       }
       catch (Exception e)
       {
         log.error("Validation Exception for new vamsas root :"+newVersion.getVorbaId(),e);
+        modflag.invalidModification=true;
       }
       return false;
     } else {
       // redundant ? if (oldVersion.is__stored_in_document())
       if (!newVersion.isRegistered())
+      {
         _registerObject(newVersion);
+        modflag.isModified=true;
+      }
       try {
-        if (SimpleClientConfig.validateMergedRoots())
+        if (getSimpleClientConfig().validateMergedRoots())
+        {
           newVersion.validate();
+        }
+        modflag.isModified=true;
         return true;
       }
       catch (Exception e)
@@ -248,6 +276,9 @@ public class ClientDocument extends uk.ac.vamsas.client.ClientDocument implement
         }
       }*/
   }
+  private SimpleClientConfig getSimpleClientConfig() {
+    return sclient.getSimpleClientConfig();
+}
   /**
    * merge old and new root vectors
    * @param newr This array may be written to
@@ -257,12 +288,11 @@ public class ClientDocument extends uk.ac.vamsas.client.ClientDocument implement
    */
   private VAMSAS[] _combineRoots(VAMSAS[] newr, final VAMSAS[] original, ClientDocument modflag) {
     Vector rts = new Vector();
-    boolean modified=false;
     for (int i=0,j=original.length; i<j; i++) {
       int k = _contains(original[i], newr);
       if (k>-1) {
-        if (isValidUpdate(newr[k], original[i])) {
-          modified=true;
+        if (isValidUpdate(newr[k], original[i], modflag)) {
+          // set by isValidUpdate if the hashcodes were really different from last store 
           rts.add(newr[k]);
           newr[k]=null;
         } else {
@@ -279,14 +309,12 @@ public class ClientDocument extends uk.ac.vamsas.client.ClientDocument implement
     for (int i=0,j=newr.length; i<j; i++) {
       if (newr[i]!=null) {
         rts.add(newr[i]);
-        modified=true;
+        modflag.isModified=true;
       }
     }
     newr = new VAMSAS[rts.size()];
     for (int i=0,j=rts.size(); i<j; i++)
       newr[i] = (VAMSAS) rts.get(i);
-    if (modflag!=null)
-      modflag.isModified = modified;
     return newr;
   }
   
@@ -452,7 +480,7 @@ public class ClientDocument extends uk.ac.vamsas.client.ClientDocument implement
       throw new java.io.IOException("Document is closed.");
     }
     if (iohandler==null) {
-      log.warn("updateSessionDocument called document iohandler handler.");
+      log.warn("updateSessionDocument called on null document iohandler handler.");
       throw new java.io.IOException("Document is closed.");
     }
     
@@ -553,7 +581,7 @@ public class ClientDocument extends uk.ac.vamsas.client.ClientDocument implement
       doc = null;
     }
     // disengage from client
-    if (sclient!=null)
+    if (sclient!=null && sclient.cdocument==this)
       sclient.cdocument = null;
     sclient=null;
     
@@ -563,4 +591,19 @@ public class ClientDocument extends uk.ac.vamsas.client.ClientDocument implement
     // TODO: WALK through the document objects calling the update mechanism for each one, or just pass this vector back to client ?return updatedObjects;
     return null;
   }
+  /**
+   * if this is set the document will never be written back to the session unless the corruption is fixed.
+   * @return the invalidModification
+   */
+  public boolean isInvalidModification() {
+    return invalidModification;
+  }
+  /**
+   * set if the client has corrupted the Vamsas Document structure somehow.
+   * if this is set the document will never be written back to the session unless the corruption is fixed.
+   * @param invalidModification the invalidModification to set
+   */
+  public void setInvalidModification(boolean invalidModification) {
+    this.invalidModification = invalidModification;
+  }
 }