1 /***
2 * Copyright (c) 2002, CodeStreet LLC. All rights reserved.<p>
3 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
4 * conditions are met:<p>
5 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
7 * in the documentation and/or other materials provided with the distribution. Neither the name of CodeStreet LLC. nor the
8 * names of its contributors may be used to endorse or promote products derived from this software without specific prior written
9 * permission.<p>
10 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
11 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
12 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
14 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<p>
16 */
17
18 package com.codestreet.messageforge;
19
20 /***
21 * Class to represent a <tt>String</tt> field. There are other fields that can also be used to represent
22 * a <tt>String</tt>. For example, <code?RFldString</tt>. The reason for different fields to represent
23 * <tt>String</tt> is to allow application developers to attach additional semantics to message definitions.
24 * For example, an application could map <tt>RFldMemo</tt> fields to large string columns in the database.
25 * @author Jawaid Hakim.
26 */
27 public class RFldMemo extends RFld
28 {
29 static final long serialVersionUID = 4897653670438967731L; //Generated by jserial
30
31 /***
32 * Default constructor.
33 */
34 public RFldMemo()
35 {
36 }
37
38 /***
39 * Constructor.
40 * @param name Field name.
41 * @param fieldId Field id. Field ids must be either <tt>0</tt>
42 * to indicate that there is no id on the field, or greater. In addition,
43 * field ids must be unique within a messages - no two fields are allowed
44 * to have the same field id.
45 */
46 public RFldMemo(String name, int fieldId)
47 {
48 super(name, fieldId);
49 }
50
51 /***
52 * Constructor.
53 * @param name Field name.
54 * @param fieldId Field id. Field ids must be either <tt>0</tt>
55 * to indicate that there is no id on the field, or greater. In addition,
56 * field ids must be unique within a messages - no two fields are allowed
57 * to have the same field id.
58 * @param desc Field description.
59 */
60 public RFldMemo(String name, int fieldId, String desc)
61 {
62 super(name, fieldId, desc);
63 }
64
65 /***
66 * Get field type.
67 * @return Field type <tt>MEMO</tt>.
68 * @see RFldType
69 */
70 public final RFldType getType()
71 {
72 return RFldType.MEMO;
73 }
74
75 /***
76 * Check if another field is equal to this field. Equality is defined
77 * as the fields having the same value - the comparison is case-sensitive.
78 * @param anObject Another field.
79 * @return <tt>true</tt> if another message is equal to this message.
80 * Otherwise, returns <tt>false</tt>.
81 */
82 public boolean equals(Object anObject)
83 {
84 if (this == anObject)
85 {
86 return true;
87 }
88 else if (! (anObject instanceof RFldMemo))
89 {
90 return false;
91 }
92 RFldMemo another = (RFldMemo)anObject;
93 if (valSet_ != another.isValSet())
94 return false;
95 else
96 return (! valSet_ || dataObj_.equals(another.getValue()));
97 }
98
99 /***
100 * Returns a hash code value for the field.
101 * @return a hash code value for the field, uses the java.lang.String
102 * hashCode method to compute the value.
103 */
104 public int hashCode()
105 {
106 return (valSet_) ? dataObj_.hashCode() : 0;
107 }
108
109 /***
110 * Add a constraint.
111 * @param cons Constraint.
112 */
113 public void addConstraint(RFldConstraintString cons) throws FieldValidationException
114 {
115 super.addConstraint(cons);
116 }
117
118 /***
119 * Reset the field value.
120 * @see #isValSet()
121 */
122 public void reset() throws FieldValidationException
123 {
124 if (isLocked())
125 throw new FieldValidationException("Field " + getName() + " is locked");
126
127 valSet_ = false;
128 dataObj_ = null;
129 }
130
131 /***
132 * Set data.
133 * @param newData New data.
134 */
135 public RFld set(Object newData) throws FieldValidationException
136 {
137 try
138 {
139 return set((String)newData);
140 }
141 catch (ClassCastException e)
142 {
143 return set(newData.toString());
144 }
145 }
146
147 /***
148 * Set data.
149 * @param newData New data.
150 */
151 public RFld set(String newData) throws FieldValidationException
152 {
153 validate(newData);
154
155 dataObj_ = newData;
156 valSet_ = true;
157
158 return this;
159 }
160
161 /***
162 * Set the field value from a JDOM element.
163 * @param elem Field value as a JDOM element.
164 */
165 public final RFld set(org.jdom.Element elem) throws FieldValidationException
166 {
167 return set(elem.getAttributeValue(XML_ATTR_VALUE));
168 }
169
170 /***
171 * Validate against constraints. A field is valid if either it's value is set
172 * and satisfies all constraints, or the the field is optional.
173 */
174 public void validate() throws FieldValidationException
175 {
176 // Only need to check that non-optional fields have been set. If a
177 // field has been set then it must be valid since validation is done
178 // with each set.
179 if (! valSet_ && ! optional_)
180 throw new FieldValidationException("Field not set: " + getName());
181 }
182
183 /***
184 * Check if a new value will satifsy constraints.
185 * @param newData New value.
186 */
187 public void validate(String newData) throws FieldValidationException
188 {
189 if (locked_)
190 throw new FieldValidationException("Cannot modify locked field: " + getName());
191
192 if (newData == null)
193 throw new FieldValidationException("New value is NULL for field: " + getName());;
194
195 for (int i = getConstraintCount() - 1; i >= 0; --i)
196 ((RFldConstraintString)getConstraint(i)).validate(this, newData);
197 }
198
199 /***
200 * Get data.
201 * @return data Data. Returns <tt>null</tt> if the field value is not set.
202 */
203 public String getValue()
204 {
205 return dataObj_;
206 }
207
208 /***
209 * Get the field value as an object.
210 * @return Field value as an object. Returns <tt>null</tt> if the field value is not set.
211 */
212 public Object getValueAsObject()
213 {
214 return dataObj_;
215 }
216
217 /***
218 * Get the field value as a string.
219 * @return Field value as a string. Returns <tt>null</tt> if the field value is not set.
220 */
221 public String getValueAsString()
222 {
223 return dataObj_;
224 }
225
226 /***
227 * Get the field value as a <tt>java.util.Hashtable</tt>. Not supported - throws
228 * an exception.
229 * @return Field value as java.util.Hashtable. Returns <tt>null</tt> if the field
230 * value is not set.
231 */
232 public java.util.Hashtable getValueAsHashtable() throws FieldValidationException
233 {
234 throw new FieldValidationException("Not supported");
235 }
236
237 /***
238 * Get the XML tag for this field type.
239 * @return XML tag for this field type.
240 */
241 public final String getTag()
242 {
243 return XML_TAG;
244 }
245
246 /***
247 * Set the XML tag for this field type.
248 * @param tag New XML tag for this field type.
249 */
250 public static void setTag(String tag)
251 {
252 XML_TAG = tag;
253 }
254
255 /***
256 * XML tag for this element type.
257 */
258 protected static transient String XML_TAG = "memo";
259
260 /***
261 * Data.
262 */
263 protected String dataObj_;
264 }
This page was automatically generated by Maven