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

Coverage

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