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.event.listener.unlisten;
023
024import de.novanic.eventservice.client.event.filter.EventFilter;
025import de.novanic.eventservice.client.event.Event;
026import de.novanic.eventservice.client.event.domain.Domain;
027import de.novanic.eventservice.client.event.listener.unlisten.UnlistenEvent;
028import de.novanic.eventservice.client.event.listener.unlisten.UnlistenEventListener;
029import de.novanic.eventservice.service.registry.domain.ListenDomainAccessor;
030
031import java.util.Set;
032
033/**
034 * The UnlistenEventFilter filters all {@link de.novanic.eventservice.client.event.listener.unlisten.UnlistenEvent} instances
035 * by default, which doesn't match the registered domains of the user/client for report reasons.
036 *
037 * @author sstrohschein
038 *         <br>Date: 16.08.2009
039 *         <br>Time: 17:59:36
040 */
041public class UnlistenEventFilter implements EventFilter
042{
043    private final String myUserId;
044    private final ListenDomainAccessor myListenDomainAccessor;
045    private final UnlistenEventListener.Scope myUnlistenScope;
046
047    public UnlistenEventFilter(ListenDomainAccessor aListenDomainAccessor, String aUserId, UnlistenEventListener.Scope anUnlistenScope) {
048        myUserId = aUserId;
049        myListenDomainAccessor = aListenDomainAccessor;
050        myUnlistenScope = anUnlistenScope;
051    }
052
053    /**
054     * Filters all {@link de.novanic.eventservice.client.event.listener.unlisten.UnlistenEvent} instances which doesn't
055     * match the registered domains for the current user/client or which are sent on a other scope ({@link UnlistenEventListener.Scope}.
056     * @param anEvent event to check
057     * @return true when the event should be filtered, otherwise false
058     */
059    public boolean match(Event anEvent) {
060        if(anEvent instanceof UnlistenEvent) {
061            final UnlistenEvent theUnlistenEvent = (UnlistenEvent)anEvent;
062            if(UnlistenEventListener.Scope.UNLISTEN == myUnlistenScope ||
063                    (UnlistenEventListener.Scope.TIMEOUT == myUnlistenScope && theUnlistenEvent.isTimeout())) {
064                final Set<Domain> theUnlistenedDomains = theUnlistenEvent.getDomains();
065                if(theUnlistenedDomains != null && !theUnlistenedDomains.isEmpty()) {
066                    final Set<Domain> theRegisteredListenDomains = myListenDomainAccessor.getListenDomains(myUserId);
067                    for(Domain theUnlistenedDomain: theUnlistenedDomains) {
068                        if(theRegisteredListenDomains.contains(theUnlistenedDomain)) {
069                            return false;
070                        }
071                    }
072                }
073            }
074            return true;
075        }
076        return false;
077    }
078}