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    public class TynamoPropertyDescriptorImpl extends TynamoDescriptor implements TynamoPropertyDescriptor
020    {
021    
022            private Class propertyType;
023    
024            private String name;
025    
026            private boolean searchable = true;
027    
028            private boolean required;
029    
030            private boolean readOnly;
031    
032            private int length = DEFAULT_LENGTH;
033    
034            private boolean large;
035    
036            private String format;
037    
038            private boolean richText;
039    
040            /**
041             * It's kinda like an old-skool C++ copy constructor
042             */
043            public TynamoPropertyDescriptorImpl(Class beanType, TynamoPropertyDescriptor descriptor)
044            {
045                    this(beanType, descriptor.getPropertyType());
046    
047                    try
048                    {
049                            BeanUtils.copyProperties(this, (TynamoPropertyDescriptorImpl) descriptor);
050                    } catch (IllegalAccessException e)
051                    {
052                            LOG.error(e.getMessage());
053                            e.printStackTrace();
054                    } catch (InvocationTargetException e)
055                    {
056                            LOG.error(e.getMessage());
057                            e.printStackTrace();
058                    } catch (Exception e)
059                    {
060                            LOG.error(e.toString());
061                            e.printStackTrace();
062                    }
063            }
064    
065            public TynamoPropertyDescriptorImpl(Class beanType, Class propertyType)
066            {
067                    super(beanType);
068                    this.propertyType = propertyType;
069            }
070    
071            public TynamoPropertyDescriptorImpl(Class beanType, String name, Class propertyType)
072            {
073                    this(beanType, propertyType);
074                    this.setName(name);
075            }
076    
077            /**
078             * @param dto
079             */
080            public TynamoPropertyDescriptorImpl(TynamoPropertyDescriptorImpl dto)
081            {
082                    super(dto);
083    
084                    try
085                    {
086                            BeanUtils.copyProperties(this, dto);
087                    } catch (IllegalAccessException e)
088                    {
089                            LOG.error(e.getMessage());
090                            e.printStackTrace();
091                    } catch (InvocationTargetException e)
092                    {
093                            LOG.error(e.getMessage());
094                            e.printStackTrace();
095                    } catch (Exception e)
096                    {
097                            LOG.error(e.toString());
098                            e.printStackTrace();
099                    }
100            }
101    
102            public Class getPropertyType()
103            {
104                    return propertyType;
105            }
106    
107            public void setPropertyType(Class propertyType)
108            {
109                    this.propertyType = propertyType;
110            }
111    
112            /**
113             * @return
114             */
115            public boolean isNumeric()
116            {
117                    return getPropertyType().getName().endsWith("Double")
118                                    || getPropertyType().getName().endsWith("Integer")
119                                    || getPropertyType().getName().endsWith("Float")
120                                    || getPropertyType().getName().endsWith("double")
121                                    || getPropertyType().getName().endsWith("int")
122                                    || getPropertyType().getName().endsWith("float")
123                                    || getPropertyType().getName().endsWith("BigDecimal");
124            }
125    
126            public boolean isBoolean()
127            {
128                    return getPropertyType().getName().endsWith("boolean")
129                                    || getPropertyType().getName().endsWith("Boolean");
130            }
131    
132            /**
133             * @return
134             */
135            public boolean isDate()
136            {
137                    // TODO Auto-generated method stub
138                    return getPropertyType().getName().endsWith("Date");
139            }
140    
141            /**
142             * @return
143             */
144            public boolean isString()
145            {
146                    // TODO Auto-generated method stub
147                    return getPropertyType().getName().endsWith("String");
148            }
149    
150            public boolean isObjectReference()
151            {
152                    return false;
153            }
154    
155            /**
156             * @return Returns the required.
157             */
158            public boolean isRequired()
159            {
160                    return required;
161            }
162    
163            /**
164             * @param required The required to set.
165             */
166            public void setRequired(boolean required)
167            {
168                    this.required = required;
169            }
170    
171            /**
172             * @return
173             */
174            public boolean isReadOnly()
175            {
176                    return readOnly;
177            }
178    
179            /**
180             * @param readOnly The readOnly to set.
181             */
182            public void setReadOnly(boolean readOnly)
183            {
184                    this.readOnly = readOnly;
185            }
186    
187            /**
188             * @return Returns the identifier.
189             */
190            public boolean isIdentifier()
191            {
192                    return false;
193            }
194    
195            /**
196             * @return Returns the collection.
197             */
198            public boolean isCollection()
199            {
200                    return false;
201            }
202    
203            @Override
204            public Object clone()
205            {
206                    return new TynamoPropertyDescriptorImpl(this);
207            }
208    
209            @Override
210            public void copyFrom(Descriptor descriptor)
211            {
212                    super.copyFrom(descriptor);
213    
214                    if (descriptor instanceof TynamoPropertyDescriptorImpl)
215                    {
216                            try
217                            {
218                                    BeanUtils.copyProperties(this, (TynamoPropertyDescriptorImpl) descriptor);
219                            } catch (IllegalAccessException e)
220                            {
221                                    LOG.error(e.getMessage());
222                                    e.printStackTrace();
223                            } catch (InvocationTargetException e)
224                            {
225                                    LOG.error(e.getMessage());
226                                    e.printStackTrace();
227                            } catch (Exception e)
228                            {
229                                    LOG.error(e.toString());
230                                    e.printStackTrace();
231                            }
232                    }
233            }
234    
235            public boolean equals(Object obj)
236            {
237                    return EqualsBuilder.reflectionEquals(this, obj);
238            }
239    
240            public int getLength()
241            {
242                    return length;
243            }
244    
245            public void setLength(int length)
246            {
247                    this.length = length;
248            }
249    
250            public boolean isLarge()
251            {
252                    return large;
253            }
254    
255            public void setLarge(boolean large)
256            {
257                    this.large = large;
258            }
259    
260            public String getFormat()
261            {
262                    return format;
263            }
264    
265            public void setFormat(String format)
266            {
267                    this.format = format;
268            }
269    
270            public boolean isSearchable()
271            {
272                    return searchable;
273            }
274    
275            public void setSearchable(boolean searchable)
276            {
277                    this.searchable = searchable;
278            }
279    
280            public boolean isRichText()
281            {
282                    return richText;
283            }
284    
285            public void setRichText(boolean richText)
286            {
287                    this.richText = richText;
288            }
289    
290            public boolean isEmbedded()
291            {
292                    return false;
293            }
294    
295            public String getName()
296            {
297                    return name;
298            }
299    
300            public void setName(String name)
301            {
302                    this.name = name;
303            }
304    }