}
private void fillObject(final Object result, final ResultTree tree) throws SQLException {
for (final Mapping mapping : getMappings()) {
try {
final DbResultNode node = tree.getChildByName(mapping.getName());
// TODO pribeiro we need to distinguish between null value and a mapping not defined in the tree
if (node == null) {
// this may be okay - if any return value is NULL, we will reach this path.
// to classify and mark this as an error, we need more information.
LOG.trace("Could not map property {} of class {}: field not in result tree, field may be nullable.",
mapping.getName(), resultClass.getSimpleName());
continue;
}
if (DbResultNodeType.MAP != node.getNodeType() && node.getValue() == null) {
if (mapping.isOptionalField()) {
mapping.map(result, null);
}
}
// TODO pribeiro we should use polymorphism instead. Build like a chain.
if (DbResultNodeType.SIMPLE == node.getNodeType()) {
final String fieldStringValue = node.getValue();
final Object value = mapping.getFieldMapper().mapField(fieldStringValue, mapping.getFieldClass());
mapping.map(result, value);
} else if (DbResultNodeType.MAP == node.getNodeType()) {
// TODO all fields are being converted to String and reverted later. The API forces this approach
// (DbResultNode.getValue). This should be improved because it's just causing overhead. The driver
// can convert at least the basic types so we should reuse this logic. Result tree should be
// improved.
// Refactor the if/else statements to a more object-based or polymorphic solution.
final Object value = ((MapResultNode) node).getMap();
mapping.map(result, value);
} else if (DbResultNodeType.OBJECT == node.getNodeType()) {
final Object value = ObjectFieldMapper.mapFromDbObjectNode(mapping.getFieldClass(),
(ObjectResultNode) node, mapping);
mapping.map(result, value);
} else if (DbResultNodeType.ARRAY == node.getNodeType()) {
final Object value = ArrayFieldMapper.mapField(mapping.getField(), (ArrayResultNode) node);
mapping.map(result, value);
}
} catch (final Exception e) {