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;
030
031import org.ametys.plugins.workflow.support.WorkflowHelper;
032import org.ametys.runtime.i18n.I18nizableText;
033import org.ametys.runtime.model.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<Long>, Serviceable, Configurable
043{
044    /** The workflow helper */
045    protected WorkflowHelper _workflowHelper;
046    
047    /** The workflow name */
048    protected String _workflowName;
049
050    /** The workflow step to exclude */
051    protected List<Integer> _excludedSteps;
052    
053    @Override
054    public void service(ServiceManager smanager) throws ServiceException
055    {
056        _workflowHelper = (WorkflowHelper) smanager.lookup(WorkflowHelper.ROLE);
057    }
058    
059    @Override
060    public void configure(Configuration configuration) throws ConfigurationException
061    {
062        Configuration customConf = configuration.getChild("enumeration").getChild("custom-enumerator");
063        
064        _workflowName = customConf.getChild("workflow-name").getValue("content");
065        
066        _excludedSteps = new ArrayList<>();
067        
068        Configuration excludeConf = customConf.getChild("exclude-workflow-steps", false);
069        if (excludeConf != null)
070        {
071            Configuration[] children = excludeConf.getChildren("id");
072            for (Configuration step : children)
073            {
074                _excludedSteps.add(step.getValueAsInteger());
075            }
076        }
077    }
078    
079    @Override
080    public Map<String, Object> getConfiguration()
081    {
082        Map<String, Object> config = new HashMap<>();
083        
084        config.put("workflowName", _workflowName);
085        config.put("excludedSteps", _excludedSteps);
086        
087        return config;
088    }
089    
090    @Override
091    public Map<Long, I18nizableText> getEntries() throws Exception
092    {
093        Map<Long, I18nizableText> entries = new LinkedHashMap<>();
094        
095        WorkflowDescriptor workflowDesc = _workflowHelper.getWorkflowDescriptor(_workflowName);
096        List<StepDescriptor> steps = workflowDesc.getSteps();
097        
098        for (StepDescriptor stepDescriptor : steps)
099        {
100            int stepId = stepDescriptor.getId();
101            if (!_excludedSteps.contains(stepId))
102            {
103                entries.put(Long.valueOf(stepId), new I18nizableText("application", stepDescriptor.getName()));
104            }
105        }
106        
107        return entries;
108    }
109    
110    @Override
111    public I18nizableText getEntry(Long value) throws Exception
112    {
113        WorkflowDescriptor workflowDesc = _workflowHelper.getWorkflowDescriptor(_workflowName);
114        StepDescriptor stepDescriptor = workflowDesc.getStep(value.intValue());
115        return new I18nizableText("application", stepDescriptor.getName());
116    }
117}