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