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