001/*
002 * (C) Copyright 2010 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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.automation.task.event;
018
019import java.io.Serializable;
020
021import org.apache.commons.lang.StringUtils;
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.automation.AutomationService;
025import org.nuxeo.ecm.automation.InvalidChainException;
026import org.nuxeo.ecm.automation.OperationContext;
027import org.nuxeo.ecm.automation.OperationException;
028import org.nuxeo.ecm.automation.task.CreateTask.OperationTaskVariableName;
029import org.nuxeo.ecm.core.event.Event;
030import org.nuxeo.ecm.core.event.EventContext;
031import org.nuxeo.ecm.core.event.EventListener;
032import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
033import org.nuxeo.ecm.platform.task.Task;
034import org.nuxeo.ecm.platform.task.TaskService;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * Listener that will launch another operation when a task is accepted or rejected
039 *
040 * @author Anahide Tchertchian
041 * @since 5.5
042 */
043public class TaskEndedEventListener implements EventListener {
044
045    private static final Log log = LogFactory.getLog(TaskEndedEventListener.class);
046
047    @Override
048    public void handleEvent(Event event) {
049        EventContext eventContext = event.getContext();
050        Serializable property = eventContext.getProperty(TaskService.TASK_INSTANCE_EVENT_PROPERTIES_KEY);
051        if (property == null || !(property instanceof Task)) {
052            // do nothing
053            return;
054        }
055        Task task = (Task) property;
056
057        Boolean validated = Boolean.valueOf(task.getVariable(TaskService.VariableName.validated.name()));
058
059        String chain;
060        if (validated) {
061            chain = task.getVariable(OperationTaskVariableName.acceptOperationChain.name());
062        } else {
063            chain = task.getVariable(OperationTaskVariableName.rejectOperationChain.name());
064        }
065
066        if (!StringUtils.isEmpty(chain)) {
067            try {
068                // run the given operation
069                AutomationService os = Framework.getService(AutomationService.class);
070                OperationContext ctx = new OperationContext(eventContext.getCoreSession());
071                if (eventContext instanceof DocumentEventContext) {
072                    ctx.setInput(((DocumentEventContext) eventContext).getSourceDocument());
073                    ctx.put(OperationTaskVariableName.taskDocument.name(), task.getDocument());
074                }
075                try {
076                    os.run(ctx, chain);
077                } catch (InvalidChainException e) {
078                    log.error("Unknown chain: " + chain);
079                }
080            } catch (OperationException t) {
081                log.error(t, t);
082            }
083        }
084    }
085}