com.github.rkmk.mapper.CustomMapper Maven / Gradle / Ivy
package com.github.rkmk.mapper;
import com.github.rkmk.helper.FieldWrapper;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.ResultSetMapper;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.*;
import static com.github.rkmk.mapper.FieldHelper.getInstance;
import static java.util.Objects.nonNull;
public class CustomMapper implements ResultSetMapper
{
private final Class type;
private Map fields = new HashMap<>();
private List factories = new ArrayList<>();
private Map, AnnotatedFields> annotatedFieldsMap;
public CustomMapper(Class type) {
this(type, new ArrayList<>());
}
public CustomMapper(Class type, List overriddenFactories) {
this.type = type;
this.factories.addAll(overriddenFactories);
this.factories.addAll(new FieldMapperFactories().getValues());
this.fields = AnnotatedFieldFactory.getFields(type);
annotatedFieldsMap = AnnotatedFieldFactory.get(type);
}
private FieldWrapper processResultSetName(String resultSetName) {
String[] strings = resultSetName.split("\\$");
if(strings.length ==1) {
return fields.get(resultSetName.replace("_", ""));
}else {
String nameSpace = strings[0];
String nameWithoutUnderscore = strings[1].replace("_", "");
return fields.get(nameSpace + "$" + nameWithoutUnderscore);
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
public T map(int row, ResultSet rs, StatementContext ctx) throws SQLException {
HashMap, Object> instanceMap = new HashMap<>();
T object = getInstance(type);
instanceMap.put(type, object);
Set nestedClassNames = getNestedClassNames(rs);
ResultSetMetaData metadata = rs.getMetaData();
for (int index = 1; index <= metadata.getColumnCount(); ++index) {
String name = metadata.getColumnLabel(index).toLowerCase();
FieldWrapper fieldWrapper = processResultSetName(name);
if (fieldWrapper != null) {
Class type = fieldWrapper.getFieldType();
for (FieldMapperFactory factory : factories) {
if(factory.accepts(type)) {
Object value = factory.getValue(rs, index, type);
fieldWrapper.setValue(value, instanceMap);
break;
}
}
}
}
setNestedObjects(type, nestedClassNames, instanceMap);
return object;
}
private void setNestedObjects(Class> type, Set nestedClassNames, Map, Object> instanceMap) {
for (AnnotatedField field : annotatedFieldsMap.get(type).values()) {
if(nestedClassNames.contains(field.getNameSpace()) && instanceMap.containsKey(field.getType()) ) {
field.set(instanceMap.get(field.getDeclaringClass()), instanceMap.get(field.getType()));
setNestedObjects(field.getType(), nestedClassNames, instanceMap);
}
}
}
private Set getNestedClassNames(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
Set childClassNames = new HashSet<>();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
String name = metaData.getColumnLabel(i).toLowerCase();
String[] split = name.split("\\$");
if(split.length == 2 && nonNull(rs.getObject(name))) {
childClassNames.add(split[0]);
}
}
return childClassNames;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy