-
Notifications
You must be signed in to change notification settings - Fork 14
/
FieldResultMapper.java
96 lines (84 loc) · 3.74 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Copyright (C) 2022 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.query.mapper;
import cz.cvut.kbss.jopa.datatype.DatatypeTransformer;
import cz.cvut.kbss.jopa.datatype.exception.UnsupportedTypeTransformationException;
import cz.cvut.kbss.jopa.exception.SparqlResultMappingException;
import cz.cvut.kbss.jopa.model.annotations.FieldResult;
import cz.cvut.kbss.jopa.model.metamodel.Converters;
import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper;
import cz.cvut.kbss.jopa.sessions.UnitOfWork;
import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
import cz.cvut.kbss.ontodriver.iteration.ResultRow;
import java.util.Optional;
class FieldResultMapper {
private final String variableName;
private final FieldSpecification<?, ?> fieldSpec;
FieldResultMapper(FieldResult fieldResult, FieldSpecification<?, ?> fieldSpec) {
this.variableName = fieldResult.variable();
this.fieldSpec = fieldSpec;
}
/**
* Creates mappers for which no explicit {@link FieldResult} configuration exists.
* <p>
* Variable name is taken to be the same as the name of the field.
*/
FieldResultMapper(FieldSpecification<?, ?> fieldSpec) {
this.fieldSpec = fieldSpec;
this.variableName = fieldSpec.getName();
}
String getVariableName() {
return variableName;
}
FieldSpecification<?, ?> getFieldSpecification() {
return fieldSpec;
}
/**
* Maps value from the specified result set to the specified target object's field based on the mapping represented
* by this instance.
*
* @param resultRow Result set with value to map
* @param target Target object on which the field will be set
*/
void map(ResultRow resultRow, Object target, UnitOfWork uow) {
final Optional<Object> value = getVariableValue(resultRow);
value.ifPresent(
val -> EntityPropertiesUtils.setFieldValue(fieldSpec.getJavaField(), target, resolveValue(val)));
}
Optional<Object> getVariableValue(ResultRow resultRow) {
try {
if (!resultRow.isBound(variableName)) {
return Optional.empty();
}
return Optional.of(resultRow.getObject(variableName));
} catch (OntoDriverException e) {
throw new SparqlResultMappingException(e);
}
}
private Object resolveValue(Object queryValue) {
if (fieldSpec.getJavaType().isAssignableFrom(queryValue.getClass())) {
return queryValue;
}
try {
if (Converters.getDefaultConverters().containsKey(fieldSpec.getJavaType())) {
return ((ConverterWrapper) Converters.getDefaultConverters()
.get(fieldSpec.getJavaType())).convertToAttribute(queryValue);
}
return DatatypeTransformer.transform(queryValue, fieldSpec.getJavaType());
} catch (UnsupportedTypeTransformationException e) {
throw new SparqlResultMappingException(e);
}
}
}