Search in sources :

Example 6 with MappingContext

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

Example 7 with MappingContext

use of org.simpleflatmapper.map.MappingContext in project SimpleFlatMapper by arnaudroger.

the class ResultSetMapperFactoryTest method testMapToDbObjectStatic.

@Test
public void testMapToDbObjectStatic() throws Exception {
    DBI dbi = new DBI(DbHelper.getHsqlDataSource());
    dbi.registerMapper(new SfmResultSetMapperFactory(new UnaryFactory<Class<?>, Mapper<ResultSet, ?>>() {

        @Override
        public Mapper<ResultSet, ?> newInstance(Class<?> aClass) {
            return new Mapper<ResultSet, DbObject>() {

                @Override
                public DbObject map(ResultSet source) throws MappingException {
                    return map(source, null);
                }

                @Override
                public DbObject map(ResultSet source, MappingContext<? super ResultSet> context) throws MappingException {
                    DbObject dbObject = new DbObject();
                    try {
                        mapTo(source, dbObject, context);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return dbObject;
                }

                @Override
                public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> context) throws Exception {
                    target.setId(source.getInt("id"));
                    target.setCreationTime(source.getTimestamp("creation_time"));
                    target.setEmail(source.getString("email"));
                    target.setName(source.getString("name"));
                    String type_name = source.getString("type_name");
                    if (type_name != null) {
                        target.setTypeName(DbObject.Type.valueOf(type_name));
                    }
                    target.setTypeOrdinal(DbObject.Type.values()[source.getInt("type_ordinal")]);
                }
            };
        }
    }));
    Handle handle = dbi.open();
    try {
        DbObject dbObject = handle.createQuery(DbHelper.TEST_DB_OBJECT_QUERY).mapTo(DbObject.class).first();
        DbHelper.assertDbObjectMapping(dbObject);
        SfmBindTest.SfmBindExample attach = handle.attach(SfmBindTest.SfmBindExample.class);
        attach.insert(DbObject.newInstance());
        assertTrue(handle.createQuery("select * from TEST_DB_OBJECT").mapTo(DbObject.class).list().size() > 1);
    } finally {
        handle.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) SfmResultSetMapperFactory(org.simpleflatmapper.jdbi.SfmResultSetMapperFactory) DBI(org.skife.jdbi.v2.DBI) MappingException(org.simpleflatmapper.map.MappingException) Handle(org.skife.jdbi.v2.Handle) MappingContext(org.simpleflatmapper.map.MappingContext) Mapper(org.simpleflatmapper.map.Mapper) ResultSet(java.sql.ResultSet) UnaryFactory(org.simpleflatmapper.util.UnaryFactory) Test(org.junit.Test)

Example 8 with MappingContext

use of org.simpleflatmapper.map.MappingContext in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceMapperBuilder method getConstructorFieldMappersAndInstantiator.

@SuppressWarnings("unchecked")
private InstantiatorAndFieldMappers getConstructorFieldMappersAndInstantiator() throws MapperBuildingException {
    InstantiatorFactory instantiatorFactory = reflectionService.getInstantiatorFactory();
    try {
        ConstructorInjections constructorInjections = constructorInjections();
        Map<Parameter, BiFunction<? super S, ? super MappingContext<? super S>, ?>> injections = constructorInjections.parameterGetterMap;
        MapperBiInstantiatorFactory mapperBiInstantiatorFactory = new MapperBiInstantiatorFactory(instantiatorFactory);
        GetterFactory<? super S, K> getterFactory = fieldMapperAsGetterFactory();
        BiInstantiator<S, MappingContext<? super S>, T> instantiator = mapperBiInstantiatorFactory.<S, T, K, FieldMapperColumnDefinition<K>>getBiInstantiator(mapperSource.source(), target, propertyMappingsBuilder, injections, getterFactory, reflectionService.builderIgnoresNullValues());
        return new InstantiatorAndFieldMappers(constructorInjections.fieldMappers, instantiator);
    } catch (Exception e) {
        return ErrorHelper.rethrow(e);
    }
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) MapperBuildingException(org.simpleflatmapper.map.MapperBuildingException) InstantiatorFactory(org.simpleflatmapper.reflect.InstantiatorFactory) MappingContext(org.simpleflatmapper.map.MappingContext) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter)

Aggregations

MappingContext (org.simpleflatmapper.map.MappingContext)8 Test (org.junit.Test)4 FieldMapper (org.simpleflatmapper.map.FieldMapper)3 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)3 DbObject (org.simpleflatmapper.test.beans.DbObject)3 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Mapper (org.simpleflatmapper.map.Mapper)2 MappingException (org.simpleflatmapper.map.MappingException)2 MappingContextFactoryBuilder (org.simpleflatmapper.map.context.MappingContextFactoryBuilder)2 Parameter (org.simpleflatmapper.reflect.Parameter)2 BiFunction (org.simpleflatmapper.util.BiFunction)2 UnaryFactory (org.simpleflatmapper.util.UnaryFactory)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 Before (org.junit.Before)1 UncheckedConverter (org.simpleflatmapper.converter.UncheckedConverter)1 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)1 SfmResultSetMapperFactory (org.simpleflatmapper.jdbi.SfmResultSetMapperFactory)1 MapperBuildingException (org.simpleflatmapper.map.MapperBuildingException)1