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.LinkedHashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.avalon.framework.component.Component; 025import org.apache.avalon.framework.logger.AbstractLogEnabled; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.avalon.framework.service.Serviceable; 029 030import org.ametys.core.ui.Callable; 031import org.ametys.plugins.workflow.support.WorkflowHelper; 032import org.ametys.runtime.i18n.I18nizableText; 033 034/** 035 * DAO for managing workflows 036 */ 037public class WorkflowsDAO extends AbstractLogEnabled implements Serviceable, Component 038{ 039 /** The Avalon role */ 040 public static final String ROLE = WorkflowsDAO.class.getName(); 041 042 /** The workflow helper */ 043 protected WorkflowHelper _workflowHelper; 044 045 public void service(ServiceManager manager) throws ServiceException 046 { 047 _workflowHelper = (WorkflowHelper) manager.lookup(WorkflowHelper.ROLE); 048 } 049 050 /** 051 * Get the workflow properties 052 * @param workflowName the name of the workflow to get 053 * @return the workflow properties 054 */ 055 @Callable(right = "Workflow_Right_Read") 056 public Map<String, Object> getWorkflowRootProperties(String workflowName) 057 { 058 Map<String, Object> infos = new HashMap<>(); 059 I18nizableText workflowLabel = _workflowHelper.getWorkflowLabel(workflowName); 060 infos.put("id", workflowName); 061 infos.put("label", workflowLabel); 062 infos.put("hasChildren", _workflowHelper.getWorkflowDescriptor(workflowName).getSteps().size() > 0); 063 return infos; 064 } 065 066 /** 067 * Get the list of all workflows 068 * @return a map with workflow's list as value 069 */ 070 @Callable(right = "Workflow_Right_Read") 071 public Map<String, Object> getWorkflowsList() 072 { 073 List<Map<String, Object>> workflows2json = new ArrayList<>(); 074 075 String[] workflowNames = _workflowHelper.getWorkflowNames(); 076 int nbWorkflows = workflowNames.length; 077 int currentLimit = 0; 078 while (currentLimit < nbWorkflows) 079 { 080 Map<String, Object> workflowData = new LinkedHashMap<>(); 081 String workflowName = workflowNames[currentLimit]; 082 workflowData.put("title", _workflowHelper.getWorkflowLabel(workflowName)); 083 workflowData.put("id", workflowName); 084 workflows2json.add(workflowData); 085 currentLimit++; 086 } 087 088 return Map.of("workflows", workflows2json); 089 } 090 091}