001/*
002 *  Copyright 2010 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;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.LinkedHashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.configuration.Configurable;
025import org.apache.avalon.framework.configuration.Configuration;
026import org.apache.avalon.framework.configuration.ConfigurationException;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.avalon.framework.service.Serviceable;
030import org.apache.commons.lang3.StringUtils;
031
032import org.ametys.plugins.workflow.support.WorkflowHelper;
033import org.ametys.runtime.i18n.I18nizableText;
034import org.ametys.runtime.model.Enumerator;
035
036import com.opensymphony.workflow.loader.StepDescriptor;
037import com.opensymphony.workflow.loader.WorkflowDescriptor;
038
039/**
040 * {@link Enumerator} for the default workflow steps
041 *
042 */
043public class DefaultWorkflowStepEnumerator implements Enumerator<String>, org.ametys.runtime.parameter.Enumerator, Serviceable, Configurable
044{
045    /** The workflow helper */
046    protected WorkflowHelper _workflowHelper;
047    
048    /** The workflow name */
049    protected String _workflowName;
050
051    /** All option : 'disabled' to disable the 'all' option, 
052     * 'blank' to use a blank value for 'all' option,
053     * or 'concat' to concat all content types' ids for 'all' option
054     **/
055    protected String _allOption;
056    
057    /** The workflow step to exclude */
058    protected List<Integer> _excludedSteps;
059    
060    @Override
061    public void service(ServiceManager smanager) throws ServiceException
062    {
063        _workflowHelper = (WorkflowHelper) smanager.lookup(WorkflowHelper.ROLE);
064    }
065    
066    @Override
067    public void configure(Configuration configuration) throws ConfigurationException
068    {
069        Configuration customConf = configuration.getChild("enumeration").getChild("custom-enumerator");
070        
071        _workflowName = customConf.getChild("workflow-name").getValue("content");
072        
073        Configuration allOptConf = customConf.getChild("all-option", false);
074        _allOption = allOptConf != null ? allOptConf.getValue("blank") : "blank";
075        
076        _excludedSteps = new ArrayList<>();
077        
078        Configuration excludeConf = customConf.getChild("exclude-workflow-steps", false);
079        if (excludeConf != null)
080        {
081            Configuration[] children = excludeConf.getChildren("id");
082            for (Configuration step : children)
083            {
084                _excludedSteps.add(step.getValueAsInteger());
085            }
086        }
087    }
088    
089    @Override
090    public Map<String, Object> getConfiguration()
091    {
092        Map<String, Object> config = new HashMap<>();
093        
094        config.put("workflowName", _workflowName);
095        config.put("allOption", _allOption);
096        config.put("excludedSteps", _excludedSteps);
097        
098        return config;
099    }
100    
101    public Map<Object, I18nizableText> getEntries() throws Exception
102    {
103        return (Map<Object, I18nizableText>) (Object) getTypedEntries();
104    }
105    
106    @Override
107    public Map<String, I18nizableText> getTypedEntries() throws Exception
108    {
109        Map<String, I18nizableText> entries = new LinkedHashMap<>();
110        
111        // All steps
112        if (!_allOption.equals("disabled"))
113        {
114            entries.put(StringUtils.EMPTY, new I18nizableText("plugin.cms", "WIDGET_COMBOBOX_ALL_OPTIONS"));
115        }
116        
117        WorkflowDescriptor workflowDesc = _workflowHelper.getWorkflowDescriptor(_workflowName);
118        List<StepDescriptor> steps = workflowDesc.getSteps();
119        
120        for (StepDescriptor stepDescriptor : steps)
121        {
122            int stepId = stepDescriptor.getId();
123            if (!_excludedSteps.contains(stepId))
124            {
125                entries.put(String.valueOf(stepId), new I18nizableText("application", stepDescriptor.getName()));
126            }
127        }
128        
129        return entries;
130    }
131    
132    @Override
133    public I18nizableText getEntry(String value) throws Exception
134    {
135        WorkflowDescriptor workflowDesc = _workflowHelper.getWorkflowDescriptor(_workflowName);
136        StepDescriptor stepDescriptor = workflowDesc.getStep(Integer.parseInt(value));
137        return new I18nizableText("application", stepDescriptor.getName());
138    }
139}