Author: bpoussin Date: 2010-12-08 16:23:46 +0100 (Wed, 08 Dec 2010) New Revision: 578 Url: http://nuiton.org/repositories/revision/wikitty/578 Log: try to fix conversion date error when date is null Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java =================================================================== --- trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java 2010-12-08 14:30:26 UTC (rev 577) +++ trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java 2010-12-08 15:23:46 UTC (rev 578) @@ -25,6 +25,7 @@ package org.nuiton.wikitty; +import com.thoughtworks.xstream.converters.basic.DateConverter; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -60,6 +61,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.beanutils.BeanUtilsBean; +import org.apache.commons.beanutils.converters.DateTimeConverter; import org.apache.commons.lang.time.DateUtils; import org.apache.commons.lang.time.FastDateFormat; @@ -1000,6 +1002,138 @@ return result; } + /** + * redefinition du DateConverter car par defaut il ne support pas la valeur + * null :( + */ + static private class WikittyDateConverter extends DateTimeConverter { + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(WikittyDateConverter.class); + + /** + * Construct a <b>java.util.Date</b> <i>Converter</i> that throws + * a <code>ConversionException</code> if an error occurs. + */ + public WikittyDateConverter() { + super(); + } + + /** + * Construct a <b>java.util.Date</b> <i>Converter</i> that returns + * a default value if an error occurs. + * + * @param defaultValue The default value to be returned + * if the value to be converted is missing or an error + * occurs converting the value. + */ + public WikittyDateConverter(Object defaultValue) { + super(defaultValue); + } + + /** + * Return the default type this <code>Converter</code> handles. + * + * @return The default type this <code>Converter</code> handles. + */ + @Override + protected Class getDefaultType() { + return Date.class; + } + + /** + * Convert the input object into an output object of the + * specified type. + * + * @param type Data type to which this value should be converted + * @param value The input value to be converted + * @return The converted value. + * @throws ConversionException if conversion cannot be performed + * successfully and no default is specified. + */ + @Override + public Object convert(Class type, Object value) { + + Class sourceType = value == null ? null : value.getClass(); + Class targetType = primitive(type == null ? getDefaultType() : type); + + if (log.isDebugEnabled()) { + log.debug("Converting" + + (value == null ? "" : " '" + toString(sourceType) + "'") + + " value '" + value + "' to type '" + toString(targetType) + "'"); + } + + value = convertArray(value); + + // Fixed poussin 20101208 le bout de code original qui pose probleme + // pour la conversion null +// // Missing Value +// if (value == null) { +// return handleMissing(targetType); +// } + + sourceType = value.getClass(); + + try { + // Convert --> String + if (targetType.equals(String.class)) { + return convertToString(value); + + // No conversion necessary + } else if (targetType.equals(sourceType)) { + if (log.isDebugEnabled()) { + log.debug(" No conversion required, value is already a " + + toString(targetType)); + } + return value; + + // Convert --> Type + } else { + Object result = convertToType(targetType, value); + if (log.isDebugEnabled()) { + log.debug(" Converted to " + toString(targetType) + + " value '" + result + "'"); + } + return result; + } + } catch (Throwable t) { + return handleError(targetType, value, t); + } + + } + + /** + * Change primitve Class types to the associated wrapper class. + * @param type The class type to check. + * @return The converted type. + */ + Class primitive(Class type) { + if (type == null || !type.isPrimitive()) { + return type; + } + + if (type == Integer.TYPE) { + return Integer.class; + } else if (type == Double.TYPE) { + return Double.class; + } else if (type == Long.TYPE) { + return Long.class; + } else if (type == Boolean.TYPE) { + return Boolean.class; + } else if (type == Float.TYPE) { + return Float.class; + } else if (type == Short.TYPE) { + return Short.class; + } else if (type == Byte.TYPE) { + return Byte.class; + } else if (type == Character.TYPE) { + return Character.class; + } else { + return type; + } + } + } + static public WikittyDateConverter dateConverter = new WikittyDateConverter(); + /** * Copy all properties (get/set) from source to destination, * except wikitty property @@ -1010,6 +1144,7 @@ */ static public void copyBean(Object source, Object dest) throws Exception { BeanUtilsBean bu = BeanUtilsBean.getInstance(); + bu.getConvertUtils().register(dateConverter, Date.class); PropertyDescriptor[] origDescriptors = bu.getPropertyUtils().getPropertyDescriptors(source);