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.tynamo.descriptor;
013    
014    import org.apache.commons.beanutils.BeanUtils;
015    import org.apache.commons.lang.builder.EqualsBuilder;
016    
017    import java.lang.reflect.InvocationTargetException;
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 TynamoPropertyDescriptorImpl extends TynamoDescriptor implements
026            TynamoPropertyDescriptor
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 TynamoPropertyDescriptorImpl(Class beanType,
057                                                                            TynamoPropertyDescriptor descriptor)
058            {
059                    this(beanType, descriptor.getPropertyType());
060    
061                    try
062                    {
063                            BeanUtils.copyProperties(this,
064                                    (TynamoPropertyDescriptorImpl) 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 TynamoPropertyDescriptorImpl(Class beanType, Class type)
081            {
082                    super(type);
083                    this.beanType = beanType;
084            }
085    
086            /**
087             * @param dto
088             */
089            public TynamoPropertyDescriptorImpl(TynamoPropertyDescriptorImpl dto)
090            {
091                    super(dto);
092    
093                    try
094                    {
095                            BeanUtils.copyProperties(this, dto);
096                    } catch (IllegalAccessException e)
097                    {
098                            LOG.error(e.getMessage());
099                            e.printStackTrace();
100                    } catch (InvocationTargetException e)
101                    {
102                            LOG.error(e.getMessage());
103                            e.printStackTrace();
104                    } catch (Exception e)
105                    {
106                            LOG.error(e.toString());
107                            e.printStackTrace();
108                    }
109            }
110    
111            // ///////////////////////////////////////////////////////////////////////////////////////////////////////
112            // methods
113            // ///////////////////////////////////////////////////////////////////////////////////////////////////////
114    
115            /**
116             * @return
117             */
118            public Class getPropertyType()
119            {
120                    return getType();
121            }
122    
123            /**
124             * @return
125             */
126            public boolean isNumeric()
127            {
128                    return getPropertyType().getName().endsWith("Double")
129                            || getPropertyType().getName().endsWith("Integer")
130                            || getPropertyType().getName().endsWith("Float")
131                            || getPropertyType().getName().endsWith("double")
132                            || getPropertyType().getName().endsWith("int")
133                            || getPropertyType().getName().endsWith("float")
134                            || getPropertyType().getName().endsWith("BigDecimal");
135            }
136    
137            public boolean isBoolean()
138            {
139                    return getPropertyType().getName().endsWith("boolean")
140                            || getPropertyType().getName().endsWith("Boolean");
141            }
142    
143            /**
144             * @return
145             */
146            public boolean isDate()
147            {
148                    // TODO Auto-generated method stub
149                    return getPropertyType().getName().endsWith("Date");
150            }
151    
152            /**
153             * @return
154             */
155            public boolean isString()
156            {
157                    // TODO Auto-generated method stub
158                    return getPropertyType().getName().endsWith("String");
159            }
160    
161            public boolean isObjectReference()
162            {
163                    return false;
164            }
165    
166            // ///////////////////////////////////////////////////////////////////////////////////////////////////////
167            // bean setters / getters
168            // ///////////////////////////////////////////////////////////////////////////////////////////////////////
169    
170            public int getIndex()
171            {
172                    return index;
173            }
174    
175            public void setIndex(int index)
176            {
177                    this.index = index;
178            }
179    
180            /**
181             * @return Returns the required.
182             */
183            public boolean isRequired()
184            {
185                    return required;
186            }
187    
188            /**
189             * @param required The required to set.
190             */
191            public void setRequired(boolean required)
192            {
193                    this.required = required;
194            }
195    
196            /**
197             * @return
198             */
199            public boolean isReadOnly()
200            {
201                    return readOnly;
202            }
203    
204            /**
205             * @param readOnly The readOnly to set.
206             */
207            public void setReadOnly(boolean readOnly)
208            {
209                    this.readOnly = readOnly;
210            }
211    
212            /**
213             * @return Returns the identifier.
214             */
215            public boolean isIdentifier()
216            {
217                    return false;
218            }
219    
220            /**
221             * @return Returns the collection.
222             */
223            public boolean isCollection()
224            {
225                    return false;
226            }
227    
228            @Override
229            public Object clone()
230            {
231                    return new TynamoPropertyDescriptorImpl(this);
232            }
233    
234            @Override
235            public void copyFrom(Descriptor descriptor)
236            {
237                    super.copyFrom(descriptor);
238    
239                    if (descriptor instanceof TynamoPropertyDescriptorImpl)
240                    {
241                            try
242                            {
243                                    BeanUtils.copyProperties(this,
244                                            (TynamoPropertyDescriptorImpl) descriptor);
245                            } catch (IllegalAccessException e)
246                            {
247                                    LOG.error(e.getMessage());
248                                    e.printStackTrace();
249                            } catch (InvocationTargetException e)
250                            {
251                                    LOG.error(e.getMessage());
252                                    e.printStackTrace();
253                            } catch (Exception e)
254                            {
255                                    LOG.error(e.toString());
256                                    e.printStackTrace();
257                            }
258                    }
259            }
260    
261            public boolean equals(Object obj)
262            {
263                    return EqualsBuilder.reflectionEquals(this, obj);
264            }
265    
266            public int getLength()
267            {
268                    return length;
269            }
270    
271            public void setLength(int length)
272            {
273                    this.length = length;
274            }
275    
276            public boolean isLarge()
277            {
278                    return large;
279            }
280    
281            public void setLarge(boolean large)
282            {
283                    this.large = large;
284            }
285    
286            public String getFormat()
287            {
288                    return format;
289            }
290    
291            public void setFormat(String format)
292            {
293                    this.format = format;
294            }
295    
296            public boolean isSearchable()
297            {
298                    return searchable;
299            }
300    
301            public void setSearchable(boolean searchable)
302            {
303                    this.searchable = searchable;
304            }
305    
306            public boolean isSummary()
307            {
308                    return summary;
309            }
310    
311            public void setSummary(boolean summary)
312            {
313                    this.summary = summary;
314            }
315    
316            public boolean isRichText()
317            {
318                    return richText;
319            }
320    
321            public void setRichText(boolean richText)
322            {
323                    this.richText = richText;
324            }
325    
326            public boolean isEmbedded()
327            {
328                    return false;
329            }
330    
331            public Class getBeanType()
332            {
333                    return beanType;
334            }
335    
336            public void setBeanType(Class beanType)
337            {
338                    this.beanType = beanType;
339            }
340    
341            public String getName()
342            {
343                    return name;
344            }
345    
346            public void setName(String name)
347            {
348                    this.name = name;
349            }
350    }