Skip to content

Package: FieldResultMapper

FieldResultMapper

nameinstructionbranchcomplexitylinemethod
FieldResultMapper(FieldResult, FieldSpecification)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
FieldResultMapper(FieldSpecification)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getFieldSpecification()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getVariableName()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getVariableValue(ResultRow)
M: 6 C: 13
68%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 3
60%
M: 0 C: 1
100%
lambda$map$0(Object, Object)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$resolveValue$1(Object, ConverterWrapper)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$resolveValue$2(Object)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
map(ResultRow, Object, UnitOfWork)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
resolveValue(Object)
M: 0 C: 27
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%

Coverage

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