-
Notifications
You must be signed in to change notification settings - Fork 14
/
FieldResultMapper.java
51 lines (44 loc) · 1.99 KB
/
FieldResultMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cz.cvut.kbss.jopa.query.mapper;
import cz.cvut.kbss.jopa.exception.SparqlResultMappingException;
import cz.cvut.kbss.jopa.model.annotations.FieldResult;
import cz.cvut.kbss.jopa.model.metamodel.EntityType;
import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
import cz.cvut.kbss.ontodriver.ResultSet;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
public class FieldResultMapper {
private final String variableName;
private final FieldSpecification<?, ?> fieldSpec;
FieldResultMapper(FieldResult fieldResult, EntityType<?> et) {
this.variableName = fieldResult.variable();
try {
this.fieldSpec = et.getFieldSpecification(fieldResult.name());
} catch (IllegalArgumentException e) {
throw new SparqlResultMappingException("Invalid FieldResult mapping for field " + fieldResult.name() + ".",
e);
}
}
/**
* Maps value from the specified result set to the specified target object's field based on the mapping represented
* by this instance.
*
* @param resultSet Result set with value to map
* @param target Target object on which the field will be set
*/
public void map(ResultSet resultSet, Object target) {
try {
final Object value = resultSet.getObject(variableName);
verifyValueRange(value);
// This does currently only literal values, no references
EntityPropertiesUtils.setFieldValue(fieldSpec.getJavaField(), target, value);
} catch (OntoDriverException e) {
throw new SparqlResultMappingException(e);
}
}
private void verifyValueRange(Object value) {
if (!fieldSpec.getJavaType().isAssignableFrom(value.getClass())) {
throw new SparqlResultMappingException(
String.format("Value %s cannot be assigned to field of type %s.", value, fieldSpec.getJavaType()));
}
}
}