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