+ _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;