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