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