Skip to content

Package: ResultSetMappingProcessor

ResultSetMappingProcessor

nameinstructionbranchcomplexitylinemethod
ResultSetMappingProcessor(MetamodelBuilder)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
buildConstructorMappers(SparqlResultSetMapping, ResultRowMapper)
M: 0 C: 50
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
buildEntityMappers(SparqlResultSetMapping, ResultRowMapper)
M: 0 C: 45
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
buildMapper(SparqlResultSetMapping)
M: 0 C: 28
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
buildVariableMappers(SparqlResultSetMapping, ResultRowMapper)
M: 0 C: 24
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
createFieldMapper(FieldResult, FieldSpecification)
M: 6 C: 25
81%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 2 C: 5
71%
M: 0 C: 1
100%
createFieldMapper(FieldSpecification)
M: 0 C: 18
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
generateFieldMappersForFieldResults(EntityResult, EntityType, EntityResultMapper)
M: 0 C: 40
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
generateFieldMappersForUnconfiguredFields(EntityType, EntityResult)
M: 0 C: 42
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getManager()
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%
getTargetType(EntityResult)
M: 0 C: 27
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
lambda$generateFieldMappersForUnconfiguredFields$0(Set, FieldSpecification)
M: 0 C: 12
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
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%

Coverage

1: /**
2: * Copyright (C) 2016 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.exception.SparqlResultMappingException;
18: import cz.cvut.kbss.jopa.model.annotations.*;
19: import cz.cvut.kbss.jopa.model.metamodel.*;
20: import cz.cvut.kbss.jopa.query.ResultSetMappingManager;
21: import org.slf4j.Logger;
22: import org.slf4j.LoggerFactory;
23:
24: import java.util.*;
25: import java.util.stream.Collectors;
26:
27: /**
28: * Builds mappers for {@link SparqlResultSetMapping} instances discovered on classpath.
29: */
30: public class ResultSetMappingProcessor {
31:
32: private static final Logger LOG = LoggerFactory.getLogger(ResultSetMappingProcessor.class);
33:
34: private final ResultSetMappingManager manager = new ResultSetMappingManager();
35:
36: private final MetamodelBuilder metamodelBuilder;
37:
38: public ResultSetMappingProcessor(MetamodelBuilder metamodelBuilder) {
39: this.metamodelBuilder = metamodelBuilder;
40: }
41:
42: /**
43: * Builds result set mapper for the specified mapping.
44: *
45: * @param mapping Mapping configuration
46: */
47: public void buildMapper(SparqlResultSetMapping mapping) {
48: Objects.requireNonNull(mapping);
49: final ResultRowMapper rowMapper = new ResultRowMapper(mapping.name());
50: buildVariableMappers(mapping, rowMapper);
51: buildConstructorMappers(mapping, rowMapper);
52: buildEntityMappers(mapping, rowMapper);
53: manager.addMapper(rowMapper.getName(), rowMapper);
54: }
55:
56: private void buildVariableMappers(SparqlResultSetMapping mapping, ResultRowMapper parent) {
57:• for (VariableResult vr : mapping.variables()) {
58: parent.addMapper(new VariableResultMapper(vr));
59: }
60: }
61:
62: private void buildConstructorMappers(SparqlResultSetMapping mapping, ResultRowMapper parent) {
63:• for (ConstructorResult cr : mapping.classes()) {
64: final ConstructorResultMapper mapper = new ConstructorResultMapper(cr.targetClass());
65:• for (VariableResult vr : cr.variables()) {
66: mapper.addParameterMapper(new VariableResultMapper(vr));
67: }
68: parent.addMapper(mapper);
69: }
70: }
71:
72: private void buildEntityMappers(SparqlResultSetMapping mapping, ResultRowMapper parent) {
73:• for (EntityResult er : mapping.entities()) {
74: final EntityType<?> et = getTargetType(er);
75: final EntityResultMapper<?> etMapper = new EntityResultMapper<>(et);
76: generateFieldMappersForFieldResults(er, et, etMapper);
77: generateFieldMappersForUnconfiguredFields(et, er).forEach(etMapper::addFieldMapper);
78: parent.addMapper(etMapper);
79: }
80: }
81:
82: private EntityType<?> getTargetType(EntityResult er) {
83: final AbstractIdentifiableType<?> targetType = metamodelBuilder.entity(er.entityClass());
84:• if (!(targetType instanceof EntityType)) {
85: throw new SparqlResultMappingException(
86: "Type " + er.entityClass() +
87: " is not a known entity type and cannot be used as @EntityResult target class.");
88: }
89: return (EntityType<?>) targetType;
90: }
91:
92: private void generateFieldMappersForFieldResults(EntityResult er, EntityType<?> et,
93: EntityResultMapper<?> etMapper) {
94:• for (FieldResult fr : er.fields()) {
95: try {
96: final FieldSpecification<?, ?> fieldSpec = et.getFieldSpecification(fr.name());
97: createFieldMapper(fr, fieldSpec).ifPresent(etMapper::addFieldMapper);
98: } catch (IllegalArgumentException e) {
99: throw new SparqlResultMappingException(e);
100: }
101: }
102: }
103:
104: private Optional<FieldResultMapper> createFieldMapper(FieldResult fr, FieldSpecification<?, ?> fieldSpec) {
105:• if (fieldSpec.isCollection()) {
106: LOG.warn(
107: "Mapping of plural attributes via @FieldResult is not supported. Check the definition of {}.",
108: fr);
109: return Optional.empty();
110: }
111:• if (fieldSpec instanceof Attribute &&
112:• ((Attribute) fieldSpec).getPersistentAttributeType() == Attribute.PersistentAttributeType.OBJECT) {
113: return Optional.of(new ObjectPropertyFieldResultMapper(fr, fieldSpec));
114: } else {
115: return Optional.of(new FieldResultMapper(fr, fieldSpec));
116: }
117: }
118:
119: private List<FieldResultMapper> generateFieldMappersForUnconfiguredFields(EntityType<?> et, EntityResult er) {
120: final Set<String> configuredFields = new HashSet<>(er.fields().length);
121:• for (FieldResult fr : er.fields()) {
122: configuredFields.add(fr.name());
123: }
124: return et.getFieldSpecifications().stream()
125:• .filter(fs -> !configuredFields.contains(fs.getName()) && !fs.isCollection())
126: .map(this::createFieldMapper)
127: .collect(Collectors.toList());
128: }
129:
130: private FieldResultMapper createFieldMapper(FieldSpecification<?, ?> fieldSpec) {
131:• if (fieldSpec instanceof Attribute &&
132:• ((Attribute) fieldSpec).getPersistentAttributeType() == Attribute.PersistentAttributeType.OBJECT) {
133: return new ObjectPropertyFieldResultMapper(fieldSpec);
134: } else {
135: return new FieldResultMapper(fieldSpec);
136: }
137: }
138:
139: public ResultSetMappingManager getManager() {
140: return manager;
141: }
142: }