Skip to contentMethod: deleteObjectFromCache(ObjectChangeSet)
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.net.URI;
21:
22: public class MergeManagerImpl implements MergeManager {
23:
24:         // The UnitOfWork instance for this MergeManager
25:         protected UnitOfWorkImpl uow;
26:
27:         protected CloneBuilder builder;
28:
29:         public MergeManagerImpl(UnitOfWorkImpl session) {
30:                 this.uow = session;
31:                 this.builder = new CloneBuilderImpl(session);
32:         }
33:
34:         protected void deleteObjectFromCache(ObjectChangeSet changeSet) {
35:                 Object original = changeSet.getChangedObject();
36:•                if (original != null) {
37:                         uow.removeObjectFromCache(original, changeSet.getEntityContext());
38:                 }
39:         }
40:
41:         public Object mergeChangesOnObject(Object clone, ObjectChangeSet changeSet) {
42:                 if (changeSet == null) {
43:                         throw new OWLPersistenceException("Change Set in Merge Manager null.");
44:                 }
45:                 if (clone == null) {
46:                         return null;
47:                 }
48:
49:                 Object original = changeSet.getChangedObject();
50:                 if (original == null) {
51:                         // If the original is null, then we may have a new object
52:                         // but this should not happen since new objects are handled
53:                         // separately
54:                         if (uow.isObjectNew(clone)) {
55:                                 mergeNewObject(changeSet);
56:                         } else {
57:                                 throw new OWLPersistenceException("Cannot find the original object.");
58:                         }
59:                 } else {
60:                         this.builder.mergeChanges(original, changeSet);
61:                 }
62:                 return clone;
63:         }
64:
65:         public void mergeChangesFromChangeSet(UnitOfWorkChangeSet changeSet) {
66:                 for (ObjectChangeSet objectChangeSet : changeSet.getExistingObjectsChanges()) {
67:                                 Object clone = objectChangeSet.getCloneObject();
68:                                 mergeChangesOnObject(clone, objectChangeSet);
69:                 }
70:                 changeSet.getNewObjects().forEach(this::mergeNewObject);
71:                 changeSet.getDeletedObjects().forEach(this::deleteObjectFromCache);
72:
73:         }
74:
75:         public void mergeNewObject(ObjectChangeSet changeSet) {
76:                 if (changeSet == null) {
77:                         return;
78:                 }
79:                 Object clone = changeSet.getCloneObject();
80:                 if (!changeSet.isNew()) {
81:                         mergeChangesOnObject(clone, changeSet);
82:                 }
83:                 // Put the original object into the shared session cache
84:                 Object newObject = changeSet.getChangedObject();
85:                 final Object primaryKey = EntityPropertiesUtils.getPrimaryKey(newObject, uow.getMetamodel());
86:                 final URI context = changeSet.getEntityContext();
87:                 uow.putObjectIntoCache(primaryKey, newObject, context);
88:         }
89:
90: }