Search in sources :

Example 11 with FieldMapperColumnDefinition

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

the class PreparedStatementMapperBuilder method buildIndexFieldMappers.

@SuppressWarnings("unchecked")
public MultiIndexFieldMapper<T>[] buildIndexFieldMappers() {
    final List<MultiIndexFieldMapper<T>> fields = new ArrayList<MultiIndexFieldMapper<T>>();
    propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>>() {

        @Override
        public void handle(PropertyMapping<T, ?, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> pm) {
            if (isMultiIndex(pm.getPropertyMeta())) {
                fields.add(newCollectionFieldMapper(pm));
            } else {
                fields.add(newFieldMapper(pm));
            }
        }

        private <P, C> MultiIndexFieldMapper<T> newCollectionFieldMapper(PropertyMapping<T, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> pm) {
            PropertyMeta<T, ?> propertyMeta = pm.getPropertyMeta();
            IndexedGetter<C, P> indexedGetter;
            IntGetter<? super C> sizeGetter;
            Getter<T, C> collectionGetter = (Getter<T, C>) propertyMeta.getGetter();
            if (TypeHelper.isAssignable(List.class, propertyMeta.getPropertyType())) {
                indexedGetter = (IndexedGetter<C, P>) new ListIndexedGetter<P>();
                sizeGetter = (IntGetter<C>) new ListSizeGetter();
            } else if (TypeHelper.isArray(propertyMeta.getPropertyType())) {
                indexedGetter = (IndexedGetter<C, P>) new ArrayIndexedGetter<P>();
                sizeGetter = new ArraySizeGetter();
            } else {
                throw new IllegalArgumentException("Unexpected elementMeta" + propertyMeta);
            }
            PropertyMeta<C, P> childProperty = (PropertyMeta<C, P>) pm.getPropertyMeta().getPropertyClassMeta().newPropertyFinder(ConstantPredicate.<PropertyMeta<?, ?>>truePredicate()).findProperty(DefaultPropertyNameMatcher.of("0"), pm.getColumnDefinition().properties());
            final PropertyMapping<C, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> pmchildProperttMeta = pm.propertyMeta(childProperty);
            IndexedSetter<PreparedStatement, P> setter = getSetter(pmchildProperttMeta);
            return new CollectionIndexFieldMapper<T, C, P>(setter, collectionGetter, sizeGetter, indexedGetter);
        }

        private <P, C> IndexedSetter<PreparedStatement, P> getSetter(PropertyMapping<C, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> pm) {
            IndexedSetter<PreparedStatement, P> setter = null;
            IndexedSetterProperty indexedSetterProperty = pm.getColumnDefinition().lookFor(IndexedSetterProperty.class);
            if (indexedSetterProperty != null) {
                setter = (IndexedSetter<PreparedStatement, P>) indexedSetterProperty.getIndexedSetter();
            }
            if (setter == null) {
                setter = indexedSetterFactory(pm);
            }
            if (setter == null) {
                mapperConfig.mapperBuilderErrorHandler().accessorNotFound("Could not find setter for " + pm.getColumnKey() + " type " + pm.getPropertyMeta().getPropertyType() + " path " + pm.getPropertyMeta().getPath() + " See " + ErrorDoc.toUrl("PS_SETTER_NOT_FOUND"));
            }
            return setter;
        }

        private <P, C> IndexedSetter<PreparedStatement, P> indexedSetterFactory(PropertyMapping<C, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> pm) {
            IndexedSetter<PreparedStatement, P> setter = null;
            final IndexedSetterFactoryProperty indexedSetterPropertyFactory = pm.getColumnDefinition().lookFor(IndexedSetterFactoryProperty.class);
            if (indexedSetterPropertyFactory != null) {
                IndexedSetterFactory<PreparedStatement, PropertyMapping<?, ?, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>> setterFactory = (IndexedSetterFactory<PreparedStatement, PropertyMapping<?, ?, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>>) indexedSetterPropertyFactory.getIndexedSetterFactory();
                setter = setterFactory.getIndexedSetter(pm);
            }
            if (setter == null) {
                setter = indexedSetterFactory.getIndexedSetter(pm);
            }
            if (setter == null) {
                final ClassMeta<P> classMeta = pm.getPropertyMeta().getPropertyClassMeta();
                if (classMeta instanceof ObjectClassMeta) {
                    ObjectClassMeta<P> ocm = (ObjectClassMeta<P>) classMeta;
                    if (ocm.getNumberOfProperties() == 1) {
                        PropertyMeta<P, ?> subProp = ocm.getFirstProperty();
                        final PropertyMapping<?, ?, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> subPropertyMapping = pm.propertyMeta(subProp);
                        IndexedSetter<PreparedStatement, ?> subSetter = indexedSetterFactory(subPropertyMapping);
                        if (subSetter != null) {
                            setter = new PreparedStatementIndexSetterOnGetter<Object, P>((PreparedStatementIndexSetter<Object>) subSetter, (Getter<P, Object>) subProp.getGetter());
                        }
                    }
                }
            }
            return setter;
        }

        private <P> MultiIndexFieldMapper<T> newFieldMapper(PropertyMapping<T, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> pm) {
            return new SingleIndexFieldMapper<T, P>(getSetter(pm), pm.getPropertyMeta().getGetter());
        }
    });
    return fields.toArray(new MultiIndexFieldMapper[0]);
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ListIndexedGetter(org.simpleflatmapper.reflect.getter.ListIndexedGetter) Getter(org.simpleflatmapper.reflect.Getter) ArraySizeGetter(org.simpleflatmapper.reflect.getter.ArraySizeGetter) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter) ArrayIndexedGetter(org.simpleflatmapper.reflect.getter.ArrayIndexedGetter) IndexedGetter(org.simpleflatmapper.reflect.IndexedGetter) PreparedStatementIndexSetterOnGetter(org.simpleflatmapper.jdbc.impl.setter.PreparedStatementIndexSetterOnGetter) ListSizeGetter(org.simpleflatmapper.reflect.getter.ListSizeGetter) ArrayIndexedGetter(org.simpleflatmapper.reflect.getter.ArrayIndexedGetter) ArrayList(java.util.ArrayList) IndexedSetterFactoryProperty(org.simpleflatmapper.jdbc.property.IndexedSetterFactoryProperty) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) ArrayList(java.util.ArrayList) List(java.util.List) IndexedSetterProperty(org.simpleflatmapper.jdbc.property.IndexedSetterProperty) PreparedStatementIndexSetterOnGetter(org.simpleflatmapper.jdbc.impl.setter.PreparedStatementIndexSetterOnGetter) ObjectClassMeta(org.simpleflatmapper.reflect.meta.ObjectClassMeta) ClassMeta(org.simpleflatmapper.reflect.meta.ClassMeta) IndexedSetterFactory(org.simpleflatmapper.reflect.IndexedSetterFactory) PreparedStatementIndexedSetterFactory(org.simpleflatmapper.jdbc.impl.PreparedStatementIndexedSetterFactory) PreparedStatement(java.sql.PreparedStatement) ArraySizeGetter(org.simpleflatmapper.reflect.getter.ArraySizeGetter) IndexedSetter(org.simpleflatmapper.reflect.IndexedSetter) ListIndexedGetter(org.simpleflatmapper.reflect.getter.ListIndexedGetter) ArrayIndexedGetter(org.simpleflatmapper.reflect.getter.ArrayIndexedGetter) IndexedGetter(org.simpleflatmapper.reflect.IndexedGetter) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) ObjectClassMeta(org.simpleflatmapper.reflect.meta.ObjectClassMeta) ListSizeGetter(org.simpleflatmapper.reflect.getter.ListSizeGetter) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter)

Example 12 with FieldMapperColumnDefinition

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

the class DataTypeTest method getSetter.

public static <N> Setter<SettableByIndexData, N> getSetter(Class<N> target, DataType dataType) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException {
    MapperConfig<DatastaxColumnKey, FieldMapperColumnDefinition<DatastaxColumnKey>> mapperConfig = MapperConfig.<DatastaxColumnKey>fieldMapperConfig();
    ReflectionService reflectionService = ReflectionService.newInstance();
    SettableDataSetterFactory factory = new SettableDataSetterFactory(mapperConfig, reflectionService);
    DatastaxColumnKey columnKey = new DatastaxColumnKey("col", 1, dataType);
    return factory.getSetter(newPM(target, dataType, columnKey));
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) DatastaxColumnKey(org.simpleflatmapper.datastax.DatastaxColumnKey) SettableDataSetterFactory(org.simpleflatmapper.datastax.impl.SettableDataSetterFactory) ReflectionService(org.simpleflatmapper.reflect.ReflectionService)

Example 13 with FieldMapperColumnDefinition

use of org.simpleflatmapper.map.property.FieldMapperColumnDefinition 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)

Example 14 with FieldMapperColumnDefinition

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

the class FieldMapperToAppendableFactory method setterFromFactory.

@SuppressWarnings("unchecked")
private <S, P> Setter<Appendable, ? super P> setterFromFactory(PropertyMapping<S, P, CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>> pm) {
    Setter<Appendable, ? super P> setter = null;
    final SetterFactoryProperty setterFactoryProperty = pm.getColumnDefinition().lookFor(SetterFactoryProperty.class);
    if (setterFactoryProperty != null) {
        final SetterFactory<Appendable, PropertyMapping<S, P, CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>>> setterFactory = (SetterFactory<Appendable, PropertyMapping<S, P, CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>>>) setterFactoryProperty.getSetterFactory();
        setter = setterFactory.getSetter(pm);
    }
    if (setter == null) {
        if (!pm.getPropertyMeta().isSelf()) {
            final ClassMeta<P> classMeta = pm.getPropertyMeta().getPropertyClassMeta();
            if (classMeta instanceof ObjectClassMeta) {
                ObjectClassMeta<P> ocm = (ObjectClassMeta<P>) classMeta;
                if (ocm.getNumberOfProperties() == 1) {
                    PropertyMeta<P, ?> subProp = ocm.getFirstProperty();
                    Setter<Appendable, Object> subSetter = (Setter<Appendable, Object>) setterFromFactory(pm.propertyMeta(subProp));
                    if (subSetter != null) {
                        setter = new SetterOnGetter<Appendable, Object, P>(subSetter, (Getter<P, Object>) subProp.getGetter());
                    } else {
                        return new ObjectToStringSetter<P>(subProp.getGetter());
                    }
                }
            }
        }
    }
    return setter;
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) CharacterGetter(org.simpleflatmapper.reflect.primitive.CharacterGetter) ShortGetter(org.simpleflatmapper.reflect.primitive.ShortGetter) LongGetter(org.simpleflatmapper.reflect.primitive.LongGetter) BooleanGetter(org.simpleflatmapper.reflect.primitive.BooleanGetter) Getter(org.simpleflatmapper.reflect.Getter) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter) FloatGetter(org.simpleflatmapper.reflect.primitive.FloatGetter) SetterOnGetter(org.simpleflatmapper.reflect.setter.SetterOnGetter) ByteGetter(org.simpleflatmapper.reflect.primitive.ByteGetter) DoubleGetter(org.simpleflatmapper.reflect.primitive.DoubleGetter) SetterFactory(org.simpleflatmapper.reflect.SetterFactory) SetterFactoryProperty(org.simpleflatmapper.map.property.SetterFactoryProperty) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) Setter(org.simpleflatmapper.reflect.Setter) ObjectClassMeta(org.simpleflatmapper.reflect.meta.ObjectClassMeta)

Example 15 with FieldMapperColumnDefinition

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

the class AbstractConstantTargetMapperBuilder method mapper.

@SuppressWarnings("unchecked")
public Mapper<T, S> mapper() {
    final List<FieldMapper<T, S>> mappers = new ArrayList<FieldMapper<T, S>>();
    final MappingContextFactoryBuilder mappingContextFactoryBuilder = new MappingContextFactoryBuilder(new KeySourceGetter<K, T>() {

        @Override
        public Object getValue(K key, T source) throws Exception {
            throw new UnsupportedOperationException();
        }
    });
    propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>>>() {

        @Override
        public void handle(PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>> pm) {
            preFieldProcess(mappers, pm);
            FieldMapper<T, S> fieldMapper = fieldAppenderFactory.newFieldMapper(pm, mappingContextFactoryBuilder, mapperConfig.mapperBuilderErrorHandler());
            mappers.add(fieldMapper);
            postFieldProcess(mappers, pm);
        }
    });
    postMapperProcess(mappers);
    Mapper<T, S> mapper;
    FieldMapper[] fields = mappers.toArray(new FieldMapper[0]);
    BiInstantiator<T, MappingContext<? super T>, S> instantiator = getInstantiator();
    if (mappers.size() < 256) {
        try {
            mapper = reflectionService.getAsmFactory().registerOrCreate(MapperAsmFactory.class, new UnaryFactory<AsmFactory, MapperAsmFactory>() {

                @Override
                public MapperAsmFactory newInstance(AsmFactory asmFactory) {
                    return new MapperAsmFactory(asmFactory);
                }
            }).<T, S>createMapper(getKeys(), fields, new FieldMapper[0], instantiator, TypeHelper.<T>toClass(classMeta.getType()), sourceClass);
        } catch (Throwable e) {
            if (mapperConfig.failOnAsm()) {
                return ErrorHelper.rethrow(e);
            } else {
                mapper = new MapperImpl<T, S>(fields, new FieldMapper[0], instantiator);
            }
        }
    } else {
        mapper = new MapperImpl<T, S>(fields, new FieldMapper[0], instantiator);
    }
    return new ContextualMapper<T, S>(mapper, mappingContextFactoryBuilder.newFactory());
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ArrayList(java.util.ArrayList) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) MappingContextFactoryBuilder(org.simpleflatmapper.map.context.MappingContextFactoryBuilder) MappingContext(org.simpleflatmapper.map.MappingContext) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) FieldMapper(org.simpleflatmapper.map.FieldMapper)

Aggregations

FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)20 Test (org.junit.Test)8 SampleFieldKey (org.simpleflatmapper.test.map.SampleFieldKey)6 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)5 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)5 FieldMapper (org.simpleflatmapper.map.FieldMapper)4 DbObject (org.simpleflatmapper.test.beans.DbObject)4 Type (java.lang.reflect.Type)3 ArrayList (java.util.ArrayList)3 MappingContext (org.simpleflatmapper.map.MappingContext)3 MappingContextFactoryBuilder (org.simpleflatmapper.map.context.MappingContextFactoryBuilder)3 MapperFieldMapper (org.simpleflatmapper.map.fieldmapper.MapperFieldMapper)3 AbstractMapperFactory (org.simpleflatmapper.map.mapper.AbstractMapperFactory)3 Getter (org.simpleflatmapper.reflect.Getter)3 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)2 DatastaxColumnKey (org.simpleflatmapper.datastax.DatastaxColumnKey)2 FieldKey (org.simpleflatmapper.map.FieldKey)2 IgnoreMapperBuilderErrorHandler (org.simpleflatmapper.map.IgnoreMapperBuilderErrorHandler)2 MapperBuilderErrorHandler (org.simpleflatmapper.map.MapperBuilderErrorHandler)2 MapperBuildingException (org.simpleflatmapper.map.MapperBuildingException)2