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: 58
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 14
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.sessions;
14:
15: import cz.cvut.kbss.jopa.adapters.IndirectCollection;
16: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
17: import cz.cvut.kbss.jopa.sessions.merge.DefaultValueMerger;
18: import cz.cvut.kbss.jopa.sessions.merge.ValueMerger;
19: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
20:
21: /**
22: * Merges changes during instance refresh.
23: * <p>
24: * This means overwriting any changes made to the entity.
25: */
26: class RefreshInstanceMerger {
27:
28: private final ValueMerger merger;
29:
30: private final IndirectWrapperHelper indirectWrapperHelper;
31:
32: RefreshInstanceMerger(IndirectWrapperHelper indirectWrapperHelper) {
33: this.indirectWrapperHelper = indirectWrapperHelper;
34: this.merger = new DefaultValueMerger();
35: }
36:
37: /**
38: * Merges changes in the opposite direction, i.e. changes made on the clone are overwritten by the original values.
39: *
40: * @param changeSet Changes done
41: */
42: void mergeChanges(ObjectChangeSet changeSet) {
43: final Object source = changeSet.getChangedObject();
44: final Object target = changeSet.getCloneObject();
45:• for (ChangeRecord change : changeSet.getChanges()) {
46: final FieldSpecification<?, ?> att = change.getAttribute();
47: final Object sourceValue = EntityPropertiesUtils.getAttributeValue(att, source);
48:• if (sourceValue instanceof IndirectCollection) {
49: final IndirectCollection col = (IndirectCollection) sourceValue;
50: final Object ic = indirectWrapperHelper
51: .createIndirectWrapper(col.unwrap(), target, att.getJavaField());
52: merger.mergeValue(att, target, null, ic, null);
53: } else {
54: merger.mergeValue(att, target, null, sourceValue, null);
55: }
56: }
57: }
58: }