*/
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.
*
*/
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
*/
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) {
*/
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)
}
/**
+ * 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
*/
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<j; i++) {
+ Class type= fields[i].getFieldType();
+ if (type.isArray()) {
+ if (object[].class.isAssignableFrom(type)) {
+ try {
+ Object val = fields[i].getHandler().getValue(this);
+ if (val!=null) {
+ object vals[] = (object[]) val;
+ for (int k=0, l=vals.length; k<l; k++) {
+ if (vals[k].__vorba==null)
+ vals[k].__vorba = __vorba; // propagate IVorbaIdFactory
+ vals[k].__ensure_instance_ids();
+ }
+ }
+ }
+ catch (Exception e) {
+ log.error("Client error - could not access array "+type.getName()+" in "+this.getClass().getName(), e);
+ }
+ }
+ } else
+ if (object.class.isAssignableFrom(type)) {
+ try {
+ FieldHandler fh = fields[i].getHandler();
+ object rf = null;
+ if (fh != null) {
+ Object fval = fh.getValue(this);
+ if (fval!=null) {
+ if (fval.getClass().isArray()) {
+ //if (object[].class.isAssignableFrom(type)) {
+ try {
+ object vals[] = (object[]) fval;
+ for (int k=0, l=vals.length; k<l; k++) {
+ if (vals[k].__vorba==null)
+ vals[k].__vorba = __vorba; // propagate IVorbaIdFactory
+ vals[k].__ensure_instance_ids();
+ }
+ }
+ catch (Exception e) {
+ log.error("Client error - could not access (fhval)array "+type.getName()+" in "+this.getClass().getName(), e);
+ }
+ //}
+ } else {
+ rf = (object) fval;
+ log.info("Got value for "+fields[i].getFieldName());
+ }
+ }
+ } else {
+ // fuck around, fuck around, jump up jump up and get down! */
+ Object o = fields[i].getClassDescriptor();
+ if (o!=null) {
+ // XMLClassDescriptorImpl fclasdes = (XMLClassDescriptorImpl) o;
+ String methname = "get"+fields[i].getFieldName();
+ Method fgetmeth = this.getClass().getMethod(methname,null);
+ if (fgetmeth!=null) {
+ Object fval = fgetmeth.invoke(this,null);
+ if (fval!=null)
+ rf = (object) fval;
+ } else {
+ log.warn("Couldn't find "+this.getClass().getName()+"."+methname);
+ }
+ }
+ }
+ if (rf!=null) {
+ if (rf.__vorba==null)
+ rf.__vorba = __vorba; // propagate IVorbaIdFactory
+ rf.__ensure_instance_ids();
+ }
+ }
+ catch (Exception e) {
+ log.error("Client error - could not access "+type.getName()+" in "+this.getClass().getName(), e);
+ }
+ }
+ }
+
+ }
}