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