001/*
002 *  Copyright 2024 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.HashMap;
019import java.util.LinkedHashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.configuration.Configurable;
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.commons.lang3.StringUtils;
029
030import org.ametys.plugins.workflow.support.WorkflowHelper;
031import org.ametys.runtime.i18n.I18nizableText;
032import org.ametys.runtime.model.Enumerator;
033
034/**
035 * {@link Enumerator} for the default workflow names
036 */
037public class WorkflowNameEnumerator implements Enumerator<String>, org.ametys.runtime.parameter.Enumerator, Serviceable, Configurable
038{
039    /** The workflow helper */
040    protected WorkflowHelper _workflowHelper;
041    
042    /**
043     * All option : 'disabled' to disable the 'all' option,
044     * 'blank' to use a blank value for 'all' option,
045     * or 'concat' to concat all content types' ids for 'all' option
046     **/
047    protected String _allOption;
048    
049    @Override
050    public void service(ServiceManager smanager) throws ServiceException
051    {
052        _workflowHelper = (WorkflowHelper) smanager.lookup(WorkflowHelper.ROLE);
053    }
054    
055    @Override
056    public void configure(Configuration configuration) throws ConfigurationException
057    {
058        Configuration customConf = configuration.getChild("enumeration").getChild("custom-enumerator");
059        
060        Configuration allOptConf = customConf.getChild("all-option", false);
061        _allOption = allOptConf != null ? allOptConf.getValue("blank") : "blank";
062    }
063    
064    @Override
065    public Map<String, Object> getConfiguration()
066    {
067        Map<String, Object> config = new HashMap<>();
068        config.put("allOption", _allOption);
069        return config;
070    }
071    
072    @SuppressWarnings("unchecked")
073    public Map<Object, I18nizableText> getEntries() throws Exception
074    {
075        return (Map<Object, I18nizableText>) (Object) getTypedEntries();
076    }
077    
078    @Override
079    public Map<String, I18nizableText> getTypedEntries() throws Exception
080    {
081        Map<String, I18nizableText> entries = new LinkedHashMap<>();
082        
083        // All workflows
084        if (!_allOption.equals("disabled"))
085        {
086            entries.put(StringUtils.EMPTY, new I18nizableText("plugin.cms", "WIDGET_COMBOBOX_ALL_OPTIONS"));
087        }
088        
089        for (String workflowName : _workflowHelper.getWorkflowNames())
090        {
091            entries.put(workflowName, _workflowHelper.getWorkflowLabel(workflowName));
092        }
093        
094        return entries;
095    }
096    
097    @Override
098    public I18nizableText getEntry(String value) throws Exception
099    {
100        return _workflowHelper.getWorkflowLabel(value);
101    }
102}