001/*
002 *  Copyright 2018 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.userdirectory.clientsideelement;
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.cms.clientsideelement.DeleteContentClientSideElement;
027import org.ametys.cms.repository.Content;
028import org.ametys.core.ui.Callable;
029import org.ametys.plugins.userdirectory.DeleteOrgUnitComponent;
030import org.ametys.plugins.userdirectory.OrganisationChartPageHandler;
031
032/**
033 * This element creates an action button to delete a orgUnit content
034 */
035public class DeleteOrgUnitClientSideElement extends DeleteContentClientSideElement
036{
037    /** The organisation chart page handler */
038    protected OrganisationChartPageHandler _oCPageHandler;
039    
040    /** The delete orgUnit component */
041    protected DeleteOrgUnitComponent _deleteOrgUnitComponent;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _oCPageHandler = (OrganisationChartPageHandler) smanager.lookup(OrganisationChartPageHandler.ROLE);
048        _deleteOrgUnitComponent = (DeleteOrgUnitComponent) smanager.lookup(DeleteOrgUnitComponent.ROLE);
049    }
050    
051    @Override
052    protected boolean _isContentReferenced(Content content)
053    {
054        return _oCPageHandler.isReferencedOrgUnit(content);
055    }
056    
057    /**
058     * Delete orgUnits contents
059     * @param contentsId The ids of contents to delete
060     * @param parameters the additional parameters
061     * @return the deleted and undeleted contents
062     */
063    @SuppressWarnings("unchecked")
064    @Callable
065    public Map<String, Object> deleteContents(List<String> contentsId, Map<String, Object> parameters)
066    {
067        Map<String, Object> results = new HashMap<>();
068        
069        Map<String, Map<String, Object>> initialContentParameters = new HashMap<>();
070        for (String contentId : contentsId)
071        {
072            initialContentParameters.put(contentId, getContentDefaultParameters(_resolver.resolveById(contentId)));
073        }
074        
075        Map<String, Object> deleteResults = _deleteOrgUnitComponent.deleteContents(contentsId, parameters, _rights);
076        for (String contentId : contentsId)
077        {
078            Map<String, Object> result = new HashMap<>();
079            Map<String, Object> deleteResult = (Map<String, Object>) deleteResults.get(contentId);
080            if (deleteResult.containsKey("check-before-deletion-failed"))
081            {
082                result.put("check-before-deletion-failed", deleteResult.get("check-before-deletion-failed"));
083            }
084            
085            String initialContentId = (String) deleteResult.get("initial-content");
086            result.put("initial-content", initialContentParameters.get(initialContentId));
087            
088            List<Map<String, Object>> deletedContents = new ArrayList<>();
089            for (String deleteContentId : (List<String>) deleteResult.get("deleted-contents"))
090            {
091                Map<String, Object> deleteParameters = new HashMap<>();
092                deleteParameters.put("id", deleteContentId);
093                deletedContents.add(deleteParameters);
094            }
095            result.put("deleted-contents", deletedContents);
096            
097            List<Map<String, Object>> undeletedContents = new ArrayList<>();
098            for (Content content : (List<Content>) deleteResult.get("undeleted-contents"))
099            {
100                undeletedContents.add(getContentDefaultParameters(content));
101            }
102            result.put("undeleted-contents", undeletedContents);
103            
104            List<Map<String, Object>> referencedContents = new ArrayList<>();
105            for (Content content : (List<Content>) deleteResult.get("referenced-contents"))
106            {
107                referencedContents.add(getContentDefaultParameters(content));
108            }
109            result.put("referenced-contents", referencedContents);
110            
111            List<Map<String, Object>> unauthorizedContents = new ArrayList<>();
112            for (Content content : (List<Content>) deleteResult.get("unauthorized-contents"))
113            {
114                Map<String, Object> contentDefaultParameters = getContentDefaultParameters(content);
115                contentDefaultParameters.put("description", _getNoRightDescription(content));
116                unauthorizedContents.add(contentDefaultParameters);
117            }
118            result.put("unauthorized-contents", unauthorizedContents);
119            
120            List<Map<String, Object>> lockedContents = new ArrayList<>();
121            for (Content content : (List<Content>) deleteResult.get("locked-contents"))
122            {
123                Map<String, Object> contentDefaultParameters = getContentDefaultParameters(content);
124                contentDefaultParameters.put("description", _getLockedDescription(content));
125                lockedContents.add(contentDefaultParameters);
126            }
127            result.put("locked-contents", lockedContents);
128            
129            results.put(contentId, result);
130        }
131        
132        return results;
133    }
134}