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.cart;
017
018import java.util.List;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.core.right.AbstractStaticRightAssignmentContext;
026import org.ametys.core.right.RightAssignmentContext;
027import org.ametys.plugins.repository.AmetysObjectResolver;
028
029/**
030 * {@link RightAssignmentContext} for assign rights to a {@link Cart}
031 */
032public class CartRightAssignmentContext extends AbstractStaticRightAssignmentContext
033{
034    /** The id if this right context */
035    public static final String ID = "right.assignment.context.cartaccess";
036
037    /** The alias id of the root */
038    private static final String __ROOT_CARTS_ID = "/carts";
039    
040    /** The Ametys object resolver */
041    protected AmetysObjectResolver _resolver;
042
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
048    }
049    
050    @Override
051    public Object convertJSContext(Object context)
052    {
053        if (context instanceof String)
054        {
055
056            if (context.equals(__ROOT_CARTS_ID))
057            {
058                return CartHelper.getCartsNode(_resolver);
059            }
060            else
061            {
062                return _resolver.resolveById((String) context);
063            }
064        }
065        
066        return null;
067    }
068    
069    @Override
070    public String getContextIdentifier(Object context)
071    {
072        if (context instanceof Cart)
073        {
074            return ((Cart) context).getTitle();
075        }
076        return null;
077    }
078    
079    @Override
080    public Set<Object> getParentContexts(Object context)
081    {
082        if (context instanceof Cart)
083        {
084            return Set.of(((Cart) context).getParent());
085        }
086        return null;
087    }
088    
089    @Override
090    public List<Object> getRootContexts(Map<String, Object> contextParameters)
091    {
092        return List.of(CartHelper.getCartsNode(_resolver));
093    }
094}