Skip to content

Method: MergeManagerImpl(UnitOfWorkImpl)

1: /**
2: * Copyright (C) 2020 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.exceptions.OWLPersistenceException;
18: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
19:
20: import java.util.Objects;
21:
22: public class MergeManagerImpl implements MergeManager {
23:
24: protected UnitOfWorkImpl uow;
25:
26: protected CloneBuilder builder;
27:
28: MergeManagerImpl(UnitOfWorkImpl session) {
29: this.uow = session;
30: this.builder = session.getCloneBuilder();
31: }
32:
33: private void deleteObjectFromCache(ObjectChangeSet changeSet) {
34: Object toDelete = changeSet.getChangedObject();
35: assert toDelete != null;
36: uow.removeObjectFromCache(toDelete, changeSet.getEntityContext());
37: }
38:
39: @Override
40: public Object mergeChangesOnObject(ObjectChangeSet changeSet) {
41: Objects.requireNonNull(changeSet);
42: final Object clone = changeSet.getCloneObject();
43: if (clone == null) {
44: return null;
45: }
46: if (changeSet.getChangedObject() == null) {
47: // If the original is null, then we may have a new object
48: // but this should not happen since new objects are handled separately
49: if (uow.isObjectNew(clone)) {
50: mergeNewObject(changeSet);
51: } else {
52: throw new OWLPersistenceException("Cannot find the original object.");
53: }
54: } else {
55: builder.mergeChanges(changeSet);
56: updateCache(changeSet);
57: }
58: return clone;
59: }
60:
61: private void updateCache(ObjectChangeSet changeSet) {
62: final Object changedObject = changeSet.getChangedObject();
63: final Object identifier = EntityPropertiesUtils.getIdentifier(changedObject, uow.getMetamodel());
64: if (changeSet.isNew()) {
65: uow.putObjectIntoCache(identifier, changedObject, changeSet.getEntityDescriptor());
66: } else {
67: boolean preventCaching = changeSet.getChanges().stream().anyMatch(ChangeRecord::doesPreventCaching);
68: if (preventCaching) {
69: uow.removeObjectFromCache(changedObject, changeSet.getEntityContext());
70: } else {
71: uow.putObjectIntoCache(identifier, changedObject, changeSet.getEntityDescriptor());
72: }
73: }
74: }
75:
76: @Override
77: public void mergeChangesFromChangeSet(UnitOfWorkChangeSet changeSet) {
78: Objects.requireNonNull(changeSet);
79: for (ObjectChangeSet objectChangeSet : changeSet.getExistingObjectsChanges()) {
80: mergeChangesOnObject(objectChangeSet);
81: }
82: changeSet.getNewObjects().forEach(this::mergeNewObject);
83: changeSet.getDeletedObjects().forEach(this::deleteObjectFromCache);
84:
85: }
86:
87: @Override
88: public void mergeNewObject(ObjectChangeSet changeSet) {
89: Objects.requireNonNull(changeSet);
90: if (!changeSet.isNew()) {
91: mergeChangesOnObject(changeSet);
92: return;
93: }
94: // Put the original object into the shared session cache
95: updateCache(changeSet);
96: }
97: }