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