Skip to content

Package: EntityConstructor

EntityConstructor

nameinstructionbranchcomplexitylinemethod
EntityConstructor(ObjectOntologyMapperImpl)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
axiomsContainEntityClassAssertion(Collection, EntityType)
M: 0 C: 19
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createEntityInstance(URI, EntityType, Descriptor)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getFieldLoader(Axiom, Map, Map, EntityType, Descriptor)
M: 2 C: 39
95%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 1 C: 9
90%
M: 0 C: 1
100%
getFieldSpecification(Field, EntityType)
M: 0 C: 29
100%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 5
100%
M: 0 C: 1
100%
indexEntityAttributes(EntityType)
M: 0 C: 38
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
populateAttributes(Object, EntityType, Descriptor, Collection)
M: 8 C: 59
88%
M: 4 C: 8
67%
M: 3 C: 4
57%
M: 1 C: 15
94%
M: 0 C: 1
100%
reconstructEntity(URI, EntityType, Descriptor, Collection)
M: 4 C: 29
88%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 7
100%
M: 0 C: 1
100%
setFieldValue(Object, Field, Collection, EntityType, Descriptor)
M: 0 C: 29
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
shouldSkipICValidationOnLoad()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
validateIntegrityConstraints(Object, EntityType)
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
validateIntegrityConstraints(Object, FieldSpecification)
M: 0 C: 13
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
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.oom;
16:
17: import cz.cvut.kbss.jopa.CommonVocabulary;
18: import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
19: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
20: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
21: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
22: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
23: import cz.cvut.kbss.jopa.sessions.validator.IntegrityConstraintsValidator;
24: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
25: import cz.cvut.kbss.ontodriver.model.Axiom;
26:
27: import java.lang.reflect.Field;
28: import java.net.URI;
29: import java.util.Collection;
30: import java.util.HashMap;
31: import java.util.Map;
32:
33: class EntityConstructor {
34:
35: private final ObjectOntologyMapperImpl mapper;
36:
37: public EntityConstructor(ObjectOntologyMapperImpl mapper) {
38: this.mapper = mapper;
39: }
40:
41: <T> T reconstructEntity(URI primaryKey, EntityType<T> et, Descriptor descriptor,
42: Collection<Axiom<?>> axioms) throws InstantiationException, IllegalAccessException {
43:• assert !axioms.isEmpty();
44:
45:• if (!axiomsContainEntityClassAssertion(axioms, et)) {
46: return null;
47: }
48: final T instance = createEntityInstance(primaryKey, et, descriptor);
49: populateAttributes(instance, et, descriptor, axioms);
50: validateIntegrityConstraints(instance, et);
51:
52: return instance;
53: }
54:
55: private boolean axiomsContainEntityClassAssertion(Collection<Axiom<?>> axioms, EntityType<?> et) {
56:• for (Axiom<?> ax : axioms) {
57:• if (MappingUtils.isEntityClassAssertion(ax, et)) {
58: return true;
59: }
60: }
61: return false;
62: }
63:
64: private <T> T createEntityInstance(URI primaryKey, EntityType<T> et, Descriptor descriptor)
65: throws InstantiationException, IllegalAccessException {
66: final T instance = et.getJavaType().newInstance();
67: EntityPropertiesUtils.setPrimaryKey(primaryKey, instance, et);
68: mapper.registerInstance(primaryKey, instance, descriptor.getContext());
69: return instance;
70: }
71:
72: private <T> void populateAttributes(final T instance, EntityType<T> et,
73: Descriptor entityDescriptor, Collection<Axiom<?>> axioms)
74: throws IllegalAccessException {
75: final Map<URI, FieldSpecification<? super T, ?>> attributes = indexEntityAttributes(et);
76: final Map<FieldSpecification<? super T, ?>, FieldStrategy<? extends FieldSpecification<? super T, ?>, T>> fieldLoaders = new HashMap<>(
77: et.getAttributes().size());
78:• for (Axiom<?> ax : axioms) {
79:• if (MappingUtils.isEntityClassAssertion(ax, et)) {
80: continue;
81: }
82: final FieldStrategy<? extends FieldSpecification<? super T, ?>, T> fs = getFieldLoader(
83: ax, attributes, fieldLoaders, et, entityDescriptor);
84:• if (fs == null && MappingUtils.isClassAssertion(ax)) {
85: // This means the axiom is class assertion but the entity contains no types field
86: continue;
87: }
88:• assert fs != null;
89: fs.addValueFromAxiom(ax);
90: }
91: // We need to build the field values separately because some may be
92: // plural and we have to wait until all values are prepared
93:• for (FieldStrategy<? extends FieldSpecification<?, ?>, ?> fs : fieldLoaders.values()) {
94: fs.buildInstanceFieldValue(instance);
95: }
96: }
97:
98: private <T> Map<URI, FieldSpecification<? super T, ?>> indexEntityAttributes(EntityType<T> et) {
99: final Map<URI, FieldSpecification<? super T, ?>> atts = new HashMap<>(et.getAttributes()
100: .size());
101:• for (Attribute<? super T, ?> at : et.getAttributes()) {
102: atts.put(at.getIRI().toURI(), at);
103: }
104:• if (et.getTypes() != null) {
105: atts.put(URI.create(CommonVocabulary.RDF_TYPE), et.getTypes());
106: }
107: return atts;
108: }
109:
110: private <T> FieldStrategy<? extends FieldSpecification<? super T, ?>, T> getFieldLoader(
111: Axiom<?> ax,
112: Map<URI, FieldSpecification<? super T, ?>> attributes,
113: Map<FieldSpecification<? super T, ?>, FieldStrategy<? extends FieldSpecification<? super T, ?>, T>> loaders,
114: EntityType<T> et, Descriptor desc) {
115: final URI attId = ax.getAssertion().getIdentifier();
116: FieldSpecification<? super T, ?> att = attributes.get(attId);
117:• if (att == null) {
118:• if (et.getProperties() != null) {
119: att = et.getProperties();
120: } else {
121: return null;
122: }
123: }
124:• if (!loaders.containsKey(att)) {
125: loaders.put(att, FieldStrategy.createFieldStrategy(et, att,
126: desc.getAttributeDescriptor(att), mapper));
127: }
128: return loaders.get(att);
129: }
130:
131: private <T> void validateIntegrityConstraints(T entity, EntityType<T> et) {
132:• if (shouldSkipICValidationOnLoad()) {
133: return;
134: }
135: IntegrityConstraintsValidator.getValidator().validate(entity, et, true);
136: }
137:
138: private boolean shouldSkipICValidationOnLoad() {
139: return mapper.getConfiguration().is(JOPAPersistenceProperties.DISABLE_IC_VALIDATION_ON_LOAD);
140: }
141:
142: private <T> void validateIntegrityConstraints(T entity, FieldSpecification<T, ?> fieldSpec) {
143:• if (shouldSkipICValidationOnLoad()) {
144: return;
145: }
146: final Object value = EntityPropertiesUtils.getAttributeValue(fieldSpec, entity);
147: IntegrityConstraintsValidator.getValidator().validate(fieldSpec, value);
148: }
149:
150: <T> void setFieldValue(T entity, Field field, Collection<Axiom<?>> axioms, EntityType<T> et,
151: Descriptor entityDescriptor) throws IllegalArgumentException, IllegalAccessException {
152: final FieldSpecification<? super T, ?> fieldSpec = getFieldSpecification(field, et);
153: final FieldStrategy<? extends FieldSpecification<? super T, ?>, T> fs = FieldStrategy
154: .createFieldStrategy(et, fieldSpec,
155: entityDescriptor.getAttributeDescriptor(fieldSpec), mapper);
156: axioms.forEach(fs::addValueFromAxiom);
157: fs.buildInstanceFieldValue(entity);
158: validateIntegrityConstraints(entity, fieldSpec);
159: }
160:
161: private <T> FieldSpecification<? super T, ?> getFieldSpecification(Field field, EntityType<T> et) {
162:• if (et.getTypes() != null && et.getTypes().getJavaField().equals(field)) {
163: return et.getTypes();
164:• } else if (et.getProperties() != null && et.getProperties().getJavaField().equals(field)) {
165: return et.getProperties();
166: } else {
167: return et.getAttribute(field.getName());
168: }
169: }
170: }