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

org.simpleflatmapper.datastax.impl.setter.UDTObjectSettableDataSetter Maven / Gradle / Ivy

There is a newer version: 8.2.3
Show newest version
package org.simpleflatmapper.datastax.impl.setter;

import com.datastax.driver.core.*;
import org.simpleflatmapper.datastax.DatastaxColumnKey;
import org.simpleflatmapper.datastax.SettableDataMapperBuilder;
import org.simpleflatmapper.datastax.impl.SettableDataSetterFactory;
import org.simpleflatmapper.map.FieldMapper;
import org.simpleflatmapper.map.SourceMapper;
import org.simpleflatmapper.map.MapperConfig;
import org.simpleflatmapper.map.property.FieldMapperColumnDefinition;
import org.simpleflatmapper.map.mapper.ConstantTargetFieldMapperFactoryImpl;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.reflect.Setter;
import org.simpleflatmapper.reflect.meta.ClassMeta;

import java.lang.reflect.Type;
import java.util.Iterator;

public class UDTObjectSettableDataSetter implements Setter {
    private final int index;
    private final UserType udtType;
    private final FieldMapper mapper;

    public UDTObjectSettableDataSetter(int index, UserType udtType, FieldMapper mapper) {
        this.index = index;
        this.udtType = udtType;
        this.mapper = mapper;
    }

    @Override
    public void set(SettableByIndexData target, T value) throws Exception {
        if (value == null) {
            target.setToNull(index);
        } else {
            UDTValue udtValue = udtType.newValue();
            mapper.mapTo(value, udtValue, null);
            target.setUDTValue(index, udtValue);
        }
    }

    public static  Setter newInstance(Type target,  UserType tt, int index, MapperConfig config,
                                                                                      ReflectionService reflectionService) {
        FieldMapper mapper = newUDTMapper(target, tt, config, reflectionService);
        return new UDTObjectSettableDataSetter(index, tt, mapper);
    }

    public static  FieldMapper newUDTMapper(Type target, UserType tt,
                                                                        MapperConfig config,
                                                                        ReflectionService reflectionService) {
        SettableDataMapperBuilder builder = newFieldMapperBuilder(config, reflectionService, target);
        Iterator iterator = tt.iterator();
        int i = 0;
        while(iterator.hasNext()) {
            UserType.Field f = iterator.next();
            builder.addColumn(new DatastaxColumnKey(f.getName(), i, f.getType()));
            i++;
        }
        return builder.mapper();
    }

    public static  SettableDataMapperBuilder newFieldMapperBuilder(MapperConfig config,
                                                                         ReflectionService reflectionService,  Type target) {
        ClassMeta classMeta = reflectionService.getClassMeta(target);
        return new SettableDataMapperBuilder(classMeta, config, ConstantTargetFieldMapperFactoryImpl.newInstance(new SettableDataSetterFactory(config, reflectionService), SettableByIndexData.class));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy