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 060 //Transform float types to Double 061 typeHandlerRegistry.register(Object.class, JdbcType.FLOAT, SQLDoubleTypeHandler.class); 062 typeHandlerRegistry.register(Object.class, JdbcType.NUMERIC, SQLDoubleTypeHandler.class); 063 typeHandlerRegistry.register(Object.class, JdbcType.DECIMAL, SQLDoubleTypeHandler.class); 064 typeHandlerRegistry.register(Object.class, JdbcType.REAL, SQLDoubleTypeHandler.class); 065 066 //Transform clob types to String 067 typeHandlerRegistry.register(Object.class, JdbcType.CLOB, SQLClobTypeHandler.class); 068 typeHandlerRegistry.register(Object.class, JdbcType.LONGVARCHAR, SQLClobTypeHandler.class); 069 070 //Transform blob types to String 071 typeHandlerRegistry.register(Object.class, JdbcType.BLOB, SQLBlobTypeHandler.class); 072 typeHandlerRegistry.register(Object.class, JdbcType.LONGVARBINARY, SQLBlobTypeHandler.class); 073 074 return config; 075 } 076 077 /** 078 * Get the list of synchronized content 079 * @param params the filter paramaters 080 * @param dataSourceId the datasource ID 081 * @return the list of synchronized content 082 */ 083 public List<Map<String, Object>> search(Map<String, Object> params, String dataSourceId) 084 { 085 _setDataSourceId(dataSourceId); 086 try (SqlSession session = getSession(); 087 Connection connection = session.getConnection();) 088 { 089 params.put("databaseType", ConnectionHelper.getDatabaseType(connection)); 090 return session.selectList("ContentIO.search", params); 091 } 092 catch (SQLException e) 093 { 094 throw new IllegalStateException("A database access error occured, connection could not be closed.", e); 095 } 096 } 097 098 /** 099 * Get total count of synchronized content 100 * @param params the filter paramaters 101 * @param dataSourceId the datasource ID 102 * @return the total count of synchronized content 103 */ 104 public int getTotalCount(Map<String, Object> params, String dataSourceId) 105 { 106 _setDataSourceId(dataSourceId); 107 try (SqlSession session = getSession(); 108 Connection connection = session.getConnection();) 109 { 110 111 params.put("databaseType", ConnectionHelper.getDatabaseType(connection)); 112 return session.selectOne("ContentIO.count", params); 113 } 114 catch (SQLException e) 115 { 116 throw new IllegalStateException("A database access error occured, connection could not be closed.", e); 117 } 118 } 119 120 @Override 121 protected void _configureDatasource(Configuration configuration) throws ConfigurationException 122 { 123 //Do Nothing 124 } 125 126 @Override 127 protected String _getDataSourceId() 128 { 129 return _dataSourceId; 130 } 131 132 /** 133 * Set the datasource id 134 * @param dataSourceId the datasource id 135 */ 136 protected void _setDataSourceId(String dataSourceId) 137 { 138 this._dataSourceId = dataSourceId; 139 } 140 141}