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.person;
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.plugins.repository.AmetysObjectResolver;
030
031/**
032 * DAO for manipulating {@link Person}
033 */
034public class PersonDAO implements Serviceable, Component
035{
036    /** The Avalon role */
037    public static final String ROLE = PersonDAO.class.getName();
038    
039    /** The Ametys Object Resolver */
040    private AmetysObjectResolver _resolver;
041
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
045    }
046    
047    /**
048     * Gets the information about the given persons
049     * @param personIds The person ids
050     * @return A map of information
051     */
052    @Callable
053    public Map<String, Object> getPersonsInfos (List<String> personIds)
054    {
055        Map<String, Object> result = new HashMap<>();
056        
057        List<Map<String, Object>> persons = new ArrayList<>();
058        List<String> notFound = new ArrayList<>();
059        
060        for (String id : personIds)
061        {
062            persons.add(getPersonInfos(id));
063        }
064        
065        result.put("persons", persons);
066        result.put("personsNotFound", notFound);
067        
068        return result;
069    }
070    
071    /**
072     * Gets the information about the given person
073     * @param personId The person id
074     * @return A map of information
075     */
076    @Callable
077    public Map<String, Object> getPersonInfos (String personId)
078    {
079        Person person = _resolver.resolveById(personId);
080        return getPersonInfos(person);
081    }
082
083    /**
084     * Gets the information about the given person
085     * @param person The person
086     * @return A map of information
087     */
088    public Map<String, Object> getPersonInfos(Person person)
089    {
090        Map<String, Object> infos = new HashMap<>();
091        
092        infos.put("id", person.getId());
093        infos.put("name", person.getName());
094        infos.put("title", person.getTitle());
095        infos.put("personTitle", person.getPersonTitle());
096        infos.put("lastName", person.getLastName());
097        infos.put("givenName", person.getGivenName());
098        
099        return infos;
100    }
101}