-
Notifications
You must be signed in to change notification settings - Fork 14
/
FieldStrategy.java
176 lines (159 loc) · 7.36 KB
/
FieldStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.oom;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.metamodel.*;
import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
import cz.cvut.kbss.ontodriver.exception.NotYetImplementedException;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import java.net.URI;
abstract class FieldStrategy<T extends FieldSpecification<? super X, ?>, X> {
final EntityType<X> et;
final T attribute;
final Descriptor attributeDescriptor;
final EntityMappingHelper mapper;
CascadeResolver cascadeResolver;
FieldStrategy(EntityType<X> et, T att, Descriptor attributeDescriptor, EntityMappingHelper mapper) {
this.et = et;
this.attribute = att;
this.attributeDescriptor = attributeDescriptor;
this.mapper = mapper;
}
static <X> FieldStrategy<? extends FieldSpecification<? super X, ?>, X> createFieldStrategy(
EntityType<X> et, FieldSpecification<? super X, ?> att,
Descriptor fieldDescriptor, EntityMappingHelper mapper) {
if (att instanceof TypesSpecification) {
return new TypesFieldStrategy<>(et, (TypesSpecification<? super X, ?>) att, fieldDescriptor, mapper);
} else if (att instanceof PropertiesSpecification) {
return new PropertiesFieldStrategy<>(et, (PropertiesSpecification<? super X, ?, ?, ?>) att, fieldDescriptor,
mapper);
}
final Attribute<? super X, ?> attribute = (Attribute<? super X, ?>) att;
if (attribute.isCollection()) {
switch (attribute.getPersistentAttributeType()) {
case ANNOTATION:
case DATA:
throw new NotYetImplementedException();
case OBJECT:
return createPluralObjectPropertyStrategy(et, (PluralAttribute<? super X, ?, ?>) attribute,
fieldDescriptor, mapper);
default:
break;
}
} else {
switch (attribute.getPersistentAttributeType()) {
case ANNOTATION:
return new SingularAnnotationPropertyStrategy<>(et, attribute, fieldDescriptor, mapper);
case DATA:
return new SingularDataPropertyStrategy<>(et, attribute, fieldDescriptor, mapper);
case OBJECT:
return new SingularObjectPropertyStrategy<>(et, attribute, fieldDescriptor, mapper);
default:
break;
}
}
// Shouldn't happen
throw new IllegalArgumentException();
}
private static <Y> FieldStrategy<? extends FieldSpecification<? super Y, ?>, Y> createPluralObjectPropertyStrategy(
EntityType<Y> et, PluralAttribute<? super Y, ?, ?> attribute,
Descriptor descriptor, EntityMappingHelper mapper) {
switch (attribute.getCollectionType()) {
case LIST:
return createOwlListPropertyStrategy(et, (ListAttribute<? super Y, ?>) attribute, descriptor, mapper);
case COLLECTION:
case SET:
return new SimpleSetPropertyStrategy<>(et, attribute, descriptor, mapper);
default:
throw new NotYetImplementedException(
"Unsupported plural attribute collection type " + attribute.getCollectionType());
}
}
private static <Y> FieldStrategy<? extends FieldSpecification<? super Y, ?>, Y> createOwlListPropertyStrategy(
EntityType<Y> et, ListAttribute<? super Y, ?> attribute, Descriptor descriptor,
EntityMappingHelper mapper) {
switch (attribute.getSequenceType()) {
case referenced:
return new ReferencedListPropertyStrategy<>(et, attribute, descriptor, mapper);
case simple:
return new SimpleListPropertyStrategy<>(et, attribute, descriptor, mapper);
default:
throw new NotYetImplementedException(
"Unsupported list attribute sequence type " + attribute.getSequenceType());
}
}
void setCascadeResolver(CascadeResolver resolver) {
this.cascadeResolver = resolver;
}
/**
* Sets the specified value on the specified instance, the field is taken from the attribute represented by this
* strategy. </p>
* <p>
* Note that this method assumes the value and the field are of compatible types, no check is done here.
*/
void setValueOnInstance(Object instance, Object value) {
EntityPropertiesUtils.setFieldValue(attribute.getJavaField(), instance, value);
}
/**
* Extracts the attribute value from the specified instance. </p>
*
* @return Attribute value, possibly {@code null}
*/
Object extractFieldValueFromInstance(Object instance) throws IllegalAccessException {
return EntityPropertiesUtils.getAttributeValue(attribute, instance);
}
<E> URI resolveValueIdentifier(E instance, EntityType<E> valEt) {
URI id = EntityPropertiesUtils.getPrimaryKey(instance, valEt);
if (id == null) {
id = mapper.generateIdentifier(valEt);
EntityPropertiesUtils.setPrimaryKey(id, instance, valEt);
}
return id;
}
URI getAttributeContext() {
return attributeDescriptor.getContext();
}
/**
* Adds value from the specified axioms to this strategy. </p>
* <p>
* The value(s) is/are then set on entity field using {@link #buildInstanceFieldValue(Object)}.
*
* @param ax Axiom to extract value from
*/
abstract void addValueFromAxiom(Axiom<?> ax);
/**
* Sets instance field from values gathered in this strategy.
*
* @param instance The instance to receive the field value
* @throws IllegalArgumentException Access error
* @throws IllegalAccessException Access error
*/
abstract void buildInstanceFieldValue(Object instance) throws IllegalAccessException;
/**
* Extracts values of field represented by this strategy from the specified instance.
*
* @param instance The instance to extract values from
* @param valueBuilder Builder into which the attribute value(s) are extracted
* @throws IllegalArgumentException Access error
* @throws IllegalAccessException Access error
*/
abstract void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder)
throws IllegalAccessException;
/**
* Creates property assertion appropriate for the attribute represented by this strategy.
*
* @return Property assertion
*/
abstract Assertion createAssertion();
}