001/*
002 *  Copyright 2018 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.plugins.userdirectory.synchronize;
017
018import java.sql.Connection;
019import java.sql.SQLException;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.ibatis.session.SqlSession;
026
027import org.ametys.core.datasource.AbstractMyBatisDAO;
028import org.ametys.core.datasource.ConnectionHelper;
029
030/**
031 * DAO for {@link SQLSynchronizableUDOrgunitCollection}s which need to access a SQL database
032 */
033public class SQLUserSearchDAO extends AbstractMyBatisDAO
034{
035    /** Avalon ROLE */
036    public static final String ROLE = SQLUserSearchDAO.class.getName();
037    
038    /** The datesource id */
039    protected String _dataSourceId;
040    
041    /**
042     * Get the list of synchronized content
043     * @param params the filter paramaters
044     * @param dataSourceId the datasource ID
045     * @return the list of synchronized content
046     */
047    public List<Map<String, Object>> searchUser(Map<String, Object> params, String dataSourceId)
048    {
049        _setDataSourceId(dataSourceId);
050        try (SqlSession session = getSession();
051             Connection connection = session.getConnection();)
052        {
053            params.put("databaseType", ConnectionHelper.getDatabaseType(connection));
054            return session.selectList("UserDirectory.search", params);
055        }
056        catch (SQLException e)
057        {
058            throw new IllegalStateException("A database access error occured, connection could not be closed.", e);
059        }
060    }
061    
062    @Override
063    protected void _configureDatasource(Configuration configuration) throws ConfigurationException
064    {
065        //Do Nothing
066    }
067    
068    @Override
069    protected String _getDataSourceId()
070    {
071        return _dataSourceId;
072    }
073    
074    /**
075     * Set the datasource id
076     * @param dataSourceId the datasource id
077     */
078    protected void _setDataSourceId(String dataSourceId)
079    {
080        this._dataSourceId = dataSourceId;
081    }
082    
083}