001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.events.operations;
013
014import org.nuxeo.ecm.automation.OperationContext;
015import org.nuxeo.ecm.automation.core.Constants;
016import org.nuxeo.ecm.automation.core.annotations.Context;
017import org.nuxeo.ecm.automation.core.annotations.Operation;
018import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
019import org.nuxeo.ecm.automation.core.annotations.Param;
020import org.nuxeo.ecm.core.api.CoreSession;
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.api.DocumentModelList;
023import org.nuxeo.ecm.core.api.DocumentRef;
024import org.nuxeo.ecm.core.event.Event;
025import org.nuxeo.ecm.core.event.EventProducer;
026import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
027import org.nuxeo.ecm.core.event.impl.EventContextImpl;
028
029/**
030 * Save the session - TODO remove this?
031 *
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034@Operation(id = FireEvent.ID, category = Constants.CAT_NOTIFICATION, label = "Send Event", description = "Send a Nuxeo event.", aliases = { "Notification.SendEvent" })
035public class FireEvent {
036
037    public static final String ID = "Event.Fire";
038
039    @Context
040    protected OperationContext ctx;
041
042    @Context
043    protected EventProducer service;
044
045    @Param(name = "name")
046    protected String name;
047
048    @OperationMethod
049    public void run() {
050        CoreSession session = ctx.getCoreSession();
051        Object input = ctx.getInput();
052        if (input instanceof DocumentModel) {
053            sendDocumentEvent((DocumentModel) input);
054        } else if (input instanceof DocumentRef) {
055            sendDocumentEvent(session.getDocument((DocumentRef) input));
056        } else if (input instanceof DocumentModelList) {
057            DocumentModelList docs = (DocumentModelList) input;
058            for (DocumentModel documentModel : docs) {
059                sendDocumentEvent(documentModel);
060            }
061        } else {
062            sendUnknownEvent(input);
063        }
064    }
065
066    protected void sendDocumentEvent(DocumentModel input) {
067        CoreSession session = ctx.getCoreSession();
068        EventContextImpl evctx = new DocumentEventContext(session, session.getPrincipal(), input);
069        Event event = evctx.newEvent(name);
070        service.fireEvent(event);
071    }
072
073    protected void sendUnknownEvent(Object input) {
074        CoreSession session = ctx.getCoreSession();
075        EventContextImpl evctx = new EventContextImpl(session, session.getPrincipal(), input);
076        Event event = evctx.newEvent(name);
077        service.fireEvent(event);
078    }
079
080}