Skip to content

Method: DetachedInstanceMerger(UnitOfWorkImpl)

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.model.descriptors.Descriptor;
21: import cz.cvut.kbss.jopa.sessions.merge.DetachedValueMerger;
22: import cz.cvut.kbss.jopa.sessions.merge.ValueMerger;
23:
24: class DetachedInstanceMerger {
25:
26: private final ValueMerger valueMerger;
27:
28: DetachedInstanceMerger(UnitOfWorkImpl uow) {
29: this.valueMerger = new DetachedValueMerger(uow);
30: }
31:
32: /**
33: * Merges changes from the detached instance being merged into persistence context (clone in the specified change
34: * set) into the corresponding managed instance (original in the change set).
35: *
36: * @param changeSet Set of changes to apply to the managed instance
37: * @return Managed instance with changes merged into it
38: */
39: Object mergeChangesFromDetachedToManagedInstance(ObjectChangeSet changeSet, Descriptor descriptor) {
40: assert changeSet != null;
41: assert changeSet.getCloneObject() != null;
42: final Object target = changeSet.getChangedObject();
43: assert target != null;
44:
45: for (ChangeRecord change : changeSet.getChanges()) {
46: valueMerger.mergeValue(target, change, descriptor.getAttributeDescriptor(change.getAttribute()));
47: }
48: return target;
49: }
50: }