KickJava   Java API By Example, From Geeks To Geeks.

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


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

10 package org.mmbase.core.event;
11
12 import java.util.*;
13
14 import org.mmbase.bridge.*;
15 import org.mmbase.module.core.*;
16 import org.mmbase.module.corebuilders.InsRel;
17
18 /**
19  * @author Ernst Bunders
20  *this is a utility class to create node event and relation event instances. the reason for it is
21  *that we want to references to core classes in the NodeEvent and RelationEvent classes, to keep them bridge-friendly,
22  *but we need a little help for easy instantiation.
23  */

24 public class NodeEventHelper {
25
26     public static NodeEvent createNodeEventInstance(Node node, int eventType, String JavaDoc machineName){
27         if(machineName == null)machineName = MMBase.getMMBase().getMachineName();
28         MMObjectNode coreNode = MMBase.getMMBase().getBuilder(node.getNodeManager().getName()).getNode(node.getNumber());
29         return createNodeEventInstance(coreNode, eventType, machineName);
30     }
31
32     /**
33      * create an NodeEvent instance with an MMObjectNode
34      * @param node
35      * @param eventType
36      * @param machineName or null to create a node event with local machine name
37      * @return new instance of NodeEvent
38      */

39     public static NodeEvent createNodeEventInstance(MMObjectNode node, int eventType, String JavaDoc machineName){
40         if(machineName == null) machineName = MMBase.getMMBase().getMachineName();
41         Map oldEventValues;
42         Map newEventValues;
43
44         //fill the old and new values maps for the event
45
switch(eventType) {
46         case Event.TYPE_NEW:
47             newEventValues = Collections.unmodifiableMap(node.getValues());
48             oldEventValues = Collections.EMPTY_MAP;
49             break;
50         case Event.TYPE_CHANGE:
51             oldEventValues = Collections.unmodifiableMap(node.getOldValues());
52             newEventValues = new HashMap();
53             Map values = node.getValues();
54             for(Iterator i = oldEventValues.keySet().iterator(); i.hasNext(); ) {
55                 Object JavaDoc key = i.next();
56                 newEventValues.put(key, values.get(key));
57             }
58             break;
59         case Event.TYPE_DELETE:
60             newEventValues = Collections.EMPTY_MAP;
61             oldEventValues = Collections.unmodifiableMap(node.getValues());
62             break;
63         default: {
64             oldEventValues = Collections.EMPTY_MAP;
65             newEventValues = Collections.EMPTY_MAP;
66             // err.
67
}
68         }
69
70         return new NodeEvent(machineName, node.getBuilder().getTableName(), node.getNumber(), oldEventValues, newEventValues, eventType);
71     }
72
73     public static RelationEvent createRelationEventInstance(Relation node, int eventType, String JavaDoc machineName){
74         MMObjectNode coreNode = MMBase.getMMBase().getBuilder(node.getNodeManager().getName()).getNode(node.getNumber());
75         return createRelationEventInstance(coreNode, eventType, machineName);
76     }
77
78     /**
79      * create an RelationEvent instnce with an MMObjectNode (builder should be specialization of insrel)
80      * @param node
81      * @param eventType
82      * @param machineName
83      * @return a new RelationEvetn instance
84      * @throws IllegalArgumentException when given node's builder is not a specialization of insrel
85      */

86     public static RelationEvent createRelationEventInstance(MMObjectNode node, int eventType, String JavaDoc machineName){
87         if (!(node.getBuilder() instanceof InsRel)) {
88             throw new IllegalArgumentException JavaDoc( "you can not create a relation changed event with this node");
89         }
90         if(machineName == null) machineName = MMBase.getMMBase().getMachineName();
91         MMObjectNode reldef = node.getNodeValue("rnumber");
92         
93         int relationSourceNumber = node.getIntValue("snumber");
94         int relationDestinationNumber = node.getIntValue("dnumber");
95
96         String JavaDoc relationSourceType = MMBase.getMMBase().getBuilderNameForNode(relationSourceNumber);
97         String JavaDoc relationDestinationType =MMBase.getMMBase().getBuilderNameForNode(relationDestinationNumber);
98         NodeEvent nodeEvent = createNodeEventInstance(node, eventType, machineName);
99         int role = reldef.getNumber();
100         return new RelationEvent(nodeEvent, relationSourceNumber, relationDestinationNumber, relationSourceType, relationDestinationType, role);
101     }
102 }
103
Popular Tags