Search in sources :

Example 1 with ContextualConverter

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

the class AbstractMultiFormatContextualConverterFactory method newConverter.

@SuppressWarnings("unchecked")
@Override
public ContextualConverter<? super I, ? extends O> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
    DateTimeFormatter[] dateTimeFormatters = JodaTimeHelper.getDateTimeFormatters(params);
    DateTimeZone zoneId = JodaTimeHelper.getDateTimeZoneOrDefault(params);
    ContextualConverter<I, O>[] converters = new ContextualConverter[dateTimeFormatters.length];
    for (int i = 0; i < dateTimeFormatters.length; i++) {
        DateTimeFormatter dateTimeFormatter = dateTimeFormatters[i];
        if (dateTimeFormatter.getZone() == null) {
            dateTimeFormatter.withZone(zoneId);
        }
        converters[i] = newConverter(dateTimeFormatter);
    }
    if (converters.length == 1) {
        return converters[0];
    } else {
        return new MultiDateTimeFormatterConverter<I, O>(converters);
    }
}
Also used : DateTimeFormatter(org.joda.time.format.DateTimeFormatter) ContextualConverter(org.simpleflatmapper.converter.ContextualConverter) DateTimeZone(org.joda.time.DateTimeZone)

Example 2 with ContextualConverter

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

the class AbstractMultiFormatContextualConverterFactory method newConverter.

@SuppressWarnings("unchecked")
@Override
public ContextualConverter<? super I, ? extends O> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
    DateTimeFormatter[] dateTimeFormatters = JavaTimeHelper.getDateTimeFormatters(params);
    if (dateTimeFormatters.length == 0) {
        return null;
    }
    ZoneId zoneId = JavaTimeHelper.getZoneId(params);
    ContextualConverter<I, O>[] converters = new ContextualConverter[dateTimeFormatters.length];
    for (int i = 0; i < dateTimeFormatters.length; i++) {
        DateTimeFormatter dateTimeFormatter = dateTimeFormatters[i];
        if (dateTimeFormatter.getZone() == null) {
            dateTimeFormatter.withZone(zoneId);
        }
        converters[i] = newConverter(dateTimeFormatter);
    }
    if (converters.length == 1) {
        return converters[0];
    } else {
        return new MultiDateTimeFormatterConverter<I, O>(converters);
    }
}
Also used : ZoneId(java.time.ZoneId) DateTimeFormatter(java.time.format.DateTimeFormatter) ContextualConverter(org.simpleflatmapper.converter.ContextualConverter)

Example 3 with ContextualConverter

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

the class SettableDataSetterFactory method getSetter.

@SuppressWarnings("unchecked")
@Override
public <P> Setter<SettableByIndexData, P> getSetter(PropertyMapping<?, ?, 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>> setterFactory = this.factoryPerClass.get(TypeHelper.toClass(type));
    EmptyContextFactoryBuilder contextFactoryBuilder = EmptyContextFactoryBuilder.INSTANCE;
    if (setterFactory != null) {
        setter = setterFactory.getSetter(arg);
        if (!TypeHelper.areEquals(TypeHelper.toBoxedClass(type), TypeHelper.toBoxedClass(propertyType))) {
            ContextualConverter<?, ?> converter = getConverter(propertyType, TypeHelper.toClass(type), dataType, arg.getColumnDefinition(), contextFactoryBuilder);
            if (converter != null) {
                setter = (Setter<SettableByIndexData, P>) new ConvertDelegateSetter<SettableByIndexData, Object, P>(setter, (ContextualConverter<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 {
                ContextualConverter<?, ?> converter = getConverter(lEltType, dEltType, dataTypeElt, arg.getColumnDefinition(), contextFactoryBuilder);
                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 {
                ContextualConverter<?, ?> converter = getConverter(lEltType, dEltType, dataTypeElt, arg.getColumnDefinition(), contextFactoryBuilder);
                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(), contextFactoryBuilder), getConverter(keyValueTypeOfMap.getValueType(), DataTypeHelper.asJavaClass(dtValueType), dtValueType, arg.getColumnDefinition(), contextFactoryBuilder));
            }
        }
    }
    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) ListSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListSettableDataSetter) UDTValueSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UDTValueSettableDataSetter) MapSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapSettableDataSetter) EmptyContextFactoryBuilder(org.simpleflatmapper.converter.EmptyContextFactoryBuilder) ContextualConverter(org.simpleflatmapper.converter.ContextualConverter) 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 4 with ContextualConverter

use of org.simpleflatmapper.converter.ContextualConverter 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 5 with ContextualConverter

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

the class JdbcConverterFactoryProducer method produce.

@Override
public void produce(Consumer<? super ContextualConverterFactory<?, ?>> consumer) {
    // IFJAVA8_START
    constantConverter(consumer, Time.class, LocalTime.class, new TimeToLocalTimeConverter());
    constantConverter(consumer, Date.class, LocalDate.class, new DateToLocalDateConverter());
    factoryConverter(consumer, new AbstractContextualConverterFactory<Time, OffsetTime>(Time.class, OffsetTime.class) {

        @Override
        public ContextualConverter<Time, OffsetTime> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
            ZoneOffset zoneOffset = getZoneOffset(params);
            return new TimeToOffsetTimeConverter(zoneOffset);
        }

        @SuppressWarnings("unchecked")
        private ZoneOffset getZoneOffset(Object[] params) {
            for (Object prop : params) {
                if (prop instanceof ZoneOffset) {
                    return (ZoneOffset) prop;
                } else if (SupplierHelper.isSupplierOf(prop, ZoneOffset.class)) {
                    return ((Supplier<ZoneOffset>) prop).get();
                }
            }
            return ZoneOffset.UTC;
        }
    });
    // IFJAVA8_END
    factoryConverter(consumer, new AbstractContextualConverterFactory<Array, Object>(Array.class, java.lang.reflect.Array.class) {

        @Override
        public ContextualConverter<? super Array, ? extends Object> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
            Type elementType = TypeHelper.getComponentTypeOfListOrArray(targetedTypes.getTo());
            Getter<? super ResultSet, ?> getter = ResultSetGetterFactory.INSTANCE.newGetter(elementType, new JdbcColumnKey("elt", 2), params);
            return new SqlArrayToJavaArrayConverter<Object>(TypeHelper.<Object>toClass(elementType), getter);
        }

        @Override
        public ConvertingScore score(ConvertingTypes targetedTypes) {
            return new ConvertingScore(super.score(targetedTypes).getFromScore(), TypeHelper.isArray(targetedTypes.getTo()) ? 1 : -1);
        }
    });
    factoryConverter(consumer, new AbstractContextualConverterFactory<Array, List>(Array.class, List.class) {

        @Override
        public ContextualConverter<? super Array, ? extends List> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
            Type elementType = TypeHelper.getComponentTypeOfListOrArray(targetedTypes.getTo());
            Getter<? super ResultSet, ?> getter = ResultSetGetterFactory.INSTANCE.newGetter(elementType, new JdbcColumnKey("elt", 2), params);
            return new SqlArrayToListConverter<Object>(getter);
        }
    });
    constantConverter(consumer, Calendar.class, Timestamp.class, new CalendarToTimestampConverter());
    constantConverter(consumer, java.util.Date.class, Timestamp.class, new UtilDateToTimestampConverter());
    constantConverter(consumer, java.util.Date.class, Time.class, new UtilDateToTimeConverter());
    constantConverter(consumer, java.util.Date.class, Date.class, new UtilDateToDateConverter());
    constantConverter(consumer, Blob.class, byte[].class, new BlobToByteConverter());
}
Also used : Getter(org.simpleflatmapper.reflect.Getter) Time(java.sql.Time) LocalTime(java.time.LocalTime) OffsetTime(java.time.OffsetTime) ZoneOffset(java.time.ZoneOffset) TimeToLocalTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToLocalTimeConverter) ContextFactoryBuilder(org.simpleflatmapper.converter.ContextFactoryBuilder) ConvertingTypes(org.simpleflatmapper.converter.ConvertingTypes) OffsetTime(java.time.OffsetTime) ResultSet(java.sql.ResultSet) TimeToOffsetTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToOffsetTimeConverter) Supplier(org.simpleflatmapper.util.Supplier) List(java.util.List) ConvertingScore(org.simpleflatmapper.converter.ConvertingScore) ContextualConverter(org.simpleflatmapper.converter.ContextualConverter) Array(java.sql.Array) JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) Type(java.lang.reflect.Type) DateToLocalDateConverter(org.simpleflatmapper.jdbc.converter.time.DateToLocalDateConverter)

Aggregations

ContextualConverter (org.simpleflatmapper.converter.ContextualConverter)8 Type (java.lang.reflect.Type)3 Context (org.simpleflatmapper.converter.Context)3 ResultSet (java.sql.ResultSet)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2 Test (org.junit.Test)2 ContextFactoryBuilder (org.simpleflatmapper.converter.ContextFactoryBuilder)2 ConvertingTypes (org.simpleflatmapper.converter.ConvertingTypes)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2 Array (java.sql.Array)1 Time (java.sql.Time)1 LocalTime (java.time.LocalTime)1 OffsetTime (java.time.OffsetTime)1 ZoneId (java.time.ZoneId)1 ZoneOffset (java.time.ZoneOffset)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Date (java.util.Date)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 DateTimeZone (org.joda.time.DateTimeZone)1