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