Skip to content

Method: createListValueDescriptor(Object)

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.Descriptor;
21: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
22: import cz.cvut.kbss.jopa.model.metamodel.ListAttributeImpl;
23: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
24: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
25: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
26: import cz.cvut.kbss.ontodriver.model.Axiom;
27: import cz.cvut.kbss.ontodriver.model.NamedResource;
28:
29: import java.util.Collection;
30: import java.util.List;
31:
32: class ReferencedListPropertyStrategy<X> extends
33: ListPropertyStrategy<ReferencedListDescriptor, ReferencedListValueDescriptor<NamedResource>, X> {
34:
35: ReferencedListPropertyStrategy(EntityType<X> et, ListAttributeImpl<? super X, ?> att, Descriptor descriptor,
36: EntityMappingHelper mapper) {
37: super(et, att, descriptor, mapper);
38: }
39:
40: @Override
41: void addAxiomValue(Axiom<?> ax) {
42: final ReferencedListDescriptor listDescriptor = createListDescriptor(ax);
43: final Collection<Axiom<?>> sequence = mapper.loadReferencedList(listDescriptor);
44: sequence.stream()
45: .filter(a -> a.getAssertion().getIdentifier().equals(attribute.getHasContentsPropertyIRI().toURI()))
46: .forEach(super::addAxiomValue);
47: }
48:
49: ReferencedListDescriptor createListDescriptor(Axiom<?> ax) {
50: final NamedResource owner = ax.getSubject();
51: final ReferencedListDescriptor listDescriptor = ListDescriptorFactory.createReferencedListDescriptor(owner, attribute);
52: listDescriptor.setContext(getAttributeWriteContext());
53: return listDescriptor;
54: }
55:
56: @Override
57: <K> void extractListValues(List<K> list, X instance, AxiomValueGatherer valueBuilder) {
58: final ReferencedListValueDescriptor<K> listDescriptor = createListValueDescriptor(instance);
59: final List<K> pendingItems = resolveUnpersistedItems(list);
60: if (!pendingItems.isEmpty()) {
61: pendingItems.forEach(item -> referenceSavingResolver.registerPendingReference(item, listDescriptor, list));
62: return;
63: }
64: addListElementsToListValueDescriptor(listDescriptor, list);
65: valueBuilder.addReferencedListValues(listDescriptor);
66: }
67:
68: <V> ReferencedListValueDescriptor<V> createListValueDescriptor(X instance) {
69: final NamedResource owner = NamedResource.create(EntityPropertiesUtils.getIdentifier(instance, et));
70: final ReferencedListValueDescriptor<V> descriptor = ListDescriptorFactory.createReferencedListValueDescriptor(owner, attribute);
71: descriptor.setContext(getAttributeWriteContext());
72: return descriptor;
73: }
74: }