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