X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Forg%2Fvamsas%2Fclient%2Fobject.java;h=6498d7047a59945313fbff895c6c065a90cdc886;hb=8d474dd77c01354948a5db3ee135e04af8f97503;hp=fc13b4fedc618eee815a18f32d2936b33f93fd98;hpb=49874b2de657c9b2cfbb00d7600941f7cb7f2552;p=vamsas.git diff --git a/src/org/vamsas/client/object.java b/src/org/vamsas/client/object.java index fc13b4f..6498d70 100644 --- a/src/org/vamsas/client/object.java +++ b/src/org/vamsas/client/object.java @@ -3,6 +3,18 @@ */ package org.vamsas.client; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Iterator; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.exolab.castor.mapping.FieldDescriptor; +import org.exolab.castor.mapping.FieldHandler; +import org.exolab.castor.xml.util.XMLClassDescriptorImpl; +import org.vamsas.test.simpleclient.VamsasArchive; + /** * Base class for all Vamsas objects extracted from an IClientDocument. An * object maybe registered or unregistered. @@ -11,7 +23,8 @@ package org.vamsas.client; * */ public abstract class object { - + static Log log = LogFactory.getLog(object.class); + /** * unique id for all vamsas objects allows unambiguous referencing to any * object in the vamsas document @@ -40,11 +53,10 @@ public abstract class object { */ private void testInstanceForIdField() { // look for the id field (should be an NCName string) - // TODO: decide if 'id' is an appropriate reserved attribute name for the - // VorbaId + // TODO: decide if 'id' is an appropriate reserved attribute name for the VorbaId try { - java.lang.reflect.Field fd = this.getClass().getField("id"); - if (fd.getType().getClass().equals("astring".getClass())) { + java.lang.reflect.Field fd = this.getClass().getDeclaredField("_id"); + if (String.class.isAssignableFrom(fd.getType())) { this.setRegisterable(true); } } catch (SecurityException e) { @@ -60,25 +72,35 @@ public abstract class object { */ protected void setInstanceIdField() { if (registerable) { - if (vorbaId!=null) + if (__vorba != null) try { - java.lang.reflect.Field fd = this.getClass().getField("id"); - fd.set((Object) this, (Object) new String(vorbaId.id)); - } catch (IllegalAccessException e) { - System.err.println("SourceGeneration of "+this.getClass().toString() - +"\n has resulted in an inaccessible 'id' field!\nCannot set ID from the vorbaId object.") - e.printStackTrace(System.err); + Method fd = this.getClass().getMethod("setId", new Class[] { String.class }); + fd.invoke((Object) this, new Object[] {new String(this.getVorbaId().id)}); + log.info(this.getClass().getName()+" called setInstanceIdField!"); + } catch (InvocationTargetException e) { + System.err + .println("SourceGeneration of " + + this.getClass().toString() + + "\n has resulted in an inaccessible 'setId' method!\nCannot set ID from the vorbaId object."); + e.printStackTrace(System.err); } - catch (SecurityException e) { + catch (IllegalAccessException e) { + System.err + .println("SourceGeneration of " + + this.getClass().toString() + + "\n has resulted in an inaccessible 'setId' method!\nCannot set ID from the vorbaId object."); e.printStackTrace(System.err); - } catch (NoSuchFieldException e) { + } catch (SecurityException e) { + e.printStackTrace(System.err); + } catch (NoSuchMethodException e) { this.setRegisterable(false); } } else { System.err.println("Client error. Trying to setInstanceIdField on a " - +this.getClass().toString()+" (which cannot be given a vorbaId)"); + + this.getClass().toString() + " (which cannot be given a vorbaId)"); } } + /** * calculate a hash for the object with all housekeeping fields at standard * values. (isRegisterable is an immutable attribute property) @@ -97,6 +119,19 @@ public abstract class object { } /** + * TODO: combine two versions of the same collection object to resolve + * asynchronous updates to the same vamsas object Merges two vamsas objects, + * one of which is a later version of the earlier (ie they have the same + * vorbaId but one is a later version recently read from the vamsasDocument + * collection. + * + * @return + */ + protected boolean merge(object laterCopy) { + return true; + } + + /** * * @return true if object is registered */ @@ -178,4 +213,103 @@ public abstract class object { protected void setRegisterable(boolean registerable) { this.registerable = registerable; } + /** + * ensure's internal id field corresponds to vorbaId and + * cascade through all fields referring to an instance of object + * calling the same method on them. + * TODO: LATER: properly apply castors own field mechanisms to get at accessors + * + */ + protected void __ensure_instance_ids() { + if (__vorba==null) + throw new Error("Improperly intialised org.vamsas.client.object - no VorbaFactory given."); + log.info(this.getClass()+".__ensure_instance_ids()"); + if (!__stored_in_document && registerable) + setInstanceIdField(); + Class descriptor = null; + XMLClassDescriptorImpl descimpl = null; + try { + // castor descriptor resolver magic + descriptor = this.getClass().getClassLoader().loadClass(this.getClass().getName()+"Descriptor"); + descimpl = (XMLClassDescriptorImpl) descriptor.getConstructor(null).newInstance(null); + } catch (Exception e) { + log.error("Couldn't resolve descriptor for "+this.getClass().getName()); + return; + } + FieldDescriptor fields[] = descimpl.getFields(); + for (int i=0,j=fields.length; i