Search in sources :

Example 1 with DateFormatProperty

use of org.simpleflatmapper.map.property.DateFormatProperty in project SimpleFlatMapper by arnaudroger.

the class CsvMapperDateFormatTest method testReadMultipleFormat.

@Test
public void testReadMultipleFormat() throws Exception {
    String format1 = "dd/MM/yyyy";
    String format2 = "MM-dd-yyyy";
    String format3 = "yyyyMMdd";
    CsvMapper<ObjectWithDate> mapper = CsvMapperFactory.newInstance().addColumnProperty(TRUE, new DateFormatProperty(format1)).addColumnProperty(TRUE, new DateFormatProperty(format2)).addColumnProperty(TRUE, new DateFormatProperty(format3)).newMapper(ObjectWithDate.class);
    String data = "date1\n18/06/2016\n06-19-2016\n20160620";
    List<ObjectWithDate> list = mapper.forEach(new StringReader(data), new ListCollector<ObjectWithDate>()).getList();
    assertEquals(3, list.size());
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    assertEquals(sdf.parse("20160618"), list.get(0).date1);
    assertEquals(sdf.parse("20160619"), list.get(1).date1);
    assertEquals(sdf.parse("20160620"), list.get(2).date1);
}
Also used : ListCollector(org.simpleflatmapper.util.ListCollector) StringReader(java.io.StringReader) SimpleDateFormat(java.text.SimpleDateFormat) DateFormatProperty(org.simpleflatmapper.map.property.DateFormatProperty) Test(org.junit.Test)

Example 2 with DateFormatProperty

use of org.simpleflatmapper.map.property.DateFormatProperty in project SimpleFlatMapper by arnaudroger.

the class FieldMapperToAppendableFactory method newFieldMapper.

@Override
@SuppressWarnings("unchecked")
public <S, P> FieldMapper<S, Appendable> newFieldMapper(PropertyMapping<S, P, CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>> pm, MappingContextFactoryBuilder builder, MapperBuilderErrorHandler mappingErrorHandler) {
    if (pm == null)
        throw new NullPointerException("pm is null");
    Getter<? super S, ? extends P> getter;
    Getter<?, ?> customGetter = pm.getColumnDefinition().getCustomGetterFrom(pm.getPropertyMeta().getOwnerType());
    if (customGetter != null) {
        getter = (Getter<S, P>) customGetter;
    } else {
        getter = pm.getPropertyMeta().getGetter();
    }
    ColumnDefinition<CsvColumnKey, ?> columnDefinition = pm.getColumnDefinition();
    Type type = pm.getPropertyMeta().getPropertyType();
    if (TypeHelper.isPrimitive(type) && !columnDefinition.has(FormatProperty.class)) {
        if (getter instanceof BooleanGetter) {
            return new BooleanFieldMapper<S, Appendable>((BooleanGetter) getter, new BooleanAppendableSetter(cellWriter));
        } else if (getter instanceof ByteGetter) {
            return new ByteFieldMapper<S, Appendable>((ByteGetter) getter, new ByteAppendableSetter(cellWriter));
        } else if (getter instanceof CharacterGetter) {
            return new CharacterFieldMapper<S, Appendable>((CharacterGetter) getter, new CharacterAppendableSetter(cellWriter));
        } else if (getter instanceof ShortGetter) {
            return new ShortFieldMapper<S, Appendable>((ShortGetter) getter, new ShortAppendableSetter(cellWriter));
        } else if (getter instanceof IntGetter) {
            return new IntFieldMapper<S, Appendable>((IntGetter) getter, new IntegerAppendableSetter(cellWriter));
        } else if (getter instanceof LongGetter) {
            return new LongFieldMapper<S, Appendable>((LongGetter) getter, new LongAppendableSetter(cellWriter));
        } else if (getter instanceof FloatGetter) {
            return new FloatFieldMapper<S, Appendable>((FloatGetter) getter, new FloatAppendableSetter(cellWriter));
        } else if (getter instanceof DoubleGetter) {
            return new DoubleFieldMapper<S, Appendable>((DoubleGetter) getter, new DoubleAppendableSetter(cellWriter));
        }
    }
    Setter<Appendable, ? super P> setter = null;
    if (TypeHelper.isEnum(type) && columnDefinition.has(EnumOrdinalFormatProperty.class)) {
        setter = (Setter) new EnumOrdinalAppendableSetter(cellWriter);
    }
    Format format = null;
    if (columnDefinition.has(FormatProperty.class)) {
        format = columnDefinition.lookFor(FormatProperty.class).format();
    } else if (TypeHelper.areEquals(type, Date.class)) {
        String df = DEFAULT_DATE_FORMAT;
        DateFormatProperty dfp = columnDefinition.lookFor(DateFormatProperty.class);
        if (dfp != null) {
            df = dfp.get();
        }
        format = new SimpleDateFormat(df);
    }
    if (format != null) {
        final Format f = format;
        builder.addSupplier(pm.getColumnKey().getIndex(), new CloneFormatSupplier(f));
        return new FormatingAppender<S>(getter, new MappingContextFormatGetter<S>(pm.getColumnKey().getIndex()), cellWriter);
    }
    if (setter == null) {
        setter = getSetter(pm, cellWriter);
    }
    if (setter == null) {
        Converter<? super P, ? extends CharSequence> converter = converterService.findConverter(pm.getPropertyMeta().getPropertyType(), CharSequence.class, columnDefinition != null ? columnDefinition.properties() : new Object[0]);
        if (converter != null) {
            return new ConvertingAppender<S, P>(getter, converter, cellWriter);
        }
    }
    return new FieldMapperImpl<S, Appendable, P>(getter, setter);
}
Also used : ByteGetter(org.simpleflatmapper.reflect.primitive.ByteGetter) DoubleGetter(org.simpleflatmapper.reflect.primitive.DoubleGetter) LongGetter(org.simpleflatmapper.reflect.primitive.LongGetter) FieldMapperImpl(org.simpleflatmapper.map.fieldmapper.FieldMapperImpl) BooleanGetter(org.simpleflatmapper.reflect.primitive.BooleanGetter) Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) CharacterGetter(org.simpleflatmapper.reflect.primitive.CharacterGetter) IntFieldMapper(org.simpleflatmapper.map.fieldmapper.IntFieldMapper) CharacterFieldMapper(org.simpleflatmapper.map.fieldmapper.CharacterFieldMapper) FloatGetter(org.simpleflatmapper.reflect.primitive.FloatGetter) BooleanFieldMapper(org.simpleflatmapper.map.fieldmapper.BooleanFieldMapper) ShortGetter(org.simpleflatmapper.reflect.primitive.ShortGetter) EnumOrdinalFormatProperty(org.simpleflatmapper.map.property.EnumOrdinalFormatProperty) Date(java.util.Date) DateFormatProperty(org.simpleflatmapper.map.property.DateFormatProperty) Type(java.lang.reflect.Type) FloatFieldMapper(org.simpleflatmapper.map.fieldmapper.FloatFieldMapper) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) SimpleDateFormat(java.text.SimpleDateFormat) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter)

Example 3 with DateFormatProperty

use of org.simpleflatmapper.map.property.DateFormatProperty in project SimpleFlatMapper by arnaudroger.

the class ProtoClassTest method testMapPersonTs.

@Test
public void testMapPersonTs() throws IOException, ParseException {
    CsvMapper<AddressBookProtos.Person> csvMapper = CsvMapperFactory.newInstance().useAsm(false).newBuilder(AddressBookProtos.Person.class).addMapping("name").addMapping("ts", new DateFormatProperty("yyyyMMdd")).mapper();
    AddressBookProtos.Person person = csvMapper.iterator(new StringReader("arnaud,20170607")).next();
    assertEquals("arnaud", person.getName());
    assertEquals(new SimpleDateFormat("yyyyMMdd").parse("20170607").getTime(), person.getTs().getSeconds() * 1000);
}
Also used : StringReader(java.io.StringReader) SimpleDateFormat(java.text.SimpleDateFormat) DateFormatProperty(org.simpleflatmapper.map.property.DateFormatProperty) Test(org.junit.Test)

Example 4 with DateFormatProperty

use of org.simpleflatmapper.map.property.DateFormatProperty in project SimpleFlatMapper by arnaudroger.

the class CsvColumnDefinition method dateFormats.

public String[] dateFormats() {
    DateFormatProperty[] prop = lookForAll(DateFormatProperty.class);
    String[] patterns = new String[prop.length];
    for (int i = 0; i < prop.length; i++) {
        patterns[i] = prop[i].get();
    }
    if (patterns.length == 0) {
        DefaultDateFormatProperty defaultDateFormatProperty = lookFor(DefaultDateFormatProperty.class);
        if (defaultDateFormatProperty == null) {
            throw new IllegalStateException("No date format specified");
        }
        return new String[] { defaultDateFormatProperty.get() };
    }
    return patterns;
}
Also used : DefaultDateFormatProperty(org.simpleflatmapper.map.property.DefaultDateFormatProperty) DefaultDateFormatProperty(org.simpleflatmapper.map.property.DefaultDateFormatProperty) DateFormatProperty(org.simpleflatmapper.map.property.DateFormatProperty)

Example 5 with DateFormatProperty

use of org.simpleflatmapper.map.property.DateFormatProperty in project SimpleFlatMapper by arnaudroger.

the class CsvWriterBuilderTest method testWriteCsvOnDbObject.

@Test
public void testWriteCsvOnDbObject() throws Exception {
    MapperConfig<CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>> config = MapperConfig.<CsvColumnKey>fieldMapperConfig();
    CsvWriterBuilder<DbObject> builder = CsvWriterBuilder.newBuilder(DbObject.class);
    Mapper<DbObject, Appendable> mapper = builder.addColumn("id").addColumn("name").addColumn("email").addColumn("creation_time", new DateFormatProperty("dd/MM/yyyy HH:mm:ss")).addColumn("type_ordinal", new EnumOrdinalFormatProperty()).addColumn("type_name").mapper();
    DbObject dbObject = CsvWriterTest.newDbObject();
    assertEquals("13,name,email,06/06/2015 17:46:23,1,type3\r\n", mapper.map(dbObject).toString());
    dbObject.setEmail("email,e\" ");
    assertEquals("13,name,\"email,e\"\" \",06/06/2015 17:46:23,1,type3\r\n", mapper.map(dbObject).toString());
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) DbObject(org.simpleflatmapper.test.beans.DbObject) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) EnumOrdinalFormatProperty(org.simpleflatmapper.map.property.EnumOrdinalFormatProperty) DateFormatProperty(org.simpleflatmapper.map.property.DateFormatProperty) Test(org.junit.Test)

Aggregations

DateFormatProperty (org.simpleflatmapper.map.property.DateFormatProperty)8 Test (org.junit.Test)6 SimpleDateFormat (java.text.SimpleDateFormat)4 StringReader (java.io.StringReader)3 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)3 EnumOrdinalFormatProperty (org.simpleflatmapper.map.property.EnumOrdinalFormatProperty)3 DbObject (org.simpleflatmapper.test.beans.DbObject)2 ListCollector (org.simpleflatmapper.util.ListCollector)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Type (java.lang.reflect.Type)1 Format (java.text.Format)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1 MappingException (org.simpleflatmapper.map.MappingException)1 BooleanFieldMapper (org.simpleflatmapper.map.fieldmapper.BooleanFieldMapper)1 CharacterFieldMapper (org.simpleflatmapper.map.fieldmapper.CharacterFieldMapper)1 FieldMapperImpl (org.simpleflatmapper.map.fieldmapper.FieldMapperImpl)1 FloatFieldMapper (org.simpleflatmapper.map.fieldmapper.FloatFieldMapper)1 IntFieldMapper (org.simpleflatmapper.map.fieldmapper.IntFieldMapper)1