From a8ab4e4c713ac3f3139366e53dc3250a83502815 Mon Sep 17 00:00:00 2001 From: jprocter Date: Fri, 15 Dec 2006 12:05:06 +0000 Subject: [PATCH] internal flags for added and updated since last read for Vobjects tested with ArchiveClient code. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@315 be28352e-c001-0410-b1a7-c7978e42abec --- src/uk/ac/vamsas/client/Vobject.java | 21 +++- src/uk/ac/vamsas/client/VorbaId.java | 7 +- src/uk/ac/vamsas/client/VorbaXmlBinder.java | 145 +++++++++++++++------------ 3 files changed, 104 insertions(+), 69 deletions(-) diff --git a/src/uk/ac/vamsas/client/Vobject.java b/src/uk/ac/vamsas/client/Vobject.java index c5f2697..5029a31 100644 --- a/src/uk/ac/vamsas/client/Vobject.java +++ b/src/uk/ac/vamsas/client/Vobject.java @@ -35,6 +35,10 @@ public abstract class Vobject { */ protected boolean __updated_since_last_read = false; /** + * true if Vobject appeared in the document after the last access by this vamsas library instance + */ + protected boolean __added_since_last_read = false; + /** * memory of the last doHash() value computed for the Vobject * @see doHash() */ @@ -275,10 +279,16 @@ public abstract class Vobject { /** * @return true if this object has been updated in the currently stored document since the last time a Vobject with the same ID was read from a Vamsas Document */ - public boolean is__updated_since_last_read() { + public boolean isUpdated() { return __updated_since_last_read; } - + /** + * + * @return true if this object was added to the document after the last time the vamsas library acessed the session document + */ + public boolean isNewInDocument() { + return __added_since_last_read; + } /** * Set internal flag to indicate this object was updated since the last document read * @param __updated_since_last_read the __updated_since_last_read to set @@ -300,6 +310,13 @@ public abstract class Vobject { } /** + * @param __added_since_last_read the __added_since_last_read to set + */ + protected void set__added_since_last_read(boolean __added_since_last_read) { + this.__added_since_last_read = __added_since_last_read; + } + + /** * __last_hash is the hash value computed when the Vobject was last checked * against a IClientDocument generated by the Vobject's parent IClient * instance. diff --git a/src/uk/ac/vamsas/client/VorbaId.java b/src/uk/ac/vamsas/client/VorbaId.java index 8ffee9e..4ba6c0b 100644 --- a/src/uk/ac/vamsas/client/VorbaId.java +++ b/src/uk/ac/vamsas/client/VorbaId.java @@ -38,8 +38,13 @@ public class VorbaId { return vobject.vorbaId; } } + /** + * protected VorbaId constructor used when turning XML ID strings into vorba IDs + * @param id + * @return VorbaId object or null if string was null. + */ protected static VorbaId newId(String id) { - return new VorbaId(id); + return (id==null) ? null : new VorbaId(id); } /** * @return Returns the id. diff --git a/src/uk/ac/vamsas/client/VorbaXmlBinder.java b/src/uk/ac/vamsas/client/VorbaXmlBinder.java index 1834e95..9a94047 100644 --- a/src/uk/ac/vamsas/client/VorbaXmlBinder.java +++ b/src/uk/ac/vamsas/client/VorbaXmlBinder.java @@ -42,7 +42,7 @@ public class VorbaXmlBinder implements UnmarshalListener { private final Hashtable oldobjhashes; private final Hashtable objrefs; private final Vector updatedobjs; - + public VorbaXmlBinder(IVorbaIdFactory vorbafactory, Vector obj, Hashtable objrefs, Hashtable oldobjhashes, Vector updatedobjs) { this.vorbafactory = vorbafactory; this.obj = obj; @@ -81,7 +81,7 @@ public class VorbaXmlBinder implements UnmarshalListener { */ public void initialized(Object object) { } - + /* * Check if the object has an 'id' field - if it does, copy the value into * the VorbaId field of Vobject, and add the Vobject to the VorbaId hash. @@ -102,7 +102,8 @@ public class VorbaXmlBinder implements UnmarshalListener { if (idstring!=null) { if (idstring.length() > 0) { nobj.setVorbaId(VorbaId.newId(idstring)); - if (objrefs.containsKey(nobj_id=nobj.getVorbaId()) && !objrefs.get(nobj.getVorbaId()).equals(nobj)) { + nobj_id=nobj.getVorbaId(); + if (objrefs.containsKey(nobj_id) && !objrefs.get(nobj_id).equals(nobj)) { System.err.println("Serious problem : duplicate id '"+idstring+"' found! expect badness."); // TODO: HANDLE duplicate XML ids correctly } @@ -115,23 +116,35 @@ public class VorbaXmlBinder implements UnmarshalListener { // TODO: add to list of objects without a valid vorbaId obj.add(nobj); } - - nobj.doHash(); - // check to see if new object was present in old object hash - if (oldobjhashes.containsKey(nobj.getVorbaId())) { - Vobjhash oldhash = (Vobjhash) oldobjhashes.get(nobj.getVorbaId()); - if (oldhash.isUpdated(nobj)) { - // mark the object as updated in this document read. - nobj.set__updated_since_last_read(true); + nobj.doHash(); + // check to see if new object was present in old object hash + if (nobj_id!=null) { + if (oldobjhashes.containsKey(nobj_id)) { + Vobjhash oldhash = (Vobjhash) oldobjhashes.get(nobj_id); + if (oldhash.isUpdated(nobj)) { + // mark the object as updated in this document read. + nobj.set__updated_since_last_read(true); + oldobjhashes.put(nobj_id, new Vobjhash(nobj)); + updatedobjs.addElement(nobj); + } + } else { + // 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)); - updatedobjs.addElement(nobj); + } else { + // for objects yet to get a valid vorba_id + nobj.set__added_since_last_read(true); } } - } + } catch (Exception e) { return; }; - + } } @@ -146,7 +159,7 @@ public class VorbaXmlBinder implements UnmarshalListener { * @throws ValidationException */ public static void putVamsasDocument(PrintWriter outstream, VorbaIdFactory vorba, VamsasDocument doc) - throws IOException, MarshalException, ValidationException { + throws IOException, MarshalException, ValidationException { // Ensure references if (vorba==null) throw new Error("Null VorbaIdFactory Parameter"); @@ -154,7 +167,7 @@ public class VorbaXmlBinder implements UnmarshalListener { doc.__vorba = vorba; doc.__ensure_instance_ids(); // this may take a while. Do we allow for cyclic references ? doc.marshal(outstream); - + } /** * creates new VorbaId references where necessary for newly unmarshalled objects @@ -182,61 +195,61 @@ public class VorbaXmlBinder implements UnmarshalListener { return sync; } /** - * Unmarshals a vamsasDocument Vobject from a stream, registers - * unregistered objects, records existing VorbaIds, and completes - * the uk.ac.vamsas.client.Vobject housekeeping fields. - * For a valid unmarshalling, the array of returned objects also includes - * a sync 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 - * as getVamsasObjects but will detect updated objects based on differing hash values - * obtained from the VorbaIdFactory's VorbaId, Vobject.get__last_Hash() pairs (if any) - * @param instream - the XML input stream + * Unmarshals a vamsasDocument Vobject from a stream, registers + * unregistered objects, records existing VorbaIds, and completes + * the uk.ac.vamsas.client.Vobject housekeeping fields. + * For a valid unmarshalling, the array of returned objects also includes + * a sync 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 + * as getVamsasObjects but will detect updated objects based on differing hash values + * obtained from the VorbaIdFactory's VorbaId, Vobject.get__last_Hash() pairs (if any) + * @param instream - the XML input stream * @param factory - the SimpleClient's properly configured VorbaId factory to make new references. * @param root the root element's uk.ac.vamsas.objects.core Vobject. - * @return null or {(Object) VamsasDocument Vobject, (Object) Hashtable of Vobject references, (Object) Boolean(sync), (Object) Vector of updated objects in document } - */ + * @return null or {(Object) VamsasDocument Vobject, (Object) Hashtable of Vobject references, (Object) Boolean(sync), (Object) Vector of updated objects in document } + */ public static Object[] getVamsasObjects(Reader instream, - VorbaIdFactory factory, Vobject root) { + VorbaIdFactory factory, Vobject root) { Unmarshaller unmarshaller = new Unmarshaller(root); - unmarshaller.setIDResolver(new IDResolver() { - public Object resolve(String id) { - VorbaXmlBinder.log.warn("Warning - id " + id - + " is not found in the Vamsas XML!"); + unmarshaller.setIDResolver(new IDResolver() { + public Object resolve(String id) { + VorbaXmlBinder.log.warn("Warning - id " + id + + " is not found in the Vamsas XML!"); + return null; + } + }); + final Hashtable objrefs = new Hashtable(); + if (factory.extanthashv==null) + factory.extanthashv=new Hashtable(); + final Hashtable oobjhashes=factory.extanthashv; + final VorbaIdFactory vorbafactory = factory; + final Vector unrefedObj = new Vector(); + final Vector updatedObj = new Vector(); + unmarshaller.setUnmarshalListener(new VorbaXmlBinder(vorbafactory, unrefedObj, objrefs, oobjhashes, updatedObj)); + // 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); + boolean sync=ensure_references(unrefedObj, objrefs); + if (!(obj instanceof Vobject)) return null; - } - }); - final Hashtable objrefs = new Hashtable(); - if (factory.extanthashv==null) - factory.extanthashv=new Hashtable(); - final Hashtable oobjhashes=factory.extanthashv; - final VorbaIdFactory vorbafactory = factory; - final Vector unrefedObj = new Vector(); - final Vector updatedObj = new Vector(); - unmarshaller.setUnmarshalListener(new VorbaXmlBinder(vorbafactory, unrefedObj, objrefs, oobjhashes, updatedObj)); - // 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); - boolean sync=ensure_references(unrefedObj, objrefs); - if (!(obj instanceof Vobject)) - return null; - vorbafactory.setNewIdHash(objrefs); // update the Document IO Handler's set of vorbaId<>Object bindings. - return new Object[] { obj, objrefs, new Boolean(sync),updatedObj}; - } - } catch (MarshalException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (ValidationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + vorbafactory.setNewIdHash(objrefs); // update the Document IO Handler's set of vorbaId<>Object bindings. + return new Object[] { obj, objrefs, new Boolean(sync),updatedObj}; } - return null; + } catch (MarshalException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ValidationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } + return null; + } } \ No newline at end of file -- 1.7.10.2