org.simpleflatmapper.datastax.impl.getter.DatastaxUDTGetter Maven / Gradle / Ivy
Show all versions of sfm-datastax Show documentation
package org.simpleflatmapper.datastax.impl.getter;
import com.datastax.driver.core.GettableByIndexData;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.UserType;
import org.simpleflatmapper.datastax.DatastaxColumnKey;
import org.simpleflatmapper.datastax.DatastaxMapperBuilder;
import org.simpleflatmapper.datastax.DatastaxMapperFactory;
import org.simpleflatmapper.datastax.impl.DatastaxMappingContextFactoryBuilder;
import org.simpleflatmapper.datastax.impl.RowGetterFactory;
import org.simpleflatmapper.map.ContextualSourceMapper;
import org.simpleflatmapper.map.MapperConfig;
import org.simpleflatmapper.map.getter.ContextualGetterFactoryAdapter;
import org.simpleflatmapper.map.mapper.DefaultConstantSourceMapperBuilder;
import org.simpleflatmapper.map.property.FieldMapperColumnDefinition;
import org.simpleflatmapper.map.mapper.ConstantSourceMapperBuilder;
import org.simpleflatmapper.map.mapper.MapperSourceImpl;
import org.simpleflatmapper.reflect.Getter;
import org.simpleflatmapper.reflect.meta.ClassMeta;
import java.lang.reflect.Type;
import java.util.Iterator;
public class DatastaxUDTGetter implements Getter {
private final ContextualSourceMapper mapper;
private final int index;
public DatastaxUDTGetter(ContextualSourceMapper mapper, int index) {
this.mapper = mapper;
this.index = index;
}
@Override
public T get(GettableByIndexData target) throws Exception {
return mapper.map(target.getUDTValue(index));
}
@SuppressWarnings("unchecked")
public static Getter newInstance(DatastaxMapperFactory factory, Type target, UserType tt, int index) {
ContextualSourceMapper mapper = newUDTMapper(target, tt, factory);
return new DatastaxUDTGetter(mapper, index);
}
public static
ContextualSourceMapper newUDTMapper(Type target, UserType tt, DatastaxMapperFactory factory) {
ConstantSourceMapperBuilder builder = newFieldMapperBuilder(factory, target);
Iterator iterator = tt.iterator();
int i = 0;
while(iterator.hasNext()) {
UserType.Field f = iterator.next();
FieldMapperColumnDefinition identity = FieldMapperColumnDefinition.identity();
builder.addMapping(new DatastaxColumnKey(f.getName(), i, f.getType()), identity);
i ++;
}
return builder.mapper();
}
public static ConstantSourceMapperBuilder newFieldMapperBuilder(DatastaxMapperFactory factory, Type target) {
MapperConfig config = factory.mapperConfig();
MapperSourceImpl mapperSource = new MapperSourceImpl(GettableByIndexData.class, new ContextualGetterFactoryAdapter(new RowGetterFactory(factory)));
ClassMeta classMeta = factory.getClassMeta(target);
return ConstantSourceMapperBuilder.newConstantSourceMapperBuilder(
mapperSource,
classMeta,
config,
new DatastaxMappingContextFactoryBuilder(),
DatastaxMapperBuilder.KEY_FACTORY);
}
}