All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleflatmapper.csv.mapper.FieldMapperToAppendableFactory Maven / Gradle / Ivy

package org.simpleflatmapper.csv.mapper;

import org.simpleflatmapper.converter.ContextualConverter;
import org.simpleflatmapper.converter.ConverterService;
import org.simpleflatmapper.lightningcsv.CellWriter;
import org.simpleflatmapper.csv.CsvColumnKey;
import org.simpleflatmapper.csv.impl.writer.*;

import org.simpleflatmapper.map.MapperBuilderErrorHandler;
import org.simpleflatmapper.map.property.SetterFactoryProperty;
import org.simpleflatmapper.map.property.SetterProperty;
import org.simpleflatmapper.map.fieldmapper.*;
import org.simpleflatmapper.map.mapper.ColumnDefinition;
import org.simpleflatmapper.map.FieldMapper;
import org.simpleflatmapper.map.MappingContext;
import org.simpleflatmapper.map.property.DateFormatProperty;
import org.simpleflatmapper.map.property.EnumOrdinalFormatProperty;
import org.simpleflatmapper.map.property.FormatProperty;
import org.simpleflatmapper.map.mapper.ConstantTargetFieldMapperFactory;
import org.simpleflatmapper.map.mapper.PropertyMapping;
import org.simpleflatmapper.map.context.MappingContextFactoryBuilder;
import org.simpleflatmapper.map.setter.ContextualSetter;
import org.simpleflatmapper.map.setter.ContextualSetterAdapter;
import org.simpleflatmapper.map.setter.ContextualSetterOnGetter;
import org.simpleflatmapper.reflect.Getter;
import org.simpleflatmapper.reflect.Setter;
import org.simpleflatmapper.reflect.SetterFactory;
import org.simpleflatmapper.reflect.meta.ClassMeta;
import org.simpleflatmapper.reflect.meta.ObjectClassMeta;
import org.simpleflatmapper.reflect.meta.PropertyMeta;
import org.simpleflatmapper.reflect.primitive.BooleanGetter;
import org.simpleflatmapper.reflect.primitive.ByteGetter;
import org.simpleflatmapper.reflect.primitive.CharacterGetter;
import org.simpleflatmapper.reflect.primitive.DoubleGetter;
import org.simpleflatmapper.reflect.primitive.FloatGetter;
import org.simpleflatmapper.reflect.primitive.IntGetter;
import org.simpleflatmapper.reflect.primitive.LongGetter;
import org.simpleflatmapper.reflect.primitive.ShortGetter;
import org.simpleflatmapper.util.Supplier;
import org.simpleflatmapper.util.TypeHelper;

import java.lang.reflect.Type;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FieldMapperToAppendableFactory implements ConstantTargetFieldMapperFactory {

    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private final CellWriter cellWriter;
    private final ConverterService converterService = ConverterService.getInstance();

    public FieldMapperToAppendableFactory(CellWriter cellWriter) {
        this.cellWriter = cellWriter;
    }

    @Override
    @SuppressWarnings("unchecked")
    public  FieldMapper newFieldMapper(PropertyMapping pm, MappingContextFactoryBuilder builder, MapperBuilderErrorHandler mappingErrorHandler) {
        if (pm == null) throw new NullPointerException("pm is null");

        Getter getter;

        Getter customGetter = pm.getColumnDefinition().getCustomGetterFrom(pm.getPropertyMeta().getOwnerType());
        if (customGetter != null) {
            getter = (Getter) customGetter;
        } else {
            getter = pm.getPropertyMeta().getGetter();
        }


        ColumnDefinition columnDefinition = pm.getColumnDefinition();
        
        if (columnDefinition == null) throw new NullPointerException("Unexpected null columnDefinition");
        
        Type type = pm.getPropertyMeta().getPropertyType();
        if (TypeHelper.isPrimitive(type) && !columnDefinition.has(FormatProperty.class)) {
            if (getter instanceof BooleanGetter) {
                return new BooleanConstantTargetFieldMapper((BooleanGetter) getter, new BooleanAppendableSetter(cellWriter));
            } else if (getter instanceof ByteGetter) {
                return new ByteConstantTargetFieldMapper((ByteGetter) getter, new ByteAppendableSetter(cellWriter));
            } else if (getter instanceof CharacterGetter) {
                return new CharacterConstantTargetFieldMapper((CharacterGetter) getter, new CharacterAppendableSetter(cellWriter));
            } else if (getter instanceof ShortGetter) {
                return new ShortConstantTargetFieldMapper((ShortGetter) getter, new ShortAppendableSetter(cellWriter));
            } else if (getter instanceof IntGetter) {
                return new IntConstantTargetFieldMapper((IntGetter) getter, new IntegerAppendableSetter(cellWriter));
            } else if (getter instanceof LongGetter) {
                return new LongConstantTargetFieldMapper((LongGetter) getter, new LongAppendableSetter(cellWriter));
            } else if (getter instanceof FloatGetter) {
                return new FloatConstantTargetFieldMapper((FloatGetter) getter, new FloatAppendableSetter(cellWriter));
            } else if (getter instanceof DoubleGetter) {
                return new DoubleConstantTargetFieldMapper((DoubleGetter) getter, new DoubleAppendableSetter(cellWriter));
            }
        }

        ContextualSetter setter = null;

        if (TypeHelper.isEnum(type) && columnDefinition.has(EnumOrdinalFormatProperty.class)) {
            setter = (ContextualSetter) new EnumOrdinalAppendableSetter(cellWriter);
        }

        Format format = null;

        if (columnDefinition.has(FormatProperty.class)) {
            format = columnDefinition.lookFor(FormatProperty.class).format();
        } else if (TypeHelper.areEquals(type, Date.class)) {
            String df = DEFAULT_DATE_FORMAT;

            DateFormatProperty dfp = columnDefinition.lookFor(DateFormatProperty.class);
            if (dfp != null) {
                df = dfp.get();
            }
            format = new SimpleDateFormat(df);
        }

        if (format != null) {
            final Format f = format;
            int  i = builder.addSupplier(new CloneFormatSupplier(f));
            return new FormatingAppender(getter, new MappingContextFormatGetter(i), cellWriter);
        }


        if (setter == null) {
            setter = getSetter(pm, cellWriter);
        }

        if (setter == null) {
            ContextualConverter converter =
                    converterService.findConverter(
                            pm.getPropertyMeta().getPropertyType(),
                            CharSequence.class,
                            builder,
                            columnDefinition.properties());

            if (converter != null) {
                return new ConvertingAppender(getter, converter, cellWriter);
            }
        }

        return new ConstantTargetFieldMapper(getter, setter);
    }

    @SuppressWarnings("unchecked")
    private  ContextualSetter getSetter(PropertyMapping pm, CellWriter cellWriter) {

        final SetterProperty setterProperty = pm.getColumnDefinition().lookFor(SetterProperty.class);

        if (setterProperty != null) {
            return new CellWriterSetterWrapper

(cellWriter, ContextualSetterAdapter.of ((Setter) setterProperty.getSetter())); } ContextualSetter setter = setterFromFactory(pm); if (setter != null) { return new CellWriterSetterWrapper

(cellWriter, setter); } return null; } @SuppressWarnings("unchecked") private ContextualSetter setterFromFactory(PropertyMapping pm) { ContextualSetter setter = null; final SetterFactoryProperty setterFactoryProperty = pm.getColumnDefinition().lookFor(SetterFactoryProperty.class); if (setterFactoryProperty != null) { final SetterFactory> setterFactory = (SetterFactory>) setterFactoryProperty.getSetterFactory(); setter = ContextualSetterAdapter.of(setterFactory.getSetter(pm)); } if (setter == null) { if (!pm.getPropertyMeta().isSelf()) { final ClassMeta

classMeta = pm.getPropertyMeta().getPropertyClassMeta(); if (classMeta instanceof ObjectClassMeta) { ObjectClassMeta

ocm = (ObjectClassMeta

) classMeta; if (ocm.getNumberOfProperties() == 1) { PropertyMeta subProp = ocm.getFirstProperty(); ContextualSetter subSetter = (ContextualSetter) setterFromFactory(pm.propertyMeta(subProp)); if (subSetter != null) { setter = new ContextualSetterOnGetter(subSetter, (Getter) subProp.getGetter()); } else { return new ObjectToStringSetter

(subProp.getGetter()); } } } } } return setter; } private static class MappingContextFormatGetter implements Getter, Format> { private final int index; public MappingContextFormatGetter(int index) { this.index = index; } @Override public Format get(MappingContext target) throws Exception { return target.context(index); } } private static class CloneFormatSupplier implements Supplier { private final Format f; public CloneFormatSupplier(Format f) { this.f = f; } @Override public Format get() { return (Format) f.clone(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy