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 *     Nuxeo - initial API and implementation
016 */
017package org.nuxeo.ecm.platform.routing.api.helper;
018
019import java.io.Serializable;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.nuxeo.ecm.automation.AutomationService;
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.automation.OperationException;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.NuxeoException;
028import org.nuxeo.ecm.platform.routing.api.ActionableObject;
029import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * An actionable validator allows to run an {@link ActionableObject}.
034 *
035 * @deprecated since 5.9.2 - Use only routes of type 'graph'
036 * @author <a href="mailto:arussel@nuxeo.com">Alexandre Russel</a>
037 */
038@Deprecated
039public class ActionableValidator {
040
041    protected ActionableObject actionnable;
042
043    protected CoreSession session;
044
045    protected Map<String, Serializable> additionalProperties = new HashMap<String, Serializable>();
046
047    public ActionableValidator(ActionableObject actionnable, CoreSession session) {
048        this.actionnable = actionnable;
049        this.session = session;
050    }
051
052    public ActionableValidator(ActionableObject actionnable, CoreSession session,
053            Map<String, Serializable> additionalProperties) {
054        this.actionnable = actionnable;
055        this.session = session;
056        this.additionalProperties = additionalProperties;
057    }
058
059    public void validate() {
060        String chainId = actionnable.getValidateOperationChainId();
061        runChain(chainId);
062    }
063
064    public void refuse() {
065        String chainId = actionnable.getRefuseOperationChainId();
066        runChain(chainId);
067    }
068
069    protected void runChain(String chainId) {
070        AutomationService automationService = getAutomationService();
071        OperationContext context = new OperationContext(session);
072        context.put(DocumentRoutingConstants.OPERATION_STEP_DOCUMENT_KEY, actionnable.getDocumentRouteStep(session));
073        context.setInput(actionnable.getAttachedDocuments(session));
074        context.putAll(additionalProperties);
075        try {
076            automationService.run(context, chainId);
077        } catch (OperationException e) {
078            throw new NuxeoException(e);
079        }
080    }
081
082    protected AutomationService getAutomationService() {
083        return Framework.getService(AutomationService.class);
084    }
085
086}