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

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

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

import com.datastax.driver.core.*;
import org.sfm.datastax.DatastaxColumnKey;
import org.sfm.datastax.SettableDataMapperBuilder;
import org.sfm.datastax.impl.SettableDataSetterFactory;
import org.sfm.map.Mapper;
import org.sfm.map.MapperConfig;
import org.sfm.map.column.FieldMapperColumnDefinition;
import org.sfm.map.mapper.ConstantTargetFieldMapperFactorImpl;
import org.sfm.reflect.ReflectionService;
import org.sfm.reflect.Setter;
import org.sfm.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 Mapper mapper;

    public UDTObjectSettableDataSetter(int index, UserType udtType, Mapper 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) {
        Mapper mapper = newUDTMapper(target, tt, config, reflectionService);
        return new UDTObjectSettableDataSetter(index, tt, mapper);
    }

    public static  Mapper 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, ConstantTargetFieldMapperFactorImpl.instance(new SettableDataSetterFactory(config, reflectionService)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy