001/*
002 *  Copyright 2014 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.cms.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.commons.lang3.StringUtils;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.core.right.RightManager.RightResult;
027import org.ametys.core.ui.Callable;
028import org.ametys.plugins.repository.AmetysRepositoryException;
029
030/**
031 * This element creates a ribbon button to archive a content.
032 */
033public class ArchiveContentClientSideElement extends SmartContentClientSideElement
034{
035    /** the right to archive a content */
036    public static final String WORKFLOW_RIGHTS_ARCHIVE = "Workflow_Rights_Archive";
037
038    /**
039     * Test if contents can be archived.
040     * @param contentIds The ids of contents to test.
041     * @return the deleted and undeleted contents.
042     */
043    @SuppressWarnings("unchecked")
044    @Callable(rights = Callable.SKIP_BUILTIN_CHECK)
045    public Map<String, Object> canArchive(List<String> contentIds)
046    {
047        Map<String, Object> results = new HashMap<>();
048        
049        results.put("no-right-contents", new ArrayList<>());
050        results.put("referenced-contents", new ArrayList<>());
051        
052        for (String contentId : contentIds)
053        {
054            Content content = _resolver.resolveById(contentId);
055            String contentName = content.getName();
056            String contentTitle = StringUtils.defaultString(_contentHelper.getTitle(content), contentName);
057            
058            Map<String, Object> contentParams = new HashMap<>();
059            contentParams.put("id", content.getId());
060            contentParams.put("title", contentTitle);
061            contentParams.put("name", contentName);
062            
063            if (_rightManager.currentUserHasRight(ArchiveContentClientSideElement.WORKFLOW_RIGHTS_ARCHIVE, content) != RightResult.RIGHT_ALLOW)
064            {
065                List<Map<String, Object>> noRightContents = (List<Map<String, Object>>) results.get("no-right-contents");
066                noRightContents.add(contentParams);
067            }
068            else
069            {
070                try
071                {
072                    // Test if the content is referenced.
073                    if (content.hasReferencingContents())
074                    {
075                        // Indicate that the content is referenced.
076                        List<Map<String, Object>> referencedContents = (List<Map<String, Object>>) results.get("referenced-contents");
077                        referencedContents.add(contentParams);
078                    }
079                }
080                catch (AmetysRepositoryException e)
081                {
082                    getLogger().error("Unable to test if a content can be archived: '" + contentId + "'", e);
083                }
084            }
085        }
086        
087        return results;
088    }
089    
090}