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