001/*
002 *  Copyright 2009 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.orgunit;
017
018import java.util.HashSet;
019import java.util.List;
020import java.util.Set;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.core.ui.Callable;
028import org.ametys.odf.workflow.CreateOrgUnitFunction;
029import org.ametys.plugins.repository.AmetysObjectIterable;
030import org.ametys.plugins.repository.AmetysObjectIterator;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.plugins.repository.UnknownAmetysObjectException;
033import org.ametys.runtime.plugin.component.AbstractLogEnabled;
034
035/**
036 * Component to provide the root OrgUnit
037 */
038public class RootOrgUnitProvider extends AbstractLogEnabled implements Component, Serviceable
039{
040    /** Avalon role. */
041    public static final String ROLE = RootOrgUnitProvider.class.getName();
042    
043    private AmetysObjectResolver _resolver;
044    
045    @Override
046    public void service(ServiceManager smanager) throws ServiceException
047    {
048        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
049    }
050    
051    /**
052     * Returns the root {@link OrgUnit}.
053     * @return the root {@link OrgUnit}.
054     */
055    public OrgUnit getRoot()
056    {
057        AmetysObjectIterable<OrgUnit> ouIterable = _resolver.query("//element(" + CreateOrgUnitFunction.CONTENT_NAME_PREFIX + OrgUnitFactory.ODF_ORGUNIT_ROOT_NODE + ", " + OrgUnitFactory.ORGUNIT_NODETYPE + ")");
058        AmetysObjectIterator<OrgUnit> ouIterator = ouIterable.iterator();
059        
060        if (ouIterator.hasNext())
061        {
062            return ouIterator.next();
063        }
064
065        return null;
066    }
067    
068    /**
069     * Get the root id
070     * @return the root id
071     */
072    @Callable
073    public String getRootId()
074    {
075        OrgUnit root = getRoot();
076        return root != null ? getRoot().getId() : null;
077    }
078    
079    /**
080     * Determines the given id is the root orgunit id
081     * @param id the id to test
082     * @return true if is root
083     */
084    public boolean isRoot (String id)
085    {
086        return id.equals(getRootId());
087    }
088    
089    /**
090     * Determines the given orgunit is the root orgunit
091     * @param orgUnit the orgunit to test
092     * @return true if is root
093     */
094    public boolean isRoot (OrgUnit orgUnit)
095    {
096        return orgUnit.getId().equals(getRootId());
097    }
098    
099    /**
100     * Get the child orgunits of an orgunit
101     * @param orgUnitId The identifier of the main orgunit
102     * @param recursive true to children recursively
103     * @return A set containing all the child orgunit identifiers.
104     */
105    public Set<String> getChildOrgUnitIds(String orgUnitId, boolean recursive)
106    {
107        Set<String> childIds = new HashSet<>();
108        
109        try
110        {
111            OrgUnit orgUnit = _resolver.resolveById(orgUnitId);
112            List<String> childOrgUnitIds = orgUnit.getSubOrgUnits();
113            childIds.addAll(childOrgUnitIds);
114            
115            if (recursive)
116            {
117                for (String childOrgUnitD : orgUnit.getSubOrgUnits())
118                {
119                    childIds.addAll(getChildOrgUnitIds(childOrgUnitD, recursive));
120                }
121            }
122        }
123        catch (UnknownAmetysObjectException e)
124        {
125            // log and ignore
126            getLogger().warn("Encountered a reference to an unknown orgunit : {}", orgUnitId, e);
127        }
128        
129        return childIds;
130    }
131}