Search in sources :

Example 1 with Context

use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.

the class AbstractBatchInsertQueryExecutor method bindTo.

private void bindTo(PreparedStatement preparedStatement, Collection<T> values) throws Exception {
    int i = 0;
    Context context = contextFactory.newContext();
    for (T value : values) {
        for (MultiIndexFieldMapper<T> multiIndexFieldMapper : multiIndexFieldMappers) {
            multiIndexFieldMapper.map(preparedStatement, value, i, context);
            i++;
        }
    }
}
Also used : Context(org.simpleflatmapper.converter.Context)

Example 2 with Context

use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.

the class ConvertDelegateSetterTest method testSet.

@Test
public void testSet() throws Exception {
    ConvertDelegateSetter<DbObject, UUID, String> setter = new ConvertDelegateSetter<DbObject, UUID, String>(new Setter<DbObject, String>() {

        @Override
        public void set(DbObject target, String value) throws Exception {
            target.setName(value);
        }
    }, new ContextualConverter<UUID, String>() {

        @Override
        public String convert(UUID in, Context context) throws Exception {
            return in.toString();
        }
    });
    DbObject o = new DbObject();
    UUID uuid = UUID.randomUUID();
    setter.set(o, uuid);
    assertEquals(uuid.toString(), o.getName());
}
Also used : Context(org.simpleflatmapper.converter.Context) ConvertDelegateSetter(org.simpleflatmapper.reflect.setter.ConvertDelegateSetter) DbObject(org.simpleflatmapper.test.beans.DbObject) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with Context

use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.

the class AbstractMapperFactoryTest method testErrorHandlers.

@Test
public void testErrorHandlers() {
    MapperFactory mapperFactory = new MapperFactory();
    FieldMapperErrorHandler<SampleFieldKey> fieldMapperErrorHandler = new FieldMapperErrorHandler<SampleFieldKey>() {

        @Override
        public void errorMappingField(SampleFieldKey key, Object source, Object target, Exception error, Context mappingContext) throws MappingException {
        }
    };
    MapperBuilderErrorHandler mapperBuilderErrorHandler = new MapperBuilderErrorHandler() {

        @Override
        public void accessorNotFound(String msg) {
        }

        @Override
        public void propertyNotFound(Type target, String property) {
        }

        @Override
        public void customFieldError(FieldKey<?> key, String message) {
        }
    };
    ConsumerErrorHandler consumerErrorHandler = new ConsumerErrorHandler() {

        @Override
        public void handlerError(Throwable error, Object target) {
        }
    };
    mapperFactory.fieldMapperErrorHandler(fieldMapperErrorHandler);
    mapperFactory.mapperBuilderErrorHandler(mapperBuilderErrorHandler);
    mapperFactory.consumerErrorHandler(consumerErrorHandler);
    MapperConfig<SampleFieldKey, Object[]> mapperConfig = mapperFactory.mapperConfig();
    assertEquals(fieldMapperErrorHandler, mapperConfig.fieldMapperErrorHandler());
    assertEquals(mapperBuilderErrorHandler, mapperConfig.mapperBuilderErrorHandler());
    assertEquals(consumerErrorHandler, mapperConfig.consumerErrorHandler());
    Assert.assertEquals(consumerErrorHandler, mapperFactory.consumerErrorHandler());
}
Also used : MappingContext(org.simpleflatmapper.map.MappingContext) Context(org.simpleflatmapper.converter.Context) RethrowMapperBuilderErrorHandler(org.simpleflatmapper.map.error.RethrowMapperBuilderErrorHandler) IgnoreMapperBuilderErrorHandler(org.simpleflatmapper.map.IgnoreMapperBuilderErrorHandler) MapperBuilderErrorHandler(org.simpleflatmapper.map.MapperBuilderErrorHandler) MappingException(org.simpleflatmapper.map.MappingException) Type(java.lang.reflect.Type) RethrowConsumerErrorHandler(org.simpleflatmapper.map.error.RethrowConsumerErrorHandler) ConsumerErrorHandler(org.simpleflatmapper.map.ConsumerErrorHandler) AbstractMapperFactory(org.simpleflatmapper.map.mapper.AbstractMapperFactory) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) FieldMapperErrorHandler(org.simpleflatmapper.map.FieldMapperErrorHandler) FieldKey(org.simpleflatmapper.map.FieldKey) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) Test(org.junit.Test)

Example 4 with Context

use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceMapperBuilderTest method testFieldMapperErrorHandler.

@Test
public void testFieldMapperErrorHandler() throws Exception {
    ConstantSourceMapperBuilder<Object, MyObjectWithInner, SampleFieldKey> constantSourceMapperBuilder = ConstantSourceMapperBuilder.<Object, MyObjectWithInner, SampleFieldKey>newConstantSourceMapperBuilder(mapperSource, classMeta, MapperConfig.config(new IdentityFieldMapperColumnDefinitionProvider<SampleFieldKey>()).fieldMapperErrorHandler(new FieldMapperErrorHandler<SampleFieldKey>() {

        @Override
        public void errorMappingField(SampleFieldKey key, Object source, Object target, Exception error, Context mappingContext) throws MappingException {
        }
    }), new MappingContextFactoryBuilder<Object, SampleFieldKey>(null, true), SampleFieldKey.KEY_FACTORY);
    constantSourceMapperBuilder.addMapping(new SampleFieldKey("prop_date", 16), FieldMapperColumnDefinition.<SampleFieldKey>identity());
    SourceMapper<Object, MyObjectWithInner> mapper = constantSourceMapperBuilder.mapper();
    MyObjectWithInner o = mapper.map(null, null);
    System.out.println("mapper = " + mapper);
    assertNull(o.prop.date);
}
Also used : Context(org.simpleflatmapper.converter.Context) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) FieldMapperErrorHandler(org.simpleflatmapper.map.FieldMapperErrorHandler) MappingException(org.simpleflatmapper.map.MappingException) Test(org.junit.Test)

Example 5 with Context

use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.

the class KeyTupleQueryPreparer method bindTo.

public void bindTo(Collection<T> values, PreparedStatement ps, int offset) {
    int index = offset;
    Context context = contextFactory.newContext();
    for (T value : values) {
        for (MultiIndexFieldMapper<T> mapper : multiIndexFieldMappers) {
            try {
                mapper.map(ps, value, index, context);
            } catch (Exception e) {
                ErrorHelper.rethrow(e);
            }
            index++;
        }
    }
}
Also used : Context(org.simpleflatmapper.converter.Context) SQLException(java.sql.SQLException)

Aggregations

Context (org.simpleflatmapper.converter.Context)13 Test (org.junit.Test)9 DbObject (org.simpleflatmapper.test.beans.DbObject)6 MappingException (org.simpleflatmapper.map.MappingException)4 ContextualConverter (org.simpleflatmapper.converter.ContextualConverter)3 Type (java.lang.reflect.Type)2 BigInteger (java.math.BigInteger)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ParseException (java.text.ParseException)2 FieldMapperErrorHandler (org.simpleflatmapper.map.FieldMapperErrorHandler)2 SampleFieldKey (org.simpleflatmapper.test.map.SampleFieldKey)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 UUID (java.util.UUID)1