1 package com.codestreet.messageforge;
2
3 import java.util.*;
4
5 /// <summary> Class to represent a list of (Java) bean field. The bean
6 // implementation must override
7 /// <code>Equals</code> in order to work correctly with equality methods of
8 // the framework.
9 /// </summary>
10 /// <author> Jawaid Hakim.
11 /// </author>
12 public class RFldBeanList extends RFldList
13 {
14 public RFldType getType()
15 {
16 return RFldType.BEANLIST;
17 }
18
19 public List getValue()
20 {
21 return (valSet_) ? dataObj_ : null;
22 }
23
24 public Class getClassType()
25 {
26 return cls_;
27 }
28
29 public Object getValueAsObject()
30 {
31 return (valSet_) ? dataObj_ : null;
32 }
33
34 public String getValueAsString()
35 {
36 if (!valSet_)
37 return null;
38
39 return dataObj_.toString();
40 }
41
42 public Hashtable getValueAsHashtable()
43 {
44 return null;
45 }
46
47 public String getTag()
48 {
49 return XML_TAG;
50 }
51
52 /// <summary> Default constructor.
53 /// </summary>
54 public RFldBeanList()
55 {
56 }
57
58 /// <summary> Constructor.
59 /// </summary>
60 /// <param name="name">Field name.</param>
61 /// <param name="fieldId">Field id. Field ids must be either
62 // <code>0</code>
63 /// to indicate that there is no id on the field, or greater. In addition,
64 /// field ids must be unique within a messages - no two fields are allowed
65 /// to have the same field id.
66 /// </param>
67 /// <param name="className">Fully qualified class name. For example,
68 /// <code>CodeStreet.Beans.CoolBean</code>
69 /// </param>
70 public RFldBeanList(String name, int fieldId, String className)
71 {
72 super(name, fieldId);
73 try
74 {
75 this.cls_ = Class.forName(className);
76 }
77 catch (ClassNotFoundException ex)
78 {
79 this.cls_ = null;
80 }
81 }
82
83 /// <summary> Constructor.
84 /// </summary>
85 /// <param name="name">Field name.
86 /// </param>
87 /// <param name="fieldId">Field id. Field ids must be either
88 // <code>0</code>
89 /// to indicate that there is no id on the field, or greater. In addition,
90 /// field ids must be unique within a messages - no two fields are allowed
91 /// to have the same field id.
92 /// </param>
93 /// <param name="desc">Field description.
94 /// </param>
95 /// <param name="className">Fully qualified class name. For example,
96 /// <code>CodeStreet.Beans.CoolBean</code>
97 /// </param>
98 /// </param>
99 public RFldBeanList(String name, int fieldId, String desc, String className)
100 {
101 super(name, fieldId, desc);
102 try
103 {
104 this.cls_ = Class.forName(className);
105 }
106 catch (ClassNotFoundException ex)
107 {
108 this.cls_ = null;
109 }
110 }
111
112 /// <summary> Check if another field is equal to this field. Equality is
113 // defined
114 /// as the fields having the same string representation. A short cut
115 /// that needs to be fixed for cases where the string representation is
116 /// rounding off numbers etc.
117 /// </summary>
118 /// <param name="anObject">Another field.
119 /// </param>
120 /// <returns><code>true</code> if another field is equal to this field.
121 /// Otherwise, returns <code>false</code>.
122 /// </returns>
123 public boolean equals(Object anObject)
124 {
125 if (this == anObject)
126 {
127 return true;
128 }
129 else if (anObject == null || !(anObject instanceof RFldBeanList))
130 {
131 return false;
132 }
133 RFldBeanList another = (RFldBeanList) anObject;
134 if (valSet_ && another.isValSet())
135 {
136 List anotherVal = another.getValue();
137 if (dataObj_.size() != anotherVal.size())
138 return false;
139 for (int i = 0; i < dataObj_.size(); ++i)
140 {
141 if (! (dataObj_.get(i).equals(anotherVal.get(i))))
142 return false;
143 }
144 return true;
145 }
146 else
147 return false;
148 }
149
150 /***
151 * Return the hash code value for the field.
152 *
153 * @return a hash code value for the field, equal to the integer value
154 * represented by this field.
155 */
156 public final int hashCode()
157 {
158 return (valSet_) ? dataObj_.hashCode() : 1231;
159 }
160
161 /// <summary> Returns the hash code value for the field.
162 /// </summary>
163 /// <returns>Hash code value for the field.
164 ///
165 /// </returns>
166 public int GetHashCode()
167 {
168 return (valSet_) ? dataObj_.hashCode() : 1231;
169 }
170
171 /// <summary> Reset the field value.
172 /// </summary>
173 /// <seealso cref=" #isValSet()
174 ///
175 /// "/>
176 public void reset() throws FieldValidationException
177 {
178 if (isLocked())
179 throw new FieldValidationException(
180 "Field " + getName() + " is locked");
181
182 if (valSet_)
183 {
184 dataObj_ = null;
185 valSet_ = false;
186 }
187 }
188
189 /// <summary> Set data.
190 /// </summary>
191 /// <param name="newData">New data.
192 ///
193 /// </param>
194 public RFld set(Object newData) throws FieldValidationException
195 {
196 if (newData instanceof List)
197 set((List)newData);
198
199 throw new FieldValidationException("Invalid argument type");
200 }
201
202 /// <summary> Set data.
203 /// </summary>
204 /// <param name="newData">New data.
205 ///
206 /// </param>
207 public RFld set(List newData) throws FieldValidationException
208 {
209 validate(newData);
210
211 dataObj_ = newData;
212 valSet_ = true;
213
214 return this;
215 }
216
217 /// <summary> Set data.
218 /// </summary>
219 /// <param name="newData">New data.
220 ///
221 /// </param>
222 public RFld set(Hashtable newData) throws FieldValidationException
223 {
224 if (isLocked())
225 throw new FieldValidationException(
226 "Cannot modify locked field: " + getName());
227
228 throw new FieldValidationException("Unable to set field value from Hashtable");
229 }
230
231 /***
232 * Set the field value from a JDOM element.
233 *
234 * @param elem
235 * Field value as a JDOM element.
236 */
237 public final RFld set(org.jdom.Element elem)
238 throws FieldValidationException
239 {
240 return set(elem.getAttributeValue(XML_ATTR_VALUE));
241 }
242
243 /// <summary> Validate against constraints. A field is valid if either
244 // it's value is set
245 /// and satisfies all constraints, or the the field is optional.
246 /// </summary>
247 public void validate() throws FieldValidationException
248 {
249 // Only need to check that non-optional fields have been set. If a
250 // field has been set then it must be valid since validation is done
251 // with each set.
252 if (!valSet_ && !optional_)
253 throw new FieldValidationException("Field not set: " + getName());
254 }
255
256 /// <summary> Check if a new value will satifsy constraints.
257 /// </summary>
258 /// <param name="newData">New value.
259 /// </param>
260 public void validate(List newData) throws FieldValidationException
261 {
262 if (locked_)
263 throw new FieldValidationException(
264 "Cannot modify locked field: " + getName());
265
266 if (newData == null)
267 throw new FieldValidationException(
268 "New value is NULL for field: " + getName());
269
270 for (int i = 0; i < newData.size(); ++i)
271 {
272 Object elem = newData.get(i);
273 if (this.cls_ != null && !this.cls_.equals(elem.getClass()))
274 throw new FieldValidationException(
275 "New value is of wrong type: " + elem.getClass());
276 }
277 }
278
279 /// <summary> Get the XML tag for this field type.
280 /// </summary>
281 /// <returns>XML tag for this field type.
282 ///
283 /// </returns>
284
285 /// <summary> Set the XML tag for this field type.
286 /// </summary>
287 /// <param name="tag">New XML tag for this field type.
288 ///
289 /// </param>
290
291 /// <summary> XML tag for this element type.
292 /// </summary>
293 static final String XML_TAG = "beanlist";
294
295 /// <summary> Data.
296 /// </summary>
297 protected List dataObj_;
298 protected Class cls_;
299 }
This page was automatically generated by Maven