Skip to content

Method: getEntityContext()

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
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: * <p>
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.sessions.ChangeRecord;
18: import cz.cvut.kbss.jopa.sessions.ObjectChangeSet;
19:
20: import java.net.URI;
21: import java.util.HashMap;
22: import java.util.Map;
23:
24: public class ObjectChangeSetImpl implements ObjectChangeSet {
25:
26: // Class this ObjectChangeSet represents
27: private Class<?> objectClass;
28:
29: // The object the changes are bound to
30: private Object changedObject;
31:
32: // Reference to the clone
33: private Object cloneObject;
34:
35: // A map of attributeName-ChangeRecord pairs to easily find the attributes to change
36: private final Map<String, ChangeRecord> attributesToChange = new HashMap<>();
37:
38: // Does this change set represent a new object
39: private boolean isNew;
40:
41: private URI context;
42:
43: public ObjectChangeSetImpl(Object changedObject, Object cloneObject, URI context) {
44: this.changedObject = changedObject;
45: this.cloneObject = cloneObject;
46: this.objectClass = cloneObject.getClass();
47: this.context = context;
48: }
49:
50: /**
51: * Add a change record to this change set
52: *
53: * @param record ChangeRecord
54: */
55: public void addChangeRecord(ChangeRecord record) {
56: if (record == null)
57: return;
58: String attributeName = record.getAttributeName();
59: attributesToChange.put(attributeName, record);
60: }
61:
62: /**
63: * Returns the map with attribute names and changes made to them.
64: *
65: * @return java.util.Map
66: */
67: public Map<String, ChangeRecord> getChanges() {
68: return attributesToChange;
69: }
70:
71: @Override
72: public boolean hasChanges() {
73: return !attributesToChange.isEmpty();
74: }
75:
76: public Class<?> getObjectClass() {
77: return objectClass;
78: }
79:
80: public Object getChangedObject() {
81: return changedObject;
82: }
83:
84: public Object getCloneObject() {
85: return cloneObject;
86: }
87:
88: public void setCloneObject(Object cloneObject) {
89: this.cloneObject = cloneObject;
90: }
91:
92: public void setNew(boolean isNew) {
93: this.isNew = isNew;
94: }
95:
96: /**
97: * Returns true if this change set represents a new object
98: *
99: * @return boolean
100: */
101: public boolean isNew() {
102: return isNew;
103: }
104:
105: @Override
106: public URI getEntityContext() {
107: return context;
108: }
109: }