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