Skip to content

Package: ChangeManagerImpl

ChangeManagerImpl

nameinstructionbranchcomplexitylinemethod
ChangeManagerImpl(MetamodelProvider)
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
calculateChanges(ObjectChangeSet)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
calculateChangesInternal(ObjectChangeSet)
M: 0 C: 82
100%
M: 0 C: 11
100%
M: 0 C: 7
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
getFields(Class)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hasChanges(Object, Object)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
hasChangesInternal(Object, Object)
M: 4 C: 92
96%
M: 4 C: 15
79%
M: 4 C: 7
64%
M: 2 C: 22
92%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
valueChanged(Object, Object)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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.change;
16:
17: import cz.cvut.kbss.jopa.exceptions.OWLInferredAttributeModifiedException;
18: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
19: import cz.cvut.kbss.jopa.sessions.ChangeManager;
20: import cz.cvut.kbss.jopa.sessions.ChangeRecord;
21: import cz.cvut.kbss.jopa.sessions.MetamodelProvider;
22: import cz.cvut.kbss.jopa.sessions.ObjectChangeSet;
23: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
24: import cz.cvut.kbss.jopa.utils.ErrorUtils;
25: import org.slf4j.Logger;
26: import org.slf4j.LoggerFactory;
27:
28: import java.lang.reflect.Field;
29: import java.util.*;
30:
31: public class ChangeManagerImpl implements ChangeManager {
32:
33: private static final Logger LOG = LoggerFactory.getLogger(ChangeManagerImpl.class);
34:
35: private final Map<Object, Object> visitedObjects;
36:
37: private final MetamodelProvider metamodelProvider;
38: private final ChangeDetector changeDetector;
39:
40: public ChangeManagerImpl(MetamodelProvider metamodelProvider) {
41: this.metamodelProvider = metamodelProvider;
42: this.changeDetector = new ChangeDetectors(metamodelProvider, this);
43: visitedObjects = new IdentityHashMap<>();
44: }
45:
46: public boolean hasChanges(Object original, Object clone) {
47: LOG.trace("Checking for changes...");
48: boolean res = hasChangesInternal(original, clone);
49: visitedObjects.clear();
50: return res;
51: }
52:
53: /**
54: * This method does the actual check for changes. It is wrapped in the public method since the IdentityMap for
55: * visited objects has to be cleared after the whole check is done.
56: *
57: * @param original The original object.
58: * @param clone The clone that may have changed.
59: * @return True if the clone is in different state than the original.
60: */
61: boolean hasChangesInternal(Object original, Object clone) {
62:• if (clone == null && original == null) {
63: return false;
64: }
65:• if (clone == null || original == null) {
66: return true;
67: }
68:• if (visitedObjects.containsKey(clone)) {
69: return false;
70: }
71: final Class<?> cls = clone.getClass();
72: Map<Object, Object> composedObjects = new HashMap<>();
73:• for (FieldSpecification<?, ?> fs : getFields(cls)) {
74: final Field f = fs.getJavaField();
75: Object clVal = EntityPropertiesUtils.getFieldValue(f, clone);
76: Object origVal = EntityPropertiesUtils.getFieldValue(f, original);
77: final Changed ch = valueChanged(origVal, clVal);
78:• switch (ch) {
79: case TRUE:
80: return true;
81: case UNDETERMINED:
82: visitedObjects.put(clVal, clVal);
83: composedObjects.put(clVal, origVal);
84: break;
85: default:
86: break;
87: }
88: }
89: // First check all primitive values - performance, then do composed
90:• for (Object cl : composedObjects.keySet()) {
91:• if (hasChangesInternal(cl, composedObjects.get(cl))) {
92: return true;
93: }
94: }
95: return false;
96: }
97:
98: private <X> Set<FieldSpecification<? super X, ?>> getFields(Class<X> cls) {
99: return metamodelProvider.getMetamodel().entity(cls).getFieldSpecifications();
100: }
101:
102: Changed valueChanged(Object orig, Object clone) {
103: return changeDetector.hasChanges(clone, orig);
104: }
105:
106: public boolean calculateChanges(ObjectChangeSet changeSet) throws IllegalAccessException,
107: IllegalArgumentException, OWLInferredAttributeModifiedException {
108: Objects.requireNonNull(changeSet, ErrorUtils.constructNPXMessage("changeSet"));
109:
110: return calculateChangesInternal(changeSet);
111: }
112:
113: /**
114: * This internal method does the actual changes calculation. It compares every non-static attribute of the clone to
115: * the original value. If the values are different, a change record is added into the change set.
116: *
117: * @param changeSet The change set where change records will be put in. It also contains reference to the clone and
118: * original object.
119: * @throws IllegalArgumentException
120: * @throws IllegalAccessException
121: * @throws OWLInferredAttributeModifiedException
122: */
123: protected boolean calculateChangesInternal(ObjectChangeSet changeSet)
124: throws IllegalArgumentException, IllegalAccessException {
125: LOG.trace("Calculating changes for change set {}.", changeSet);
126: Object original = changeSet.getChangedObject();
127: Object clone = changeSet.getCloneObject();
128: boolean changes = false;
129:• for (FieldSpecification<?, ?> fs : getFields(clone.getClass())) {
130: final Field f = fs.getJavaField();
131: Object clVal = EntityPropertiesUtils.getFieldValue(f, clone);
132: Object origVal = EntityPropertiesUtils.getFieldValue(f, original);
133:• if (clVal == null && origVal == null) {
134: continue;
135: }
136: final String attName = f.getName();
137: Changed changed = valueChanged(origVal, clVal);
138:• switch (changed) {
139: case TRUE:
140: changeSet.addChangeRecord(new ChangeRecordImpl(attName, clVal));
141: changes = true;
142: break;
143: case UNDETERMINED:
144:• if (hasChanges(origVal, clVal)) {
145: changeSet.addChangeRecord(new ChangeRecordImpl(attName, clVal));
146: changes = true;
147: }
148: break;
149: default:
150: break;
151: }
152: }
153: return changes;
154: }
155: }