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.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    /**
035     * Test if contents can be archived.
036     * @param contentIds The ids of contents to test.
037     * @return the deleted and undeleted contents.
038     */
039    @SuppressWarnings("unchecked")
040    @Callable
041    public Map<String, Object> canArchive(List<String> contentIds)
042    {
043        Map<String, Object> results = new HashMap<>();
044        
045        results.put("referenced-contents", new ArrayList<Map<String, Object>>());
046        
047        for (String contentId : contentIds)
048        {
049            Content content = _resolver.resolveById(contentId);
050            String contentName = content.getName();
051            String contentTitle = StringUtils.defaultString(_contentHelper.getTitle(content), contentName);
052            
053            Map<String, Object> contentParams = new HashMap<>();
054            contentParams.put("id", content.getId());
055            contentParams.put("title", contentTitle);
056            contentParams.put("name", contentName);
057            
058            try
059            {
060                // Test if the content is referenced.
061                if (content.hasReferencingContents())
062                {
063                    // Indicate that the content is referenced.
064                    List<Map<String, Object>> referencedContents = (List<Map<String, Object>>) results.get("referenced-contents");
065                    referencedContents.add(contentParams);
066                }
067            }
068            catch (AmetysRepositoryException e)
069            {
070                getLogger().error("Unable to test if a content can be archived: '" + contentId + "'", e);
071            }
072        }
073        
074        return results;
075    }
076    
077}