update detection mechanism based comparing the difference between the position of...
[vamsas.git] / src / uk / ac / vamsas / client / VorbaXmlBinder.java
index 9a94047..12543f2 100644 (file)
@@ -25,6 +25,7 @@ import org.exolab.castor.xml.UnmarshalListener;
 import org.exolab.castor.xml.Unmarshaller;
 import org.exolab.castor.xml.ValidationException;
 
+import uk.ac.vamsas.client.utils.ChecksummedReader;
 import uk.ac.vamsas.objects.core.VamsasDocument;
 /**
  * Implements the Vamsas Vobject ID machinery for translating 
@@ -41,14 +42,20 @@ public class VorbaXmlBinder implements UnmarshalListener {
   private final Vector obj;
   private final Hashtable oldobjhashes;
   private final Hashtable objrefs;
-  private final Vector updatedobjs;
-
+  private final Vector updatedobjs; // not yet used elswhere ?
+  private final ChecksummedReader inStream;
   public VorbaXmlBinder(IVorbaIdFactory vorbafactory, Vector obj, Hashtable objrefs, Hashtable oldobjhashes, Vector updatedobjs) {
-    this.vorbafactory = vorbafactory;
-    this.obj = obj;
-    this.objrefs = objrefs;
+    this(vorbafactory, obj, objrefs, oldobjhashes, updatedobjs, null);
+
+  }
+
+  public VorbaXmlBinder(IVorbaIdFactory vorbafactory2, Vector unrefedObj, Hashtable objrefs2, Hashtable oldobjhashes, Vector updatedObj, ChecksummedReader ckedInstream) {
+    this.inStream = ckedInstream;
+    this.vorbafactory = vorbafactory2;
+    this.obj = unrefedObj;
+    this.objrefs = objrefs2;
     this.oldobjhashes = oldobjhashes;
-    this.updatedobjs = updatedobjs;
+    this.updatedobjs = updatedObj;
   }
 
   /*
@@ -57,6 +64,7 @@ public class VorbaXmlBinder implements UnmarshalListener {
    * @see org.exolab.castor.xml.UnmarshalListener#attributesProcessed(java.lang.Object)
    */
   public void attributesProcessed(Object object) {
+
   }
 
   /*
@@ -80,6 +88,11 @@ public class VorbaXmlBinder implements UnmarshalListener {
    * @see org.exolab.castor.xml.UnmarshalListener#initialized(java.lang.Object)
    */
   public void initialized(Object object) {
+    if (object instanceof Vobject) {
+      Vobject nobj = (Vobject) object;
+      if (nobj.isRegisterable() && nobj.___id_field!=null && inStream!=null)
+        nobj.__setInitHash(inStream.getChecksum()); // record start tag position
+    }
   }
 
   /*
@@ -94,6 +107,8 @@ public class VorbaXmlBinder implements UnmarshalListener {
       nobj.set__stored_in_document(true);
       try {
         if (nobj.isRegisterable() && nobj.___id_field!=null) {
+          if (inStream!=null)
+            nobj.__setFinalHash(inStream.getChecksum()); // compute LHash as difference between end and start tag positions
           VorbaId nobj_id=null;
           // look for the id field (should be an NCName string)
           nobj.__vorba = vorbafactory;
@@ -116,7 +131,8 @@ public class VorbaXmlBinder implements UnmarshalListener {
             // TODO: add to list of objects without a valid vorbaId
             obj.add(nobj);
           }
-          nobj.doHash();
+          nobj.doHash(); // TODO: DECIDE IF WE NEED TO DO THIS STILL: THIS DOESNT WORK! updates detected by comparing with last hash when marshalling
+
           // check to see if new object was present in old object hash
           if (nobj_id!=null) {
             if (oldobjhashes.containsKey(nobj_id)) {
@@ -131,7 +147,6 @@ public class VorbaXmlBinder implements UnmarshalListener {
               // object has no entry in the oldhashvalue list but
               // there is a valid vorbaId so
               nobj.set__added_since_last_read(true);
-
             }
             // and record the just-unmarshalled hash value
             oldobjhashes.put(nobj_id, new Vobjhash(nobj));
@@ -227,13 +242,14 @@ public class VorbaXmlBinder implements UnmarshalListener {
     final VorbaIdFactory vorbafactory = factory;
     final Vector unrefedObj =  new Vector();
     final Vector updatedObj = new Vector();
-    unmarshaller.setUnmarshalListener(new VorbaXmlBinder(vorbafactory, unrefedObj, objrefs, oobjhashes, updatedObj));
+    ChecksummedReader ckedInstream = new ChecksummedReader(instream);
+    unmarshaller.setUnmarshalListener(new VorbaXmlBinder(vorbafactory, unrefedObj, objrefs, oobjhashes,updatedObj, ckedInstream));
     // Call the unmarshaller.
     try {
       while (instream.ready()) {
         // TODO: mark objects in oobjhash prior to unmarshalling, to detect when objects have been lost through an update.
         //tohere
-        Object obj = unmarshaller.unmarshal(instream);
+        Object obj = unmarshaller.unmarshal(ckedInstream);
         boolean sync=ensure_references(unrefedObj, objrefs);
         if (!(obj instanceof Vobject))
           return null;