View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * ---------------------------------------------------------
5    * ivata groupware may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * ---------------------------------------------------------
30   * $Log: DefaultFieldValueConvertorFactory.java,v $
31   * Revision 1.1  2005/04/11 12:27:02  colinmacleod
32   * Added preliminary support for filters.
33   * Added FieldValueConvertor factor interface
34   * to split off value convertors for reuse.
35   *
36   * ---------------------------------------------------------
37   */
38  package com.ivata.mask.field;
39  
40  import java.io.Serializable;
41  import java.math.BigDecimal;
42  import java.util.Date;
43  import java.util.HashMap;
44  import java.util.Map;
45  
46  import javax.servlet.ServletException;
47  
48  import com.ivata.mask.field.date.DateFieldValueConvertor;
49  import com.ivata.mask.field.number.NumberFieldValueConvertor;
50  import com.ivata.mask.util.SystemException;
51  
52  /***
53   * @since ivata groupware 0.11 (17-Mar-2005)
54   * @author Colin MacLeod
55   * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
56   * @version $Revision: 1.1 $
57   */
58  
59  public class DefaultFieldValueConvertorFactory implements
60          FieldValueConvertorFactory, Serializable {
61      /***
62       * Convert a primitive type (such as <code>int</code>, <code>char</code>) to
63       * the wrapper class associated with it (such as <code>Integer</code>,
64       * <code>Character</code>).
65       *
66       * @param type The type to be converted.
67       * @return Wrapper class for the type provided.
68       * @throws ServletException if an unknown type is encountered.
69       */
70      public static Class convertPrimitiveType(final Class type)
71              throws SystemException {
72          String typeName = type.getName();
73          if ("boolean".equals(typeName)) {
74              return Boolean.class;
75          } else if ("byte".equals(typeName)) {
76              return Byte.class;
77          } else if ("char".equals(typeName)) {
78              return Character.class;
79          } else if ("double".equals(typeName)) {
80              return Double.class;
81          } else if ("float".equals(typeName)) {
82              return Float.class;
83          } else if ("int".equals(typeName)) {
84              return Integer.class;
85          } else if ("long".equals(typeName)) {
86              return Long.class;
87          } else if ("short".equals(typeName)) {
88              return Short.class;
89          } else {
90              throw new SystemException (
91                      "Unknown primitive type: "
92                      + type);
93          }
94      }
95      /***
96       * <p>
97       * Stores all the field value convertors, indexed by the class name they
98       * operate on.
99       * </p>
100      */
101     private Map fieldValueConvertors = new HashMap();
102 
103     /***
104      * Constructor.
105      */
106     public DefaultFieldValueConvertorFactory() {
107         super();
108         // NOTE: the field value convertor for java.lang.Object expects the
109         // class to have a single string parameter. This will work for Boolean,
110         // for example.
111         fieldValueConvertors.put(BigDecimal.class.getName(),
112                 new NumberFieldValueConvertor(
113                         FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
114         fieldValueConvertors.put(Date.class.getName(),
115                 new DateFieldValueConvertor(
116                         FieldValueConvertorConstants.DEFAULT_DATE_PATTERN));
117         fieldValueConvertors.put(Class.class.getName(),
118                 new ClassFieldValueConvertor());
119         fieldValueConvertors.put(Double.class.getName(),
120                 new NumberFieldValueConvertor(
121                         FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
122         fieldValueConvertors.put(Float.class.getName(),
123                 new NumberFieldValueConvertor(
124                         FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
125         fieldValueConvertors.put(Integer.class.getName(),
126                 new NumberFieldValueConvertor(
127                         FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
128         fieldValueConvertors.put(Object.class.getName(),
129                 new FieldValueConvertor());
130         fieldValueConvertors.put(Short.class.getName(),
131                 new NumberFieldValueConvertor(
132                         FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
133     }
134     /***
135      * <p>
136      * Find the appropriate convertor for the field value class provided.
137      * This method goes thro' all the parent classes of the class provided
138      * till it finds a suitable convertor.
139      * </p>
140      *
141      * @param fieldValueClass Class of the field value we are tryinh to convert
142      * from a string.
143      * @return value field value convertor.
144      * @throws SystemException thrown if there is a class in the hierarchy
145      * for which we have no field value convertor.
146      */
147     public FieldValueConvertor getFieldValueConvertorForClass(
148             final Class fieldValueClass)
149             throws SystemException {
150         return getFieldValueConvertorForClass(fieldValueClass, fieldValueClass);
151     }
152     /***
153      * <p>
154      * Find the appropriate convertor for the field value class provided.
155      * This method goes thro' all the parents till it finds a suitable
156      * convertor.
157      * </p>
158      *
159      * @param originalClass
160      *            original class we searched for (used in error reporting).
161      * @param fieldValueClass
162      *            class of the field value to convert.
163      * @return value field value convertor.
164      * @throws ServletException thrown if there is a class in the hierarchy
165      * for which we have no field value convertor.
166      */
167     protected FieldValueConvertor getFieldValueConvertorForClass(
168             final Class originalClass,
169             final Class fieldValueClassParam) throws SystemException {
170         Class fieldValueClass = fieldValueClassParam;
171         String className = fieldValueClass.getName();
172         FieldValueConvertor convertor =
173             (FieldValueConvertor) fieldValueConvertors
174                 .get(className);
175         if (convertor != null) {
176             return convertor;
177         }
178         // if this is a primitive type, get the convertor for the wrapper
179         // class (i.e. java.lang.Boolean for boolean)
180         if (fieldValueClass.isPrimitive()) {
181             fieldValueClass = DefaultFieldValueConvertorFactory
182                 .convertPrimitiveType(fieldValueClass);
183             className = fieldValueClass.getName();
184             convertor =
185                 (FieldValueConvertor) fieldValueConvertors
186                     .get(className);
187             if (convertor != null) {
188                 return convertor;
189             }
190         }
191 
192         Class parentClass = fieldValueClass.getSuperclass();
193         // there _has_ to be a convertor for Object!
194         if ("java.lang.Object".equals(className) || (parentClass == null)) {
195             throw new SystemException(
196                     "ERROR: no field value convertor defined for '"
197                             + originalClass.getName() + "'.");
198         }
199         return getFieldValueConvertorForClass(originalClass, parentClass);
200     }
201     /***
202      * <p>
203      * Set the field value convertor to use for a particular class.
204      * </p>
205      *
206      * @param fieldClass
207      *            class to be converted.
208      * @param convertor
209      *            convertor to use for this class.
210      */
211     public void setFieldValueConvertor(final Class fieldClass,
212             final FieldValueConvertor convertor) {
213         fieldValueConvertors.put(fieldClass.getName(), convertor);
214     }
215 
216 }