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
048    public Map<String, Object> getStatus(List<String> contentsId)
049    {
050        Map<String, Object> results = super.getStatus(contentsId);
051        
052        results.put("shared-contents", new ArrayList<Map<String, Object>>());
053        
054        @SuppressWarnings("unchecked")
055        List<Map<String, Object>> allrightContents = (List<Map<String, Object>>) results.get("allright-contents");
056        for (Map<String, Object> allRightContent : allrightContents)
057        {
058            String contentId = (String) allRightContent.get("id");
059            Content content = _resolver.resolveById(contentId);
060            
061            if (_sharedContentManager.hasSharedContents(content))
062            {
063                Map<String, Object> contentParams = getContentDefaultParameters (content);
064                @SuppressWarnings("unchecked")
065                List<Map<String, Object>> sharedContents = (List<Map<String, Object>>) results.get("shared-contents");
066                sharedContents.add(contentParams);
067            }
068        }
069        
070        return results;
071    }
072    
073    @Override
074    protected boolean _isContentReferenced(Content content)
075    {
076        if (content instanceof WebContent && !((WebContent) content).getReferencingPages().isEmpty())
077        {
078            return true;
079        }
080        
081        Collection<Content> referencingContents = content.getReferencingContents();
082        for (Content refContent : referencingContents)
083        {
084            // Ignore shared contents
085            if (!(refContent instanceof SharedContent))
086            {
087                return true;
088            }
089        }
090        return false;
091    }
092}