001: /*
002: * This software is OSI Certified Open Source Software.
003: * OSI Certified is a certification mark of the Open Source Initiative. The
004: * license (Mozilla version 1.0) can be read at the MMBase site. See
005: * http://www.MMBase.org/license
006: */
007: package org.mmbase.core.event;
008:
009: import org.mmbase.module.core.*;
010: import org.mmbase.util.HashCodeUtil;
011: import org.mmbase.storage.search.RelationStep;
012:
013: /**
014: * This class is a wrapper for relation event listeners that only want to listen
015: * to events concerning a specific builder - more specifically, events concerning changes
016: * in the relations from a specific builder's nodes.
017: *
018: * @author Ernst Bunders
019: * @since MMBase-1.8
020: * @version $Id: TypedRelationEventListenerWrapper.java,v 1.11 2007/07/26 11:45:54 michiel Exp $
021: */
022: public class TypedRelationEventListenerWrapper implements
023: RelationEventListener {
024:
025: private final MMObjectBuilder builder;
026: private final String nodeType;
027: private final RelationEventListener wrappedListener;
028: private final int direction;
029: private final boolean descendants;
030:
031: /**
032: * @param builder The builder which must constrain the listener
033: * @param wrappedListener the relation event listener you want to wrap
034: * @param direction At which side of the relation nodes of this builders can be: {@link org.mmbase.storage.search.RelationStep#DIRECTIONS_SOURCE}, {@link org.mmbase.storage.search.RelationStep#DIRECTIONS_DESTINATION}, or {@link org.mmbase.storage.search.RelationStep#DIRECTIONS_BOTH}
035: * @param descendants Whether also descendants of the given builder must be listened to. ('true' would be the must logical value).
036: */
037: public TypedRelationEventListenerWrapper(MMObjectBuilder builder,
038: RelationEventListener wrappedListener, int direction,
039: boolean descendants) {
040: this .builder = builder;
041: this .nodeType = builder.getTableName();
042: this .wrappedListener = wrappedListener;
043: this .direction = direction;
044: this .descendants = descendants;
045: }
046:
047: private boolean notify(RelationEvent event, String eventNodeType) {
048: if (nodeType.equals(eventNodeType)) {
049: wrappedListener.notify(event);
050: return true;
051: } else if (descendants) {
052: MMObjectBuilder eventBuilder = MMBase.getMMBase()
053: .getBuilder(eventNodeType);
054: if (nodeType.equals("object")
055: || (eventBuilder != null && eventBuilder
056: .isExtensionOf(builder))) {
057: wrappedListener.notify(event);
058: return true;
059: }
060: }
061: return false;
062: }
063:
064: public void notify(RelationEvent event) {
065: switch (direction) {
066: case RelationStep.DIRECTIONS_SOURCE:
067: notify(event, event.getRelationSourceType());
068: break;
069: case RelationStep.DIRECTIONS_DESTINATION:
070: notify(event, event.getRelationDestinationType());
071: break;
072: case RelationStep.DIRECTIONS_BOTH:
073: default:
074: if (!notify(event, event.getRelationSourceType())) {
075: notify(event, event.getRelationDestinationType());
076: }
077: }
078: }
079:
080: public String toString() {
081: return "TypedRelationEventListenerWrapper(" + wrappedListener
082: + ")";
083: }
084:
085: public boolean equals(Object o) {
086: if (o instanceof TypedRelationEventListenerWrapper) {
087: TypedRelationEventListenerWrapper tw = (TypedRelationEventListenerWrapper) o;
088: return builder.equals(tw.builder)
089: && wrappedListener.equals(tw.wrappedListener)
090: && direction == tw.direction
091: && descendants == tw.descendants;
092: } else {
093: return false;
094: }
095: }
096:
097: public int hashCode() {
098: int result = 0;
099: result = HashCodeUtil.hashCode(result, builder);
100: result = HashCodeUtil.hashCode(result, wrappedListener);
101: result = HashCodeUtil.hashCode(result, direction);
102: return result;
103:
104: }
105:
106: }
|