001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * ---------------------------------------------------------
005: * ivata groupware may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * ---------------------------------------------------------
030: * $Log: DefaultFieldValueConvertorFactory.java,v $
031: * Revision 1.5 2005/10/11 18:55:29 colinmacleod
032: * Fixed some checkstyle and javadoc issues.
033: *
034: * Revision 1.4 2005/10/02 14:06:32 colinmacleod
035: * Added/improved log4j logging.
036: *
037: * Revision 1.3 2005/09/16 13:41:18 colinmacleod
038: * Created new convertor class to handle timestamps.
039: *
040: * Revision 1.2 2005/09/14 12:51:52 colinmacleod
041: * Added serialVersionUID.
042: *
043: * Revision 1.1 2005/04/11 12:27:02 colinmacleod
044: * Added preliminary support for filters.
045: * Added FieldValueConvertor factor interface
046: * to split off value convertors for reuse.
047: *
048: * ---------------------------------------------------------
049: */
050: package com.ivata.mask.field;
051:
052: import org.apache.log4j.Logger;
053:
054: import java.io.Serializable;
055: import java.math.BigDecimal;
056: import java.sql.Timestamp;
057: import java.util.Date;
058: import java.util.HashMap;
059: import java.util.Map;
060:
061: import com.ivata.mask.field.date.DateFieldValueConvertor;
062: import com.ivata.mask.field.date.TimestampFieldValueConvertor;
063: import com.ivata.mask.field.number.NumberFieldValueConvertor;
064: import com.ivata.mask.util.SystemException;
065:
066: /**
067: * <p>
068: * This default implementation of <code>FieldValueConvertorFactory</code>
069: * creates field value convertors for all types.
070: * </p>
071: *
072: * <copyDoc>Refer to {@link com.ivata.mask.field.FieldValueConvertor}.</copyDoc>
073: *
074: * @since ivata masks 0.5.1 (17-Mar-2005)
075: * @author Colin MacLeod
076: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
077: * @version $Revision: 1.5 $
078: */
079: public class DefaultFieldValueConvertorFactory implements
080: FieldValueConvertorFactory, Serializable {
081: /**
082: * Logger for this class.
083: */
084: private static final Logger logger = Logger
085: .getLogger(DefaultFieldValueConvertorFactory.class);
086:
087: /**
088: * Serialization version (for <code>Serializable</code> interface).
089: */
090: private static final long serialVersionUID = 1L;
091:
092: /**
093: * Convert a primitive type (such as <code>int</code>, <code>char</code>) to
094: * the wrapper class associated with it (such as <code>Integer</code>,
095: * <code>Character</code>).
096: *
097: * @param type The type to be converted.
098: * @return Wrapper class for the type provided.
099: * @throws SystemException if an unknown type is encountered.
100: */
101: public static Class convertPrimitiveType(final Class type)
102: throws SystemException {
103: if (logger.isDebugEnabled()) {
104: logger.debug("convertPrimitiveType(Class type = " + type
105: + ") - start");
106: }
107:
108: String typeName = type.getName();
109: if ("boolean".equals(typeName)) {
110: if (logger.isDebugEnabled()) {
111: logger
112: .debug("convertPrimitiveType - end - return value = "
113: + Boolean.class);
114: }
115: return Boolean.class;
116: } else if ("byte".equals(typeName)) {
117: if (logger.isDebugEnabled()) {
118: logger
119: .debug("convertPrimitiveType - end - return value = "
120: + Byte.class);
121: }
122: return Byte.class;
123: } else if ("char".equals(typeName)) {
124: if (logger.isDebugEnabled()) {
125: logger
126: .debug("convertPrimitiveType - end - return value = "
127: + Character.class);
128: }
129: return Character.class;
130: } else if ("double".equals(typeName)) {
131: if (logger.isDebugEnabled()) {
132: logger
133: .debug("convertPrimitiveType - end - return value = "
134: + Double.class);
135: }
136: return Double.class;
137: } else if ("float".equals(typeName)) {
138: if (logger.isDebugEnabled()) {
139: logger
140: .debug("convertPrimitiveType - end - return value = "
141: + Float.class);
142: }
143: return Float.class;
144: } else if ("int".equals(typeName)) {
145: if (logger.isDebugEnabled()) {
146: logger
147: .debug("convertPrimitiveType - end - return value = "
148: + Integer.class);
149: }
150: return Integer.class;
151: } else if ("long".equals(typeName)) {
152: if (logger.isDebugEnabled()) {
153: logger
154: .debug("convertPrimitiveType - end - return value = "
155: + Long.class);
156: }
157: return Long.class;
158: } else if ("short".equals(typeName)) {
159: if (logger.isDebugEnabled()) {
160: logger
161: .debug("convertPrimitiveType - end - return value = "
162: + Short.class);
163: }
164: return Short.class;
165: } else {
166: throw new SystemException("Unknown primitive type: " + type);
167: }
168: }
169:
170: /**
171: * <p>
172: * Stores all the field value convertors, indexed by the class name they
173: * operate on.
174: * </p>
175: */
176: private Map fieldValueConvertors = new HashMap();
177:
178: /**
179: * Constructor.
180: */
181: public DefaultFieldValueConvertorFactory() {
182: super ();
183: // NOTE: the field value convertor for java.lang.Object expects the
184: // class to have a single string parameter. This will work for Boolean,
185: // for example.
186: fieldValueConvertors
187: .put(
188: BigDecimal.class.getName(),
189: new NumberFieldValueConvertor(
190: FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
191: fieldValueConvertors
192: .put(
193: Date.class.getName(),
194: new DateFieldValueConvertor(
195: FieldValueConvertorConstants.DEFAULT_DATE_PATTERN));
196: fieldValueConvertors.put(Timestamp.class.getName(),
197: new TimestampFieldValueConvertor());
198: fieldValueConvertors.put(Class.class.getName(),
199: new ClassFieldValueConvertor());
200: fieldValueConvertors
201: .put(
202: Double.class.getName(),
203: new NumberFieldValueConvertor(
204: FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
205: fieldValueConvertors
206: .put(
207: Float.class.getName(),
208: new NumberFieldValueConvertor(
209: FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
210: fieldValueConvertors
211: .put(
212: Integer.class.getName(),
213: new NumberFieldValueConvertor(
214: FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
215: fieldValueConvertors.put(Object.class.getName(),
216: new FieldValueConvertor());
217: fieldValueConvertors
218: .put(
219: Short.class.getName(),
220: new NumberFieldValueConvertor(
221: FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
222: }
223:
224: /**
225: * <p>
226: * Find the appropriate convertor for the field value class provided.
227: * This method goes thro' all the parent classes of the class provided
228: * till it finds a suitable convertor.
229: * </p>
230: *
231: * @param fieldValueClass Class of the field value we are tryinh to convert
232: * from a string.
233: * @return value field value convertor.
234: * @throws SystemException thrown if there is a class in the hierarchy
235: * for which we have no field value convertor.
236: */
237: public FieldValueConvertor getFieldValueConvertorForClass(
238: final Class fieldValueClass) throws SystemException {
239: if (logger.isDebugEnabled()) {
240: logger
241: .debug("getFieldValueConvertorForClass(Class fieldValueClass = "
242: + fieldValueClass + ") - start");
243: }
244:
245: FieldValueConvertor returnFieldValueConvertor = getFieldValueConvertorForClass(
246: fieldValueClass, fieldValueClass);
247: if (logger.isDebugEnabled()) {
248: logger
249: .debug("getFieldValueConvertorForClass - end - return "
250: + "value = " + returnFieldValueConvertor);
251: }
252: return returnFieldValueConvertor;
253: }
254:
255: /**
256: * <p>
257: * Find the appropriate convertor for the field value class provided.
258: * This method goes thro' all the parents till it finds a suitable
259: * convertor.
260: * </p>
261: *
262: * @param originalClass
263: * original class we searched for (used in error reporting).
264: * @param fieldValueClassParam
265: * class of the field value to convert.
266: * @return value field value convertor.
267: * @throws SystemException thrown if there is a class in the hierarchy
268: * for which we have no field value convertor.
269: */
270: protected FieldValueConvertor getFieldValueConvertorForClass(
271: final Class originalClass, final Class fieldValueClassParam)
272: throws SystemException {
273: if (logger.isDebugEnabled()) {
274: logger
275: .debug("getFieldValueConvertorForClass(Class originalClass = "
276: + originalClass
277: + ", Class fieldValueClassParam = "
278: + fieldValueClassParam + ") - start");
279: }
280:
281: Class fieldValueClass = fieldValueClassParam;
282: String className = fieldValueClass.getName();
283: FieldValueConvertor convertor = (FieldValueConvertor) fieldValueConvertors
284: .get(className);
285: if (convertor != null) {
286: if (logger.isDebugEnabled()) {
287: logger.debug("getFieldValueConvertorForClass - end - "
288: + "return value = " + convertor);
289: }
290: return convertor;
291: }
292: // if this is a primitive type, get the convertor for the wrapper
293: // class (i.e. java.lang.Boolean for boolean)
294: if (fieldValueClass.isPrimitive()) {
295: fieldValueClass = DefaultFieldValueConvertorFactory
296: .convertPrimitiveType(fieldValueClass);
297: className = fieldValueClass.getName();
298: convertor = (FieldValueConvertor) fieldValueConvertors
299: .get(className);
300: if (convertor != null) {
301: if (logger.isDebugEnabled()) {
302: logger
303: .debug("getFieldValueConvertorForClass - end - "
304: + "return value = " + convertor);
305: }
306: return convertor;
307: }
308: }
309:
310: Class parentClass = fieldValueClass.getSuperclass();
311: // there _has_ to be a convertor for Object!
312: if ("java.lang.Object".equals(className)
313: || (parentClass == null)) {
314: throw new SystemException(
315: "ERROR: no field value convertor defined for '"
316: + originalClass.getName() + "'.");
317: }
318: FieldValueConvertor returnFieldValueConvertor = getFieldValueConvertorForClass(
319: originalClass, parentClass);
320: if (logger.isDebugEnabled()) {
321: logger.debug("getFieldValueConvertorForClass - end - "
322: + "return value = " + returnFieldValueConvertor);
323: }
324: return returnFieldValueConvertor;
325: }
326:
327: /**
328: * <p>
329: * Set the field value convertor to use for a particular class.
330: * </p>
331: *
332: * @param fieldClass
333: * class to be converted.
334: * @param convertor
335: * convertor to use for this class.
336: */
337: public void setFieldValueConvertor(final Class fieldClass,
338: final FieldValueConvertor convertor) {
339: if (logger.isDebugEnabled()) {
340: logger.debug("setFieldValueConvertor(Class fieldClass = "
341: + fieldClass + ", FieldValueConvertor convertor = "
342: + convertor + ") - start");
343: }
344:
345: fieldValueConvertors.put(fieldClass.getName(), convertor);
346:
347: if (logger.isDebugEnabled()) {
348: logger.debug("setFieldValueConvertor - end");
349: }
350: }
351:
352: }
|