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.process;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.acting.ServiceableAction;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029import org.apache.commons.lang3.StringUtils;
030
031import org.ametys.cms.transformation.xslt.ResolveURIComponent;
032import org.ametys.core.cocoon.JSonReader;
033import org.ametys.plugins.bpm.BPMWorkflowManager;
034import org.ametys.plugins.bpm.jcr.JCRWorkflowProcess;
035import org.ametys.plugins.repository.AmetysObjectResolver;
036import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
037import org.ametys.web.repository.page.Page;
038
039/**
040 * Action for deleting a process
041 */
042public class DeleteProcessAction extends ServiceableAction
043{
044    private static final String PARAM_PROCESS_ID = "processId";
045
046    private BPMWorkflowManager _bpmWorkflowManager;
047    
048    private AmetysObjectResolver _resolver;
049
050    
051    @Override
052    public void service(ServiceManager smanager) throws ServiceException
053    {
054        super.service(smanager);
055        _bpmWorkflowManager = (BPMWorkflowManager) smanager.lookup(BPMWorkflowManager.ROLE);
056        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
057    }
058
059    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
060    {
061        Map<String, String> result = new HashMap<>();
062        Request request = ObjectModelHelper.getRequest(objectModel);
063
064        String processId = request.getParameter(PARAM_PROCESS_ID);
065        
066        if (StringUtils.isEmpty(processId))
067        {
068            throw new IllegalArgumentException("Missing mandatory arguments for deleting a process");
069        }
070
071        String siteName = request.getParameter("siteName");
072        String lang = request.getParameter("lang");
073        
074        String currentWorkspace = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
075        try
076        {
077            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, "default");
078            
079            JCRWorkflowProcess process = _resolver.resolveById(processId);
080            result.put("title", process.getTitle());
081            
082            _bpmWorkflowManager.deleteProcess(process);
083        }
084        finally
085        {
086            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWorkspace);
087        }
088        
089        Page dashboardPage = _bpmWorkflowManager.getDashboardPage(siteName, lang);
090        
091        if (dashboardPage != null)
092        {
093            String path = ResolveURIComponent.resolve("page", dashboardPage.getId());
094            result.put("dashboardPageId", dashboardPage.getId());
095            result.put("dashboardPagePath", path);
096        }
097        
098        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
099        return EMPTY_MAP;
100    }
101}