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.lang.StringUtils;
031
032import org.ametys.plugins.workflow.support.WorkflowHelper;
033import org.ametys.runtime.i18n.I18nizableText;
034import org.ametys.runtime.parameter.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, 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    
102    @Override
103    public Map<Object, I18nizableText> getEntries() throws Exception
104    {
105        Map<Object, I18nizableText> entries = new LinkedHashMap<>();
106        
107        // All steps
108        if (!_allOption.equals("disabled"))
109        {
110            String value = _allOption.equals("concat") ? StringUtils.join(entries.keySet(), ',') : "";
111            entries.put(value, new I18nizableText("plugin.cms", "WIDGET_COMBOBOX_ALL_OPTIONS"));
112        }
113        
114        WorkflowDescriptor workflowDesc = _workflowHelper.getWorkflowDescriptor(_workflowName);
115        List<StepDescriptor> steps = workflowDesc.getSteps();
116        
117        for (StepDescriptor stepDescriptor : steps)
118        {
119            int stepId = stepDescriptor.getId();
120            if (!_excludedSteps.contains(stepId))
121            {
122                entries.put(stepId, new I18nizableText("application", stepDescriptor.getName()));
123            }
124        }
125        
126        return entries;
127    }
128
129    @Override
130    public I18nizableText getEntry(String value) throws Exception
131    {
132        WorkflowDescriptor workflowDesc = _workflowHelper.getWorkflowDescriptor(_workflowName);
133        StepDescriptor stepDescriptor = workflowDesc.getStep(Integer.parseInt(value));
134        return new I18nizableText("application", stepDescriptor.getName());
135    }
136
137}