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.web.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.Collection;
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.repository.Content;
027import org.ametys.core.ui.Callable;
028import org.ametys.web.repository.content.SharedContent;
029import org.ametys.web.repository.content.WebContent;
030import org.ametys.web.repository.content.shared.SharedContentManager;
031
032/**
033 * This element creates an action button to delete a content
034 */
035public class DeleteContentClientSideElement extends org.ametys.cms.clientsideelement.DeleteContentClientSideElement
036{
037    private SharedContentManager _sharedContentManager;
038    
039    @Override
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        super.service(smanager);
043        _sharedContentManager = (SharedContentManager) smanager.lookup(SharedContentManager.ROLE);
044    }
045
046    @Override
047    @Callable(rights = Callable.CHECKED_BY_IMPLEMENTATION)
048    public Map<String, Object> getStatus(List<String> contentsId)
049    {
050        // perform the right checks on every contents
051        // all contents in "allright-contents" are allowed
052        Map<String, Object> results = super.getStatus(contentsId);
053        
054        results.put("shared-contents", new ArrayList<>());
055        
056        @SuppressWarnings("unchecked")
057        List<Map<String, Object>> allrightContents = (List<Map<String, Object>>) results.get("allright-contents");
058        for (Map<String, Object> allRightContent : allrightContents)
059        {
060            String contentId = (String) allRightContent.get("id");
061            Content content = _resolver.resolveById(contentId);
062            
063            if (_sharedContentManager.hasSharedContents(content))
064            {
065                Map<String, Object> contentParams = getContentDefaultParameters (content);
066                @SuppressWarnings("unchecked")
067                List<Map<String, Object>> sharedContents = (List<Map<String, Object>>) results.get("shared-contents");
068                sharedContents.add(contentParams);
069            }
070        }
071        
072        return results;
073    }
074    
075    @Override
076    protected boolean _isContentReferenced(Content content)
077    {
078        if (content instanceof WebContent && !((WebContent) content).getReferencingPages().isEmpty())
079        {
080            return true;
081        }
082        
083        Collection<Content> referencingContents = content.getReferencingContents();
084        for (Content refContent : referencingContents)
085        {
086            // Ignore shared contents
087            if (!(refContent instanceof SharedContent))
088            {
089                return true;
090            }
091        }
092        return false;
093    }
094}