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