Author: tchemit Date: 2011-11-11 18:15:10 +0100 (Fri, 11 Nov 2011) New Revision: 36 Url: http://forge.codelutin.com/repositories/revision/echobase/36 Log: - change type to text - add a special date converter (for dbeditor) Added: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/EchoBaseDateConverter.java trunk/echobase-entities/src/test/java/fr/ifremer/echobase/EchoBaseDateConverterTest.java Modified: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/EchoBaseConfiguration.java trunk/echobase-entities/src/main/xmi/echobase.properties Modified: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/EchoBaseConfiguration.java =================================================================== --- trunk/echobase-entities/src/main/java/fr/ifremer/echobase/EchoBaseConfiguration.java 2011-11-11 02:13:58 UTC (rev 35) +++ trunk/echobase-entities/src/main/java/fr/ifremer/echobase/EchoBaseConfiguration.java 2011-11-11 17:15:10 UTC (rev 36) @@ -54,6 +54,9 @@ protected ApplicationConfig applicationConfig; public EchoBaseConfiguration() { + + EchoBaseDateConverter.initDateConverter(); + applicationConfig = new ApplicationConfig(); applicationConfig.setConfigFileName("echobase.properties"); @@ -128,7 +131,7 @@ public boolean getOptionAsBoolean(String propertyName) { Boolean result = applicationConfig.getOptionAsBoolean(propertyName); - return result!=null && result; + return result != null && result; } /** Added: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/EchoBaseDateConverter.java =================================================================== --- trunk/echobase-entities/src/main/java/fr/ifremer/echobase/EchoBaseDateConverter.java (rev 0) +++ trunk/echobase-entities/src/main/java/fr/ifremer/echobase/EchoBaseDateConverter.java 2011-11-11 17:15:10 UTC (rev 36) @@ -0,0 +1,71 @@ +/* + * #%L + * EchoBase :: Entities + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase; + +import org.apache.commons.beanutils.ConvertUtils; +import org.apache.commons.beanutils.Converter; +import org.apache.commons.beanutils.converters.DateTimeConverter; +import org.nuiton.util.converter.ConverterUtil; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * A date converter to make possible convert from string using our default + * format pattern. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.1 + */ +public class EchoBaseDateConverter extends DateTimeConverter { + + public static void initDateConverter() { + Converter converter = ConverterUtil.getConverter(Date.class); + if (converter != null && !(converter instanceof EchoBaseDateConverter)) { + ConvertUtils.deregister(Date.class); + ConvertUtils.register(new EchoBaseDateConverter(), Date.class); + } + } + + protected final DateFormat format = + new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss"); + + + @Override + protected Class getDefaultType() { + return Date.class; + } + + @Override + protected Object convertToType(Class targetType, Object value) throws Exception { + if (getDefaultType().equals(targetType) && value instanceof String) { + + // let's do our own stuff + Date parse = format.parse((String) value); + return parse; + } + return super.convertToType(targetType, value); + } +} Property changes on: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/EchoBaseDateConverter.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/echobase-entities/src/main/xmi/echobase.properties =================================================================== --- trunk/echobase-entities/src/main/xmi/echobase.properties 2011-11-11 02:13:58 UTC (rev 35) +++ trunk/echobase-entities/src/main/xmi/echobase.properties 2011-11-11 17:15:10 UTC (rev 36) @@ -31,6 +31,8 @@ model.tagValue.noPCS=true model.tagValue.doNotGenerateBooleanGetMethods=true +fr.ifremer.echobase.entities.EntityModificationLog.attribute.modificationText.tagvalue.type=text + ############################################################################### ### Lazy attributes for references ############################################ ############################################################################### Added: trunk/echobase-entities/src/test/java/fr/ifremer/echobase/EchoBaseDateConverterTest.java =================================================================== --- trunk/echobase-entities/src/test/java/fr/ifremer/echobase/EchoBaseDateConverterTest.java (rev 0) +++ trunk/echobase-entities/src/test/java/fr/ifremer/echobase/EchoBaseDateConverterTest.java 2011-11-11 17:15:10 UTC (rev 36) @@ -0,0 +1,84 @@ +/* + * #%L + * EchoBase :: Entities + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase; + +import fr.ifremer.echobase.entities.EntityModificationLog; +import fr.ifremer.echobase.entities.EntityModificationLogImpl; +import junit.framework.Assert; +import org.apache.commons.beanutils.BeanUtils; +import org.apache.commons.beanutils.ConversionException; +import org.junit.Test; + +import java.util.Calendar; +import java.util.Date; + +/** + * Tests the {@link EchoBaseDateConverter}. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.1 + */ +public class EchoBaseDateConverterTest { + + public static final String DATE = "2011-11-08T20:54:59"; + + @Test + public void testConvert() throws Exception { + EchoBaseDateConverter converter = new EchoBaseDateConverter(); + Object date = converter.convert(Date.class, DATE); + assertDate(date); + } + + @Test(expected = ConversionException.class) + public void testConvertFromDefaultBeanUtilsConverter() throws Exception { + + EntityModificationLog bean = new EntityModificationLogImpl(); + BeanUtils.setProperty(bean, EntityModificationLog.PROPERTY_MODIFICATION_DATE, DATE); + + } + + @Test + public void testConvertFromBeanUtilsConverterWithOur() throws Exception { + + EchoBaseDateConverter.initDateConverter(); + EntityModificationLog bean = new EntityModificationLogImpl(); + BeanUtils.setProperty(bean,EntityModificationLog.PROPERTY_MODIFICATION_DATE, DATE); + Date date = bean.getModificationDate(); + assertDate(date); + } + + private void assertDate(Object date) { + Assert.assertNotNull(date); + Assert.assertTrue(date instanceof Date); + Calendar calendar = Calendar.getInstance(); + calendar.setTime((Date) date); + Assert.assertEquals(2011,calendar.get(Calendar.YEAR)); + Assert.assertEquals(10,calendar.get(Calendar.MONTH)); + Assert.assertEquals(8,calendar.get(Calendar.DAY_OF_MONTH)); + Assert.assertEquals(20,calendar.get(Calendar.HOUR_OF_DAY)); + Assert.assertEquals(54,calendar.get(Calendar.MINUTE)); + Assert.assertEquals(59,calendar.get(Calendar.SECOND)); + } + +} Property changes on: trunk/echobase-entities/src/test/java/fr/ifremer/echobase/EchoBaseDateConverterTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native