Package: FieldResultMapper
FieldResultMapper
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
FieldResultMapper(FieldResult, FieldSpecification) |
|
|
|
|
|
||||||||||||||||||||
FieldResultMapper(FieldSpecification) |
|
|
|
|
|
||||||||||||||||||||
getFieldSpecification() |
|
|
|
|
|
||||||||||||||||||||
getVariableName() |
|
|
|
|
|
||||||||||||||||||||
getVariableValue(ResultRow) |
|
|
|
|
|
||||||||||||||||||||
lambda$map$0(Object, Object) |
|
|
|
|
|
||||||||||||||||||||
lambda$resolveValue$1(Object, ConverterWrapper) |
|
|
|
|
|
||||||||||||||||||||
lambda$resolveValue$2(Object) |
|
|
|
|
|
||||||||||||||||||||
map(ResultRow, Object, UnitOfWork) |
|
|
|
|
|
||||||||||||||||||||
resolveValue(Object) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2023 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.query.mapper;
16:
17: import cz.cvut.kbss.jopa.datatype.DatatypeTransformer;
18: import cz.cvut.kbss.jopa.datatype.exception.UnsupportedTypeTransformationException;
19: import cz.cvut.kbss.jopa.exception.SparqlResultMappingException;
20: import cz.cvut.kbss.jopa.model.annotations.FieldResult;
21: import cz.cvut.kbss.jopa.model.metamodel.Converters;
22: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
23: import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper;
24: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
25: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
26: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
27: import cz.cvut.kbss.ontodriver.iteration.ResultRow;
28:
29: import java.util.Optional;
30:
31: class FieldResultMapper {
32:
33: private final String variableName;
34:
35: private final FieldSpecification<?, ?> fieldSpec;
36:
37: FieldResultMapper(FieldResult fieldResult, FieldSpecification<?, ?> fieldSpec) {
38: this.variableName = fieldResult.variable();
39: this.fieldSpec = fieldSpec;
40: }
41:
42: /**
43: * Creates mappers for which no explicit {@link FieldResult} configuration exists.
44: * <p>
45: * Variable name is taken to be the same as the name of the field.
46: */
47: FieldResultMapper(FieldSpecification<?, ?> fieldSpec) {
48: this.fieldSpec = fieldSpec;
49: this.variableName = fieldSpec.getName();
50: }
51:
52: String getVariableName() {
53: return variableName;
54: }
55:
56: FieldSpecification<?, ?> getFieldSpecification() {
57: return fieldSpec;
58: }
59:
60: /**
61: * Maps value from the specified result set to the specified target object's field based on the mapping represented
62: * by this instance.
63: *
64: * @param resultRow Result set with value to map
65: * @param target Target object on which the field will be set
66: */
67: void map(ResultRow resultRow, Object target, UnitOfWork uow) {
68: final Optional<Object> value = getVariableValue(resultRow);
69: value.ifPresent(
70: val -> EntityPropertiesUtils.setFieldValue(fieldSpec.getJavaField(), target, resolveValue(val)));
71: }
72:
73: Optional<Object> getVariableValue(ResultRow resultRow) {
74: try {
75:• if (!resultRow.isBound(variableName)) {
76: return Optional.empty();
77: }
78: return Optional.of(resultRow.getObject(variableName));
79: } catch (OntoDriverException e) {
80: throw new SparqlResultMappingException(e);
81: }
82: }
83:
84: private Object resolveValue(Object queryValue) {
85:• if (fieldSpec.getJavaType().isAssignableFrom(queryValue.getClass())) {
86: return queryValue;
87: }
88: try {
89: return Converters.getDefaultConverter(fieldSpec.getJavaType())
90: .map(converter -> ((ConverterWrapper) converter).convertToAttribute(queryValue))
91: .orElseGet(() -> DatatypeTransformer.transform(queryValue, fieldSpec.getJavaType()));
92: } catch (UnsupportedTypeTransformationException e) {
93: throw new SparqlResultMappingException(e);
94: }
95: }
96: }