001/*
002 *  Copyright 2021 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.extraction;
017
018import java.util.ArrayList;
019import java.util.HashSet;
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.RightAssignmentContext;
028import org.ametys.plugins.core.impl.right.StringRightAssignmentContext;
029import org.ametys.plugins.extraction.execution.Extraction;
030
031/**
032 * {@link RightAssignmentContext} for assign rights to a {@link Extraction}
033 */
034public class ExtractionRightAssignmentContext extends StringRightAssignmentContext
035{
036
037    /** The prefix for rights on the root of a collection */
038    public static final String ROOT_CONTEXT_PREFIX = "/extraction";
039    
040    @Override
041    public Object convertJSContext(Object context)
042    {
043        if (context instanceof String)
044        {
045            return context;
046        }
047        return null;
048    }
049    
050    @Override
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        super.service(smanager);
054    }
055    
056    @Override
057    public Set<Object> getParentContexts(Object context)
058    {
059        Set<Object> parents = new HashSet<>();
060        if (context instanceof String && !context.equals(ROOT_CONTEXT_PREFIX))
061        {
062            String contextString = (String) context;
063            String parent = contextString.substring(0, contextString.lastIndexOf('/'));
064            parents.add(parent);
065        }
066        return parents;
067    }
068
069    @Override
070    public List<Object> getRootContexts(Map<String, Object> contextParameters)
071    {
072        List<Object> rootContexts = new ArrayList<>();
073
074        rootContexts.add(ROOT_CONTEXT_PREFIX);
075        
076        return rootContexts;
077    }
078}