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.odf.rights;
017
018import java.util.Collections;
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.cms.rights.ContentRightAssignmentContext;
027import org.ametys.core.right.RightAssignmentContext;
028import org.ametys.odf.ODFHelper;
029import org.ametys.odf.orgunit.OrgUnit;
030import org.ametys.odf.orgunit.RootOrgUnitProvider;
031
032/**
033 * {@link RightAssignmentContext} for assign rights to a {@link OrgUnit}
034 */
035public class ODFRightAssignmentContext extends ContentRightAssignmentContext
036{
037    /** The Right assignment context id */
038    @SuppressWarnings("hiding")
039    public static final String ID = "right.assignment.context.orgunit";
040    
041    private RootOrgUnitProvider _orgUnitProvider;
042    private ODFHelper _odfHelper;
043    
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        super.service(smanager);
048        _orgUnitProvider = (RootOrgUnitProvider) smanager.lookup(RootOrgUnitProvider.ROLE);
049        _odfHelper = (ODFHelper) smanager.lookup(ODFHelper.ROLE);
050    }
051    
052    @Override
053    public Set<Object> getParentContexts(Object context)
054    {
055        if (context instanceof OrgUnit)
056        {
057            return Collections.singleton(((OrgUnit) context).getParentOrgUnit());
058        }
059        
060        return super.getParentContexts(context);
061    }
062
063    @Override
064    public List<Object> getRootContexts(Map<String, Object> contextParameters)
065    {
066        List<Object> rootContexts = super.getRootContexts(contextParameters);
067        
068        if (matchWorkspace(contextParameters))
069        {
070            rootContexts.add(_orgUnitProvider.getRoot());
071        }
072        return rootContexts;
073    }
074    
075    @Override
076    protected Object getRootContent()
077    {
078        return _odfHelper.getRootContent();
079    }
080}