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.service;
023
024import de.novanic.eventservice.service.registry.EventRegistry;
025import de.novanic.eventservice.service.registry.EventRegistryFactory;
026import de.novanic.eventservice.service.exception.NoSessionAvailableException;
027import de.novanic.eventservice.client.event.Event;
028import de.novanic.eventservice.client.event.filter.EventFilter;
029import de.novanic.eventservice.client.event.domain.Domain;
030
031/**
032 * DefaultEventExecutorService can be used to add events via the server side.
033 *
034 * @author sstrohschein
035 * <br>Date: 20.07.2008
036 * <br>Time: 14:20:48
037 */
038public class DefaultEventExecutorService implements EventExecutorService
039{
040    private static EventRegistry myEventRegistry;
041
042    private final String myClientId;
043
044    static {
045        init();
046    }
047
048    /**
049     * Creates a new EventExecutorService with a client/user id. All methods of EventExecutorService use the client/user
050     * id as default.
051     * This method shouldn't called directly. To create an instance of the service, {@link EventExecutorServiceFactory}
052     * should be used.
053     * @param aClientId client/user id
054     * @see EventExecutorServiceFactory
055     */
056    DefaultEventExecutorService(String aClientId) {
057        myClientId = aClientId;
058    }
059
060    /**
061     * Checks if the user is registered for event listening.
062     * @return true when the user is listening, otherwise false
063     */
064    public boolean isUserRegistered() {
065        return myEventRegistry.isUserRegistered(getClientId());
066    }
067
068    /**
069     * Checks if the user is registered for event listening.
070     * @param aDomain domain to check the registration for the user
071     * @return true when the user is listening, otherwise false
072     */
073    public boolean isUserRegistered(Domain aDomain) {
074        return myEventRegistry.isUserRegistered(aDomain, getClientId());
075    }
076
077    /**
078     * Adds an event for all users
079     * @param aDomain the domain to add the event
080     * @param anEvent event to add
081     */
082    public void addEvent(Domain aDomain, Event anEvent) {
083        myEventRegistry.addEvent(aDomain, anEvent);
084    }
085
086    /**
087     * Adds an event for a specific user
088     * @param anEvent event to add
089     */
090    public void addEventUserSpecific(Event anEvent) {
091        myEventRegistry.addEventUserSpecific(getClientId(), anEvent);
092    }
093
094    /**
095     * Changes the {@link de.novanic.eventservice.client.event.filter.EventFilter} for the user-domain combination.
096     * The {@link de.novanic.eventservice.client.event.filter.EventFilter} can be removed with the method
097     * {@link de.novanic.eventservice.service.EventExecutorService#removeEventFilter(de.novanic.eventservice.client.event.domain.Domain)}
098     * or when that method is called with NULL as the {@link de.novanic.eventservice.client.event.filter.EventFilter}
099     * parameter value.
100     */
101    public void setEventFilter(Domain aDomain, EventFilter anEventFilter) {
102        myEventRegistry.setEventFilter(aDomain, getClientId(), anEventFilter);
103    }
104
105    /**
106     * Returns the EventFilter for the user domain combination.
107     * @param aDomain domain
108     * @return EventFilter for the domain
109     */
110    public EventFilter getEventFilter(Domain aDomain) {
111        return myEventRegistry.getEventFilter(aDomain, getClientId());
112    }
113
114    /**
115     * Removes the {@link de.novanic.eventservice.client.event.filter.EventFilter} of the domain.
116     * @param aDomain domain to drop the EventFilter from
117     */
118    public void removeEventFilter(Domain aDomain) {
119        myEventRegistry.removeEventFilter(aDomain, getClientId());
120    }
121
122    /**
123     * Returns the client id.
124     * @return client id
125     */
126    private String getClientId() {
127        if(myClientId == null) {
128            throw new NoSessionAvailableException();
129        }
130        return myClientId;
131    }
132
133    /**
134     * Initializes the EventExecutorService.
135     */
136    private static void init() {
137        final EventRegistryFactory theEventRegistryFactory = EventRegistryFactory.getInstance();
138        myEventRegistry = theEventRegistryFactory.getEventRegistry();
139    }
140}