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