Skip to content

Package: CollectionValueMerger

CollectionValueMerger

nameinstructionbranchcomplexitylinemethod
CollectionValueMerger(UnitOfWorkImpl, ManagedTypeValueMerger)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
extendModuleExtractionSignature(FieldSpecification, Collection)
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
isElementTypeManaged(FieldSpecification)
M: 0 C: 14
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
mergeValue(Object, ChangeRecord, Descriptor)
M: 0 C: 56
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 14
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2022 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.sessions.merge;
16:
17: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.metamodel.CollectionType;
19: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
20: import cz.cvut.kbss.jopa.model.metamodel.PluralAttribute;
21: import cz.cvut.kbss.jopa.model.metamodel.TypesSpecification;
22: import cz.cvut.kbss.jopa.sessions.ChangeRecord;
23: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
24: import cz.cvut.kbss.jopa.utils.CollectionFactory;
25: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
26: import cz.cvut.kbss.jopa.utils.MetamodelUtils;
27:
28: import java.util.Collection;
29:
30: class CollectionValueMerger implements ValueMerger {
31:
32: private final UnitOfWorkImpl uow;
33: private final ManagedTypeValueMerger managedTypeMerger;
34:
35: CollectionValueMerger(UnitOfWorkImpl uow, ManagedTypeValueMerger managedTypeMerger) {
36: this.uow = uow;
37: this.managedTypeMerger = managedTypeMerger;
38: }
39:
40: @Override
41: public void mergeValue(Object target, ChangeRecord changeRecord, Descriptor attributeDescriptor) {
42: final FieldSpecification<?, ?> att = changeRecord.getAttribute();
43: final Collection<?> mergedCol = (Collection<?>) changeRecord.getNewValue();
44:• if (mergedCol == null) {
45: EntityPropertiesUtils.setFieldValue(att.getJavaField(), target, null);
46: return;
47: }
48:
49: final Collection<Object> newValue = CollectionFactory
50: .createDefaultCollection(CollectionType.fromClass(att.getJavaType()));
51: boolean elemTypeManaged = isElementTypeManaged(att);
52:• for (Object item : mergedCol) {
53:• newValue.add(elemTypeManaged ? managedTypeMerger.getValueToSet(item, attributeDescriptor) : item);
54: }
55: extendModuleExtractionSignature(att, newValue);
56: EntityPropertiesUtils.setFieldValue(att.getJavaField(), target, newValue);
57: }
58:
59: private boolean isElementTypeManaged(FieldSpecification<?, ?> att) {
60:• return att instanceof PluralAttribute && uow.isEntityType(((PluralAttribute<?, ?, ?>) att).getBindableJavaType());
61: }
62:
63: private void extendModuleExtractionSignature(FieldSpecification<?, ?> att, Collection<?> value) {
64:• if (att instanceof TypesSpecification) {
65: MetamodelUtils.checkForModuleSignatureExtension(value, uow.getMetamodel());
66: }
67: }
68: }