org.simpleflatmapper.datastax.impl.setter.UDTObjectSettableDataSetter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfm-datastax Show documentation
Show all versions of sfm-datastax Show documentation
Cassandra Datastax SFM supports.
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.Mapper;
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 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, ConstantTargetFieldMapperFactoryImpl.newInstance(new SettableDataSetterFactory(config, reflectionService), SettableByIndexData.class));
}
}