Search in sources :

Example 11 with PropertyMeta

use of org.simpleflatmapper.reflect.meta.PropertyMeta 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 12 with PropertyMeta

use of org.simpleflatmapper.reflect.meta.PropertyMeta in project SimpleFlatMapper by arnaudroger.

the class CsvColumnDefinitionTest method testComposition.

@Test
public void testComposition() {
    TimeZone tz = TimeZone.getTimeZone("Europe/Brussels");
    CellValueReaderFactory cellValueReaderFactory = new CellValueReaderFactory() {

        @Override
        public <P> CellValueReader<P> getReader(Type propertyType, int index, CsvColumnDefinition columnDefinition, ParsingContextFactoryBuilder contextFactoryBuilder) {
            return null;
        }

        @Override
        public String toString() {
            return "CellValueReaderFactory";
        }
    };
    final Predicate<PropertyMeta<?, ?>> appliesTo = ConstantPredicate.falsePredicate();
    CsvColumnDefinition compose = CsvColumnDefinition.identity().addDateFormat("yyyyMM").addRename("blop").addCustomReader(new CellValueReader<Integer>() {

        @Override
        public Integer read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3;
        }

        @Override
        public String toString() {
            return "CellValueReader";
        }
    }).addCustomCellValueReaderFactory(cellValueReaderFactory).addTimeZone(tz).addKey(appliesTo);
    assertEquals("blop", compose.rename(new CsvColumnKey("bar", -1)).getName());
    assertEquals("blop", CsvColumnDefinition.renameDefinition("blop").rename(new CsvColumnKey("bar", -1)).getName());
    assertArrayEquals(new String[] { "yyyyMM" }, compose.dateFormats());
    assertEquals(new Integer(3), compose.getCustomReader().read(null, 0, 0, null));
    assertEquals(cellValueReaderFactory, compose.getCustomCellValueReaderFactory());
    assertEquals(tz, compose.getTimeZone());
    assertEquals(tz, CsvColumnDefinition.timeZoneDefinition(tz).getTimeZone());
    assertTrue(compose.hasCustomSourceFrom(Object.class));
    assertFalse(compose.ignore());
    assertEquals(Integer.class, compose.getCustomSourceReturnTypeFrom(Object.class));
    assertTrue(CsvColumnDefinition.identity().addIgnore().ignore());
    assertTrue(compose.isKey());
    assertTrue(CsvColumnDefinition.key(appliesTo).isKey());
    assertSame(appliesTo, compose.keyAppliesTo());
    final String toString = compose.addIgnore().toString();
    System.out.println("toString = " + toString);
}
Also used : ParsingContext(org.simpleflatmapper.csv.ParsingContext) ParsingContextFactoryBuilder(org.simpleflatmapper.csv.ParsingContextFactoryBuilder) CellValueReader(org.simpleflatmapper.csv.CellValueReader) TimeZone(java.util.TimeZone) Type(java.lang.reflect.Type) CellValueReaderFactory(org.simpleflatmapper.csv.CellValueReaderFactory) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) CsvColumnDefinition(org.simpleflatmapper.csv.CsvColumnDefinition) Test(org.junit.Test)

Example 13 with PropertyMeta

use of org.simpleflatmapper.reflect.meta.PropertyMeta in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceFieldMapperFactoryImplTest method createPropertyMapping.

public static <T, P> PropertyMapping<T, P, SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>> createPropertyMapping(Class<T> target, String property) {
    ClassMeta<T> classMeta = REFLECTION_SERVICE.getClassMeta(target);
    PropertyMeta<T, P> propertyMeta = classMeta.newPropertyFinder(ConstantPredicate.<PropertyMeta<?, ?>>truePredicate()).findProperty(DefaultPropertyNameMatcher.of(property), new Object[0]);
    PropertyMapping<T, P, SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>> pm = new PropertyMapping<T, P, SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>>(propertyMeta, new SampleFieldKey(property, 0), FieldMapperColumnDefinition.<SampleFieldKey>identity());
    return pm;
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey)

Example 14 with PropertyMeta

use of org.simpleflatmapper.reflect.meta.PropertyMeta in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceMapperBuilderTest method testIssue495.

@Test
public void testIssue495() throws Exception {
    TypeReference<Tuple2<TableA, List<Tuple3<TableB, List<TableC>, List<Tuple2<TableD, List<TableE>>>>>>> typeReference = new TypeReference<Tuple2<TableA, List<Tuple3<TableB, List<TableC>, List<Tuple2<TableD, List<TableE>>>>>>>() {
    };
    ClassMeta<Tuple2<TableA, List<Tuple3<TableB, List<TableC>, List<Tuple2<TableD, List<TableE>>>>>>> classMeta = this.classMeta.getReflectionService().getClassMeta(typeReference.getType());
    Predicate<PropertyMeta<?, ?>> propertyFilter = new Predicate<PropertyMeta<?, ?>>() {

        @Override
        public boolean test(PropertyMeta<?, ?> propertyMeta) {
            return true;
        }
    };
    PropertyFinder<Tuple2<TableA, List<Tuple3<TableB, List<TableC>, List<Tuple2<TableD, List<TableE>>>>>>> propertyFinder = classMeta.newPropertyFinder(propertyFilter);
    PropertyMeta<Tuple2<TableA, List<Tuple3<TableB, List<TableC>, List<Tuple2<TableD, List<TableE>>>>>>, Object> p1 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("variableName", 0, false, false), new Object[0]);
    PropertyMeta<Tuple2<TableA, List<Tuple3<TableB, List<TableC>, List<Tuple2<TableD, List<TableE>>>>>>, Object> p2 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("variableName", 0, false, false), new Object[0]);
    System.out.println("p1.getPath() = " + p1.getPath());
    System.out.println("p2.getPath() = " + p2.getPath());
}
Also used : DefaultPropertyNameMatcher(org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher) Predicate(org.simpleflatmapper.util.Predicate) Tuple2(org.simpleflatmapper.tuple.Tuple2) Tuple3(org.simpleflatmapper.tuple.Tuple3) List(java.util.List) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) TypeReference(org.simpleflatmapper.util.TypeReference) Test(org.junit.Test)

Example 15 with PropertyMeta

use of org.simpleflatmapper.reflect.meta.PropertyMeta in project SimpleFlatMapper by arnaudroger.

the class Issue451 method testPropertyFinderAndAppendSetter.

@Test
public void testPropertyFinderAndAppendSetter() {
    ClassMeta<List<Foo>> classMeta = ReflectionService.newInstance().getClassMeta(new TypeReference<List<Foo>>() {
    }.getType());
    PropertyFinder<List<Foo>> finder = classMeta.newPropertyFinder(new Predicate<PropertyMeta<?, ?>>() {

        @Override
        public boolean test(PropertyMeta<?, ?> propertyMeta) {
            return true;
        }
    });
    SubPropertyMeta f = (SubPropertyMeta) finder.findProperty(DefaultPropertyNameMatcher.of("b_f"), new Object[0]);
    SubPropertyMeta n = (SubPropertyMeta) finder.findProperty(DefaultPropertyNameMatcher.of("b_n"), new Object[0]);
    assertEquals(AppendCollectionSetter.class, n.getOwnerProperty().getSetter().getClass());
}
Also used : SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) List(java.util.List) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) TypeReference(org.simpleflatmapper.util.TypeReference) Test(org.junit.Test)

Aggregations

PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)19 Test (org.junit.Test)8 SubPropertyMeta (org.simpleflatmapper.reflect.meta.SubPropertyMeta)7 Predicate (org.simpleflatmapper.util.Predicate)7 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)5 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)5 SelfPropertyMeta (org.simpleflatmapper.reflect.meta.SelfPropertyMeta)5 List (java.util.List)4 ArrayClassMeta (org.simpleflatmapper.reflect.meta.ArrayClassMeta)4 ConstructorPropertyMeta (org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta)4 DefaultPropertyNameMatcher (org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher)4 TypeReference (org.simpleflatmapper.util.TypeReference)3 Type (java.lang.reflect.Type)2 ResultSet (java.sql.ResultSet)2 HashMap (java.util.HashMap)2 CellValueReaderFactory (org.simpleflatmapper.csv.CellValueReaderFactory)2 CellSetter (org.simpleflatmapper.csv.mapper.CellSetter)2 DelayedCellSetterFactory (org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory)2 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)2 FieldMapper (org.simpleflatmapper.map.FieldMapper)2