Skip to content

Package: FieldStrategy

FieldStrategy

nameinstructionbranchcomplexitylinemethod
FieldStrategy(EntityType, FieldSpecification, Descriptor, EntityMappingHelper)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
createFieldStrategy(EntityType, FieldSpecification, Descriptor, EntityMappingHelper)
M: 9 C: 73
89%
M: 3 C: 10
77%
M: 3 C: 6
67%
M: 3 C: 12
80%
M: 0 C: 1
100%
createOwlListPropertyStrategy(EntityType, ListAttribute, Descriptor, EntityMappingHelper)
M: 13 C: 22
63%
M: 1 C: 2
67%
M: 1 C: 2
67%
M: 2 C: 3
60%
M: 0 C: 1
100%
createPluralObjectPropertyStrategy(EntityType, PluralAttribute, Descriptor, EntityMappingHelper)
M: 13 C: 21
62%
M: 1 C: 2
67%
M: 1 C: 2
67%
M: 2 C: 3
60%
M: 0 C: 1
100%
extractFieldValueFromInstance(Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getAttributeContext()
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%
resolveValueIdentifier(Object, EntityType)
M: 0 C: 17
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
setCascadeResolver(CascadeResolver)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setValueOnInstance(Object, Object)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
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.model.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.metamodel.*;
19: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
20: import cz.cvut.kbss.ontodriver.exception.NotYetImplementedException;
21: import cz.cvut.kbss.ontodriver.model.Assertion;
22: import cz.cvut.kbss.ontodriver.model.Axiom;
23:
24: import java.net.URI;
25:
26: abstract class FieldStrategy<T extends FieldSpecification<? super X, ?>, X> {
27:
28: final EntityType<X> et;
29: final T attribute;
30: final Descriptor attributeDescriptor;
31: final EntityMappingHelper mapper;
32: CascadeResolver cascadeResolver;
33:
34: FieldStrategy(EntityType<X> et, T att, Descriptor attributeDescriptor, EntityMappingHelper mapper) {
35: this.et = et;
36: this.attribute = att;
37: this.attributeDescriptor = attributeDescriptor;
38: this.mapper = mapper;
39: }
40:
41: static <X> FieldStrategy<? extends FieldSpecification<? super X, ?>, X> createFieldStrategy(
42: EntityType<X> et, FieldSpecification<? super X, ?> att,
43: Descriptor fieldDescriptor, EntityMappingHelper mapper) {
44:• if (att instanceof TypesSpecification) {
45: return new TypesFieldStrategy<>(et, (TypesSpecification<? super X, ?>) att, fieldDescriptor, mapper);
46:• } else if (att instanceof PropertiesSpecification) {
47: return new PropertiesFieldStrategy<>(et, (PropertiesSpecification<? super X, ?, ?, ?>) att, fieldDescriptor,
48: mapper);
49: }
50: final Attribute<? super X, ?> attribute = (Attribute<? super X, ?>) att;
51:• if (attribute.isCollection()) {
52:• switch (attribute.getPersistentAttributeType()) {
53: case ANNOTATION:
54: case DATA:
55: throw new NotYetImplementedException();
56: case OBJECT:
57: return createPluralObjectPropertyStrategy(et, (PluralAttribute<? super X, ?, ?>) attribute,
58: fieldDescriptor, mapper);
59: default:
60: break;
61: }
62: } else {
63:• switch (attribute.getPersistentAttributeType()) {
64: case ANNOTATION:
65: return new SingularAnnotationPropertyStrategy<>(et, attribute, fieldDescriptor, mapper);
66: case DATA:
67: return new SingularDataPropertyStrategy<>(et, attribute, fieldDescriptor, mapper);
68: case OBJECT:
69: return new SingularObjectPropertyStrategy<>(et, attribute, fieldDescriptor, mapper);
70: default:
71: break;
72: }
73: }
74: // Shouldn't happen
75: throw new IllegalArgumentException();
76: }
77:
78: private static <Y> FieldStrategy<? extends FieldSpecification<? super Y, ?>, Y> createPluralObjectPropertyStrategy(
79: EntityType<Y> et, PluralAttribute<? super Y, ?, ?> attribute,
80: Descriptor descriptor, EntityMappingHelper mapper) {
81:• switch (attribute.getCollectionType()) {
82: case LIST:
83: return createOwlListPropertyStrategy(et, (ListAttribute<? super Y, ?>) attribute, descriptor, mapper);
84: case COLLECTION:
85: case SET:
86: return new SimpleSetPropertyStrategy<>(et, attribute, descriptor, mapper);
87: default:
88: throw new NotYetImplementedException(
89: "Unsupported plural attribute collection type " + attribute.getCollectionType());
90: }
91: }
92:
93: private static <Y> FieldStrategy<? extends FieldSpecification<? super Y, ?>, Y> createOwlListPropertyStrategy(
94: EntityType<Y> et, ListAttribute<? super Y, ?> attribute, Descriptor descriptor,
95: EntityMappingHelper mapper) {
96:• switch (attribute.getSequenceType()) {
97: case referenced:
98: return new ReferencedListPropertyStrategy<>(et, attribute, descriptor, mapper);
99: case simple:
100: return new SimpleListPropertyStrategy<>(et, attribute, descriptor, mapper);
101: default:
102: throw new NotYetImplementedException(
103: "Unsupported list attribute sequence type " + attribute.getSequenceType());
104: }
105: }
106:
107: void setCascadeResolver(CascadeResolver resolver) {
108: this.cascadeResolver = resolver;
109: }
110:
111: /**
112: * Sets the specified value on the specified instance, the field is taken from the attribute represented by this
113: * strategy. </p>
114: * <p>
115: * Note that this method assumes the value and the field are of compatible types, no check is done here.
116: */
117: void setValueOnInstance(Object instance, Object value) {
118: EntityPropertiesUtils.setFieldValue(attribute.getJavaField(), instance, value);
119: }
120:
121: /**
122: * Extracts the attribute value from the specified instance. </p>
123: *
124: * @return Attribute value, possibly {@code null}
125: */
126: Object extractFieldValueFromInstance(Object instance) throws IllegalAccessException {
127: return EntityPropertiesUtils.getAttributeValue(attribute, instance);
128: }
129:
130: <E> URI resolveValueIdentifier(E instance, EntityType<E> valEt) {
131: URI id = EntityPropertiesUtils.getPrimaryKey(instance, valEt);
132:• if (id == null) {
133: id = mapper.generateIdentifier(valEt);
134: EntityPropertiesUtils.setPrimaryKey(id, instance, valEt);
135: }
136: return id;
137: }
138:
139: URI getAttributeContext() {
140: return attributeDescriptor.getContext();
141: }
142:
143: /**
144: * Adds value from the specified axioms to this strategy. </p>
145: * <p>
146: * The value(s) is/are then set on entity field using {@link #buildInstanceFieldValue(Object)}.
147: *
148: * @param ax Axiom to extract value from
149: */
150: abstract void addValueFromAxiom(Axiom<?> ax);
151:
152: /**
153: * Sets instance field from values gathered in this strategy.
154: *
155: * @param instance The instance to receive the field value
156: * @throws IllegalArgumentException Access error
157: * @throws IllegalAccessException Access error
158: */
159: abstract void buildInstanceFieldValue(Object instance) throws IllegalAccessException;
160:
161: /**
162: * Extracts values of field represented by this strategy from the specified instance.
163: *
164: * @param instance The instance to extract values from
165: * @param valueBuilder Builder into which the attribute value(s) are extracted
166: * @throws IllegalArgumentException Access error
167: * @throws IllegalAccessException Access error
168: */
169: abstract void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder)
170: throws IllegalAccessException;
171:
172: /**
173: * Creates property assertion appropriate for the attribute represented by this strategy.
174: *
175: * @return Property assertion
176: */
177: abstract Assertion createAssertion();
178: }