001/*
002 * (C) Copyright 2012 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 */
016package org.nuxeo.ecm.platform.routing.core.impl;
017
018import java.io.Serializable;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.nuxeo.ecm.core.api.CoreSession;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.event.DocumentEventCategories;
025import org.nuxeo.ecm.core.event.EventProducer;
026import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
027import org.nuxeo.ecm.platform.routing.api.DocumentRouteElement;
028import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Fire events for Route audit logs (used by previous route content view)
033 *
034 * @since 5.6
035 */
036public class AuditEventFirer {
037
038    static public void fireEvent(CoreSession coreSession, DocumentRouteElement element,
039            Map<String, Serializable> eventProperties, String eventName, DocumentModel doc) {
040        if (eventProperties == null) {
041            eventProperties = new HashMap<String, Serializable>();
042        }
043        eventProperties.put(DocumentRoutingConstants.TASK_ROUTE_INSTANCE_DOCUMENT_ID_KEY,
044                element.getDocumentRoute(coreSession).getDocument().getId());
045        eventProperties.put(DocumentRoutingConstants.DOCUMENT_ELEMENT_EVENT_CONTEXT_KEY, element);
046        eventProperties.put(DocumentEventContext.CATEGORY_PROPERTY_KEY, DocumentEventCategories.EVENT_DOCUMENT_CATEGORY);
047        DocumentEventContext envContext = new DocumentEventContext(coreSession, coreSession.getPrincipal(), doc);
048        envContext.setProperties(eventProperties);
049        getEventProducer().fireEvent(envContext.newEvent(eventName));
050    }
051
052    static protected EventProducer getEventProducer() {
053        return Framework.getService(EventProducer.class);
054    }
055
056}