KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > acl > WMObject


1 package de.webman.acl;
2
3 import com.teamkonzept.lib.TKException;
4 import com.teamkonzept.lib.TKVector;
5 import de.webman.acl.db.ObjectDBData;
6
7 /**
8  * Base class of persistent objects. Provides a means for identification
9  * throughout the object system as well as the database system. Creation of
10  * such objects should be left to a corresponding factory to ensure uniqueness.
11  *
12  * @author $Author: mischa $
13  * @version $Revision: 1.1 $
14  */

15 public abstract class WMObject
16 {
17
18     // $Header: /cvsroot/webman-cms/source/webman/de/webman/acl/WMObject.java,v 1.1 2001/08/20 08:25:07 mischa Exp $
19

20     // Attributes
21

22     /**
23      * Specifies wether at least one of the attributes has been modified.
24      */

25     private boolean attributesModified = false;
26
27     /**
28      * Specifies wether at least one of the associations has been modified.
29      */

30     private boolean associationsModified = false;
31
32     /**
33      * Specifies wether the associations has been loaded.
34      */

35     private boolean associationsLoaded = false;
36
37     /**
38      * The ID of the persistent object.
39      */

40     private Integer JavaDoc id = null;
41
42     /**
43      * The string representation of the persistent object.
44      */

45     private String JavaDoc string = null;
46
47     /**
48      * The IDs of the associated persistent objects.
49      */

50     private TKVector associations = null;
51
52
53     // Constructors
54

55     /**
56      * Provide instantion only to package classes or subclasses.
57      *
58      * @param data the initial object data.
59      */

60     protected WMObject (ObjectDBData data)
61     {
62         this.id = data.getID();
63     }
64
65
66     // Standard methods
67

68     /**
69      * Checks wether the persistent and the given object can be treated as equal.
70      *
71      * @param object the object to be proved upon equality.
72      * @return <CODE>true</CODE> if both objects can be treated as equal,
73      * otherwise <CODE>false</CODE>.
74      */

75     public final boolean equals (Object JavaDoc object)
76     {
77         if (object == null)
78         {
79             return false;
80         }
81
82         if (object.getClass() != getClass())
83         {
84             return false;
85         }
86
87         return ((WMObject) object).getID().equals(this.id);
88     }
89
90     /**
91      * Returns the hash code of the persistent object.
92      *
93      * @return the hash code of the persistent object.
94      */

95     public final int hashCode ()
96     {
97         return this.id.intValue();
98     }
99
100     /**
101      * Returns the string representation of the persistent object.
102      *
103      * @return the string representation of the persistent object.
104      */

105     public final String JavaDoc toString ()
106     {
107         if (this.string == null)
108         {
109             this.string = (new StringBuffer JavaDoc(getClass().getName())).append('.')
110                                                                   .append(id)
111                                                                   .toString();
112         }
113
114         return this.string;
115     }
116
117
118     // Method signatures
119

120     /**
121      * Returns the factory of the object.
122      *
123      * @return the factory of the object.
124      * @exception com.teamkonzept.lib.TKException if an error occured during factory retrieval.
125      */

126     public abstract ObjectFactory getFactory ()
127         throws TKException;
128
129
130     // Method implementations
131

132     /**
133      * Returns the ID of the persistent object.
134      *
135      * @return the ID of the persistent object.
136      */

137     public final Integer JavaDoc getID ()
138     {
139         return id;
140     }
141
142     /**
143      * Checks wether at least one of the attributes has been modified.
144      *
145      * @return <CODE>true</CODE> at least one of the attributes has been
146      * modified, otherwise <CODE>false</CODE>.
147      */

148     public final boolean isModifiedAttributes ()
149     {
150         return this.attributesModified;
151     }
152
153     /**
154      * Checks wether at least one of the associations has been modified.
155      *
156      * @return <CODE>true</CODE> at least one of the associations has been
157      * modified, otherwise <CODE>false</CODE>.
158      */

159     public final boolean isModifiedAssociations ()
160     {
161         return this.associationsModified;
162     }
163
164
165     // Attribute handling methods
166

167     /**
168      * Informs the object about the modification of one of its attributes.
169      *
170      * @param pre the value of the attribute before the modification.
171      * @param post the value of the attribute after the modification.
172      */

173     protected final void modifyAttribute (Object JavaDoc pre, Object JavaDoc post)
174     {
175         if (pre != null || post != null)
176         {
177             try
178             {
179                 this.attributesModified = this.attributesModified ||
180                                           ! pre.equals(post);
181             }
182             catch (NullPointerException JavaDoc npe)
183             {
184                 this.attributesModified = true;
185             }
186         }
187     }
188
189     /**
190      * Informs the object about the succesful update of its attributes.
191      */

192     protected final void updatedAttributes ()
193     {
194         this.attributesModified = false;
195     }
196
197
198     // Association handling methods
199

200     /**
201      * Returns the IDs of all associated objects.
202      *
203      * @return the IDs of all associated objects.
204      * @exception com.teamkonzept.lib.TKException if an error occured during object retrieval.
205      */

206     protected final TKVector getAssociations ()
207         throws TKException
208     {
209         loadAssociations();
210
211         return this.associations;
212     }
213
214     /**
215      * Adds an association with the given object.
216      *
217      * @param object the object.
218      * @exception com.teamkonzept.lib.TKException if an error occured during object retrieval.
219      */

220     protected final void addAssociation (WMObject object)
221         throws TKException
222     {
223         loadAssociations();
224
225         if (object != null)
226         {
227             if (this.associations == null)
228             {
229                 this.associations = new TKVector();
230             }
231
232             if (! this.associations.contains(object.getID()))
233             {
234                 this.associations.addElement(object.getID());
235                 this.associationsModified = true;
236             }
237         }
238     }
239
240     /**
241      * Removes the association with the given object.
242      *
243      * @param object the object.
244      * @exception com.teamkonzept.lib.TKException if an error occured during object retrieval.
245      */

246     protected final void removeAssociation (WMObject object)
247         throws TKException
248     {
249         loadAssociations();
250
251         if (object != null &&
252             this.associations != null)
253         {
254             this.associationsModified = this.associationsModified ||
255                                         this.associations.removeElement(object.getID());
256         }
257     }
258
259     /**
260      * Removes the association with all objects.
261      *
262      * @exception com.teamkonzept.lib.TKException if an error occured during object retrieval.
263      */

264     protected final void removeAssociations ()
265         throws TKException
266     {
267         loadAssociations();
268
269         if (this.associations != null &&
270             this.associations.size() > 0)
271         {
272             this.associations.removeAllElements();
273             this.associationsModified = true;
274         }
275     }
276
277     /**
278      * Checks wether there is an association with the given object.
279      *
280      * @param object the object.
281      * @return <CODE>true</CODE> if there is an association with the
282      * given object, otherwise <CODE>false</CODE>.
283      * @exception com.teamkonzept.lib.TKException if an error occured during object retrieval.
284      */

285     protected final boolean containsAssociation (WMObject object)
286         throws TKException
287     {
288         loadAssociations();
289
290         if (object != null &&
291             this.associations != null)
292         {
293             return this.associations.contains(object.getID());
294         }
295
296         return false;
297     }
298
299     /**
300      * Informs the object about the succesful update of its associations.
301      */

302     protected final void updatedAssociations ()
303     {
304         this.associationsModified = false;
305     }
306
307     /**
308      * Loads the associations on demand.
309      *
310      * @exception com.teamkonzept.lib.TKException if an error occured during object retrieval.
311      */

312     protected final void loadAssociations ()
313         throws TKException
314     {
315         if (! this.associationsLoaded)
316         {
317             this.associations = getFactory().getObjectAssociations(this);
318             this.associationsLoaded = true;
319         }
320     }
321
322 }
323
Popular Tags