001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.event;
019:
020: import java.io.Serializable;
021: import java.util.Collections;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: /**
026: * Represents an event within the Contelligent system. <BR>
027: * For performance reasons we use simple <code>int</code> values instead of
028: * polymorphism to identify a specific event-type.
029: *
030: * For the main event types there exist subclasses {@link ComponentEvent} and
031: * {@link TypeEvent}.
032: */
033: public class ContelligentEvent implements Serializable {
034:
035: public final static int CLIENT_EVENT_MASK = 64;
036:
037: public final static int PRINCIPALGROUP_EVENT_MASK = 128;
038:
039: public final static int COMPONENT_CHANGE_EVENT = 1 | CLIENT_EVENT_MASK;
040:
041: public final static int COMPONENT_REMOVE_EVENT = 2 | CLIENT_EVENT_MASK;
042:
043: public final static int COMPONENT_ADD_EVENT = 3 | CLIENT_EVENT_MASK;
044:
045: public final static int COMPONENT_LOCK_EVENT = 4 | CLIENT_EVENT_MASK;
046:
047: public final static int COMPONENT_UNLOCK_EVENT = 5 | CLIENT_EVENT_MASK;
048:
049: public final static int COMPONENT_CURRENT_PAGE = 6 | CLIENT_EVENT_MASK;
050:
051: public final static int CREATE_CM = 7 | CLIENT_EVENT_MASK;
052:
053: public final static int DESTROY_CM = 8 | CLIENT_EVENT_MASK;
054:
055: public final static int MESSAGE_EVENT = 9 | CLIENT_EVENT_MASK;
056:
057: public final static int LOGIN_EVENT = 10 | CLIENT_EVENT_MASK;
058:
059: public final static int LOGOUT_EVENT = 11 | CLIENT_EVENT_MASK;
060:
061: public final static int TYPE_CREATION_EVENT = 12 | CLIENT_EVENT_MASK;
062:
063: public final static int TASK_ADD_EVENT = 13 | CLIENT_EVENT_MASK;
064:
065: public final static int TASK_REMOVE_EVENT = 14 | CLIENT_EVENT_MASK;
066:
067: public final static int TYPE_CHANGED_EVENT = 15 | CLIENT_EVENT_MASK;
068:
069: public final static int TYPE_DELETED_EVENT = 16 | CLIENT_EVENT_MASK;
070:
071: public final static int CHANGE_CM = 17 | CLIENT_EVENT_MASK;
072:
073: public final static int WF_DEFINITION_CHANGED = 18 | CLIENT_EVENT_MASK;
074:
075: public final static int COMPONENT_EDIT_EVENT = 19 | CLIENT_EVENT_MASK;
076:
077: public final static int COMPONENT_CONFIG_ADDED = 20 | CLIENT_EVENT_MASK;
078:
079: public final static int COMPONENT_CONFIG_CHANGED = 21 | CLIENT_EVENT_MASK;
080:
081: public final static int COMPONENT_CONFIG_REMOVED = 22 | CLIENT_EVENT_MASK;
082:
083: public final static int SESSION_TIMEOUT_EVENT = 23 | CLIENT_EVENT_MASK;
084:
085: public final static int LOG_EVENT = 24 | CLIENT_EVENT_MASK;
086:
087: public final static int PRINCIPALGROUP_CREATE_EVENT = (1 | CLIENT_EVENT_MASK | PRINCIPALGROUP_EVENT_MASK);
088:
089: public final static int PRINCIPALGROUP_CHANGE_EVENT = (2 | CLIENT_EVENT_MASK | PRINCIPALGROUP_EVENT_MASK);
090:
091: public final static int PRINCIPALGROUP_REMOVE_EVENT = (3 | CLIENT_EVENT_MASK | PRINCIPALGROUP_EVENT_MASK);
092:
093: public final static String INFO_TASK_PRINCIPALS = "principals";
094:
095: public final static String INFO_TASK_ID = "taskId";
096:
097: private final String origin;
098:
099: private final int eventType; // one of the constants defined in this
100:
101: // class
102:
103: private Map info;
104:
105: /**
106: * Creates a new <code>ContelligentEvent</code> instance. <tt>origin</tt>
107: * specifies the name of the {@link de.finix.contelligent.ComponentManager}
108: * the event belongs and should be null if the event does not belong to any
109: * specific manager.
110: */
111: public ContelligentEvent(int eventType, String origin) {
112: this (eventType, origin, (Map) null);
113: }
114:
115: public ContelligentEvent(int eventType, Map info) {
116: this (eventType, null, info);
117: }
118:
119: public ContelligentEvent(int eventType, String origin, Map info) {
120: this .origin = origin;
121: this .eventType = eventType;
122: this .info = (info != null) ? Collections.unmodifiableMap(info)
123: : null;
124: }
125:
126: public int getType() {
127: return eventType;
128: }
129:
130: public String getOrigin() {
131: return origin;
132: }
133:
134: /**
135: * Returns an unmodifiable <code>Map</code> which was either set at
136: * instantiation time of this event or via {@link #setInfo}.
137: *
138: * @return an unmodifiable <code>Map</code>
139: */
140: public Map getInfo() {
141: return info;
142: }
143:
144: /**
145: * Sets the info map of this event. Note that any content of the map must be
146: * {@link Serializable}.
147: *
148: * @param info
149: * a <code>Map</code> value
150: */
151: public void setInfo(Map info) {
152: this .info = (info != null) ? Collections.unmodifiableMap(info)
153: : null;
154: }
155:
156: public boolean isClientEvent() {
157: return (getType() & CLIENT_EVENT_MASK) == CLIENT_EVENT_MASK;
158: }
159:
160: public boolean isLockEvent() {
161: return eventType == COMPONENT_LOCK_EVENT
162: || eventType == COMPONENT_UNLOCK_EVENT;
163: }
164:
165: public String toString() {
166: StringBuffer rs = new StringBuffer("[ContelligentEvent(");
167: switch (eventType) {
168: case COMPONENT_CHANGE_EVENT:
169: rs.append("Change");
170: break;
171: case COMPONENT_REMOVE_EVENT:
172: rs.append("Remove");
173: break;
174: case COMPONENT_ADD_EVENT:
175: rs.append("Add");
176: break;
177: case COMPONENT_LOCK_EVENT:
178: rs.append("Lock");
179: break;
180: case COMPONENT_UNLOCK_EVENT:
181: rs.append("Unlock");
182: break;
183: case MESSAGE_EVENT:
184: rs.append("Message");
185: break;
186: case LOGIN_EVENT:
187: rs.append("Login");
188: break;
189: case LOGOUT_EVENT:
190: rs.append("Logout");
191: break;
192: case TASK_ADD_EVENT:
193: rs.append("Task added");
194: break;
195: case TASK_REMOVE_EVENT:
196: rs.append("Task removed");
197: break;
198: case TYPE_CREATION_EVENT:
199: rs.append("BlueprintCreation");
200: break;
201: case CREATE_CM:
202: rs.append("CreateCM");
203: break;
204: case DESTROY_CM:
205: rs.append("DestroyCM");
206: break;
207: default:
208: rs.append("UNKNOWN-TYPE ");
209: rs.append(getType());
210: break;
211: }
212: rs.append(") origin=").append(origin).append("]");
213: if (info != null) {
214: rs.append(info);
215: }
216: return rs.toString();
217: }
218:
219: final public void toXML(StringBuffer buf) {
220: buf.append("<Event>\n");
221: buf.append("<Type>").append(eventType).append("</Type>\n");
222: if (origin != null) {
223: buf.append("<Server>").append(origin).append("</Server>\n");
224: }
225:
226: additionalXML(buf);
227:
228: infoMapToXML(buf);
229:
230: buf.append("</Event>\n");
231: }
232:
233: // subclasses should override this method to add special information to the
234: // XML representation
235: protected void additionalXML(StringBuffer buf) {
236: }
237:
238: public void infoMapToXML(StringBuffer buf) {
239: buf.append("<Info>\n");
240: if (getInfo() != null) {
241: Iterator iterator = getInfo().entrySet().iterator();
242: while (iterator.hasNext()) {
243: Map.Entry entry = (Map.Entry) iterator.next();
244: buf.append("<");
245: buf.append(entry.getKey());
246: buf.append("><![CDATA[");
247: buf.append(entry.getValue());
248: buf.append("]]></");
249: buf.append(entry.getKey());
250: buf.append(">\n");
251: }
252: }
253: buf.append("</Info>\n");
254: }
255:
256: }
|