Search in sources :

Example 1 with ContextualGetter

use of org.simpleflatmapper.map.getter.ContextualGetter in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceFieldMapperFactoryImpl method getGetterInstantiator.

private <T, P> ContextualGetter<? super S, ? extends P> getGetterInstantiator(ClassMeta<P> classMeta, InstantiatorDefinition id, K key, ColumnDefinition<K, ?> columnDefinition, Collection<Type> types, MappingContextFactoryBuilder<?, ? extends FieldKey<?>> mappingContextFactoryBuilder) {
    Instantiator<? super T, ? extends P> instantiator = classMeta.getReflectionService().getInstantiatorFactory().getOneArgIdentityInstantiator(id, classMeta.getReflectionService().builderIgnoresNullValues());
    final Type paramType = id.getParameters()[0].getGenericType();
    ContextualGetter<? super S, ? extends T> subGetter = getterFactory.newGetter(paramType, key, mappingContextFactoryBuilder, columnDefinition);
    if (subGetter == null) {
        if (types.contains(paramType)) {
            // loop circuit cutter
            return null;
        }
        types.add(paramType);
        // converter?
        Type sourceType = key.getType(paramType);
        ContextualConverter converter = converterService.findConverter(sourceType, paramType, mappingContextFactoryBuilder, columnDefinition.properties());
        if (converter != null) {
            ContextualGetter sourceTypeGetter = getterFactory.newGetter(sourceType, key, mappingContextFactoryBuilder, columnDefinition);
            subGetter = new FieldMapperGetterWithConverter(converter, sourceTypeGetter);
        } else {
            subGetter = lookForInstantiatorGetter(classMeta.getReflectionService().<T>getClassMeta(paramType), key, columnDefinition, types, mappingContextFactoryBuilder);
        }
    }
    if (subGetter != null) {
        return new InstantiatorContextualGetter<T, S, P>(instantiator, subGetter);
    } else
        return null;
}
Also used : Type(java.lang.reflect.Type) CharacterContextualGetter(org.simpleflatmapper.map.getter.CharacterContextualGetter) BoxedFloatContextualGetter(org.simpleflatmapper.map.getter.BoxedFloatContextualGetter) BoxedByteContextualGetter(org.simpleflatmapper.map.getter.BoxedByteContextualGetter) BoxedBooleanContextualGetter(org.simpleflatmapper.map.getter.BoxedBooleanContextualGetter) ContextualGetter(org.simpleflatmapper.map.getter.ContextualGetter) FloatContextualGetter(org.simpleflatmapper.map.getter.FloatContextualGetter) BoxedIntContextualGetter(org.simpleflatmapper.map.getter.BoxedIntContextualGetter) BoxedDoubleContextualGetter(org.simpleflatmapper.map.getter.BoxedDoubleContextualGetter) InstantiatorContextualGetter(org.simpleflatmapper.map.getter.InstantiatorContextualGetter) IntContextualGetter(org.simpleflatmapper.map.getter.IntContextualGetter) BooleanContextualGetter(org.simpleflatmapper.map.getter.BooleanContextualGetter) ShortContextualGetter(org.simpleflatmapper.map.getter.ShortContextualGetter) DoubleContextualGetter(org.simpleflatmapper.map.getter.DoubleContextualGetter) LongContextualGetter(org.simpleflatmapper.map.getter.LongContextualGetter) BoxedShortContextualGetter(org.simpleflatmapper.map.getter.BoxedShortContextualGetter) ByteContextualGetter(org.simpleflatmapper.map.getter.ByteContextualGetter) BoxedLongContextualGetter(org.simpleflatmapper.map.getter.BoxedLongContextualGetter) BoxedCharacterContextualGetter(org.simpleflatmapper.map.getter.BoxedCharacterContextualGetter) InstantiatorContextualGetter(org.simpleflatmapper.map.getter.InstantiatorContextualGetter) ContextualConverter(org.simpleflatmapper.converter.ContextualConverter)

Example 2 with ContextualGetter

use of org.simpleflatmapper.map.getter.ContextualGetter in project SimpleFlatMapper by arnaudroger.

the class DefaultConstantSourceMapperBuilder method getConstructorFieldMappersAndInstantiator.

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

Example 3 with ContextualGetter

use of org.simpleflatmapper.map.getter.ContextualGetter in project SimpleFlatMapper by arnaudroger.

the class JdbcTemplateMapperFactoryTest method testIssue618OverrideEnum.

@Test
public void testIssue618OverrideEnum() throws SQLException {
    JdbcTemplateMapperFactory config = JdbcTemplateMapperFactory.newInstance().addGetterForType(new Predicate<Type>() {

        @Override
        public boolean test(Type type) {
            return TypeHelper.isAssignable(LookupValue.class, type);
        }
    }, new ContextualGetterFactory<ResultSet, JdbcColumnKey>() {

        @Override
        public <P> ContextualGetter<ResultSet, P> newGetter(Type target, JdbcColumnKey key, MappingContextFactoryBuilder<?, ? extends FieldKey<?>> mappingContextFactoryBuilder, Object... properties) {
            Class enumClass = TypeHelper.toClass(target);
            final Enum[] values = EnumHelper.getValues(enumClass);
            final int index = key.getIndex();
            return new ContextualGetter<ResultSet, P>() {

                @Override
                public P get(ResultSet resultSet, Context context) throws Exception {
                    int i = resultSet.getInt(index);
                    for (Enum e : values) {
                        if (((LookupValue) e).lookupValue() == i) {
                            return (P) e;
                        }
                    }
                    return null;
                }
            };
        }
    });
    ResultSetExtractorImpl<Issue618> rse = JdbcTemplateMapperFactory.newInstance(config).newResultSetExtractor(Issue618.class);
    Connection conn = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);
    Statement st = null;
    ResultSet rs = null;
    if (conn == null)
        return;
    try {
        st = conn.createStatement();
        rs = st.executeQuery("SELECT * FROM (VALUES (1, 1), (2, 2)) AS t (id, enum618) ");
        List<Issue618> issue618s = rse.extractData(rs);
        assertEquals(Arrays.asList(new Issue618(1, Enum618.A), new Issue618(2, Enum618.B)), issue618s);
    } finally {
        if (rs != null)
            rs.close();
        if (st != null)
            st.close();
        conn.close();
    }
}
Also used : Context(org.simpleflatmapper.converter.Context) Statement(java.sql.Statement) ContextualGetter(org.simpleflatmapper.map.getter.ContextualGetter) Connection(java.sql.Connection) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) JdbcTemplateMapperFactory(org.simpleflatmapper.jdbc.spring.JdbcTemplateMapperFactory) JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) Type(java.lang.reflect.Type) ResultSet(java.sql.ResultSet) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Aggregations

ContextualGetter (org.simpleflatmapper.map.getter.ContextualGetter)3 Type (java.lang.reflect.Type)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ParseException (java.text.ParseException)1 Test (org.junit.Test)1 Context (org.simpleflatmapper.converter.Context)1 ContextualConverter (org.simpleflatmapper.converter.ContextualConverter)1 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)1 JdbcTemplateMapperFactory (org.simpleflatmapper.jdbc.spring.JdbcTemplateMapperFactory)1 MapperBuildingException (org.simpleflatmapper.map.MapperBuildingException)1 MappingContext (org.simpleflatmapper.map.MappingContext)1 BooleanContextualGetter (org.simpleflatmapper.map.getter.BooleanContextualGetter)1 BoxedBooleanContextualGetter (org.simpleflatmapper.map.getter.BoxedBooleanContextualGetter)1 BoxedByteContextualGetter (org.simpleflatmapper.map.getter.BoxedByteContextualGetter)1 BoxedCharacterContextualGetter (org.simpleflatmapper.map.getter.BoxedCharacterContextualGetter)1 BoxedDoubleContextualGetter (org.simpleflatmapper.map.getter.BoxedDoubleContextualGetter)1 BoxedFloatContextualGetter (org.simpleflatmapper.map.getter.BoxedFloatContextualGetter)1