001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010:
011: package org.mmbase.bridge.util;
012:
013: import java.util.*;
014: import org.mmbase.bridge.*;
015:
016: /**
017: * Wraps another Node, and adds 'isChangedByThis'.
018: *
019: * @author Michiel Meeuwissen
020: * @version $Id: NodeChanger.java,v 1.3 2007/02/10 15:47:42 nklasens Exp $
021: * @since MMBase-1.8
022: */
023:
024: public class NodeChanger extends NodeWrapper {
025: protected Map<String, Object> originalValues = null;
026:
027: public NodeChanger(Node node) {
028: super (node);
029: }
030:
031: protected void change(String fieldName) {
032: if (originalValues == null)
033: originalValues = new HashMap<String, Object>();
034: if (!originalValues.containsKey(fieldName)) {
035: originalValues.put(fieldName, node
036: .getValueWithoutProcess(fieldName));
037: }
038: }
039:
040: @Override
041: public void setValue(String fieldName, Object value) {
042: change(fieldName);
043: super .setValue(fieldName, value);
044: }
045:
046: @Override
047: public void setValueWithoutProcess(String fieldName, Object value) {
048: change(fieldName);
049: super .setValueWithoutProcess(fieldName, value);
050: }
051:
052: @Override
053: public void setObjectValue(String fieldName, Object value) {
054: change(fieldName);
055: super .setObjectValue(fieldName, value);
056: }
057:
058: @Override
059: public void setBooleanValue(String fieldName, boolean value) {
060: change(fieldName);
061: super .setBooleanValue(fieldName, value);
062: }
063:
064: @Override
065: public void setNodeValue(String fieldName, Node value) {
066: change(fieldName);
067: super .setNodeValue(fieldName, value);
068: }
069:
070: @Override
071: public void setIntValue(String fieldName, int value) {
072: change(fieldName);
073: super .setIntValue(fieldName, value);
074: }
075:
076: @Override
077: public void setFloatValue(String fieldName, float value) {
078: change(fieldName);
079: super .setFloatValue(fieldName, value);
080: }
081:
082: @Override
083: public void setDoubleValue(String fieldName, double value) {
084: change(fieldName);
085: super .setDoubleValue(fieldName, value);
086: }
087:
088: @Override
089: public void setByteValue(String fieldName, byte[] value) {
090: change(fieldName);
091: super .setByteValue(fieldName, value);
092: }
093:
094: @Override
095: public void setInputStreamValue(String fieldName,
096: java.io.InputStream value, long size) {
097: change(fieldName);
098: super .setInputStreamValue(fieldName, value, size);
099: }
100:
101: @Override
102: public void setLongValue(String fieldName, long value) {
103: change(fieldName);
104: super .setLongValue(fieldName, value);
105: }
106:
107: @Override
108: public void setStringValue(String fieldName, String value) {
109: change(fieldName);
110: super .setStringValue(fieldName, value);
111: }
112:
113: @Override
114: public void setDateValue(String fieldName, Date value) {
115: change(fieldName);
116: super .setDateValue(fieldName, value);
117: }
118:
119: @Override
120: public void setListValue(String fieldName, List value) {
121: change(fieldName);
122: super .setListValue(fieldName, value);
123: }
124:
125: /**
126: * The isChanged method reflects the isChanged status of the underlying core node.isChanged. Before
127: * commiting the node, you may want to check if _you_ changed to node, and not some other
128: * thread. In that case you can first wrap your Node in a NodeChanger object.
129: *
130: */
131: public boolean isChangedByThis() {
132: return originalValues != null && super .isChanged();
133: }
134:
135: public void cancelThis() {
136: if (originalValues != null) {
137: for (Map.Entry<String, Object> entry : originalValues
138: .entrySet()) {
139: getNode().setValueWithoutProcess(entry.getKey(),
140: entry.getValue());
141: }
142:
143: }
144: }
145:
146: }
|