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    private RootOrgUnitProvider _orgUnitProvider;
038    private ODFHelper _odfHelper;
039    
040    @Override
041    public void service(ServiceManager smanager) throws ServiceException
042    {
043        super.service(smanager);
044        _orgUnitProvider = (RootOrgUnitProvider) smanager.lookup(RootOrgUnitProvider.ROLE);
045        _odfHelper = (ODFHelper) smanager.lookup(ODFHelper.ROLE);
046    }
047    
048    @Override
049    public Set<Object> getParentContexts(Object context)
050    {
051        if (context instanceof OrgUnit)
052        {
053            return Collections.singleton(((OrgUnit) context).getParentOrgUnit());
054        }
055        
056        return super.getParentContexts(context);
057    }
058
059    @Override
060    public List<Object> getRootContexts(Map<String, Object> contextParameters)
061    {
062        List<Object> rootContexts = super.getRootContexts(contextParameters);
063        
064        if (matchWorkspace(contextParameters))
065        {
066            rootContexts.add(_orgUnitProvider.getRoot());
067        }
068        return rootContexts;
069    }
070    
071    @Override
072    protected Object getRootContent()
073    {
074        return _odfHelper.getRootContent();
075    }
076
077}