001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webapp.helpers;
021
022import static org.jboss.seam.ScopeType.APPLICATION;
023import static org.jboss.seam.annotations.Install.FRAMEWORK;
024
025import java.io.Serializable;
026import java.util.ArrayList;
027import java.util.List;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.jboss.seam.annotations.Install;
032import org.jboss.seam.annotations.Name;
033import org.jboss.seam.annotations.Scope;
034import org.jboss.seam.annotations.Startup;
035import org.jboss.seam.core.Events;
036import org.nuxeo.ecm.core.api.DocumentModel;
037
038/**
039 * Knows what events need to be raised based on the user selected document.
040 *
041 * @author <a href="mailto:rcaraghin@nuxeo.com">Razvan Caraghin</a>
042 */
043@Name("eventManager")
044@Scope(APPLICATION)
045@Startup
046@Install(precedence = FRAMEWORK)
047public class EventManager implements Serializable {
048
049    private static final long serialVersionUID = -7572053704069819975L;
050
051    private static final Log log = LogFactory.getLog(EventManager.class);
052
053    /**
054     * Raises events on going home, will be processed immediately.
055     *
056     * @return events fired
057     */
058    public static List<String> raiseEventsOnGoingHome() {
059        List<String> eventsFired = new ArrayList<String>();
060
061        Events evtManager = Events.instance();
062
063        log.debug("Fire Event: " + EventNames.LOCATION_SELECTION_CHANGED);
064        evtManager.raiseEvent(EventNames.LOCATION_SELECTION_CHANGED);
065        eventsFired.add(EventNames.LOCATION_SELECTION_CHANGED);
066
067        log.debug("Fire Event: " + EventNames.GO_HOME);
068        evtManager.raiseEvent(EventNames.GO_HOME);
069        eventsFired.add(EventNames.GO_HOME);
070
071        return eventsFired;
072    }
073
074    /**
075     * Raises events on location selection change, will be processed immediately.
076     *
077     * @return events fired
078     */
079    public static List<String> raiseEventsOnLocationSelectionChanged() {
080        List<String> eventsFired = new ArrayList<String>();
081
082        Events evtManager = Events.instance();
083
084        log.debug("Fire Event: " + EventNames.LOCATION_SELECTION_CHANGED);
085        evtManager.raiseEvent(EventNames.LOCATION_SELECTION_CHANGED);
086        eventsFired.add(EventNames.LOCATION_SELECTION_CHANGED);
087
088        log.debug("Fire Event: " + EventNames.USER_ALL_DOCUMENT_TYPES_SELECTION_CHANGED);
089        evtManager.raiseEvent(EventNames.USER_ALL_DOCUMENT_TYPES_SELECTION_CHANGED);
090        eventsFired.add(EventNames.USER_ALL_DOCUMENT_TYPES_SELECTION_CHANGED);
091
092        return eventsFired;
093    }
094
095    /**
096     * Fires the necessary events so that the nuxeo infrastructure components get updated. The raised events will be
097     * processed immediately, before this call is ended. Intended to be used when a document gets selected. If the
098     * docType is NULL then the GO_HOME event is fired.
099     *
100     * @return events fired
101     */
102    public static List<String> raiseEventsOnDocumentSelected(DocumentModel document) {
103        List<String> eventsFired = new ArrayList<String>();
104
105        if (document == null) {
106            // XXX AT: kind of BBB, not sure why this was used like this
107            eventsFired = raiseEventsOnLocationSelectionChanged();
108        } else {
109            Events evtManager = Events.instance();
110
111            String docType = document.getType();
112            String eventName;
113
114            if ("Domain".equals(docType)) {
115                eventName = EventNames.DOMAIN_SELECTION_CHANGED;
116            } else if ("Root".equals(docType)) {
117                eventName = EventNames.GO_HOME;
118            } else if ("WorkspaceRoot".equals(docType) || "SectionRoot".equals(docType)) {
119                eventName = EventNames.CONTENT_ROOT_SELECTION_CHANGED;
120            } else {
121                // regular document is selected
122                eventName = EventNames.DOCUMENT_SELECTION_CHANGED;
123            }
124
125            if (document.isFolder()) {
126                evtManager.raiseEvent(EventNames.FOLDERISHDOCUMENT_SELECTION_CHANGED, document);
127            }
128
129            log.debug("Fire Event: " + eventName);
130            evtManager.raiseEvent(eventName, document);
131            eventsFired.add(eventName);
132
133            log.debug("Fire Event: " + EventNames.USER_ALL_DOCUMENT_TYPES_SELECTION_CHANGED);
134            evtManager.raiseEvent(EventNames.USER_ALL_DOCUMENT_TYPES_SELECTION_CHANGED);
135            eventsFired.add(EventNames.USER_ALL_DOCUMENT_TYPES_SELECTION_CHANGED);
136        }
137
138        return eventsFired;
139    }
140
141    /**
142     * Fires the necessary events so that the nuxeo infrastructure components get updated. The raised events will be
143     * processed immediately, before this call is ended. Intended to be used when a document gets edited/changed.
144     *
145     * @return events fired
146     */
147    public static List<String> raiseEventsOnDocumentChange(DocumentModel document) {
148        List<String> eventsFired = new ArrayList<String>();
149        // TODO: parameterize on document type
150        Events evtManager = Events.instance();
151        log.debug("Fire Event: " + EventNames.DOCUMENT_CHANGED);
152        evtManager.raiseEvent(EventNames.DOCUMENT_CHANGED, document);
153        eventsFired.add(EventNames.DOCUMENT_CHANGED);
154
155        log.debug("Fire Event: " + EventNames.USER_ALL_DOCUMENT_TYPES_SELECTION_CHANGED);
156        evtManager.raiseEvent(EventNames.USER_ALL_DOCUMENT_TYPES_SELECTION_CHANGED);
157        eventsFired.add(EventNames.USER_ALL_DOCUMENT_TYPES_SELECTION_CHANGED);
158        return eventsFired;
159    }
160
161    /**
162     * Dispatches an event to get interested components informed when a changeable document was created (thus not saved)
163     * and before the form is displayed.
164     */
165    public static void raiseEventsOnDocumentCreate(DocumentModel document) {
166        Events.instance().raiseEvent(EventNames.NEW_DOCUMENT_CREATED);
167    }
168
169    /**
170     * Fires the necessary events so that the nuxeo infrastructure components get updated. The raised events will be
171     * processed immediately, before this call is ended. Intended to be used when a the content of a folderish document
172     * gets changed.
173     *
174     * @return events fired
175     */
176    public static List<String> raiseEventsOnDocumentChildrenChange(DocumentModel document) {
177        List<String> eventsFired = new ArrayList<String>();
178        Events.instance().raiseEvent(EventNames.DOCUMENT_CHILDREN_CHANGED, document);
179        eventsFired.add(EventNames.DOCUMENT_CHILDREN_CHANGED);
180        return eventsFired;
181    }
182
183}