Skip to content

Package: RefreshInstanceMerger

RefreshInstanceMerger

nameinstructionbranchcomplexitylinemethod
RefreshInstanceMerger(IndirectWrapperHelper)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
mergeChanges(ObjectChangeSet)
M: 0 C: 54
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 12
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;
19:
20: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
21: import cz.cvut.kbss.jopa.proxy.change.ChangeTrackingIndirectCollection;
22: import cz.cvut.kbss.jopa.sessions.change.ChangeRecord;
23: import cz.cvut.kbss.jopa.sessions.change.ObjectChangeSet;
24: import cz.cvut.kbss.jopa.sessions.merge.DefaultValueMerger;
25: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
26:
27: /**
28: * Merges changes during instance refresh.
29: * <p>
30: * This means overwriting any changes made to the entity.
31: */
32: class RefreshInstanceMerger {
33:
34: private final DefaultValueMerger merger;
35:
36: private final IndirectWrapperHelper indirectWrapperHelper;
37:
38: RefreshInstanceMerger(IndirectWrapperHelper indirectWrapperHelper) {
39: this.indirectWrapperHelper = indirectWrapperHelper;
40: this.merger = new DefaultValueMerger();
41: }
42:
43: /**
44: * Merges changes in the opposite direction, i.e. changes made on the clone are overwritten by the original values.
45: *
46: * @param changeSet Changes done
47: */
48: void mergeChanges(ObjectChangeSet changeSet) {
49: final Object source = changeSet.getOriginal();
50: final Object target = changeSet.getClone();
51:• for (ChangeRecord change : changeSet.getChanges()) {
52: final FieldSpecification<?, ?> att = change.getAttribute();
53: final Object sourceValue = EntityPropertiesUtils.getAttributeValue(att, source);
54:• if (sourceValue instanceof ChangeTrackingIndirectCollection<?> col) {
55: final Object ic = indirectWrapperHelper.createIndirectWrapper(col.unwrap(), target, att.getJavaField());
56: merger.mergeValue(att, target, ic);
57: } else {
58: merger.mergeValue(att, target, sourceValue);
59: }
60: }
61: }
62: }