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        if (!_workflowDefEP.hasExtension(name))
077        {
078            throw new FactoryException("Unknown workflow name \"" + name + '\"');
079        }
080        
081        WorkflowDefinition definition = _workflowDefEP.getExtension(name);
082        try
083        {
084            return definition.getDescriptor(validate);
085        }
086        catch (Exception e)
087        {
088            throw new FactoryException("Error in workflow descriptor: " + definition.getLocation(), e);
089        }
090    }
091
092    public String[] getWorkflowNames() throws FactoryException
093    {
094        return _workflowDefEP.getExtensionsIds().toArray(String[]::new);
095    }
096
097    public void createWorkflow(String name)
098    {
099        // Nothing to do
100    }
101
102    public boolean removeWorkflow(String name) throws FactoryException
103    {
104        throw new FactoryException("remove workflow not supported");
105    }
106
107    public void renameWorkflow(String oldName, String newName)
108    {
109        // Nothing to do
110    }
111
112    public void save()
113    {
114        // Nothing to do
115    }
116
117    public boolean saveWorkflow(String name, WorkflowDescriptor descriptor, boolean replace) throws FactoryException
118    {
119        throw new FactoryException("save workflow not supported");
120    }
121}