040222a03250f46bf4e7b748eeaa2b3b9db12064
[vamsas.git] / src / org / vamsas / client / object.java
1 /**
2  * 
3  */
4 package org.vamsas.client;
5
6 /**
7  * Base class for all Vamsas objects extracted from an IClientDocument. An
8  * object maybe registered or unregistered.
9  * 
10  * @author jimp
11  * 
12  */
13 public abstract class object {
14
15   /**
16    * unique id for all vamsas objects allows unambiguous referencing to any
17    * object in the vamsas document
18    */
19   protected boolean __stored_in_document = false;
20
21   protected long __last_hash = 0;
22
23   protected boolean registerable = false;
24
25   protected VorbaId vorbaId = null;
26
27   protected IVorbaIdFactory __vorba = null;
28
29   /**
30    * 
31    */
32   protected object() {
33     super();
34     testInstanceForIdField();
35   }
36
37   /**
38    * set the isRegisterable flag based on the presence of a 'String id' field in
39    * the reflected class instance.
40    */
41   private void testInstanceForIdField() {
42     // look for the id field (should be an NCName string)
43     // TODO: decide if 'id' is an appropriate reserved attribute name for the
44     // VorbaId
45     try {
46       java.lang.reflect.Field fd = this.getClass().getField("id");
47       if (fd.getType().getClass().equals("astring".getClass())) {
48         this.setRegisterable(true);
49       }
50     } catch (SecurityException e) {
51       e.printStackTrace();
52     } catch (NoSuchFieldException e) {
53       this.setRegisterable(false);
54     }
55   }
56
57   /**
58    * update the object instance's String id field, based on the contents of the
59    * VorbaId. Only call this if you mean to do it!
60    */
61   protected void setInstanceIdField() {
62     if (registerable) {
63       if (vorbaId != null)
64         try {
65           java.lang.reflect.Field fd = this.getClass().getField("id");
66           fd.set((Object) this, (Object) new String(vorbaId.id));
67         } catch (IllegalAccessException e) {
68           System.err
69               .println("SourceGeneration of "
70                   + this.getClass().toString()
71                   + "\n has resulted in an inaccessible 'id' field!\nCannot set ID from the vorbaId object.");
72           e.printStackTrace(System.err);
73         } catch (SecurityException e) {
74           e.printStackTrace(System.err);
75         } catch (NoSuchFieldException e) {
76           this.setRegisterable(false);
77         }
78     } else {
79       System.err.println("Client error. Trying to setInstanceIdField on a "
80           + this.getClass().toString() + " (which cannot be given a vorbaId)");
81     }
82   }
83
84   /**
85    * calculate a hash for the object with all housekeeping fields at standard
86    * values. (isRegisterable is an immutable attribute property)
87    */
88   synchronized protected void doHash() {
89     __last_hash = 0;
90     VorbaId thisid = vorbaId;
91     IVorbaIdFactory factory = __vorba;
92     boolean stored = __stored_in_document;
93     vorbaId = null;
94     __vorba = null;
95     __last_hash = this.hashCode();
96     vorbaId = thisid;
97     __vorba = factory;
98     __stored_in_document = stored;
99   }
100
101   /**
102    * TODO: combine two versions of the same collection object to resolve
103    * asynchronous updates to the same vamsas object Merges two vamsas objects,
104    * one of which is a later version of the earlier (ie they have the same
105    * vorbaId but one is a later version recently read from the vamsasDocument
106    * collection.
107    * 
108    * @return
109    */
110   protected boolean merge(object laterCopy) {
111     return true;
112   }
113
114   /**
115    * 
116    * @return true if object is registered
117    */
118   public boolean isRegistered() {
119     return (registerable) ? (vorbaId != null) : false;
120   }
121
122   /**
123    * Method to get fixed reference for the object in the vamsas document.
124    * 
125    * @returns null if object is neither registered or not associated with a
126    *          properly instantiated VorbaIdFactory.
127    */
128   public VorbaId getVorbaId() {
129     if (registerable && vorbaId == null) {
130       // Try to use the associated factory.
131       if (__vorba != null)
132         if ((vorbaId = __vorba.makeVorbaId()) == null)
133           return null; // Factory not valid.
134         else {
135           this.setInstanceIdField();
136           return vorbaId;
137         }
138     }
139     return vorbaId;
140   }
141
142   /**
143    * used by the IClient implementation to generate unique Id based on client
144    * applications current namespace.
145    */
146   protected void setVorbaId(VorbaId newid) {
147     vorbaId = newid;
148   }
149
150   /**
151    * @return true if object is present in Vamsas Document.
152    */
153   public boolean is__stored_in_document() {
154     return __stored_in_document;
155   }
156
157   /**
158    * for use by Vorba agent to reflect state of vamsas object to client
159    * application.
160    * 
161    * @param __stored_in_document
162    *          The __stored_in_document to set.
163    */
164   protected void set__stored_in_document(boolean __stored_in_document) {
165     this.__stored_in_document = __stored_in_document;
166   }
167
168   /**
169    * __last_hash is the hash value computed when the object was last checked
170    * against a IClientDocument generated by the object's parent IClient
171    * instance.
172    * 
173    * @return Returns the __last_hash.
174    */
175   public long get__last_hash() {
176     return __last_hash;
177   }
178
179   /**
180    * @return Returns the registerable.
181    */
182   public boolean isRegisterable() {
183     return registerable;
184   }
185
186   /**
187    * Called during unmarshalling - if object has an id string, it is
188    * registerable.
189    * 
190    * @param registerable
191    *          The registerable to set.
192    */
193   protected void setRegisterable(boolean registerable) {
194     this.registerable = registerable;
195   }
196 }