Skip to content

Package: ReferenceSavingResolver

ReferenceSavingResolver

nameinstructionbranchcomplexitylinemethod
ReferenceSavingResolver(ObjectOntologyMapperImpl)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
registerPendingReference(NamedResource, Assertion, Object, URI)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
registerPendingReference(Object, ListValueDescriptor, List)
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%
shouldSaveReference(Object, Set)
M: 0 C: 15
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
shouldSaveReferenceToItem(Object, Set)
M: 4 C: 41
91%
M: 1 C: 9
90%
M: 1 C: 5
83%
M: 0 C: 6
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.oom;
19:
20: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
21: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
22: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
23: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
24: import cz.cvut.kbss.ontodriver.descriptor.ListValueDescriptor;
25: import cz.cvut.kbss.ontodriver.model.Assertion;
26: import cz.cvut.kbss.ontodriver.model.NamedResource;
27:
28: import java.net.URI;
29: import java.util.List;
30: import java.util.Set;
31:
32: class ReferenceSavingResolver {
33:
34: private final ObjectOntologyMapperImpl mapper;
35:
36: ReferenceSavingResolver(ObjectOntologyMapperImpl mapper) {
37: this.mapper = mapper;
38: }
39:
40: /**
41: * Checks whether an object property assertion (reference to the target instance) should be inserted into the storage.
42: * <p>
43: * A reference should be saved if:
44: * <ul>
45: * <li>The value is {@code null},</li>
46: * <li>The value is a plain identifier,</li>
47: * <li>The value is already managed,</li>
48: * <li>The value is not managed, but exists in the storage.</li>
49: * </ul>
50: * <p>
51: * Otherwise, the reference should not be saved and should be registered as pending.
52: *
53: * @param value The value to save
54: * @param contexts Storage contexts
55: * @return Whether to save the corresponding assertion or not
56: */
57: boolean shouldSaveReference(Object value, Set<URI> contexts) {
58:• return value == null || IdentifierTransformer.isValidIdentifierType(value.getClass()) || shouldSaveReferenceToItem(value, contexts);
59: }
60:
61: /**
62: * Same as {@link #shouldSaveReference(Object, Set)}, but skips null-check and check whether the value is a plain identifier.
63: * <p>
64: * Used for collections.
65: */
66: boolean shouldSaveReferenceToItem(Object value, Set<URI> contexts) {
67:• if (mapper.isManaged(value) || value.getClass().isEnum()) {
68: return true;
69: }
70: final EntityType<?> et = mapper.getEntityType(value.getClass());
71:• assert et != null;
72: final URI identifier = EntityPropertiesUtils.getIdentifier(value, et);
73:• return identifier != null && mapper.containsEntity(et.getJavaType(), identifier, new EntityDescriptor(contexts));
74: }
75:
76: /**
77: * Registers a pending assertion in the mapper.
78: * <p>
79: * Before commit, all pending assertions have to be resolved, otherwise the commit fails.
80: *
81: * @param subject Subject of the assertion
82: * @param assertion Assertion representing the property
83: * @param object Value of the assertion (object)
84: * @param context Context, into which the assertion should be saved
85: */
86: void registerPendingReference(NamedResource subject, Assertion assertion, Object object, URI context) {
87: mapper.registerPendingAssertion(subject, assertion, object, context);
88: }
89:
90: /**
91: * Registers a pending reference to a list (simple or referenced).
92: * <p>
93: * This means that at least one item in the list has not yet been persisted. Since the list is linked, all items
94: * must be persisted before the list itself is inserted into the storage.
95: *
96: * @param item Pending list item
97: * @param listDescriptor Descriptor of the list
98: * @param values The whole list
99: */
100: void registerPendingReference(Object item, ListValueDescriptor listDescriptor, List<?> values) {
101: mapper.registerPendingListReference(item, listDescriptor, values);
102: }
103: }