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