001/*
002 *  Copyright 2016 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.plugins.forms.workflow;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
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;
027
028import org.ametys.core.ui.StaticClientSideElement;
029import org.ametys.plugins.workflow.store.JdbcWorkflowStore;
030import org.ametys.plugins.workflow.support.WorkflowProvider;
031import org.ametys.runtime.i18n.I18nizableText;
032
033import com.opensymphony.workflow.Workflow;
034
035/**
036 * This class allows to gather the available form-dedicated workflows
037 */
038public class FormWorkflowClientSideElement extends StaticClientSideElement
039{
040    /** The Workflow provider */
041    private WorkflowProvider _workflowProvider;
042    
043    @Override
044    public void service(ServiceManager serviceManager) throws ServiceException
045    {   
046        super.service(serviceManager);
047        _workflowProvider = (WorkflowProvider) serviceManager.lookup(WorkflowProvider.ROLE);
048    }   
049    
050    @Override
051    public Map<String, Object> configureInitialParameters(Configuration configuration) throws ConfigurationException
052    {
053        Map<String, Object> initialParameters = new HashMap<>(super.configureInitialParameters(configuration));
054
055        Workflow workflow = _workflowProvider.getExternalWorkflow(JdbcWorkflowStore.ROLE);
056        String[] workflowNames = workflow.getWorkflowNames();
057        
058        Map<String, Object> workflowBuffer = new HashMap<> ();
059        List<Map<String, Object>> workflowsList = new ArrayList<> ();
060        
061        workflowBuffer.put("label", new I18nizableText("plugin.forms", "PLUGINS_FORMS_FORMS_EDITOR_WORKFLOW_NO_WORKFLOW"));
062        workflowBuffer.put("value", "");
063        workflowsList.add(workflowBuffer);    
064        
065        for (String workflowName : workflowNames)
066        {
067            if (workflowName.startsWith("form-"))
068            {
069                workflowBuffer = new HashMap<>();
070                workflowBuffer.put("value", new I18nizableText(workflowName));
071                workflowBuffer.put("label", new I18nizableText("application", "WORKFLOW_" + workflowName));
072                workflowsList.add(workflowBuffer);   
073            }
074        }
075
076        initialParameters.put("data", workflowsList);
077        
078        return initialParameters;
079    }
080}