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