001/*
002 *  Copyright 2018 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.linkdirectory.right;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.core.right.AbstractStaticRightAssignmentContext;
027import org.ametys.core.right.RightAssignmentContext;
028import org.ametys.plugins.linkdirectory.Link;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030
031/**
032 * {@link RightAssignmentContext} for assign rights to a {@link Link}
033 */
034public class LinkDirectoryRightAssignmentContext extends AbstractStaticRightAssignmentContext
035{
036    /** The id if this right context */
037    public static final String ID = "right.assignment.context.linkdirectoryaccess";
038    
039    /** The Ametys object resolver */
040    protected AmetysObjectResolver _resolver;
041
042    @Override
043    public void service(ServiceManager smanager) throws ServiceException
044    {
045        super.service(smanager);
046        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
047    }
048    
049    @Override
050    public Object convertJSContext(Object context)
051    {
052        if (context instanceof String)
053        {
054            return _resolver.resolveById((String) context);
055        }
056        return null;
057    }
058    
059    @Override
060    public String getContextIdentifier(Object context)
061    {
062        if (context instanceof Link)
063        {
064            return ((Link) context).getTitle();
065        }
066        return null;
067    }
068    
069    @Override
070    public Set<Object> getParentContexts(Object context)
071    {
072        return null;
073    }
074    
075    @Override
076    public List<Object> getRootContexts(Map<String, Object> contextParameters)
077    {
078        return new ArrayList<>();
079    }
080}