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    /** The request attribute name to list all children contents to delete */
038    protected static final String _REQUEST_ATTRIBUTE_CONTENT_IDS = "ContentIdsToDelete";
039    
040    /** The helper component for hierarchical reference tables */
041    protected HierarchicalReferenceTablesHelper _hierarchicalReferenceTablesHelper;
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        super.service(manager);
047        _hierarchicalReferenceTablesHelper = (HierarchicalReferenceTablesHelper) manager.lookup(HierarchicalReferenceTablesHelper.ROLE);
048    }
049    
050    @Override
051    protected String getRightToDelete()
052    {
053        return "CMS_Rights_ReferenceTables_Delete";
054    }
055
056    @Override
057    @Callable
058    public Map<String, Object> forceDeleteContents(List<String> contentsId)
059    {
060        Request request = ContextHelper.getRequest(_context);
061        
062        List<String> contentsIdToDelete = new ArrayList<>(contentsId);
063        for (String contentId : contentsId)
064        {
065            Content content = _resolver.resolveById(contentId);
066            contentsIdToDelete.addAll(_hierarchicalReferenceTablesHelper.getAllChildren(content));
067        }
068        
069        request.setAttribute(_REQUEST_ATTRIBUTE_CONTENT_IDS, contentsIdToDelete);
070        Map<String, Object> returnMap = super.forceDeleteContents(contentsIdToDelete);
071        request.removeAttribute(_REQUEST_ATTRIBUTE_CONTENT_IDS);
072        
073        return returnMap;
074    }
075    
076    @Override
077    @Callable
078    public Map<String, Object> deleteContents(List<String> contentsId)
079    {
080        Request request = ContextHelper.getRequest(_context);
081        
082        List<String> contentsIdToDelete = new ArrayList<>(contentsId);
083        for (String contentId : contentsId)
084        {
085            Content content = _resolver.resolveById(contentId);
086            contentsIdToDelete.addAll(_hierarchicalReferenceTablesHelper.getAllChildren(content));
087        }
088        
089        request.setAttribute(_REQUEST_ATTRIBUTE_CONTENT_IDS, contentsIdToDelete);
090        Map<String, Object> returnMap = super.deleteContents(contentsIdToDelete);
091        request.removeAttribute(_REQUEST_ATTRIBUTE_CONTENT_IDS);
092        
093        return returnMap;
094    }
095    
096    /**
097     * Test if content is still referenced by a content other than a child or its parent before removing it
098     * @param content The content to remove
099     * @return true if content is still referenced
100     */
101    @SuppressWarnings("unchecked")
102    @Override
103    protected boolean _isContentReferenced(Content content)
104    {
105        Request request = ContextHelper.getRequest(_context);
106        return _hierarchicalReferenceTablesHelper._isContentReferenced(content, (List<String>) request.getAttribute(_REQUEST_ATTRIBUTE_CONTENT_IDS));
107    }
108}