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 java.util.Map; 022import java.util.stream.Collectors; 023 024import org.nuxeo.ecm.automation.OperationContext; 025import org.nuxeo.ecm.automation.core.Constants; 026import org.nuxeo.ecm.automation.core.annotations.Context; 027import org.nuxeo.ecm.automation.core.annotations.Operation; 028import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 029import org.nuxeo.ecm.automation.core.annotations.Param; 030import org.nuxeo.ecm.automation.core.util.Properties; 031import org.nuxeo.ecm.core.api.CoreSession; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.DocumentModelList; 034import org.nuxeo.ecm.core.api.DocumentRef; 035import org.nuxeo.ecm.core.event.Event; 036import org.nuxeo.ecm.core.event.EventContext; 037import org.nuxeo.ecm.core.event.EventProducer; 038import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 039import org.nuxeo.ecm.core.event.impl.EventContextImpl; 040 041/** 042 * Save the session - TODO remove this? 043 * 044 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 045 */ 046@Operation(id = FireEvent.ID, category = Constants.CAT_NOTIFICATION, label = "Send Event", description = "Send a Nuxeo event.", aliases = { "Notification.SendEvent" }) 047public class FireEvent { 048 049 public static final String ID = "Event.Fire"; 050 051 @Context 052 protected OperationContext ctx; 053 054 @Context 055 protected EventProducer service; 056 057 @Param(name = "name") 058 protected String name; 059 060 /** @since 11.1 */ 061 @Param(name = "properties", required = false) 062 protected Properties properties = new Properties(); 063 064 @OperationMethod 065 public void run() { 066 CoreSession session = ctx.getCoreSession(); 067 Object input = ctx.getInput(); 068 if (input instanceof DocumentModel) { 069 sendDocumentEvent((DocumentModel) input); 070 } else if (input instanceof DocumentRef) { 071 sendDocumentEvent(session.getDocument((DocumentRef) input)); 072 } else if (input instanceof DocumentModelList) { 073 DocumentModelList docs = (DocumentModelList) input; 074 for (DocumentModel documentModel : docs) { 075 sendDocumentEvent(documentModel); 076 } 077 } else { 078 sendUnknownEvent(input); 079 } 080 } 081 082 protected void sendDocumentEvent(DocumentModel input) { 083 CoreSession session = ctx.getCoreSession(); 084 EventContextImpl evctx = new DocumentEventContext(session, session.getPrincipal(), input); 085 sendEvent(evctx); 086 } 087 088 protected void sendUnknownEvent(Object input) { 089 CoreSession session = ctx.getCoreSession(); 090 EventContextImpl evctx = new EventContextImpl(session, session.getPrincipal(), input); 091 sendEvent(evctx); 092 } 093 094 protected void sendEvent(EventContext eventContext) { 095 Event event = eventContext.newEvent(name); 096 event.getContext().setProperties( 097 properties.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); 098 service.fireEvent(event); 099 } 100 101}