bugfixed org.vamsas.test.simpleclient.ArchiveClient and made VorbaId mechanism more...
[vamsas.git] / src / org / vamsas / client / Vobject.java
index a393b9e..1c7388f 100644 (file)
@@ -39,7 +39,10 @@ public abstract class Vobject {
    */
   protected boolean registerable = false; 
 
-
+  /**
+   * reference to containing object for this Vobject.
+   */
+  protected Vobject V_parent=null;
   /**
    * unique id for all vamsas objects allows unambiguous referencing to any
    * Vobject in the vamsas document
@@ -50,6 +53,16 @@ public abstract class Vobject {
    * the source of unique VorbaIds.
    */
   protected IVorbaIdFactory __vorba = null;
+  
+  /* (non-Javadoc)
+   * @see java.lang.Object#finalize()
+   */
+  protected void finalize() throws Throwable {
+    V_parent=null;
+    __vorba=null;
+    vorbaId=null;
+    super.finalize();
+  }
 
   /**
    * 
@@ -64,25 +77,35 @@ public abstract class Vobject {
    * the reflected class instance.
    */
   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
-    try {
-      java.lang.reflect.Field fd = this.getClass().getDeclaredField("_id");
-      if (String.class.isAssignableFrom(fd.getType())) {
-        this.setRegisterable(true);
+    // look for the id field in all castor classes (should be an NCName string)
+    
+    Class thisclass=this.getClass();
+    setRegisterable(false);
+    while (!thisclass.equals(Vobject.class)) {
+      try {
+        java.lang.reflect.Field fd = thisclass.getDeclaredField("_id");
+        if (String.class.isAssignableFrom(fd.getType())) {
+          this.setRegisterable(true);
+          break;
+        }
+      } catch (SecurityException e) {
+        log.error("Unexpected Security Exception whilst finding id fields to set!",e);
+      } catch (NoSuchFieldException e) {
+        thisclass=thisclass.getSuperclass();
       }
-    } catch (SecurityException e) {
-      e.printStackTrace();
-    } catch (NoSuchFieldException e) {
-      this.setRegisterable(false);
     }
   }
-
+  // boolean __testedInstance=false;
   /**
    * update the Vobject instance's _id field, based on the contents of the
    * VorbaId. Only call this if you mean to do it!
    */
   protected void setInstanceIdField() {
+    /*if (!registerable && !__testedInstance) {
+      testInstanceForIdField();
+      __testedInstance=true;
+    }*/
     if (registerable) {
       if (__vorba != null)
         try {
@@ -119,6 +142,8 @@ public abstract class Vobject {
   synchronized protected boolean doHash() {
     long __old_hash = __last_hash;
     __last_hash = 0;
+    Vobject _V_parent=V_parent;
+    V_parent=null;
     VorbaId thisid = vorbaId;
     IVorbaIdFactory factory = __vorba;
     boolean stored = __stored_in_document;
@@ -128,6 +153,7 @@ public abstract class Vobject {
     vorbaId = thisid;
     __vorba = factory;
     __stored_in_document = stored;
+    V_parent=_V_parent;
     return (__old_hash==0) || (__old_hash == __last_hash);
   }
 
@@ -160,6 +186,10 @@ public abstract class Vobject {
    *          properly instantiated VorbaIdFactory.
    */
   public VorbaId getVorbaId() {
+    /*if (!registerable && !__testedInstance) {
+      testInstanceForIdField();
+      __testedInstance=true;
+    }*/
     if (registerable && vorbaId == null) {
       // Try to use the associated factory.
       if (__vorba != null)
@@ -270,6 +300,8 @@ public abstract class Vobject {
               for (int k=0, l=vals.length; k<l; k++) {
                 if (vals[k].__vorba==null)
                   vals[k].__vorba = __vorba; // propagate IVorbaIdFactory
+                if (vals[k].V_parent==null)
+                  vals[k].V_parent=this; // propagate parent reference to this element.
                 vals[k].__ensure_instance_ids();
               }
             }
@@ -293,6 +325,8 @@ public abstract class Vobject {
                       for (int k=0, l=vals.length; k<l; k++) {
                         if (vals[k].__vorba==null)
                           vals[k].__vorba = __vorba; // propagate IVorbaIdFactory
+                        if (vals[k].V_parent==null)
+                          vals[k].V_parent=this; // propagate parent reference to this field object
                         vals[k].__ensure_instance_ids();
                       }
                     }
@@ -306,6 +340,7 @@ public abstract class Vobject {
                 }
               }
             } else {
+              // castor's mechanism doesn't work for this object... so...*/
               // fuck around, fuck around, jump up jump up and get down! */
               Object o = fields[i].getClassDescriptor();
               if (o!=null) {
@@ -324,7 +359,9 @@ public abstract class Vobject {
             if (rf!=null) {
               if (rf.__vorba==null)
                 rf.__vorba = __vorba; // propagate IVorbaIdFactory
-              rf.__ensure_instance_ids();
+              if (rf.V_parent==null)
+                rf.V_parent=this; // propagate parent reference
+             rf.__ensure_instance_ids();
             }
           }
           catch (Exception e) {
@@ -334,4 +371,18 @@ public abstract class Vobject {
     }
     
   }
+
+  /**
+   * @return the __parent
+   */
+  public Vobject getV_parent() {
+    return V_parent;
+  }
+
+  /**
+   * @param __parent the __parent to set
+   */
+  protected void setV_parent(Vobject V_parent) {
+    this.V_parent = V_parent;
+  }
 }