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