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 editing a workflow
036 */
037public class EditWorkflowClientSideElement extends StaticClientSideElement 
038{
039    private AmetysObjectResolver _resolver;
040
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
046    }
047    
048    /**
049     * Get the status of workflows for edition
050     * @param workflowIds The list of workflows
051     * @return The status of workflows
052     */
053    @SuppressWarnings("unchecked")
054    @Callable
055    public Map<String, Object> getStatus(List<String> workflowIds)
056    {
057        Map<String, Object> results = new HashMap<>();
058        
059        results.put("allright-workflows", new ArrayList<Map<String, Object>>());
060        results.put("noright-workflows", new ArrayList<Map<String, Object>>());
061        results.put("unknown-workflows", new ArrayList<String>());
062        
063        UserIdentity user = _currentUserProvider.getUser();
064        
065        for (String workflowId : workflowIds)
066        {
067            JCRWorkflow workflow = null;
068            try
069            {
070                workflow = _resolver.resolveById(workflowId);
071            }
072            catch (UnknownAmetysObjectException e)
073            {
074                List<String> unknownWorkflow = (List<String>) results.get("unknown-workflows");
075                unknownWorkflow.add(workflowId);
076            }
077            
078            if (workflow != null)
079            {
080                List<Map<String, String>> listWorkflows = null;
081                UserIdentity owner = workflow.getOwner();
082                if ((owner == null || !owner.equals(user)) && _rightManager.currentUserHasRight(BPMWorkflowManager.RIGHT_WORKFLOW_EDIT, null) != RightResult.RIGHT_ALLOW)
083                {
084                    listWorkflows = (List<Map<String, String>>) results.get("noright-workflows");
085                }
086                else
087                {
088                    listWorkflows = (List<Map<String, String>>) results.get("allright-workflows");
089                }
090                
091                Map<String, String> workflowData = new HashMap<>();
092                workflowData.put("id", workflowId);
093                workflowData.put("title", workflow.getTitle());
094                listWorkflows.add(workflowData);
095            }
096        }
097
098        return results;
099    }
100}