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.odf.tree;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.repository.Content;
024import org.ametys.plugins.workflow.repository.WorkflowAwareAmetysObject;
025import org.ametys.plugins.workflow.support.WorkflowProvider;
026import org.ametys.plugins.workflow.support.WorkflowProvider.AmetysObjectWorkflow;
027import org.ametys.runtime.i18n.I18nizableText;
028
029import com.opensymphony.workflow.loader.StepDescriptor;
030import com.opensymphony.workflow.loader.WorkflowDescriptor;
031
032/**
033 * {@link ODFTreeIndicator} to display the workflow step
034 *
035 */
036public class WorkflowStepIndicator extends AbstractStaticODFTreeIndicator 
037{
038    private WorkflowProvider _workflowProvider;
039
040    @Override
041    public void service(ServiceManager smanager) throws ServiceException
042    {
043        super.service(smanager);
044        _workflowProvider = (WorkflowProvider) smanager.lookup(WorkflowProvider.ROLE);
045    }
046    
047    public IndicatorData getIndicatorData(Content content)
048    {
049        if (content instanceof WorkflowAwareAmetysObject waContent)
050        {
051            long workflowId = waContent.getWorkflowId();
052            int currentStepId = Math.toIntExact(waContent.getCurrentStepId());
053        
054            AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(waContent);
055        
056            String workflowName = workflow.getWorkflowName(workflowId);
057            WorkflowDescriptor workflowDescriptor = workflow.getWorkflowDescriptor(workflowName);
058        
059            if (workflowDescriptor != null)
060            {
061                StepDescriptor stepDescriptor = workflowDescriptor.getStep(currentStepId);
062                if (stepDescriptor != null)
063                {
064                    return getIndicatorData(content, currentStepId, stepDescriptor);
065                }
066            }
067        }
068        
069        return null;
070    }
071    
072    /**
073     * Get indicator for the given workflow step
074     * @param content the content
075     * @param stepId the step id
076     * @param stepDescriptor the step descriptor
077     * @return the indicator data
078     */
079    protected IndicatorData getIndicatorData(Content content, int stepId, StepDescriptor stepDescriptor)
080    {
081        I18nizableText workflowStepName = new I18nizableText("application", stepDescriptor.getName());
082        
083        switch (stepId)
084        {
085            case 3:
086                return new IndicatorData(workflowStepName, null, "green-color ametysicon-sign-raw-check", Map.of());
087            case 2:
088                return new IndicatorData(workflowStepName, null, "orange-color ametysicon-body-part-finger", Map.of());
089            default:
090                return new IndicatorData(workflowStepName, null, "orange-color ametysicon-edit45", Map.of());
091        }
092    }
093}