import org.vamsas.objects.utils.AppDataReference;
/**
- * @author jimp Contains a collection of vamsas objects and reference to a
- * specified ClientHandle's information.
+ * Maintains a collection of vamsas objects, appdatas and states, and provides api for a SimpleClient's client.
+ * @author jimp
*/
public class ClientDocument extends org.vamsas.client.ClientDocument implements IClientDocument {
private static Log log = LogFactory.getLog(ClientDocument.class);
protected SimpleClient sclient;
protected VamsasArchive archive = null;
/**
+ * indicate if new data has been incorporated
+ */
+ private boolean isModified = false;
+ /**
+ * Public method for internal use by SimpleClient.
+ * @return true if document has been modified.
+ */
+ public boolean isModified() {
+ return isModified;
+ }
+ /**
*
* prepare Application-side dataset from the vamsas Document archive
* @param doc - the dataset
* @param Factory - the source of current and new vorbaIds
* @param sclient - the simpleclient instance
*/
- protected ClientDocument(VamsasDocument doc, VamsasArchive docHandler, IdFactory Factory, SimpleClient vorba) {
+ protected ClientDocument(VamsasDocument doc, VamsasArchive docHandler, IdFactory Factory, SimpleClient sclient) {
super(Factory.getVorbaIdHash(), Factory);
/**
* prepare Application-side dataset from the vamsas Document archive
*/
- this.sclient = vorba;
+ this.sclient = sclient;
archive = docHandler;
this.doc = doc;
}
// TODO: getObject in bulk
return null;
}
-
+ /**
+ * internal reference to single copy of Document Roots array
+ */
+ private VAMSAS[] _VamsasRoots=null;
/*
* (non-Javadoc)
- *
+ * LATER: currently there is only one Vector of roots ever passed to client - decide if this is correct (means this is not thread safe and may behave unexpectedly)
* @see org.vamsas.client.IClientDocument#getVamsasRoots()
*/
public VAMSAS[] getVamsasRoots() {
- if (doc.getVAMSAS() == null)
+ if (_VamsasRoots!=null)
+ return _VamsasRoots;
+ VAMSAS[] roots = doc.getVAMSAS();
+ if (roots == null) {
// Make a new one to return to client : TODO: Decide if this is correct
- return new VAMSAS[] { new VAMSAS() };
- return doc.getVAMSAS();
+ _VamsasRoots = new VAMSAS[] { new VAMSAS() };
+ } else {
+ _VamsasRoots = new VAMSAS[roots.length];
+ for (int r=0;r<roots.length; r++)
+ _VamsasRoots[r] = roots[r];
+ }
+ return _VamsasRoots;
+ }
+
+ private int _contains(VAMSAS root, VAMSAS[] docRoots) {
+ if (root==null)
+ return -1;
+ if (docRoots==null || docRoots.length==0)
+ return -1;
+ String r_id = root.getId();
+ for (int i=0,j=docRoots.length; i<j; i++)
+ if (docRoots[i]==root || (docRoots[i]!=null && docRoots[i].getId().equals(r_id)))
+ return i;
+ return -1;
+ }
+/**
+ * verify that newr version is really an intact version of the
+ * @param newVersion (may be modified)
+ * @param oldVersion
+ * @return true if newVersion is a valid root that preserves original references
+ */
+ private boolean isValidUpdate(VAMSAS newVersion, final VAMSAS oldVersion) {
+ // LATER: IMPLEMENT
+ // 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);
+ // Should retrieve original version and compare - unless local hashes can be used to determine if resultSet has been truncated.
+ // just do internal validation for moment.
+ if (newVersion.isValid())
+ return true;
+ return false;
+ } else {
+ // redundant ? if (oldVersion.is__stored_in_document())
+ if (!newVersion.isRegistered())
+ _registerObject(newVersion);
+ if (newVersion.isValid())
+ return true;
+ }
+ return false;
+ }
+ /**
+ * merge old and new root vectors
+ * @param newr This array may be written to
+ * @param original
+ * @param the client document (usually this) which this root set belongs to.
+ * @return merged vector of vamsas roots
+ */
+ 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;
+ rts.add(newr[k]);
+ newr[k]=null;
+ } else {
+ // LATER: try harder to merge ducument roots.
+ log.warn("Couldn't merge new VAMSAS root "+newr[k].getId());
+ newr[k] = null; // LATER: this means we ignore mangled roots. NOT GOOD
+ }
+ } else {
+ // add in order.
+ rts.add(original[i]);
+ }
+ }
+ // add remaining (new) roots
+ for (int i=0,j=newr.length; i<j; i++) {
+ if (newr[i]!=null) {
+ rts.add(newr[i]);
+ modified=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;
}
/**
* update the document with new roots.
+ * LATER: decide: this affects the next call to getVamsasRoots()
*/
public void setVamsasRoots(VAMSAS[] newroots) {
+
+ // are we dealing with same array ?
+ if (_VamsasRoots!=newroots) {
+ // merge roots into
+ }
// extract root objects
if (newroots != null) {
// check newroots for objects that were present in the old document
// easy - just check if anything has changed and do provenance
Vobject oldversion = getObject(newroots[i].getVorbaId());
if (oldversion instanceof VAMSAS) {
- // LATER: appropriate merging behaviour when two clients have
- // modified the same registered Vobject independently
+ // LATER: appropriate merging behaviour when two clients have improperly modified the same Vobject independently.
if (newroots[i].get__last_hash() != newroots[i].hashCode()) {
// client has modified this Vobject since last retrieval.
if (newroots[i].get__last_hash() != oldversion.get__last_hash()) {
}
/* (non-Javadoc)
+ * LATER: decide: this affects the next call to getVamsasRoots()
* @see org.vamsas.client.IClientDocument#addVamsasRoot(org.vamsas.objects.core.VAMSAS)
*/
public void addVamsasRoot(VAMSAS newroot) {
- // TODO Auto-generated method stub
-
+ VAMSAS[] newroots = _combineRoots(new VAMSAS[] {newroot}, _VamsasRoots, this);
+ _VamsasRoots = newroots;
}
/*
if (unregistered!=null) {
VorbaId ids[] = new VorbaId[unregistered.length];
for (int i=0,k=unregistered.length; i<k; i++)
- ids[i]=registerObject(unregistered[i]);
+ if (unregistered[i]!=null) {
+ log.warn("Null Vobject passed to registerObject[] at position "+i);
+ return null;
+ } else {
+ ids[i]=registerObject(unregistered[i]);
+ }
return ids;
}
return null;
}
-
/* (non-Javadoc)
* @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.Vobject)
*/
public VorbaId registerObject(Vobject unregistered) {
- // TODO: add provenance stuff to newly registered Vobject
- return _registerObject(unregistered);
+ if (unregistered!=null)
+ return _registerObject(unregistered);
+ log.warn("Null Vobject passed to registerObject.");
+ return null;
}
/**
* IClientAppdata instance - if it exists.
* @return the session's vamsas document
*/
protected VamsasDocument getVamsasDocument() {
- // TODO: IMPLEMENT
- return null;
+ return doc;
}
/**
* returns the read-only IO interface for the vamsas document Jar file
* @return
*/
protected VamsasArchiveReader getVamsasArchiveReader() {
- // TODO: IMPLEMENT getVamsasArchiveReader
+ try {
+ return archive.getOriginalArchiveReader();
+ } catch (Exception e) {
+ log.warn("Unable to create OriginalArchiveReader!", e);
+ }
return null;
}
}