001/*
002 *  Copyright 2018 Anyware Services
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 */
016package org.ametys.cms.workflow.extensions;
017
018import java.util.Collections;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024import org.apache.commons.lang3.StringUtils;
025
026import org.ametys.runtime.plugin.ExtensionPoint;
027import org.ametys.runtime.plugin.component.AbstractLogEnabled;
028
029import com.opensymphony.module.propertyset.PropertySet;
030import com.opensymphony.workflow.FunctionProvider;
031import com.opensymphony.workflow.WorkflowException;
032
033/**
034 * Workflow function that will call each function registered on the extension point given as an argument 
035 */
036public class ExtensibleFunction extends AbstractLogEnabled implements FunctionProvider, Serviceable
037{
038    /** The rights key. */
039    protected static final String EXTENSION_POINT_ROLE_ARGS_KEY = "extension-point";
040    /** The service manager */
041    protected ServiceManager _manager;
042    
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        _manager = manager;
046    }
047    
048    
049    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
050    {
051        ExtensionPoint<String> functionsExtensionPoint = _getFunctionsExtensionPoint(args);
052        for (String functionId : functionsExtensionPoint.getExtensionsIds())
053        {
054            getLogger().debug("{} is delegating to extension {} of point {}", ExtensibleFunction.class.getName(), functionId);
055            String functionProviderRole = functionsExtensionPoint.getExtension(functionId);
056            FunctionProvider functionProvider = _getFunctionProvider(functionId, functionProviderRole);
057            functionProvider.execute(transientVars, Collections.emptyMap(), ps); // We do not transmit 'args', since those args are for this function, not for the extended functions that thus cannot use args
058        }
059
060    }
061    
062    /**
063     * Get the function provider denoted
064     * @param functionId The id of the extension that gave the role
065     * @param functionProviderRole The role of the component to find
066     * @return The workflow function
067     * @throws WorkflowException If the role cannot be found
068     */
069    protected FunctionProvider _getFunctionProvider(String functionId, String functionProviderRole) throws WorkflowException
070    {
071        try
072        {
073            return (FunctionProvider) _manager.lookup(functionProviderRole);
074        }
075        catch (ServiceException e)
076        {
077            throw new WorkflowException("Cannot find workflow function with role '" + functionProviderRole + "' configured as an extension with id '" + functionId + "'", e);
078        }
079    }
080
081
082    /**
083     * Get the function extension point given in args
084     * @param args The workflow arguments
085     * @return The extension point
086     * @throws WorkflowException If the extension point is not configured or does not exist
087     */
088    @SuppressWarnings("unchecked")
089    protected ExtensionPoint<String> _getFunctionsExtensionPoint(Map args) throws WorkflowException
090    {
091        String extensionPoint = (String) args.get(EXTENSION_POINT_ROLE_ARGS_KEY);
092        if (StringUtils.isBlank(extensionPoint))
093        {
094            throw new WorkflowException("Cannot use the workflow function '" + ExtensibleFunction.class.getName() + "' without the argument '" + EXTENSION_POINT_ROLE_ARGS_KEY + "' describing the role of the extension point of function components to use");
095        }
096        
097        try
098        {
099            return (ExtensionPoint<String>) _manager.lookup(extensionPoint);
100        }
101        catch (ServiceException e)
102        {
103            throw new WorkflowException("The workflow function '" + ExtensibleFunction.class.getName() + "' has the argument '" + EXTENSION_POINT_ROLE_ARGS_KEY + "' valued to '" + extensionPoint + "', that does not denote a String extension point", e);
104        }
105    }
106
107}