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