Skip to content

Method: mergeChangesFromChangeSet(UnitOfWorkChangeSet)

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.sessions.change.Change;
21: import cz.cvut.kbss.jopa.sessions.change.ChangeRecord;
22: import cz.cvut.kbss.jopa.sessions.change.ObjectChangeSet;
23: import cz.cvut.kbss.jopa.sessions.change.UnitOfWorkChangeSet;
24: import cz.cvut.kbss.jopa.sessions.util.CloneConfiguration;
25: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
26:
27: import java.util.Objects;
28:
29: /**
30: * Merges changes that are made to clones to the registered original objects and live object cache.
31: */
32: class MergeManager {
33:
34: private final AbstractUnitOfWork uow;
35:
36: private final CloneBuilder builder;
37:
38: MergeManager(AbstractUnitOfWork session, CloneBuilder cloneBuilder) {
39: this.uow = session;
40: this.builder = cloneBuilder;
41: }
42:
43: private void deleteObjectFromCache(Change changeSet) {
44: Object toDelete = changeSet.getOriginal();
45: assert toDelete != null;
46: uow.removeObjectFromCache(toDelete, changeSet.getEntityContext());
47: }
48:
49: /**
50: * Merge changes from one {@link Change}, which represents the changes made to clone, into the original object.
51: *
52: * @param changeSet ObjectChangeSet containing changes on a single object
53: */
54: public void mergeChangesOnObject(ObjectChangeSet changeSet) {
55: Objects.requireNonNull(changeSet);
56: final Object clone = changeSet.getClone();
57: assert clone != null && changeSet.getOriginal() != null;
58: builder.mergeChanges(changeSet);
59: updateCache(changeSet);
60: }
61:
62: private void updateCache(ObjectChangeSet changeSet) {
63: final Object changedObject = changeSet.getOriginal();
64: final Object identifier = EntityPropertiesUtils.getIdentifier(changedObject, uow.getMetamodel());
65: boolean preventCaching = changeSet.getChanges().stream().anyMatch(ChangeRecord::doesPreventCaching);
66: if (preventCaching) {
67: uow.removeObjectFromCache(changedObject, changeSet.getEntityContext());
68: } else {
69: uow.putObjectIntoCache(identifier, changedObject, changeSet.getDescriptor());
70: }
71: }
72:
73: /**
74: * Merge changes from the provided {@link UnitOfWorkChangeSet}.
75: *
76: * @param changeSet Change set from a single Unit of Work
77: */
78: public void mergeChangesFromChangeSet(UnitOfWorkChangeSet changeSet) {
79: Objects.requireNonNull(changeSet);
80: changeSet.getNewObjects().forEach(this::mergeNewObject);
81: changeSet.getDeletedObjects().forEach(this::deleteObjectFromCache);
82:• for (ObjectChangeSet objectChangeSet : changeSet.getExistingObjectsChanges()) {
83: mergeChangesOnObject(objectChangeSet);
84: }
85: }
86:
87: /**
88: * Merge a newly created object represented by an {@link Change} into the shared live object cache.
89: *
90: * @param changeSet ObjectChangeSet representing the new object
91: */
92: public void mergeNewObject(Change changeSet) {
93: Objects.requireNonNull(changeSet);
94: final Object toCache = builder.buildClone(changeSet.getClone(), CloneConfiguration.withDescriptor(changeSet.getDescriptor()));
95: final Object identifier = EntityPropertiesUtils.getIdentifier(toCache, uow.getMetamodel());
96: uow.registerOriginalForNewClone(changeSet.getClone(), toCache);
97: // Put the original object into the live object cache
98: uow.putObjectIntoCache(identifier, toCache, changeSet.getDescriptor());
99: }
100: }