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