org.ict4h.atomfeed.jdbc.JdbcResultSetMapper Maven / Gradle / Ivy
package org.ict4h.atomfeed.jdbc;
import javax.persistence.Column;
import javax.persistence.Entity;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
public class JdbcResultSetMapper {
@SuppressWarnings("unchecked")
public List mapResultSetToObject(ResultSet rs, Class outputClass) {
List outputList = new ArrayList();
try {
if (rs != null) {
if (outputClass.isAnnotationPresent(Entity.class)) {
ResultSetMetaData rsmd = rs.getMetaData();
Field[] fields = outputClass.getDeclaredFields();
while (rs.next()) {
T instance = (T) outputClass.newInstance();
for (int itr = 0; itr < rsmd.getColumnCount(); itr++) {
String columnName = rsmd.getColumnName(itr + 1);
Object columnValue = rs.getObject(itr + 1);
for (Field field : fields) {
if (field.isAnnotationPresent(Column.class)) {
Column column = field.getAnnotation(Column.class);
if (column.name().equalsIgnoreCase(columnName) && columnValue != null) {
field.setAccessible(true);
field.set(instance, columnValue);
break;
}
}
}
}
if (outputList == null) {
outputList = new ArrayList();
}
outputList.add(instance);
}
} else {
throw new RuntimeException("Can not map to a class not marked with javax.persistence.Entity annotation");
}
} else {
return new ArrayList();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return outputList;
}
}