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