forgot to release a trial lock that failed.
[vamsas.git] / src / org / vamsas / client / object.java
index c34d771..040222a 100644 (file)
 /**
- * org.vamsas.client.object
  * 
  */
-
 package org.vamsas.client;
 
+/**
+ * Base class for all Vamsas objects extracted from an IClientDocument. An
+ * object maybe registered or unregistered.
+ * 
+ * @author jimp
+ * 
+ */
 public abstract class object {
 
-    /**
-     * unique id for all vamsas objects
-     * allows unambiguous referencing
-     * to any object in the vamsas document
-     */
-
-    protected String __vorba_id;
-    /**
-     * Method to get fixed reference for
-     * the object in the vamsas document.
-     */
-    public string getVorbaId();
-    /**
-     * used by the Iclient implementation
-     * to generate unique Id based on
-     * client applications current namespace.
-     */
-    protected string setVorbaId();
+  /**
+   * unique id for all vamsas objects allows unambiguous referencing to any
+   * object in the vamsas document
+   */
+  protected boolean __stored_in_document = false;
+
+  protected long __last_hash = 0;
+
+  protected boolean registerable = false;
+
+  protected VorbaId vorbaId = null;
+
+  protected IVorbaIdFactory __vorba = null;
+
+  /**
+   * 
+   */
+  protected object() {
+    super();
+    testInstanceForIdField();
+  }
+
+  /**
+   * set the isRegisterable flag based on the presence of a 'String id' field in
+   * 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().getField("id");
+      if (fd.getType().getClass().equals("astring".getClass())) {
+        this.setRegisterable(true);
+      }
+    } catch (SecurityException e) {
+      e.printStackTrace();
+    } catch (NoSuchFieldException e) {
+      this.setRegisterable(false);
+    }
+  }
+
+  /**
+   * update the object instance's String id field, based on the contents of the
+   * VorbaId. Only call this if you mean to do it!
+   */
+  protected void setInstanceIdField() {
+    if (registerable) {
+      if (vorbaId != 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);
+        } catch (SecurityException e) {
+          e.printStackTrace(System.err);
+        } catch (NoSuchFieldException e) {
+          this.setRegisterable(false);
+        }
+    } else {
+      System.err.println("Client error. Trying to setInstanceIdField on a "
+          + 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)
+   */
+  synchronized protected void doHash() {
+    __last_hash = 0;
+    VorbaId thisid = vorbaId;
+    IVorbaIdFactory factory = __vorba;
+    boolean stored = __stored_in_document;
+    vorbaId = null;
+    __vorba = null;
+    __last_hash = this.hashCode();
+    vorbaId = thisid;
+    __vorba = factory;
+    __stored_in_document = stored;
+  }
+
+  /**
+   * 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
+   */
+  public boolean isRegistered() {
+    return (registerable) ? (vorbaId != null) : false;
+  }
+
+  /**
+   * Method to get fixed reference for the object in the vamsas document.
+   * 
+   * @returns null if object is neither registered or not associated with a
+   *          properly instantiated VorbaIdFactory.
+   */
+  public VorbaId getVorbaId() {
+    if (registerable && vorbaId == null) {
+      // Try to use the associated factory.
+      if (__vorba != null)
+        if ((vorbaId = __vorba.makeVorbaId()) == null)
+          return null; // Factory not valid.
+        else {
+          this.setInstanceIdField();
+          return vorbaId;
+        }
+    }
+    return vorbaId;
+  }
+
+  /**
+   * used by the IClient implementation to generate unique Id based on client
+   * applications current namespace.
+   */
+  protected void setVorbaId(VorbaId newid) {
+    vorbaId = newid;
+  }
+
+  /**
+   * @return true if object is present in Vamsas Document.
+   */
+  public boolean is__stored_in_document() {
+    return __stored_in_document;
+  }
+
+  /**
+   * for use by Vorba agent to reflect state of vamsas object to client
+   * application.
+   * 
+   * @param __stored_in_document
+   *          The __stored_in_document to set.
+   */
+  protected void set__stored_in_document(boolean __stored_in_document) {
+    this.__stored_in_document = __stored_in_document;
+  }
+
+  /**
+   * __last_hash is the hash value computed when the object was last checked
+   * against a IClientDocument generated by the object's parent IClient
+   * instance.
+   * 
+   * @return Returns the __last_hash.
+   */
+  public long get__last_hash() {
+    return __last_hash;
+  }
+
+  /**
+   * @return Returns the registerable.
+   */
+  public boolean isRegisterable() {
+    return registerable;
+  }
+
+  /**
+   * Called during unmarshalling - if object has an id string, it is
+   * registerable.
+   * 
+   * @param registerable
+   *          The registerable to set.
+   */
+  protected void setRegisterable(boolean registerable) {
+    this.registerable = registerable;
+  }
 }