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.contentio.synchronize.impl;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.ibatis.mapping.Environment;
024import org.apache.ibatis.session.SqlSession;
025import org.apache.ibatis.type.JdbcType;
026import org.apache.ibatis.type.TypeHandlerRegistry;
027
028import org.ametys.core.datasource.AbstractMyBatisDAO;
029import org.ametys.core.datasource.ConnectionHelper;
030import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
031import org.ametys.plugins.contentio.synchronize.impl.typehandler.SQLBlobTypeHandler;
032import org.ametys.plugins.contentio.synchronize.impl.typehandler.SQLClobTypeHandler;
033import org.ametys.plugins.contentio.synchronize.impl.typehandler.SQLDoubleTypeHandler;
034import org.ametys.plugins.contentio.synchronize.impl.typehandler.SQLLongTypeHandler;
035
036/**
037 * DAO for {@link SynchronizableContentsCollection}s which need to access a SQL database
038 */
039public class SQLCollectionDAO extends AbstractMyBatisDAO
040{
041    /** Avalon ROLE */
042    public static final String ROLE = SQLCollectionDAO.class.getName();
043    
044    /** The datesource id */
045    protected String _dataSourceId;
046    
047    @Override
048    protected org.apache.ibatis.session.Configuration _getMyBatisConfiguration(Environment env)
049    {
050        org.apache.ibatis.session.Configuration config = super._getMyBatisConfiguration(env);
051        TypeHandlerRegistry typeHandlerRegistry = config.getTypeHandlerRegistry();
052        
053        //Transform int types to Long
054        typeHandlerRegistry.register(Object.class, JdbcType.BIGINT, SQLLongTypeHandler.class);
055        typeHandlerRegistry.register(Object.class, JdbcType.INTEGER, SQLLongTypeHandler.class);
056        typeHandlerRegistry.register(Object.class, JdbcType.SMALLINT, SQLLongTypeHandler.class);
057        
058        //Transform float types to Double
059        typeHandlerRegistry.register(Object.class, JdbcType.FLOAT, SQLDoubleTypeHandler.class);
060        typeHandlerRegistry.register(Object.class, JdbcType.NUMERIC, SQLDoubleTypeHandler.class);
061        typeHandlerRegistry.register(Object.class, JdbcType.DECIMAL, SQLDoubleTypeHandler.class);
062        typeHandlerRegistry.register(Object.class, JdbcType.REAL, SQLDoubleTypeHandler.class);
063
064        //Transform clob types to String
065        typeHandlerRegistry.register(Object.class, JdbcType.CLOB, SQLClobTypeHandler.class);
066        typeHandlerRegistry.register(Object.class, JdbcType.LONGVARCHAR, SQLClobTypeHandler.class);
067        
068        //Transform blob types to String
069        typeHandlerRegistry.register(Object.class, JdbcType.BLOB, SQLBlobTypeHandler.class);
070        typeHandlerRegistry.register(Object.class, JdbcType.LONGVARBINARY, SQLBlobTypeHandler.class);
071        
072        return config;
073    }
074    
075    /**
076     * Get the list of synchronized content
077     * @param params the filter paramaters
078     * @param dataSourceId the datasource ID
079     * @return the list of synchronized content
080     */
081    public List<Map<String, Object>> search(Map<String, Object> params, String dataSourceId)
082    {
083        _setDataSourceId(dataSourceId);
084        try (SqlSession session = getSession())
085        {
086            params.put("databaseType", ConnectionHelper.getDatabaseType(session.getConnection()));
087            return session.selectList("ContentIO.search", params);
088        }
089    }
090    
091    /**
092     * Get total count of synchronized content
093     * @param params the filter paramaters
094     * @param dataSourceId the datasource ID
095     * @return the total count of synchronized content
096     */
097    public int getTotalCount(Map<String, Object> params, String dataSourceId)
098    {
099        _setDataSourceId(dataSourceId);
100        try (SqlSession session = getSession())
101        {
102            params.put("databaseType", ConnectionHelper.getDatabaseType(session.getConnection()));
103            return session.selectOne("ContentIO.count", params);
104        }
105    }
106    
107    @Override
108    protected void _configureDatasource(Configuration configuration) throws ConfigurationException
109    {
110        //Do Nothing
111    }
112    
113    @Override
114    protected String _getDataSourceId()
115    {
116        return _dataSourceId;
117    }
118    
119    /**
120     * Set the datasource id
121     * @param dataSourceId the datasource id
122     */
123    protected void _setDataSourceId(String dataSourceId)
124    {
125        this._dataSourceId = dataSourceId;
126    }
127    
128}