001/*  Copyright 2018 Anyware Services
002 *
003 *  Licensed under the Apache License, Version 2.0 (the "License");
004 *  you may not use this file except in compliance with the License.
005 *  You may obtain a copy of the License at
006 *
007 *      http://www.apache.org/licenses/LICENSE-2.0
008 *
009 *  Unless required by applicable law or agreed to in writing, software
010 *  distributed under the License is distributed on an "AS IS" BASIS,
011 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 *  See the License for the specific language governing permissions and
013 *  limitations under the License.
014 */
015package org.ametys.cms.content.referencetable;
016
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.components.ContextHelper;
025import org.apache.cocoon.environment.Request;
026
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.repository.ContentDAO;
029import org.ametys.core.ui.Callable;
030
031/**
032 * DAO deleting a content from a hierarchical reference table reference table
033 *
034 */
035public class HierarchicalReferenceTablesDeleteContentDAO extends ContentDAO
036{
037    private static final String __REQUEST_ATTRIBUTE_CONTENT_IDS = "ContentIdsToDelete";
038    
039    /** The helper component for hierarchical reference tables */
040    protected HierarchicalReferenceTablesHelper _hierarchicalReferenceTablesHelper;
041    
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _hierarchicalReferenceTablesHelper = (HierarchicalReferenceTablesHelper) manager.lookup(HierarchicalReferenceTablesHelper.ROLE);
047    }
048    
049    @Override
050    @Callable
051    public Map<String, Object> deleteContents(List<String> contentsId)
052    {
053        Request request = ContextHelper.getRequest(_context);
054        
055        List<String> contentsIdToDelete = new ArrayList<>(contentsId);
056        for (String contentId : contentsId)
057        {
058            Content content = _resolver.resolveById(contentId);
059            contentsIdToDelete.addAll(_hierarchicalReferenceTablesHelper.getAllChildren(content));
060        }
061        
062        request.setAttribute(__REQUEST_ATTRIBUTE_CONTENT_IDS, contentsIdToDelete);
063        Map<String, Object> returnMap = super.deleteContents(contentsIdToDelete, "CMS_Rights_ReferenceTables_Delete");
064        request.removeAttribute(__REQUEST_ATTRIBUTE_CONTENT_IDS);
065        
066        return returnMap;
067    }
068    
069    /**
070     * Test if content is still referenced by a content other than a child or its parent before removing it
071     * @param content The content to remove
072     * @return true if content is still referenced
073     */
074    @SuppressWarnings("unchecked")
075    @Override
076    protected boolean _isContentReferenced(Content content)
077    {
078        Request request = ContextHelper.getRequest(_context);
079        return _hierarchicalReferenceTablesHelper._isContentReferenced(content, (List<String>) request.getAttribute(__REQUEST_ATTRIBUTE_CONTENT_IDS));
080    }
081}