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