001/*
002 * (C) Copyright 2010 Nuxeo SA (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 */
017package org.nuxeo.ecm.platform.routing.core.impl;
018
019import java.io.Serializable;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.event.EventProducer;
027import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
028import org.nuxeo.ecm.platform.routing.api.DocumentRouteElement;
029import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * @author <a href="mailto:arussel@nuxeo.com">Alexandre Russel</a>
034 */
035public class EventFirer {
036
037    static public void fireEvent(CoreSession coreSession, DocumentRouteElement element,
038            Map<String, Serializable> eventProperties, String eventName) {
039        if (eventProperties == null) {
040            eventProperties = new HashMap<String, Serializable>();
041        }
042        eventProperties.put(DocumentRoutingConstants.DOCUMENT_ELEMENT_EVENT_CONTEXT_KEY, element);
043        eventProperties.put(DocumentEventContext.CATEGORY_PROPERTY_KEY, DocumentRoutingConstants.ROUTING_CATEGORY);
044        DocumentEventContext envContext = new DocumentEventContext(coreSession, coreSession.getPrincipal(),
045                element.getDocument());
046        envContext.setProperties(eventProperties);
047        getEventProducer().fireEvent(envContext.newEvent(eventName));
048    }
049
050    /**
051     * Fires an event in the category 'routing' for every document in the list
052     *
053     * @since 5.7.2
054     */
055    static public void fireEvent(CoreSession coreSession, List<DocumentModel> docs,
056            Map<String, Serializable> eventProperties, String eventName) {
057        if (eventProperties == null) {
058            eventProperties = new HashMap<String, Serializable>();
059        }
060        eventProperties.put(DocumentEventContext.CATEGORY_PROPERTY_KEY, DocumentRoutingConstants.ROUTING_CATEGORY);
061        for (DocumentModel doc : docs) {
062            DocumentEventContext envContext = new DocumentEventContext(coreSession, coreSession.getPrincipal(), doc);
063            envContext.setProperties(eventProperties);
064            getEventProducer().fireEvent(envContext.newEvent(eventName));
065        }
066    }
067
068    static protected EventProducer getEventProducer() {
069        return Framework.getService(EventProducer.class);
070    }
071}