implemented state flags and automatic call to VorbaIdFactory for unregistered (but...
[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.println("SourceGeneration of "+this.getClass().toString()
69               +"\n has resulted in an inaccessible 'id' field!\nCannot set ID from the vorbaId object.")
70               e.printStackTrace(System.err);
71         }
72         catch (SecurityException e) {
73           e.printStackTrace(System.err);
74         } catch (NoSuchFieldException e) {
75           this.setRegisterable(false);
76         }
77     } else {
78       System.err.println("Client error. Trying to setInstanceIdField on a "
79           +this.getClass().toString()+" (which cannot be given a vorbaId)");
80     }
81   }
82   /**
83    * calculate a hash for the object with all housekeeping fields at standard
84    * values. (isRegisterable is an immutable attribute property)
85    */
86   synchronized protected void doHash() {
87     __last_hash = 0;
88     VorbaId thisid = vorbaId;
89     IVorbaIdFactory factory = __vorba;
90     boolean stored = __stored_in_document;
91     vorbaId = null;
92     __vorba = null;
93     __last_hash = this.hashCode();
94     vorbaId = thisid;
95     __vorba = factory;
96     __stored_in_document = stored;
97   }
98
99   /**
100    * 
101    * @return true if object is registered
102    */
103   public boolean isRegistered() {
104     return (registerable) ? (vorbaId != null) : false;
105   }
106
107   /**
108    * Method to get fixed reference for the object in the vamsas document.
109    * 
110    * @returns null if object is neither registered or not associated with a
111    *          properly instantiated VorbaIdFactory.
112    */
113   public VorbaId getVorbaId() {
114     if (registerable && vorbaId == null) {
115       // Try to use the associated factory.
116       if (__vorba != null)
117         if ((vorbaId = __vorba.makeVorbaId()) == null)
118           return null; // Factory not valid.
119         else {
120           this.setInstanceIdField();
121           return vorbaId;
122         }
123     }
124     return vorbaId;
125   }
126
127   /**
128    * used by the IClient implementation to generate unique Id based on client
129    * applications current namespace.
130    */
131   protected void setVorbaId(VorbaId newid) {
132     vorbaId = newid;
133   }
134
135   /**
136    * @return true if object is present in Vamsas Document.
137    */
138   public boolean is__stored_in_document() {
139     return __stored_in_document;
140   }
141
142   /**
143    * for use by Vorba agent to reflect state of vamsas object to client
144    * application.
145    * 
146    * @param __stored_in_document
147    *          The __stored_in_document to set.
148    */
149   protected void set__stored_in_document(boolean __stored_in_document) {
150     this.__stored_in_document = __stored_in_document;
151   }
152
153   /**
154    * __last_hash is the hash value computed when the object was last checked
155    * against a IClientDocument generated by the object's parent IClient
156    * instance.
157    * 
158    * @return Returns the __last_hash.
159    */
160   public long get__last_hash() {
161     return __last_hash;
162   }
163
164   /**
165    * @return Returns the registerable.
166    */
167   public boolean isRegisterable() {
168     return registerable;
169   }
170
171   /**
172    * Called during unmarshalling - if object has an id string, it is
173    * registerable.
174    * 
175    * @param registerable
176    *          The registerable to set.
177    */
178   protected void setRegisterable(boolean registerable) {
179     this.registerable = registerable;
180   }
181 }