001/*
002 *  Copyright 2023 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.dao;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.Map.Entry;
023
024import org.apache.avalon.framework.component.Component;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028
029import org.ametys.core.ui.Callable;
030import org.ametys.plugins.workflow.support.WorkflowHelper;
031import org.ametys.runtime.plugin.component.AbstractLogEnabled;
032
033import com.opensymphony.workflow.loader.ActionDescriptor;
034import com.opensymphony.workflow.loader.StepDescriptor;
035import com.opensymphony.workflow.loader.WorkflowDescriptor;
036
037/**
038 * DAO for workflow element's meta attributes
039 */
040public class WorkflowPropertyDAO extends AbstractLogEnabled implements Component, Serviceable
041{
042    /** The workflow helper */
043    protected WorkflowHelper _workflowHelper;
044    
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        _workflowHelper = (WorkflowHelper) smanager.lookup(WorkflowHelper.ROLE);
048    }
049    
050    @SuppressWarnings("unchecked")
051    @Callable(right = "Workflow_Right_Read")
052    /**
053     * Get properties of current step
054     * @param workflowName the workflow unique name
055     * @param stepId id of the current step
056     * @return a map of the properties
057     */
058    public Map<String, Object> getStepProperties(String workflowName, String stepId)
059    {
060        WorkflowDescriptor workflowDescriptor = _workflowHelper.getWorkflowDescriptor(workflowName);
061        StepDescriptor step = workflowDescriptor.getStep(Integer.valueOf(stepId));
062        
063        List<Map<String, Object>> properties2json = new ArrayList<>();
064        
065        if (step != null)
066        {
067            properties2json.addAll(_properties2JSON(step.getMetaAttributes()));
068        }
069        
070        return Map.of("data", properties2json);
071    }
072    
073    @SuppressWarnings("unchecked")
074    @Callable(right = "Workflow_Right_Read")
075    /**
076     * Get properties of current action
077     * @param workflowName the workflow unique name
078     * @param actionId id of the current action
079     * @return a map of the properties
080     */
081    public Map<String, Object> getActionProperties(String workflowName, String actionId)
082    {
083        WorkflowDescriptor workflowDescriptor = _workflowHelper.getWorkflowDescriptor(workflowName);
084        ActionDescriptor action = workflowDescriptor.getAction(Integer.valueOf(actionId));
085        
086        List<Map<String, Object>> propertiesToJson = new ArrayList<>();
087        
088        if (action != null)
089        {
090            propertiesToJson.addAll(_properties2JSON(action.getMetaAttributes()));
091        }
092        
093        return Map.of("data", propertiesToJson);
094    }
095    
096    private List<Map<String, Object>> _properties2JSON(Map<String, Object> metaAttributes)
097    {
098        List<Map<String, Object>> propertiesToJson = new ArrayList<>();
099        
100        for (Entry<String, Object> entry : metaAttributes.entrySet())
101        {
102            Map<String, Object> properties = new HashMap<>();
103            properties.put("key", entry.getKey());
104            properties.put("value", entry.getValue());
105            propertiesToJson.add(properties);
106        }
107        
108        return propertiesToJson;
109    }
110}