Search in sources :

Example 1 with PropertyMapping

use of org.simpleflatmapper.map.mapper.PropertyMapping in project SimpleFlatMapper by arnaudroger.

the class ConstantTargetFieldMapperFactoryImplTest method testGetterNotFound.

@Test
public void testGetterNotFound() {
    ConstantTargetFieldMapperFactory<Appendable, SampleFieldKey> factory = ConstantTargetFieldMapperFactoryImpl.<Appendable, SampleFieldKey>newInstance(setterFactory, Appendable.class);
    try {
        PropertyMeta<Object, Object> pm = mock(PropertyMeta.class);
        PropertyMapping<Object, Object, SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>> propertyMapping = new PropertyMapping<Object, Object, SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>>(pm, new SampleFieldKey("hh", 0), FieldMapperColumnDefinition.<SampleFieldKey>identity());
        factory.newFieldMapper(propertyMapping, null, RethrowMapperBuilderErrorHandler.INSTANCE);
        fail();
    } catch (MapperBuildingException e) {
    }
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) MapperBuildingException(org.simpleflatmapper.map.MapperBuildingException) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) DbObject(org.simpleflatmapper.test.beans.DbObject) ConstantSourceFieldMapperFactoryImplTest(org.simpleflatmapper.test.map.fieldmapper.ConstantSourceFieldMapperFactoryImplTest) Test(org.junit.Test)

Example 2 with PropertyMapping

use of org.simpleflatmapper.map.mapper.PropertyMapping in project SimpleFlatMapper by arnaudroger.

the class PreparedStatementFieldMapperFactoryTest method newPropertyMapping.

@SuppressWarnings("unchecked")
private <T, P> PropertyMapping<T, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> newPropertyMapping(Getter getter, Class<P> clazz, int sqltype, Object... properties) {
    PropertyMeta<T, P> propertyMeta = mock(PropertyMeta.class);
    when(propertyMeta.getGetter()).thenReturn(getter);
    when(propertyMeta.getPropertyType()).thenReturn(clazz);
    return new PropertyMapping<T, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(propertyMeta, new JdbcColumnKey("col", index++, sqltype), FieldMapperColumnDefinition.<JdbcColumnKey>of(properties));
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping)

Example 3 with PropertyMapping

use of org.simpleflatmapper.map.mapper.PropertyMapping in project SimpleFlatMapper by arnaudroger.

the class ResultSetFieldMapperFactoryTest method testPrimitiveField.

@Test
public void testPrimitiveField() {
    ClassMeta<DbObject> classMeta = ReflectionService.newInstance(false).getClassMeta(DbObject.class);
    PropertyMeta<DbObject, Long> id = classMeta.newPropertyFinder(ConstantPredicate.<PropertyMeta<?, ?>>truePredicate()).<Long>findProperty(new DefaultPropertyNameMatcher("id", 0, false, false), new Object[0]);
    FieldMapperColumnDefinition<JdbcColumnKey> identity = FieldMapperColumnDefinition.identity();
    PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> propertyMapping = new PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(id, new JdbcColumnKey("id", 1), identity);
    FieldMapper<ResultSet, DbObject> fieldMapper = factory.newFieldMapper(propertyMapping, null, RethrowMapperBuilderErrorHandler.INSTANCE);
    assertTrue(fieldMapper instanceof LongFieldMapper);
    PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> propertyMapping1 = new PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(id, new JdbcColumnKey("id", 0), identity);
    fieldMapper = factory.newFieldMapper(propertyMapping1, null, RethrowMapperBuilderErrorHandler.INSTANCE);
    assertTrue(fieldMapper instanceof LongFieldMapper);
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) LongFieldMapper(org.simpleflatmapper.map.fieldmapper.LongFieldMapper) DbObject(org.simpleflatmapper.test.beans.DbObject) DefaultPropertyNameMatcher(org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher) JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) ResultSet(java.sql.ResultSet) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) Test(org.junit.Test)

Example 4 with PropertyMapping

use of org.simpleflatmapper.map.mapper.PropertyMapping in project SimpleFlatMapper by arnaudroger.

the class SettableDataSetterFactory method getSetter.

@SuppressWarnings("unchecked")
@Override
public <P> Setter<SettableByIndexData, P> getSetter(PropertyMapping<?, ?, DatastaxColumnKey, ? extends ColumnDefinition<DatastaxColumnKey, ?>> arg) {
    Setter<SettableByIndexData, P> setter = null;
    Type propertyType = arg.getPropertyMeta().getPropertyType();
    final DataType dataType = arg.getColumnKey().getDataType();
    Type type = dataType != null ? DataTypeHelper.asJavaClass(dataType) : null;
    if (type == null) {
        type = propertyType;
    }
    if (TypeHelper.isEnum(propertyType)) {
        if (TypeHelper.areEquals(type, String.class)) {
            return (Setter<SettableByIndexData, P>) new StringEnumSettableDataSetter(arg.getColumnKey().getIndex());
        } else {
            return (Setter<SettableByIndexData, P>) new OrdinalEnumSettableDataSetter(arg.getColumnKey().getIndex());
        }
    }
    SetterFactory<SettableByIndexData, PropertyMapping<?, ?, DatastaxColumnKey, ? extends ColumnDefinition<DatastaxColumnKey, ?>>> setterFactory = this.factoryPerClass.get(TypeHelper.toClass(type));
    if (setterFactory != null) {
        setter = setterFactory.getSetter(arg);
        if (!TypeHelper.areEquals(TypeHelper.toBoxedClass(type), TypeHelper.toBoxedClass(propertyType))) {
            Converter<?, ?> converter = getConverter(propertyType, TypeHelper.toClass(type), dataType, arg.getColumnDefinition());
            if (converter != null) {
                setter = (Setter<SettableByIndexData, P>) new ConvertDelegateSetter<SettableByIndexData, Object, P>(setter, (Converter<Object, P>) converter);
            } else {
                setter = null;
            }
        }
    }
    if (setter == null && dataType != null) {
        if (dataType instanceof UserType) {
            if (propertyType.equals(UDTValue.class)) {
                setter = (Setter<SettableByIndexData, P>) new UDTValueSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                setter = (Setter<SettableByIndexData, P>) UDTObjectSettableDataSetter.newInstance(propertyType, (UserType) dataType, arg.getColumnKey().getIndex(), mapperConfig, reflectionService);
            }
        } else if (TypeHelper.isAssignable(List.class, type) && TypeHelper.isAssignable(List.class, propertyType)) {
            DataType dataTypeElt = dataType.getTypeArguments().get(0);
            Class<?> dEltType = DataTypeHelper.asJavaClass(dataTypeElt);
            Type lEltType = TypeHelper.getComponentTypeOfListOrArray(propertyType);
            if (TypeHelper.areEquals(lEltType, dEltType)) {
                setter = new ListSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                Converter<?, ?> converter = getConverter(lEltType, dEltType, dataTypeElt, arg.getColumnDefinition());
                if (converter != null) {
                    setter = new ListWithConverterSettableDataSetter(arg.getColumnKey().getIndex(), converter);
                }
            }
        } else if (TypeHelper.isAssignable(Set.class, type) && TypeHelper.isAssignable(Set.class, propertyType)) {
            DataType dataTypeElt = dataType.getTypeArguments().get(0);
            Class<?> dEltType = DataTypeHelper.asJavaClass(dataTypeElt);
            Type lEltType = TypeHelper.getComponentTypeOfListOrArray(propertyType);
            if (TypeHelper.areEquals(lEltType, dEltType)) {
                setter = new SetSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                Converter<?, ?> converter = getConverter(lEltType, dEltType, dataTypeElt, arg.getColumnDefinition());
                if (converter != null) {
                    setter = new SetWithConverterSettableDataSetter(arg.getColumnKey().getIndex(), converter);
                }
            }
        } else if (TypeHelper.isAssignable(Map.class, type) && TypeHelper.isAssignable(Map.class, propertyType)) {
            DataType dtKeyType = dataType.getTypeArguments().get(0);
            DataType dtValueType = dataType.getTypeArguments().get(1);
            TypeHelper.MapEntryTypes keyValueTypeOfMap = TypeHelper.getKeyValueTypeOfMap(propertyType);
            if (areSame(dtKeyType, keyValueTypeOfMap.getKeyType()) && areSame(dtValueType, keyValueTypeOfMap.getValueType())) {
                setter = new MapSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                setter = new MapWithConverterSettableDataSetter(arg.getColumnKey().getIndex(), getConverter(keyValueTypeOfMap.getKeyType(), DataTypeHelper.asJavaClass(dtKeyType), dtKeyType, arg.getColumnDefinition()), getConverter(keyValueTypeOfMap.getValueType(), DataTypeHelper.asJavaClass(dtValueType), dtValueType, arg.getColumnDefinition()));
            }
        }
    }
    return setter;
}
Also used : OrdinalEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.OrdinalEnumSettableDataSetter) SetSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetSettableDataSetter) ConvertDelegateSetter(org.simpleflatmapper.reflect.setter.ConvertDelegateSetter) ListWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListWithConverterSettableDataSetter) StringEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.StringEnumSettableDataSetter) MapWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapWithConverterSettableDataSetter) Converter(org.simpleflatmapper.converter.Converter) ListSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListSettableDataSetter) UDTValueSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UDTValueSettableDataSetter) MapSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapSettableDataSetter) FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ColumnDefinition(org.simpleflatmapper.map.mapper.ColumnDefinition) Type(java.lang.reflect.Type) ListSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListSettableDataSetter) DoubleSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.DoubleSettableDataSetter) BigDecimalSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.BigDecimalSettableDataSetter) TimeSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.TimeSettableDataSetter) BigIntegerSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.BigIntegerSettableDataSetter) SetWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetWithConverterSettableDataSetter) IntSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.IntSettableDataSetter) UUIDSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UUIDSettableDataSetter) TupleValueSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.TupleValueSettableDataSetter) SetSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetSettableDataSetter) InetAddressSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.InetAddressSettableDataSetter) ListWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListWithConverterSettableDataSetter) ShortSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ShortSettableDataSetter) UDTObjectSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UDTObjectSettableDataSetter) DateSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.DateSettableDataSetter) TimestampSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.TimestampSettableDataSetter) MapWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapWithConverterSettableDataSetter) FloatSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.FloatSettableDataSetter) StringEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.StringEnumSettableDataSetter) LongSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.LongSettableDataSetter) OrdinalEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.OrdinalEnumSettableDataSetter) Setter(org.simpleflatmapper.reflect.Setter) ByteSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ByteSettableDataSetter) MapSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapSettableDataSetter) ConvertDelegateSetter(org.simpleflatmapper.reflect.setter.ConvertDelegateSetter) StringSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.StringSettableDataSetter) UDTValueSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UDTValueSettableDataSetter) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) SetWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetWithConverterSettableDataSetter)

Example 5 with PropertyMapping

use of org.simpleflatmapper.map.mapper.PropertyMapping in project SimpleFlatMapper by arnaudroger.

the class SettableDataSetterFactoryTest method newPM.

@SuppressWarnings("unchecked")
private <T, P> PropertyMapping<?, ?, DatastaxColumnKey, ? extends ColumnDefinition<DatastaxColumnKey, ?>> newPM(Type clazz, DataType datatype, Object... properties) {
    PropertyMeta<T, P> propertyMeta = mock(PropertyMeta.class);
    when(propertyMeta.getPropertyType()).thenReturn(clazz);
    return new PropertyMapping<T, P, DatastaxColumnKey, FieldMapperColumnDefinition<DatastaxColumnKey>>(propertyMeta, new DatastaxColumnKey("col", index++, datatype), FieldMapperColumnDefinition.<DatastaxColumnKey>of(properties));
}
Also used : PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) DatastaxColumnKey(org.simpleflatmapper.datastax.DatastaxColumnKey)

Aggregations

PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)11 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)6 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)5 Setter (org.simpleflatmapper.reflect.Setter)4 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 CellSetter (org.simpleflatmapper.csv.mapper.CellSetter)2 DelayedCellSetterFactory (org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory)2 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)2 SetterFactoryProperty (org.simpleflatmapper.map.property.SetterFactoryProperty)2 Getter (org.simpleflatmapper.reflect.Getter)2 SetterFactory (org.simpleflatmapper.reflect.SetterFactory)2 ConstructorPropertyMeta (org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta)2 ObjectClassMeta (org.simpleflatmapper.reflect.meta.ObjectClassMeta)2 SelfPropertyMeta (org.simpleflatmapper.reflect.meta.SelfPropertyMeta)2 SubPropertyMeta (org.simpleflatmapper.reflect.meta.SubPropertyMeta)2 IntGetter (org.simpleflatmapper.reflect.primitive.IntGetter)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2 Type (java.lang.reflect.Type)1 PreparedStatement (java.sql.PreparedStatement)1