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