001/*
002 *  Copyright 2021 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.workflow;
017
018import org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.service.ServiceException;
020import org.apache.avalon.framework.service.ServiceManager;
021import org.apache.avalon.framework.service.Serviceable;
022
023import org.ametys.plugins.workflow.definition.WorkflowDefinition;
024import org.ametys.plugins.workflow.definition.WorkflowDefinitionExtensionPoint;
025import org.ametys.runtime.i18n.I18nizableText;
026
027import com.opensymphony.workflow.FactoryException;
028import com.opensymphony.workflow.loader.AbstractWorkflowFactory;
029import com.opensymphony.workflow.loader.WorkflowDescriptor;
030import com.opensymphony.workflow.loader.WorkflowFactory;
031
032/**
033 * The {@link WorkflowFactory} implementation for Ametys.
034 * Check the {@link WorkflowDefinitionExtensionPoint} and add the workflows from WEB-INF/param/workflows.
035 */
036public class AmetysWorkflowFactory extends AbstractWorkflowFactory implements Component, Serviceable
037{
038    /** Avalon role. */
039    public static final String ROLE = AmetysWorkflowFactory.class.getName();
040    
041    /** The workflow definition extension point */
042    protected WorkflowDefinitionExtensionPoint _workflowDefEP;
043    
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _workflowDefEP = (WorkflowDefinitionExtensionPoint) manager.lookup(WorkflowDefinitionExtensionPoint.ROLE);
047    }
048    
049    @Override
050    public void initDone() throws FactoryException
051    {
052        // Nothing to do
053    }
054    
055    public void setLayout(String workflowName, Object layout)
056    {
057        // Nothing to do
058    }
059
060    public Object getLayout(String workflowName)
061    {
062        return null;
063    }
064
065    public boolean isModifiable(String name)
066    {
067        return true;
068    }
069
070    public String getName()
071    {
072        return null;
073    }
074
075    public WorkflowDescriptor getWorkflow(String name, boolean validate) throws FactoryException
076    {
077        WorkflowDefinition definition = _getWorkflowDefinition(name);
078        try
079        {
080            return definition.getDescriptor(validate);
081        }
082        catch (Exception e)
083        {
084            throw new FactoryException("Error in workflow descriptor: " + definition.getLocation(), e);
085        }
086    }
087
088    public String[] getWorkflowNames() throws FactoryException
089    {
090        return _workflowDefEP.getExtensionsIds().toArray(String[]::new);
091    }
092
093    public void createWorkflow(String name)
094    {
095        // Nothing to do
096    }
097
098    public boolean removeWorkflow(String name) throws FactoryException
099    {
100        throw new FactoryException("remove workflow not supported");
101    }
102
103    public void renameWorkflow(String oldName, String newName)
104    {
105        // Nothing to do
106    }
107
108    public void save()
109    {
110        // Nothing to do
111    }
112
113    public boolean saveWorkflow(String name, WorkflowDescriptor descriptor, boolean replace) throws FactoryException
114    {
115        throw new FactoryException("save workflow not supported");
116    }
117    
118    /**
119     * Get the workflow label.
120     * @param name The name of the workflow
121     * @return The label of the workflow with an i18n format.
122     * @throws FactoryException if an exception occurs
123     */
124    public I18nizableText getWorkflowLabel(String name) throws FactoryException
125    {
126        return _getWorkflowDefinition(name).getLabel();
127    }
128    
129    private WorkflowDefinition _getWorkflowDefinition(String name) throws FactoryException
130    {
131        if (!_workflowDefEP.hasExtension(name))
132        {
133            throw new FactoryException("Unknown workflow name \"" + name + '\"');
134        }
135        
136        return _workflowDefEP.getExtension(name);
137    }
138}