001/*
002 * GWTEventService
003 * Copyright (c) 2011 and beyond, strawbill UG (haftungsbeschr?nkt)
004 *
005 * This is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU Lesser General Public License as
007 * published by the Free Software Foundation; either version 3 of
008 * the License, or (at your option) any later version.
009 * Other licensing for GWTEventService may also be possible on request.
010 * Please view the license.txt of the project for more information.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022package de.novanic.eventservice.client.event;
023
024import de.novanic.eventservice.client.event.domain.Domain;
025
026/**
027 * A {@link de.novanic.eventservice.client.event.DomainEvent} is a container and contains an event and the domain where the event has occurred.
028 *
029 * @author sstrohschein
030 * <br>Date: 05.08.2008
031 * <br>Time: 17:25:12
032 */
033public class DefaultDomainEvent implements DomainEvent, Comparable<DomainEvent>
034{
035    private Event myEvent;
036    private Domain myDomain;
037
038    /**
039     * @deprecated That constructor is only for serialization! Please use
040     * {@link de.novanic.eventservice.client.event.DefaultDomainEvent#DefaultDomainEvent(Event)} or
041     * {@link de.novanic.eventservice.client.event.DefaultDomainEvent#DefaultDomainEvent(Event, de.novanic.eventservice.client.event.domain.Domain)} instead.
042     * @see de.novanic.eventservice.client.event.DefaultDomainEvent#DefaultDomainEvent(Event)
043     * @see de.novanic.eventservice.client.event.DefaultDomainEvent#DefaultDomainEvent(Event, de.novanic.eventservice.client.event.domain.Domain)
044     */
045    @Deprecated
046    public DefaultDomainEvent() {}
047
048    /**
049     * Creates a new DomainEvent with an event and a domain.
050     * @param aEvent event
051     * @param aDomain domain where the event has occurred
052     */
053    public DefaultDomainEvent(Event aEvent, Domain aDomain) {
054        myEvent = aEvent;
055        myDomain = aDomain;
056    }
057
058    /**
059     * Creates a new DomainEvent without a domain. This can be used when the event is user specific.
060     * When the event has occurred for a domain, the constructor {@link de.novanic.eventservice.client.event.DefaultDomainEvent#DefaultDomainEvent(Event, Domain)} should be used.
061     * @param aEvent event
062     */
063    public DefaultDomainEvent(Event aEvent) {
064        myEvent = aEvent;
065    }
066
067    /**
068     * An event/DomainEvent is user specific when it is only for one user and not for the complete domain.
069     * This flag depends on the constructor. If the DomainEvent is created with a domain, the DomainEvent isn't user
070     * specific.
071     * @return true when the event is user specific, otherwise false
072     */
073    public boolean isUserSpecific() {
074        return myDomain == null;
075    }
076
077    /**
078     * Returns the event.
079     * @return event
080     */
081    public Event getEvent() {
082        return myEvent;
083    }
084
085    /**
086     * Returns the domain where the event has occurred.
087     * @return domain
088     */
089    public Domain getDomain() {
090        return myDomain;
091    }
092
093    public int compareTo(DomainEvent aDomainEvent) {
094        int theCompareResult = 0;
095        if(myDomain != null) {
096            theCompareResult = myDomain.compareTo(aDomainEvent.getDomain());
097        } else if(aDomainEvent.getDomain() != null) {
098            theCompareResult--;
099        }
100
101        //when not decided
102        if(theCompareResult == 0) {
103            if(myEvent != null) {
104                theCompareResult = compareEvent(myEvent, aDomainEvent.getEvent());
105            } else if(aDomainEvent.getEvent() != null) {
106                theCompareResult--;
107            }
108        }
109        return theCompareResult;
110    }
111
112    private int compareEvent(Event anEvent_1, Event anEvent_2) {
113        if(anEvent_2 != null) {
114            return anEvent_1.getClass().getName().compareTo(anEvent_2.getClass().getName());
115        }
116        return 1;
117    }
118
119    public boolean equals(Object anObject) {
120        if(this == anObject) {
121            return true;
122        }
123        if(anObject == null || getClass() != anObject.getClass()) {
124            return false;
125        }
126
127        DomainEvent theDomainEvent = (DomainEvent)anObject;
128        return !(myDomain != null ? !myDomain.equals(theDomainEvent.getDomain()) : theDomainEvent.getDomain() != null)
129                && !(myEvent != null ? !myEvent.equals(theDomainEvent.getEvent()) : theDomainEvent.getEvent() != null);
130    }
131
132    public int hashCode() {
133        int theResult = myEvent != null ? myEvent.hashCode() : 0;
134        theResult = 31 * theResult + (myDomain != null ? myDomain.hashCode() : 0);
135        return theResult;
136    }
137
138    public String toString() {
139        StringBuilder theStringBuilder = new StringBuilder(80);
140        theStringBuilder.append("DomainEvent (");
141        if(myDomain != null) {
142            theStringBuilder.append(myDomain.getName());
143        }
144        if(myDomain != null && myEvent != null) {
145            theStringBuilder.append(" - ");
146        }
147        if(myEvent != null) {
148            theStringBuilder.append(myEvent);
149        }
150        theStringBuilder.append(')');
151        return theStringBuilder.toString();
152    }
153}