Skip to content

Method: shouldSaveReference(Class, Object, URI)

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.EntityDescriptor;
18: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
19: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
20: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
21: import cz.cvut.kbss.ontodriver.descriptor.ListValueDescriptor;
22: import cz.cvut.kbss.ontodriver.model.Assertion;
23: import cz.cvut.kbss.ontodriver.model.NamedResource;
24:
25: import java.net.URI;
26: import java.util.List;
27:
28: class ReferenceSavingResolver {
29:
30: private final ObjectOntologyMapperImpl mapper;
31:
32: ReferenceSavingResolver(ObjectOntologyMapperImpl mapper) {
33: this.mapper = mapper;
34: }
35:
36: /**
37: * Checks whether an object property assertion (reference to the target instance) should be inserted into the storage.
38: * <p>
39: * A reference should be saved if:
40: * <ul>
41: * <li>The value is {@code null},</li>
42: * <li>The value is a plain identifier,</li>
43: * <li>The value is already managed,</li>
44: * <li>The value is not managed, but exists in the storage.</li>
45: * </ul>
46: * <p>
47: * Otherwise, the reference should not be saved and should be registered as pending.
48: *
49: * @param valueType Java type of the value
50: * @param value The value to save
51: * @param context Storage context
52: * @return Whether to save the corresponding assertion or not
53: */
54: boolean shouldSaveReference(Class<?> valueType, Object value, URI context) {
55:• return value == null || IdentifierTransformer.isValidIdentifierType(valueType) || shouldSaveReferenceToItem(
56: valueType, value, context);
57: }
58:
59: /**
60: * Same as {@link #shouldSaveReference(Class, Object, URI)}, but skips null-check and check whether the value is a plain identifier.
61: * <p>
62: * Used for collections.
63: */
64: boolean shouldSaveReferenceToItem(Class<?> valueType, Object value, URI context) {
65: if (mapper.isManaged(value)) {
66: return true;
67: }
68: final EntityType<?> et = mapper.getEntityType(valueType);
69: assert et != null;
70: final URI identifier = EntityPropertiesUtils.getIdentifier(value, et);
71: return identifier != null && mapper.containsEntity(et.getJavaType(), identifier, new EntityDescriptor(context));
72: }
73:
74: /**
75: * Registers a pending assertion in the mapper.
76: * <p>
77: * Before commit, all pending assertions have to be resolved, otherwise the commit fails.
78: *
79: * @param subject Subject of the assertion
80: * @param assertion Assertion representing the property
81: * @param object Value of the assertion (object)
82: * @param context Context, into which the assertion should be saved
83: */
84: void registerPendingReference(NamedResource subject, Assertion assertion, Object object, URI context) {
85: mapper.registerPendingAssertion(subject, assertion, object, context);
86: }
87:
88: /**
89: * Registers a pending reference to a list (simple or referenced).
90: * <p>
91: * This means that at least one item in the list has not yet been persisted. Since the list is linked, all items
92: * must be persisted before the list itself is inserted into the storage.
93: *
94: * @param item Pending list item
95: * @param listDescriptor Descriptor of the list
96: * @param values The whole list
97: */
98: void registerPendingReference(Object item, ListValueDescriptor listDescriptor, List<?> values) {
99: mapper.registerPendingListReference(item, listDescriptor, values);
100: }
101: }