001/*
002 *  Copyright 2017 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.bpm;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.core.right.RightManager.RightResult;
027import org.ametys.core.ui.Callable;
028import org.ametys.core.ui.StaticClientSideElement;
029import org.ametys.core.user.UserIdentity;
030import org.ametys.plugins.bpm.jcr.JCRWorkflow;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.plugins.repository.UnknownAmetysObjectException;
033
034/**
035 * This element creates a ribbon button to delete a workflow if it is not currently used.
036 */
037public class DeleteWorkflowClientSideElement extends StaticClientSideElement 
038{
039    private BPMWorkflowManager _bpmWorkflowManager;
040    private AmetysObjectResolver _resolver;
041
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _bpmWorkflowManager = (BPMWorkflowManager) manager.lookup(BPMWorkflowManager.ROLE);
047        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
048    }
049    
050    /**
051     * Get the status of workflows for deletion
052     * @param workflowIds The list of workflows
053     * @return The status of workflows
054     */
055    @SuppressWarnings("unchecked")
056    @Callable
057    public Map<String, Object> getStatus(List<String> workflowIds)
058    {
059        Map<String, Object> results = new HashMap<>();
060        
061        results.put("allright-workflows", new ArrayList<Map<String, Object>>());
062        results.put("noright-workflows", new ArrayList<Map<String, Object>>());
063        results.put("inuse-workflows", new ArrayList<Map<String, Object>>());
064        results.put("unknown-workflows", new ArrayList<String>());
065        
066        UserIdentity user = _currentUserProvider.getUser();
067        
068        for (String workflowId : workflowIds)
069        {
070            JCRWorkflow workflow = null;
071            try
072            {
073                workflow = _resolver.resolveById(workflowId);
074            }
075            catch (UnknownAmetysObjectException e)
076            {
077                List<String> unknownWorkflow = (List<String>) results.get("unknown-workflows");
078                unknownWorkflow.add(workflowId);
079            }
080            
081            if (workflow != null)
082            {
083                List<Object> workflowProcessus = _bpmWorkflowManager.getWorkflowProcessus(workflow);
084                List<Map<String, String>> listWorkflows = null;
085                UserIdentity owner = workflow.getOwner();
086                if ((owner == null || !owner.equals(user)) && _rightManager.currentUserHasRight(BPMWorkflowManager.RIGHT_WORKFLOW_DELETE, null) != RightResult.RIGHT_ALLOW)
087                {
088                    listWorkflows = (List<Map<String, String>>) results.get("noright-workflows");
089                }
090                else if (workflowProcessus.size() > 0)
091                {
092                    listWorkflows = (List<Map<String, String>>) results.get("inuse-workflows");
093                }
094                else
095                {
096                    listWorkflows = (List<Map<String, String>>) results.get("allright-workflows");
097                }
098                
099                Map<String, String> workflowData = new HashMap<>();
100                workflowData.put("id", workflowId);
101                workflowData.put("title", workflow.getTitle());
102                listWorkflows.add(workflowData);
103            }
104        }
105
106        return results;
107    }
108}