001/*
002 *  Copyright 2015 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.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.component.Component;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027
028import org.ametys.core.ui.Callable;
029import org.ametys.odf.ODFHelper;
030import org.ametys.plugins.repository.AmetysObjectResolver;
031
032/**
033 * DAO for manipulating OrgUnits
034 */
035public class OrgUnitDAO implements Serviceable, Component
036{
037    /** The Avalon role */
038    public static final String ROLE = OrgUnitDAO.class.getName();
039    
040    /** The Ametys Object Resolver */
041    private AmetysObjectResolver _resolver;
042
043    /** The ODF helper */
044    private ODFHelper _odfHelper;
045    
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
049        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
050    }
051    
052    /**
053     * Gets the information about the given orgunits
054     * @param orgUnitIds The orgunits ids
055     * @return A map of information
056     */
057    @Callable
058    public Map<String, Object> getOrgUnitsInfos (List<String> orgUnitIds)
059    {
060        return getOrgUnitsInfos(orgUnitIds, null);
061    }
062    
063    /**
064     * Gets the information about the given orgunits
065     * @param orgUnitIds The orgunits ids
066     * @param rootOrgUnitId the root orgUnitId
067     * @return A map of information
068     */
069    @Callable
070    public Map<String, Object> getOrgUnitsInfos (List<String> orgUnitIds, String rootOrgUnitId)
071    {
072        Map<String, Object> result = new HashMap<>();
073        
074        List<Map<String, Object>> orgUnits = new ArrayList<>();
075        List<String> notFound = new ArrayList<>();
076        
077        for (String id : orgUnitIds)
078        {
079            orgUnits.add(getOrgUnitInfos(id, rootOrgUnitId));
080        }
081        
082        result.put("orgUnits", orgUnits);
083        result.put("orgUnitsNotFound", notFound);
084        
085        return result;
086    }
087    
088    /**
089     * Gets the information about the given orgunit
090     * @param orgUnitId The orgunit id
091     * @return A map of information
092     */
093    @Callable
094    public Map<String, Object> getOrgUnitInfos (String orgUnitId)
095    {
096        return getOrgUnitInfos(orgUnitId, null);
097    }
098    
099    /**
100     * Gets the information about the given orgunit
101     * @param orgUnitId The orgunit id
102     * @param rootOrgUnitId the root orgUnit id
103     * @return A map of information
104     */
105    @Callable
106    public Map<String, Object> getOrgUnitInfos (String orgUnitId, String rootOrgUnitId)
107    {
108        OrgUnit orgUnit = _resolver.resolveById(orgUnitId);
109        return getOrgUnitInfos(orgUnit, rootOrgUnitId);
110    }
111
112    /**
113     * Gets the information about the given orgunit
114     * @param orgUnit The orgunit
115     * @param rootOrgUnitId the root orgUnit id
116     * @return A map of information
117     */
118    public Map<String, Object> getOrgUnitInfos(OrgUnit orgUnit, String rootOrgUnitId)
119    {
120        Map<String, Object> infos = new HashMap<>();
121        
122        infos.put("id", orgUnit.getId());
123        infos.put("name", orgUnit.getName());
124        infos.put("title", orgUnit.getTitle());
125        infos.put("path", _odfHelper.getOrgUnitPath(orgUnit.getId(), rootOrgUnitId));
126        
127        return infos;
128    }
129}