001 /*
002 * Copyright 2004 Chris Nelson
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
007 * Unless required by applicable law or agreed to in writing,
008 * software distributed under the License is distributed on an "AS IS" BASIS,
009 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010 * See the License for the specific language governing permissions and limitations under the License.
011 */
012 package org.trails.descriptor;
013
014 import java.lang.reflect.InvocationTargetException;
015
016 import org.apache.commons.beanutils.BeanUtils;
017 import org.apache.commons.lang.builder.EqualsBuilder;
018
019 /**
020 * @author fus8882
021 * <p/>
022 * TODO To change the template for this generated type comment go to Window -
023 * Preferences - Java - Code Style - Code Templates
024 */
025 public class TrailsPropertyDescriptor extends TrailsDescriptor implements
026 IPropertyDescriptor
027 {
028 private Class beanType;
029
030 private String name;
031
032 private boolean searchable = true;
033
034 private boolean required;
035
036 private boolean readOnly;
037
038 private int index = UNDEFINED_INDEX;
039
040 private int length = DEFAULT_LENGTH;
041
042 private boolean large;
043
044 private String format;
045
046 private boolean summary = true;
047
048 private boolean richText;
049
050 // ///////////////////////////////////////////////////////////////////////////////////////////////////////
051 // constructors
052 // ///////////////////////////////////////////////////////////////////////////////////////////////////////
053 /**
054 * It's kinda like an old-skool C++ copy constructor
055 */
056 public TrailsPropertyDescriptor(Class beanType,
057 IPropertyDescriptor descriptor)
058 {
059 this(beanType, descriptor.getPropertyType());
060
061 try
062 {
063 BeanUtils.copyProperties(this,
064 (TrailsPropertyDescriptor) descriptor);
065 } catch (IllegalAccessException e)
066 {
067 LOG.error(e.getMessage());
068 e.printStackTrace();
069 } catch (InvocationTargetException e)
070 {
071 LOG.error(e.getMessage());
072 e.printStackTrace();
073 } catch (Exception e)
074 {
075 LOG.error(e.toString());
076 e.printStackTrace();
077 }
078 }
079
080 public TrailsPropertyDescriptor(Class beanType, Class type)
081 {
082 super(type);
083 this.beanType = beanType;
084 }
085
086 public TrailsPropertyDescriptor(Class beanType, String name, Class type)
087 {
088 this(beanType, type);
089 this.setName(name);
090 setDisplayName(name);
091 }
092
093 /**
094 * @param dto
095 */
096 public TrailsPropertyDescriptor(TrailsPropertyDescriptor dto)
097 {
098 super(dto);
099
100 try
101 {
102 BeanUtils.copyProperties(this, dto);
103 } catch (IllegalAccessException e)
104 {
105 LOG.error(e.getMessage());
106 e.printStackTrace();
107 } catch (InvocationTargetException e)
108 {
109 LOG.error(e.getMessage());
110 e.printStackTrace();
111 } catch (Exception e)
112 {
113 LOG.error(e.toString());
114 e.printStackTrace();
115 }
116 }
117
118 // ///////////////////////////////////////////////////////////////////////////////////////////////////////
119 // methods
120 // ///////////////////////////////////////////////////////////////////////////////////////////////////////
121
122 /**
123 * @return
124 */
125 public Class getPropertyType()
126 {
127 return getType();
128 }
129
130 /**
131 * @return
132 */
133 public boolean isNumeric()
134 {
135 return getPropertyType().getName().endsWith("Double")
136 || getPropertyType().getName().endsWith("Integer")
137 || getPropertyType().getName().endsWith("Float")
138 || getPropertyType().getName().endsWith("double")
139 || getPropertyType().getName().endsWith("int")
140 || getPropertyType().getName().endsWith("float")
141 || getPropertyType().getName().endsWith("BigDecimal");
142 }
143
144 public boolean isBoolean()
145 {
146 return getPropertyType().getName().endsWith("boolean")
147 || getPropertyType().getName().endsWith("Boolean");
148 }
149
150 /**
151 * @return
152 */
153 public boolean isDate()
154 {
155 // TODO Auto-generated method stub
156 return getPropertyType().getName().endsWith("Date");
157 }
158
159 /**
160 * @return
161 */
162 public boolean isString()
163 {
164 // TODO Auto-generated method stub
165 return getPropertyType().getName().endsWith("String");
166 }
167
168 public boolean isObjectReference()
169 {
170 return false;
171 }
172
173 // ///////////////////////////////////////////////////////////////////////////////////////////////////////
174 // bean setters / getters
175 // ///////////////////////////////////////////////////////////////////////////////////////////////////////
176
177 public int getIndex()
178 {
179 return index;
180 }
181
182 public void setIndex(int index)
183 {
184 this.index = index;
185 }
186
187 /**
188 * @return Returns the required.
189 */
190 public boolean isRequired()
191 {
192 return required;
193 }
194
195 /**
196 * @param required The required to set.
197 */
198 public void setRequired(boolean required)
199 {
200 this.required = required;
201 }
202
203 /**
204 * @return
205 */
206 public boolean isReadOnly()
207 {
208 return readOnly;
209 }
210
211 /**
212 * @param readOnly The readOnly to set.
213 */
214 public void setReadOnly(boolean readOnly)
215 {
216 this.readOnly = readOnly;
217 }
218
219 /**
220 * @return Returns the identifier.
221 */
222 public boolean isIdentifier()
223 {
224 return false;
225 }
226
227 /**
228 * @return Returns the collection.
229 */
230 public boolean isCollection()
231 {
232 return false;
233 }
234
235 @Override
236 public Object clone()
237 {
238 return new TrailsPropertyDescriptor(this);
239 }
240
241 @Override
242 public void copyFrom(IDescriptor descriptor)
243 {
244 super.copyFrom(descriptor);
245
246 if (descriptor instanceof TrailsPropertyDescriptor)
247 {
248 try
249 {
250 BeanUtils.copyProperties(this,
251 (TrailsPropertyDescriptor) descriptor);
252 } catch (IllegalAccessException e)
253 {
254 LOG.error(e.getMessage());
255 e.printStackTrace();
256 } catch (InvocationTargetException e)
257 {
258 LOG.error(e.getMessage());
259 e.printStackTrace();
260 } catch (Exception e)
261 {
262 LOG.error(e.toString());
263 e.printStackTrace();
264 }
265 }
266 }
267
268 public boolean equals(Object obj)
269 {
270 return EqualsBuilder.reflectionEquals(this, obj);
271 }
272
273 public int getLength()
274 {
275 return length;
276 }
277
278 public void setLength(int length)
279 {
280 this.length = length;
281 }
282
283 public boolean isLarge()
284 {
285 return large;
286 }
287
288 public void setLarge(boolean large)
289 {
290 this.large = large;
291 }
292
293 public String getFormat()
294 {
295 return format;
296 }
297
298 public void setFormat(String format)
299 {
300 this.format = format;
301 }
302
303 public boolean isSearchable()
304 {
305 return searchable;
306 }
307
308 public void setSearchable(boolean searchable)
309 {
310 this.searchable = searchable;
311 }
312
313 public boolean isSummary()
314 {
315 return summary;
316 }
317
318 public void setSummary(boolean summary)
319 {
320 this.summary = summary;
321 }
322
323 public boolean isRichText()
324 {
325 return richText;
326 }
327
328 public void setRichText(boolean richText)
329 {
330 this.richText = richText;
331 }
332
333 public boolean isEmbedded()
334 {
335 return false;
336 }
337
338 public Class getBeanType()
339 {
340 return beanType;
341 }
342
343 public void setBeanType(Class beanType)
344 {
345 this.beanType = beanType;
346 }
347
348 public String getName()
349 {
350 return name;
351 }
352
353 public void setName(String name)
354 {
355 this.name = name;
356 }
357 }