001/*
002 * (C) Copyright 2006-2011 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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.core.events.operations;
020
021import org.nuxeo.ecm.automation.OperationContext;
022import org.nuxeo.ecm.automation.core.Constants;
023import org.nuxeo.ecm.automation.core.annotations.Context;
024import org.nuxeo.ecm.automation.core.annotations.Operation;
025import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
026import org.nuxeo.ecm.automation.core.annotations.Param;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentModelList;
030import org.nuxeo.ecm.core.api.DocumentRef;
031import org.nuxeo.ecm.core.event.Event;
032import org.nuxeo.ecm.core.event.EventProducer;
033import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
034import org.nuxeo.ecm.core.event.impl.EventContextImpl;
035
036/**
037 * Save the session - TODO remove this?
038 *
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041@Operation(id = FireEvent.ID, category = Constants.CAT_NOTIFICATION, label = "Send Event", description = "Send a Nuxeo event.", aliases = { "Notification.SendEvent" })
042public class FireEvent {
043
044    public static final String ID = "Event.Fire";
045
046    @Context
047    protected OperationContext ctx;
048
049    @Context
050    protected EventProducer service;
051
052    @Param(name = "name")
053    protected String name;
054
055    @OperationMethod
056    public void run() {
057        CoreSession session = ctx.getCoreSession();
058        Object input = ctx.getInput();
059        if (input instanceof DocumentModel) {
060            sendDocumentEvent((DocumentModel) input);
061        } else if (input instanceof DocumentRef) {
062            sendDocumentEvent(session.getDocument((DocumentRef) input));
063        } else if (input instanceof DocumentModelList) {
064            DocumentModelList docs = (DocumentModelList) input;
065            for (DocumentModel documentModel : docs) {
066                sendDocumentEvent(documentModel);
067            }
068        } else {
069            sendUnknownEvent(input);
070        }
071    }
072
073    protected void sendDocumentEvent(DocumentModel input) {
074        CoreSession session = ctx.getCoreSession();
075        EventContextImpl evctx = new DocumentEventContext(session, session.getPrincipal(), input);
076        Event event = evctx.newEvent(name);
077        service.fireEvent(event);
078    }
079
080    protected void sendUnknownEvent(Object input) {
081        CoreSession session = ctx.getCoreSession();
082        EventContextImpl evctx = new EventContextImpl(session, session.getPrincipal(), input);
083        Event event = evctx.newEvent(name);
084        service.fireEvent(event);
085    }
086
087}