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.plugins.explorer.rights;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.core.right.AbstractStaticRightAssignmentContext;
028import org.ametys.core.right.RightAssignmentContext;
029import org.ametys.plugins.explorer.ExplorerNode;
030import org.ametys.plugins.explorer.resources.Resource;
031import org.ametys.plugins.explorer.resources.ResourceCollection;
032import org.ametys.plugins.explorer.resources.actions.ExplorerResourcesDAO;
033import org.ametys.plugins.repository.AmetysObject;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035
036/**
037 * {@link RightAssignmentContext} for assign rights to a {@link Resource} or a {@link ResourceCollection}
038 */
039public class ResourceRightAssignmentContext extends AbstractStaticRightAssignmentContext
040{
041    /** The Ametys object resolver */
042    protected AmetysObjectResolver _resolver;
043    /** The resources DAO */
044    protected ExplorerResourcesDAO _resourcesDAO;
045
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        super.service(smanager);
050        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
051        _resourcesDAO = (ExplorerResourcesDAO) smanager.lookup(ExplorerResourcesDAO.ROLE);
052    }
053    
054    @Override
055    public Object convertJSContext(Object context)
056    {
057        if (context instanceof String)
058        {
059            return _resolver.resolveById((String) context);
060        }
061        return null;
062    }
063    
064    @Override
065    public String getContextIdentifier(Object context)
066    {
067        if (context instanceof ExplorerNode)
068        {
069            return ((ExplorerNode) context).getId();
070        }
071        return null;
072    }
073    
074    @Override
075    public Set<Object> getParentContexts(Object context)
076    {
077        if (context instanceof ExplorerNode)
078        {
079            AmetysObject parent = ((ExplorerNode) context).getParent();
080            if (parent instanceof ExplorerNode)
081            {
082                return Collections.singleton(parent);
083            }
084        }
085        return null;
086    }
087    
088    @Override
089    public List<Object> getRootContexts(Map<String, Object> contextParameters)
090    {
091        List<Object> rootContexts = new ArrayList<>();
092        
093        if (matchWorkspace(contextParameters))  
094        {
095            rootContexts.addAll(_resourcesDAO.getResourcesRootNodes());
096        }
097        
098        return rootContexts;
099    }
100}