Search in sources :

Example 1 with IndexedSetter

use of org.simpleflatmapper.reflect.IndexedSetter 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 2 with IndexedSetter

use of org.simpleflatmapper.reflect.IndexedSetter in project SimpleFlatMapper by arnaudroger.

the class CrudTest method testDbObjectCrudWithCustomSetter.

@Test
public void testDbObjectCrudWithCustomSetter() throws Exception {
    JdbcMapperFactory mapperFactory = JdbcMapperFactory.newInstance().addColumnProperty("name", new IndexedSetterProperty(new IndexedSetter<PreparedStatement, Object>() {

        @Override
        public void set(PreparedStatement target, Object value, int index) throws Exception {
            target.setString(index, "customname");
        }
    }));
    Connection connection = DbHelper.getDbConnection(targetDB);
    if (connection == null) {
        System.err.println("Db " + targetDB + " not available");
        return;
    }
    try {
        Crud<TestDbObject, Long> objectCrud = mapperFactory.<TestDbObject, Long>crud(TestDbObject.class, Long.class).to(connection);
        TestDbObject testDbObject = DbObject.newInstance(new TestDbObject());
        assertNotEquals("customname", testDbObject);
        objectCrud.create(connection, testDbObject);
        TestDbObject read = objectCrud.read(connection, testDbObject.getId());
        assertEquals("customname", read.getName());
        assertEquals(testDbObject.getEmail(), read.getEmail());
    } finally {
        connection.close();
    }
}
Also used : IndexedSetter(org.simpleflatmapper.reflect.IndexedSetter) JdbcMapperFactory(org.simpleflatmapper.jdbc.JdbcMapperFactory) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DbObject(org.simpleflatmapper.test.beans.DbObject) IndexedSetterProperty(org.simpleflatmapper.jdbc.property.IndexedSetterProperty) Test(org.junit.Test)

Example 3 with IndexedSetter

use of org.simpleflatmapper.reflect.IndexedSetter in project SimpleFlatMapper by arnaudroger.

the class PreparedStatementSetterFactory method getSetter.

@SuppressWarnings("unchecked")
@Override
public <P> Setter<PreparedStatement, P> getSetter(PropertyMapping<?, ?, JdbcColumnKey, ? extends ColumnDefinition<JdbcColumnKey, ?>> pm) {
    int columnIndex = pm.getColumnKey().getIndex();
    Type type = pm.getPropertyMeta().getPropertyType();
    Class<?> clazz = TypeHelper.toBoxedClass(type);
    if (Boolean.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new BooleanPreparedStatementSetter(columnIndex);
    } else if (Byte.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new BytePreparedStatementSetter(columnIndex);
    } else if (Character.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new CharacterPreparedStatementSetter(columnIndex);
    } else if (Short.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new ShortPreparedStatementSetter(columnIndex);
    } else if (Integer.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new IntegerPreparedStatementSetter(columnIndex);
    } else if (Long.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new LongPreparedStatementSetter(columnIndex);
    } else if (Double.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new DoublePreparedStatementSetter(columnIndex);
    } else if (Float.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new FloatPreparedStatementSetter(columnIndex);
    }
    IndexedSetter<PreparedStatement, P> setter = preparedStatementIndexedSetterFactory.getIndexedSetter(pm);
    if (setter != null) {
        return new PreparedStatementSetterImpl<P>(columnIndex, setter);
    } else
        return null;
}
Also used : BooleanPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.BooleanPreparedStatementSetter) FloatPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.FloatPreparedStatementSetter) Type(java.lang.reflect.Type) DoublePreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.DoublePreparedStatementSetter) BytePreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.BytePreparedStatementSetter) ShortPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.ShortPreparedStatementSetter) PreparedStatementSetterImpl(org.simpleflatmapper.jdbc.impl.setter.PreparedStatementSetterImpl) ShortPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.ShortPreparedStatementSetter) DoublePreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.DoublePreparedStatementSetter) IndexedSetter(org.simpleflatmapper.reflect.IndexedSetter) BooleanPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.BooleanPreparedStatementSetter) Setter(org.simpleflatmapper.reflect.Setter) CharacterPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.CharacterPreparedStatementSetter) BytePreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.BytePreparedStatementSetter) IntegerPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.IntegerPreparedStatementSetter) FloatPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.FloatPreparedStatementSetter) LongPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.LongPreparedStatementSetter) IntegerPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.IntegerPreparedStatementSetter) LongPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.LongPreparedStatementSetter) CharacterPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.CharacterPreparedStatementSetter)

Aggregations

IndexedSetter (org.simpleflatmapper.reflect.IndexedSetter)3 PreparedStatement (java.sql.PreparedStatement)2 IndexedSetterProperty (org.simpleflatmapper.jdbc.property.IndexedSetterProperty)2 Type (java.lang.reflect.Type)1 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.junit.Test)1 JdbcMapperFactory (org.simpleflatmapper.jdbc.JdbcMapperFactory)1 PreparedStatementIndexedSetterFactory (org.simpleflatmapper.jdbc.impl.PreparedStatementIndexedSetterFactory)1 BooleanPreparedStatementSetter (org.simpleflatmapper.jdbc.impl.setter.BooleanPreparedStatementSetter)1 BytePreparedStatementSetter (org.simpleflatmapper.jdbc.impl.setter.BytePreparedStatementSetter)1 CharacterPreparedStatementSetter (org.simpleflatmapper.jdbc.impl.setter.CharacterPreparedStatementSetter)1 DoublePreparedStatementSetter (org.simpleflatmapper.jdbc.impl.setter.DoublePreparedStatementSetter)1 FloatPreparedStatementSetter (org.simpleflatmapper.jdbc.impl.setter.FloatPreparedStatementSetter)1 IntegerPreparedStatementSetter (org.simpleflatmapper.jdbc.impl.setter.IntegerPreparedStatementSetter)1 LongPreparedStatementSetter (org.simpleflatmapper.jdbc.impl.setter.LongPreparedStatementSetter)1 PreparedStatementIndexSetterOnGetter (org.simpleflatmapper.jdbc.impl.setter.PreparedStatementIndexSetterOnGetter)1 PreparedStatementSetterImpl (org.simpleflatmapper.jdbc.impl.setter.PreparedStatementSetterImpl)1 ShortPreparedStatementSetter (org.simpleflatmapper.jdbc.impl.setter.ShortPreparedStatementSetter)1