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