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.ametys.cms.repository.Content;
022import org.ametys.plugins.explorer.ExplorerNode;
023import org.ametys.plugins.explorer.resources.Resource;
024import org.ametys.plugins.repository.AmetysObject;
025
026/**
027 * This DAO overrides {@link org.ametys.plugins.explorer.resources.actions.ExplorerResourcesDAO} to content attachments.
028 */
029public class ExplorerResourcesDAO extends org.ametys.plugins.explorer.resources.actions.ExplorerResourcesDAO
030{
031    @Override
032    public Map<String, Object> getResourceProperties(Resource resource)
033    {
034        Map<String, Object> infos = super.getResourceProperties(resource);
035        
036        // Override 'rootOwnerType' if the resource in attached to a content
037        if (_getParentContent(resource) != null)
038        {
039            infos.put("rootOwnerType", "content");
040        }
041        
042        return infos;
043    }
044    
045    @Override
046    public Map<String, Object> getExplorerNodeProperties(ExplorerNode node)
047    {
048        Map<String, Object> infos = super.getExplorerNodeProperties(node);
049        
050        // Override 'rootOwnerType' if the node in attached to a content
051        if (_getParentContent(node) != null)
052        {
053            infos.put("rootOwnerType", "content");
054        }
055        
056        return infos;
057    }
058    
059    @Override
060    protected Set<String> getUserRights(ExplorerNode node)
061    {
062        Content parentContent = _getParentContent(node);
063        if (parentContent != null)
064        {
065            return _rightManager.getUserRights(_currentUserProvider.getUser(), parentContent);
066        }
067        
068        return super.getUserRights(node);
069    }
070    
071    /**
072     * Get the parent content of a resource or collection
073     * @param ao The resource or collection
074     * @return the parent content or null if the object is not part of content attachments
075     */
076    protected Content _getParentContent (AmetysObject ao)
077    {
078        AmetysObject parent = ao.getParent();
079        while (parent != null)
080        {
081            if (parent instanceof Content)
082            {
083                return (Content) parent;
084            }
085            parent = parent.getParent();
086        }
087        return null;
088    }
089}