001: /*
002: * This software is OSI Certified Open Source Software.
003: * OSI Certified is a certification mark of the Open Source Initiative.
004: *
005: * The license (Mozilla version 1.0) can be read at the MMBase site.
006: * See http://www.MMBase.org/license
007: */
008: package org.mmbase.core.event;
009:
010: import java.util.*;
011:
012: import org.mmbase.bridge.*;
013: import org.mmbase.module.core.*;
014: import org.mmbase.module.corebuilders.InsRel;
015:
016: /**
017: * This is a utility class to create node event and relation event instances. the reason for it is
018: * that we want to references to core classes in the NodeEvent and RelationEvent classes, to keep them bridge-friendly,
019: * but we need a little help for easy instantiation.
020: * @author Ernst Bunders
021: * @since MMBase-1.8
022: * @version $Id: NodeEventHelper.java,v 1.11 2008/02/07 16:22:34 michiel Exp $
023:
024: */
025: public class NodeEventHelper {
026:
027: public static NodeEvent createNodeEventInstance(Node node,
028: int eventType, String machineName) {
029: if (machineName == null)
030: machineName = MMBase.getMMBase().getMachineName();
031: MMObjectNode coreNode = MMBase.getMMBase().getBuilder(
032: node.getNodeManager().getName()).getNode(
033: node.getNumber());
034: return createNodeEventInstance(coreNode, eventType, machineName);
035: }
036:
037: /**
038: * create an NodeEvent instance with an MMObjectNode
039: * @param node
040: * @param eventType
041: * @param machineName or null to create a node event with local machine name
042: * @return new instance of NodeEvent
043: */
044: public static NodeEvent createNodeEventInstance(MMObjectNode node,
045: int eventType, String machineName) {
046: if (machineName == null)
047: machineName = MMBase.getMMBase().getMachineName();
048: Map<String, Object> oldEventValues;
049: Map<String, Object> newEventValues;
050:
051: //fill the old and new values maps for the event
052: switch (eventType) {
053: case Event.TYPE_NEW:
054: newEventValues = removeBinaryValues(node.getValues());
055: oldEventValues = Collections.emptyMap();
056: break;
057: case Event.TYPE_CHANGE:
058: oldEventValues = removeBinaryValues(node.getOldValues());
059: newEventValues = new HashMap<String, Object>();
060: Map<String, Object> values = node.getValues();
061: for (String key : oldEventValues.keySet()) {
062: newEventValues.put(key, values.get(key));
063: }
064: newEventValues = removeBinaryValues(newEventValues);
065: break;
066: case Event.TYPE_DELETE:
067: newEventValues = Collections.emptyMap();
068: oldEventValues = removeBinaryValues(node.getValues());
069: break;
070: default: {
071: oldEventValues = Collections.emptyMap();
072: newEventValues = Collections.emptyMap();
073: // err.
074: }
075: }
076:
077: return new NodeEvent(machineName, node.getBuilder()
078: .getTableName(), node.getNumber(), oldEventValues,
079: newEventValues, eventType);
080: }
081:
082: private static Map<String, Object> removeBinaryValues(
083: Map<String, Object> oldEventValues) {
084: Set<String> toremove = null;
085: for (Map.Entry<String, Object> entry : oldEventValues
086: .entrySet()) {
087: if (entry.getValue() != null
088: && (entry.getValue() instanceof byte[])) {
089: if (toremove == null)
090: toremove = new HashSet<String>();
091: toremove.add(entry.getKey());
092: }
093: }
094: if (toremove != null) {
095: Map<String, Object> newMap = new HashMap<String, Object>();
096: newMap.putAll(oldEventValues);
097: for (String k : toremove) {
098: newMap.remove(k);
099: }
100: return Collections.unmodifiableMap(newMap);
101: } else {
102: return oldEventValues;
103: }
104: }
105:
106: public static RelationEvent createRelationEventInstance(
107: Relation node, int eventType, String machineName) {
108: MMObjectNode coreNode = MMBase.getMMBase().getBuilder(
109: node.getNodeManager().getName()).getNode(
110: node.getNumber());
111: return createRelationEventInstance(coreNode, eventType,
112: machineName);
113: }
114:
115: /**
116: * create an RelationEvent instnce with an MMObjectNode (builder should be specialization of insrel)
117: * @param node
118: * @param eventType
119: * @param machineName
120: * @return a new RelationEvetn instance
121: * @throws IllegalArgumentException when given node's builder is not a specialization of insrel
122: */
123: public static RelationEvent createRelationEventInstance(
124: MMObjectNode node, int eventType, String machineName) {
125: if (!(node.getBuilder() instanceof InsRel)) {
126: throw new IllegalArgumentException(
127: "you can not create a relation changed event with this node");
128: }
129: MMBase mmbase = MMBase.getMMBase();
130: if (machineName == null)
131: machineName = mmbase.getMachineName();
132: MMObjectNode reldef = node.getNodeValue("rnumber");
133:
134: int relationSourceNumber = node.getIntValue("snumber");
135: int relationDestinationNumber = node.getIntValue("dnumber");
136:
137: String relationSourceType = mmbase
138: .getBuilderNameForNode(relationSourceNumber);
139: if (relationSourceType == null)
140: relationSourceType = "object";
141: String relationDestinationType = mmbase
142: .getBuilderNameForNode(relationDestinationNumber);
143: if (relationDestinationType == null)
144: relationDestinationType = "object";
145: NodeEvent nodeEvent = createNodeEventInstance(node, eventType,
146: machineName);
147: int role = reldef.getNumber();
148: return new RelationEvent(nodeEvent, relationSourceNumber,
149: relationDestinationNumber, relationSourceType,
150: relationDestinationType, role);
151: }
152: }
|