KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > core > event > NodeEvent


1 /*
2  * Created on 21-jun-2005
3  * This software is OSI Certified Open Source Software.
4  * OSI Certified is a certification mark of the Open Source Initiative. The
5  * license (Mozilla version 1.0) can be read at the MMBase site. See
6  * http://www.MMBase.org/license
7  */

8 package org.mmbase.core.event;
9
10 import java.io.*;
11 import java.util.*;
12
13 import org.mmbase.util.HashCodeUtil;
14 import org.mmbase.module.core.MMBase;
15 import org.mmbase.util.logging.Logger;
16 import org.mmbase.util.logging.Logging;
17
18
19 /**
20  * This class communicates a node event. in case of a change event, it contains
21  * a map of changed values, mapped to their field's name, as well as the
22  * previous values of the changed fields.
23  *
24  * @author Ernst Bunders
25  * @since MMBase-1.8
26  * @version $Id: NodeEvent.java,v 1.27 2006/06/20 21:23:57 michiel Exp $
27  */

28 public class NodeEvent extends Event {
29
30
31     private static final long serialVersionUID = 1L;
32
33     private static final Logger log = Logging.getLoggerInstance(NodeEvent.class);
34
35     /**
36      * Event type speicfic for MMBase nodes.
37      */

38     public static final int TYPE_RELATION_CHANGE = 3;
39
40     private final int nodeNumber;
41     private String JavaDoc builderName;
42
43     private final Map oldValues;
44     private final Map newValues;
45
46     /**
47     *@param machineName (MMBase) name of the server
48     *@param builderName name of builder of node event is about
49     *@param oldValues map with fields and their values that have been changed by the event
50     *@param newValues map with new values of changed fields
51     *@param eventType the type of event
52     **/

53     public NodeEvent(String JavaDoc machineName, String JavaDoc builderName, int nodeNumber, Map oldValues, Map newValues, int eventType ){
54         super(machineName, eventType);
55         this.builderName = builderName;
56         this.nodeNumber = nodeNumber;
57         this.oldValues = oldValues == null ? Collections.EMPTY_MAP : Collections.unmodifiableMap(new HashMap(oldValues));
58         this.newValues = newValues == null ? Collections.EMPTY_MAP : Collections.unmodifiableMap(new HashMap(newValues));
59     }
60
61
62     /**
63      * @param fieldName the field you want to get the old value of
64      * @return an Object containing the old value (in case of change event), or
65      * null if the fieldName was not found in the old value list
66      */

67     public final Object JavaDoc getOldValue(String JavaDoc fieldName) {
68         return oldValues.get(fieldName);
69     }
70
71     /**
72      * @return a set containing the names of the fields that have changed
73      */

74     public final Set getChangedFields() {
75         switch(getType()) {
76         case TYPE_NEW:
77             return newValues.keySet();
78         case TYPE_CHANGE:
79             //for changed both old and new values are good (similar keys)
80
return newValues.keySet();
81         case TYPE_DELETE:
82             return oldValues.keySet();
83         default:
84             return Collections.EMPTY_SET;
85         }
86     }
87
88     /**
89      * @param fieldName the field you want the new value of (in case of change
90      * event), or null if the fieldName was not found in the new value
91      * list
92      * @return the new value of the field
93      */

94     public final Object JavaDoc getNewValue(String JavaDoc fieldName) {
95         return newValues.get(fieldName);
96     }
97
98     /**
99      * @return Returns the builderName.
100      */

101     public final String JavaDoc getBuilderName() {
102         return builderName;
103     }
104     /**
105      * @return Returns the nodeNumber.
106      */

107     public final int getNodeNumber() {
108         return nodeNumber;
109     }
110
111
112     public String JavaDoc toString() {
113         String JavaDoc changedFields = "";
114         for (Iterator i = getChangedFields().iterator(); i.hasNext();) {
115             changedFields = changedFields + (String JavaDoc) i.next() + ",";
116         }
117         return "Node event: '" + getEventTypeGuiName(eventType) + "', node: " + nodeNumber + ", nodetype: " + builderName + ", oldValues: " + oldValues + ", newValues: " + newValues + "changedFields: " + getChangedFields();
118     }
119
120     protected static String JavaDoc getEventTypeGuiName(int eventType) {
121         switch (eventType) {
122         case Event.TYPE_CHANGE:
123             return "node changed";
124         case Event.TYPE_DELETE:
125             return "node deleted";
126         case Event.TYPE_NEW:
127             return "new node";
128         case NodeEvent.TYPE_RELATION_CHANGE:
129             return "relation changed";
130         default:
131             throw new IllegalArgumentException JavaDoc("HELP! event of type " + eventType + " is unknown. This should not happen");
132         }
133     }
134
135
136
137     /**
138      * I think this method is not needed.
139      * @deprecated
140      */

141     /*
142     public NodeEvent clone(String builderName) {
143         NodeEvent clone = (NodeEvent) super.clone();
144         clone.builderName = builderName;
145         return clone;
146     }
147     */

148
149     /**
150      * For conveneance: conversion of the new event type indication to the old
151      * style
152      *
153      * @param eventType must be c,d,n or r
154      * @return A String describing the type of an event. (like "c" (change), "d" (delete), "n" (new), or "r" (relation change))
155      */

156     public static String JavaDoc newTypeToOldType(int eventType) {
157         switch (eventType) {
158         case Event.TYPE_CHANGE: return "c";
159         case Event.TYPE_DELETE: return "d";
160         case Event.TYPE_NEW: return "n";
161         case NodeEvent.TYPE_RELATION_CHANGE: return "r";
162         default: throw new IllegalArgumentException JavaDoc("HELP! event of type " + eventType + " is unknown. This should not happen");
163         }
164     }
165
166     /**
167      * For conveneance: conversion of the old event type indication to the new
168      * style
169      *
170      * @param eventType
171      */

172     public static int oldTypeToNewType(String JavaDoc eventType) {
173         if (eventType.length() > 1) {
174             throw new IllegalArgumentException JavaDoc("HELP! event of type '" + eventType + "' is unknown. This should not happen. (length = " + eventType.length() + ")");
175         }
176         switch(eventType.charAt(0)) {
177         case 'c': return Event.TYPE_CHANGE;
178         case 'd': return Event.TYPE_DELETE;
179         case 'n': return Event.TYPE_NEW;
180         case 'r': return NodeEvent.TYPE_RELATION_CHANGE;
181         default: throw new IllegalArgumentException JavaDoc("HELP! event of type " + eventType + " is unknown. This should not happen");
182         }
183     }
184
185     /**
186      * utility method: check if a certain field has changed
187      * @param fieldName
188      * @return true if the field of given name is among the changed fields
189      */

190     public boolean hasChanged(String JavaDoc fieldName){
191         return oldValues.keySet().contains(fieldName) || newValues.keySet().contains(fieldName);
192     }
193
194
195     public int hashCode() {
196         int result = 0;
197         result = HashCodeUtil.hashCode(result, eventType);
198         result = HashCodeUtil.hashCode(result, nodeNumber);
199         result = HashCodeUtil.hashCode(result, builderName);
200         return result;
201
202     }
203     public boolean equals(Object JavaDoc o) {
204         if (o instanceof NodeEvent) {
205             NodeEvent ne = (NodeEvent) o;
206             return eventType == ne.eventType && nodeNumber == ne.nodeNumber && builderName.equals(ne.builderName);
207         } else {
208             return false;
209         }
210     }
211
212     /**
213      * old values can be different things.
214      * <ul>
215      * <li>if the event type is 'new' this collection is empty.
216      * <li>if the event type is 'changed' this collection contains the old values of the changed fields.
217      * <li>if the event type is 'delete' this collection contains all the values of the node to be deleted.
218      * </ul>
219      * @return a map where the key is a fieldname and the value the field's value
220      */

221     public final Map getOldValues(){
222         return oldValues;
223     }
224
225     /**
226      * new values can be different things.
227      * <ul>
228      * <li>if the event type is 'new' this collection contains all the fields of the node.
229      * <li>if the event type is 'changed' this collection contains the new values of the changed fields.
230      * <li>if the event type is 'delete' this collection is empty.
231      * </ul>
232      * @return a map where the key is a fieldname and the value the field's value
233      */

234     public final Map getNewValues(){
235         return newValues;
236     }
237
238
239     public static void main(String JavaDoc[] args) {
240         //test serializable
241
Map oldv = new HashMap(), newv = new HashMap();
242         oldv.put("een","veen");
243         oldv.put("twee","vtwee");
244         newv.putAll(oldv);
245
246         NodeEvent event = new NodeEvent( "local", "builder", 0, oldv, newv, NodeEvent.TYPE_CHANGE);
247         System.out.println("event 1: " + event.toString());
248
249     }
250
251 }
252
Popular Tags