Skip to content

Method: MergeManagerImpl(UnitOfWorkImpl)

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.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 original = changeSet.getChangedObject();
35: if (original != null) {
36: uow.removeObjectFromCache(original, changeSet.getEntityContext());
37: }
38: }
39:
40: @Override
41: public Object mergeChangesOnObject(ObjectChangeSet changeSet) {
42: Objects.requireNonNull(changeSet);
43: final Object clone = changeSet.getCloneObject();
44: if (clone == null) {
45: return null;
46: }
47:
48: final Object original = changeSet.getChangedObject();
49: if (original == 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: final Object identifier = EntityPropertiesUtils.getIdentifier(original, uow.getMetamodel());
60: uow.putObjectIntoCache(identifier, original, changeSet.getEntityDescriptor());
61: }
62: return clone;
63: }
64:
65: @Override
66: public void mergeChangesFromChangeSet(UnitOfWorkChangeSet changeSet) {
67: for (ObjectChangeSet objectChangeSet : changeSet.getExistingObjectsChanges()) {
68: mergeChangesOnObject(objectChangeSet);
69: }
70: changeSet.getNewObjects().forEach(this::mergeNewObject);
71: changeSet.getDeletedObjects().forEach(this::deleteObjectFromCache);
72:
73: }
74:
75: @Override
76: public void mergeNewObject(ObjectChangeSet changeSet) {
77: if (changeSet == null) {
78: return;
79: }
80: if (!changeSet.isNew()) {
81: mergeChangesOnObject(changeSet);
82: return;
83: }
84: // Put the original object into the shared session cache
85: Object newObject = changeSet.getChangedObject();
86: final Object identifier = EntityPropertiesUtils.getIdentifier(newObject, uow.getMetamodel());
87: uow.putObjectIntoCache(identifier, newObject, changeSet.getEntityDescriptor());
88: }
89: }