KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > form > Form


1 package de.webman.form;
2
3 import java.sql.ResultSet JavaDoc;
4 import com.teamkonzept.db.TKDBManager;
5 import com.teamkonzept.db.TKQuery;
6 import com.teamkonzept.lib.TKException;
7 import com.teamkonzept.webman.mainint.WebmanExceptionHandler;
8 import de.webman.form.db.FieldConstants;
9 import de.webman.form.db.FormConstants;
10 import de.webman.form.db.queries.FormDepends;
11 import de.webman.form.db.queries.SelectFormProperties;
12
13 /**
14  * A simple holder for form definitions.
15  * <P>
16  * Its intended use is to ease form processing within
17  * event handlers, since there nothing like an object
18  * model in sight ... However, this class is far from being
19  * an object model implementation, too.
20  *
21  * @author $Author: uli $
22  * @version $Revision: 1.1 $
23  */

24 public class Form
25 {
26
27     // Attributes.
28

29     /**
30      * The form identifier.
31      */

32     private Integer JavaDoc id = null;
33
34     /**
35      * The form type.
36      */

37     private Integer JavaDoc type = null;
38
39     /**
40      * The form name.
41      */

42     private String JavaDoc name = null;
43
44     /**
45      * The form description.
46      */

47     private String JavaDoc description = null;
48
49
50     // Constructors.
51

52     /**
53      * Avoids parameterless instantiation.
54      */

55     public Form ()
56     {
57         this(null);
58     }
59
60     /**
61      * Creates a new form holder.
62      *
63      * @param id the form identifier.
64      */

65     public Form (Integer JavaDoc id)
66     {
67         if (id == null)
68         {
69             throw new IllegalArgumentException JavaDoc();
70         }
71
72         this.id = id;
73     }
74
75
76     // Accessors.
77

78     /**
79      * Returns the form identifier.
80      *
81      * @return the form identifier.
82      */

83     public Integer JavaDoc getIdentifier ()
84     {
85         return this.id;
86     }
87
88     /**
89      * Returns the form type.
90      *
91      * @return the form type.
92      * @throws TKException if an error occurred during property loading.
93      */

94     public Integer JavaDoc getType ()
95         throws TKException
96     {
97         loadFormProperties();
98         return this.type;
99     }
100
101     /**
102      * Returns the form name.
103      *
104      * @return the form name.
105      * @throws TKException if an error occurred during property loading.
106      */

107     public String JavaDoc getName ()
108         throws TKException
109     {
110         loadFormProperties();
111         return this.name;
112     }
113
114     /**
115      * Returns the form description.
116      *
117      * @return the form description.
118      * @throws TKException if an error occurred during property loading.
119      */

120     public String JavaDoc getDescription ()
121         throws TKException
122     {
123         loadFormProperties();
124         return this.description;
125     }
126
127
128     // Standard.
129

130     /**
131      * Checks wether this object and the specified object
132      * may be treated as equal.
133      *
134      * @param object the object to checked for equality.
135      * @return <CODE>true</CODE> if this object and the
136      * specified object may be treated as equal, otherwise
137      * <CODE>false</CODE>.
138      */

139     public boolean equals (Object JavaDoc object)
140     {
141         if (object == null)
142         {
143             return false;
144         }
145
146         if (object == this)
147         {
148             return true;
149         }
150
151         if (! this.getClass().equals(object.getClass()))
152         {
153             return false;
154         }
155
156         return ((Form) object).getIdentifier().equals(this.id);
157     }
158
159     /**
160      * Returns the hash code for this object.
161      *
162      * @return the hash code for this object.
163      */

164     public int hashCode ()
165     {
166         return this.id.hashCode();
167     }
168
169     /**
170      * Returns a string representation of this object.
171      *
172      * @return a string representation of this object.
173      */

174     public String JavaDoc toString ()
175     {
176         return (new StringBuffer JavaDoc(this.getClass().getName())).append('@')
177                                                             .append(this.id)
178                                                             .toString();
179     }
180
181
182     // Convenience.
183

184     /**
185      * Checks wether this form has any dependencies.
186      *
187      * @return <CODE>true</CODE> if the form has any dependencies,
188      * otherwise <CODE>false</CODE>.
189      * @throws TKException if an error occurred during the check.
190      */

191     public boolean hasDependencies ()
192         throws TKException
193     {
194         try
195         {
196             TKQuery query = TKDBManager.newQuery(FormDepends.class);
197             query.setQueryParams(FormConstants.COLUMN_NAMES[FormConstants.FORM_ID], this.id);
198             query.execute();
199
200             ResultSet JavaDoc result = query.fetchResultSet();
201
202             return result.next();
203         }
204         catch (Exception JavaDoc e)
205         {
206             throw WebmanExceptionHandler.getException(e);
207         }
208     }
209
210     /**
211      * Checks wether the form type is
212      * {@link de.webman.form.db.FormConstants#FORM_TYPE_FORM_FRAGMENT form fragment}.
213      *
214      * @return <CODE>true</CODE> if the form type is
215      * {@link de.webman.form.db.FormConstants#FORM_TYPE_FORM_FRAGMENT form fragment},
216      * otherwise <CODE>false</CODE>.
217      * @throws TKException if an error occurred during property loading.
218      */

219     public boolean isFormFragment ()
220         throws TKException
221     {
222         return FormConstants.FORM_TYPE_FORM_FRAGMENT.equals(getType());
223     }
224
225     /**
226      * Checks wether the form type is
227      * {@link de.webman.form.db.FormConstants#FORM_TYPE_CONTENT_FORM content form}.
228      *
229      * @return <CODE>true</CODE> if the form type is
230      * {@link de.webman.form.db.FormConstants#FORM_TYPE_CONTENT_FORM content form},
231      * otherwise <CODE>false</CODE>.
232      * @throws TKException if an error occurred during property loading.
233      */

234     public boolean isContentForm ()
235         throws TKException
236     {
237         return FormConstants.FORM_TYPE_CONTENT_FORM.equals(getType());
238     }
239
240     /**
241      * Checks wether the form type is
242      * {@link de.webman.form.db.FormConstants#FORM_TYPE_STRUCTURE_FORM structure form}.
243      *
244      * @return <CODE>true</CODE> if the form type is
245      * {@link de.webman.form.db.FormConstants#FORM_TYPE_STRUCTURE_FORM structure form},
246      * otherwise <CODE>false</CODE>.
247      * @throws TKException if an error occurred during property loading.
248      */

249     public boolean isStructureForm ()
250         throws TKException
251     {
252         return FormConstants.FORM_TYPE_STRUCTURE_FORM.equals(getType());
253     }
254
255     /**
256      * Checks wether the form type is
257      * {@link de.webman.form.db.FormConstants#FORM_TYPE_TEMPORARY_FORM temporary form}.
258      *
259      * @return <CODE>true</CODE> if the form type is
260      * {@link de.webman.form.db.FormConstants#FORM_TYPE_TEMPORARY_FORM temporary form},
261      * otherwise <CODE>false</CODE>.
262      * @throws TKException if an error occurred during property loading.
263      */

264     public boolean isTemporaryForm ()
265         throws TKException
266     {
267         return FormConstants.FORM_TYPE_TEMPORARY_FORM.equals(getType());
268     }
269
270     /**
271      * Returns the form identifier.
272      *
273      * @return the form identifier.
274      */

275     public int getIdentifierAsInt ()
276     {
277         return getIdentifier().intValue();
278     }
279
280     /**
281      * Returns the form type.
282      *
283      * @return the form type.
284      * @throws TKException if an error occurred during property loading.
285      */

286     public int getTypeAsInt ()
287         throws TKException
288     {
289         return getType().intValue();
290     }
291
292
293     // Helpers.
294

295     /**
296      * Loads the form properties from the database.
297      * <P>
298      * The form properties are loaded at most once.
299      *
300      * @throws TKException if an error occurred during property loading.
301      */

302     private void loadFormProperties ()
303         throws TKException
304     {
305         if (this.type != null)
306         {
307             return;
308         }
309
310         try
311         {
312             TKQuery query = TKDBManager.newQuery(SelectFormProperties.class);
313             query.setQueryParams(FormConstants.COLUMN_NAMES[FormConstants.FORM_ID], this.id);
314             query.execute();
315
316             ResultSet JavaDoc result = query.fetchResultSet();
317
318             if (result.next())
319             {
320                 this.type = new Integer JavaDoc(result.getInt(FormConstants.COLUMN_NAMES[FormConstants.FORM_TYPE]));
321                 this.name = result.getString(FieldConstants.COLUMN_NAMES[FieldConstants.FIELD_NAME]);
322                 this.description = result.getString(FieldConstants.COLUMN_NAMES[FieldConstants.FIELD_SHOW_NAME]);
323             }
324         }
325         catch (Exception JavaDoc e)
326         {
327             throw WebmanExceptionHandler.getException(e);
328         }
329     }
330
331 }
332
Popular Tags