001/*
002 *  Copyright 2016 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.explorer;
017
018import java.util.Map;
019import java.util.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.cms.trash.element.TrashElementDAO;
026import org.ametys.plugins.explorer.ExplorerNode;
027import org.ametys.plugins.explorer.resources.Resource;
028import org.ametys.plugins.repository.AmetysObject;
029import org.ametys.plugins.repository.RemovableAmetysObject;
030import org.ametys.plugins.repository.trash.TrashableAmetysObject;
031
032/**
033 * This DAO overrides {@link org.ametys.plugins.explorer.resources.actions.ExplorerResourcesDAO} to content attachments.
034 */
035public class ExplorerResourcesDAO extends org.ametys.plugins.explorer.resources.actions.ExplorerResourcesDAO
036{
037    private TrashElementDAO _trashElementDAO;
038
039    @Override
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        super.service(manager);
043        _trashElementDAO = (TrashElementDAO) manager.lookup(TrashElementDAO.ROLE);
044    }
045    
046    @Override
047    public Map<String, Object> getResourceProperties(Resource resource)
048    {
049        Map<String, Object> infos = super.getResourceProperties(resource);
050        
051        // Override 'rootOwnerType' if the resource in attached to a content
052        if (_getParentContent(resource) != null)
053        {
054            infos.put("rootOwnerType", "content");
055        }
056        
057        return infos;
058    }
059    
060    @Override
061    public Map<String, Object> getExplorerNodeProperties(ExplorerNode node)
062    {
063        Map<String, Object> infos = super.getExplorerNodeProperties(node);
064        
065        // Override 'rootOwnerType' if the node in attached to a content
066        if (_getParentContent(node) != null)
067        {
068            infos.put("rootOwnerType", "content");
069        }
070        
071        return infos;
072    }
073    
074    @Override
075    protected Set<String> getUserRights(ExplorerNode node)
076    {
077        Content parentContent = _getParentContent(node);
078        if (parentContent != null)
079        {
080            return _rightManager.getUserRights(_currentUserProvider.getUser(), parentContent);
081        }
082        
083        return super.getUserRights(node);
084    }
085    
086    /**
087     * Get the parent content of a resource or collection
088     * @param ao The resource or collection
089     * @return the parent content or null if the object is not part of content attachments
090     */
091    protected Content _getParentContent (AmetysObject ao)
092    {
093        AmetysObject parent = ao.getParent();
094        while (parent != null)
095        {
096            if (parent instanceof Content)
097            {
098                return (Content) parent;
099            }
100            parent = parent.getParent();
101        }
102        return null;
103    }
104    
105    @Override
106    protected void doDeleteObject(RemovableAmetysObject object)
107    {
108        if (object instanceof TrashableAmetysObject trashableAO)
109        {
110            _trashElementDAO.trash(trashableAO);
111        }
112        else
113        {
114            super.doDeleteObject(object);
115        }
116    }
117}