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