Skip to content

Package: ManagedTypeValueMerger

ManagedTypeValueMerger

nameinstructionbranchcomplexitylinemethod
ManagedTypeValueMerger(UnitOfWork)
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%
getValueToSet(Object, Descriptor)
M: 0 C: 37
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
mergeValue(Object, ChangeRecord, Descriptor)
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
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.sessions.merge;
19:
20: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
21: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
22: import cz.cvut.kbss.jopa.sessions.change.ChangeRecord;
23: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
24:
25: class ManagedTypeValueMerger implements ValueMerger {
26:
27: private final UnitOfWork uow;
28:
29: ManagedTypeValueMerger(UnitOfWork uow) {
30: this.uow = uow;
31: }
32:
33: @Override
34: public void mergeValue(Object target, ChangeRecord changeRecord, Descriptor attributeDescriptor) {
35: final Object mergedValue = changeRecord.getNewValue();
36: final Object toSet = getValueToSet(mergedValue, attributeDescriptor);
37: EntityPropertiesUtils.setFieldValue(changeRecord.getAttribute().getJavaField(), target, toSet);
38: // Replace the value in the change record as the mergedValue may not have been managed
39: changeRecord.setNewValue(toSet);
40: }
41:
42: Object getValueToSet(Object mergedValue, Descriptor descriptor) {
43:• if (mergedValue == null) {
44: return null;
45: }
46:• if (uow.contains(mergedValue)) {
47: return mergedValue;
48: }
49: final Object identifier = EntityPropertiesUtils.getIdentifier(mergedValue, uow.getMetamodel());
50:• if (identifier == null) {
51: return mergedValue;
52: }
53: final Class<?> type = mergedValue.getClass();
54: final Object managedInstance = uow.readObject(type, identifier, descriptor);
55: // If the object cannot be found, it is a new one (not yet registered), which we can assign directly
56:• return managedInstance != null ? managedInstance : mergedValue;
57: }
58: }