001/*
002 *  Copyright 2010 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.odf.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024import java.util.stream.Collectors;
025
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028
029import org.ametys.cms.repository.Content;
030import org.ametys.core.ui.Callable;
031import org.ametys.odf.ProgramItem;
032import org.ametys.odf.helper.DeleteODFContentHelper;
033import org.ametys.odf.orgunit.OrgUnit;
034import org.ametys.odf.orgunit.RootOrgUnitProvider;
035
036/**
037 * This element creates an action button to delete a ODF content
038 */
039public class DeleteContentClientSideElement extends org.ametys.cms.clientsideelement.DeleteContentClientSideElement
040{
041    /** The root orgunit provider */
042    protected RootOrgUnitProvider _rootOrgUnitProvider;
043    
044    /** The delete ODF content helper */
045    protected DeleteODFContentHelper _deleteODFContentHelper;
046
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        super.service(manager);
051        _rootOrgUnitProvider = (RootOrgUnitProvider) manager.lookup(RootOrgUnitProvider.ROLE);
052        _deleteODFContentHelper = (DeleteODFContentHelper) manager.lookup(DeleteODFContentHelper.ROLE);
053    }
054
055    @Override
056    protected boolean _hasRight(Content content)
057    {
058        return _deleteODFContentHelper.hasRight(content);
059    }
060    
061    @Override
062    protected boolean _isModifiable(Content content)
063    {
064        if (content instanceof OrgUnit && _rootOrgUnitProvider.isRoot(content.getId()))
065        {
066            // The root orgunit can not be deleted
067            return false;
068        }
069        
070        return super._isModifiable(content);
071    }
072    
073    @Override
074    protected boolean _isContentReferenced(Content content)
075    {
076        return _deleteODFContentHelper.isContentReferenced(content);
077    }
078    
079    /**
080     * Delete ODF contents
081     * @param contentsId The ids of contents to delete
082     * @param parameters the additional parameters
083     * @return the deleted and undeleted contents
084     */
085    @SuppressWarnings("unchecked")
086    @Callable
087    public Map<String, Object> deleteContents(List<String> contentsId, Map<String, Object> parameters)
088    {
089        Map<String, Map<String, Object>> initialContentParameters = new HashMap<>();
090        for (String contentId : contentsId)
091        {
092            initialContentParameters.put(contentId, getContentDefaultParameters(_resolver.resolveById(contentId)));
093        }
094        
095        Map<String, Object> deleteResults = _deleteODFContentHelper.deleteContents(contentsId, (String) parameters.get("mode"));
096        
097        Map<String, Object> updatedResults = new HashMap<>();
098        Set<Content> allReferencedContents = new HashSet<>();
099        Set<Content> allLockedContents = new HashSet<>();
100        Set<Content> allUnauthorizedContents = new HashSet<>();
101        Set<Content> allUnDeletedContents = new HashSet<>();
102        for (String deletedContentId : deleteResults.keySet())
103        {
104            Map<String, Object> deleteResult = (Map<String, Object>) deleteResults.get(deletedContentId);
105            Map<String, Object> updatedContentResults = new HashMap<>();
106            
107            if (deleteResult.containsKey("check-before-deletion-failed"))
108            {
109                updatedContentResults.put("check-before-deletion-failed", deleteResult.get("check-before-deletion-failed"));
110            }
111            
112            String initialContentId = (String) deleteResult.get("initial-content");
113            updatedContentResults.put("initial-content", initialContentParameters.get(initialContentId));
114            
115            List<Map<String, Object>> deletedContents = new ArrayList<>();
116            for (String deleteContentId : (Set<String>) deleteResult.get("deleted-contents"))
117            {
118                Map<String, Object> deleteParameters = new HashMap<>();
119                deleteParameters.put("id", deleteContentId);
120                deletedContents.add(deleteParameters);
121            }
122            updatedContentResults.put("deleted-contents", deletedContents);
123            
124            List<Map<String, Object>> undeletedContents = new ArrayList<>();
125            for (Content content : (Set<Content>) deleteResult.get("undeleted-contents"))
126            {
127                allUnDeletedContents.add(content);
128                undeletedContents.add(getContentDefaultParameters(content));
129            }
130            updatedContentResults.put("undeleted-contents", undeletedContents);
131            
132            List<Map<String, Object>> referencedContents = new ArrayList<>();
133            for (Content content : (Set<Content>) deleteResult.get("referenced-contents"))
134            {
135                allReferencedContents.add(content);
136                referencedContents.add(getContentDefaultParameters(content));
137            }
138            updatedContentResults.put("referenced-contents", referencedContents);
139            
140            // Update the description for locked contents
141            List<Map<String, Object>> updatedLockedContents = new ArrayList<>();
142            for (Content content : (Set<Content>) deleteResult.get("locked-contents"))
143            {
144                allLockedContents.add(content);
145                Map<String, Object> contentParameters = getContentDefaultParameters(content);
146                contentParameters.put("description", _getLockedDescription(content));
147                updatedLockedContents.add(contentParameters);
148            }
149            updatedContentResults.put("locked-contents", updatedLockedContents);
150    
151            // Update the description for unauthorized contents
152            List<Map<String, Object>> updatedUnauthorizedContents = new ArrayList<>();
153            for (Content content : (Set<Content>) deleteResult.get("unauthorized-contents"))
154            {
155                allUnauthorizedContents.add(content);
156                Map<String, Object> contentParameters = getContentDefaultParameters(content);
157                contentParameters.put("description", _getNoRightDescription(content));
158                updatedUnauthorizedContents.add(contentParameters);
159            }
160            updatedContentResults.put("unauthorized-contents", updatedUnauthorizedContents);
161            
162            updatedResults.put(deletedContentId, updatedContentResults);
163        }
164        
165        // Add the list of all referenced contents (removing duplicate contents)
166        List<Map<String, Object>> allReferencedContentsMap = allReferencedContents.stream()
167            .map(c -> getContentDefaultParameters(c))
168            .collect(Collectors.toList());
169        
170        updatedResults.put("all-referenced-contents", allReferencedContentsMap);
171        
172        // Add the list of all locked contents (removing duplicate contents)
173        List<Map<String, Object>> allLockedContentsMap = allLockedContents.stream()
174                .map(c -> getContentDefaultParameters(c))
175                .collect(Collectors.toList());
176            
177        updatedResults.put("all-locked-contents", allLockedContentsMap);
178            
179        // Add the list of all unauthorized contents (removing duplicate contents)
180        List<Map<String, Object>> allUnauthorizedContentsMap = allUnauthorizedContents.stream()
181                .map(c -> getContentDefaultParameters(c))
182                .collect(Collectors.toList());
183            
184        updatedResults.put("all-unauthorized-contents", allUnauthorizedContentsMap);
185        
186        // Add the list of all undeleted contents (removing duplicate contents)
187        List<Map<String, Object>> allUnDeletedContentsMap = allUnDeletedContents.stream()
188                .map(c -> getContentDefaultParameters(c))
189                .collect(Collectors.toList());
190            
191        updatedResults.put("all-undeleted-contents", allUnDeletedContentsMap);
192            
193        return updatedResults;
194    }
195    
196    @Override
197    protected Map<String, Object> getContentDefaultParameters(Content content)
198    {
199        Map<String, Object> contentDefaultParameters = super.getContentDefaultParameters(content);
200        
201        if (content instanceof ProgramItem && content.hasValue(ProgramItem.CODE) && contentDefaultParameters.containsKey("title"))
202        {
203            String title = (String) contentDefaultParameters.get("title");
204            contentDefaultParameters.put("title", title + " (" + content.getValue(ProgramItem.CODE) + ")");
205        }
206        
207        return contentDefaultParameters;
208    }
209}