001/** 002 * Copyright 2014 Tampere University of Technology, Pori Department 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 core.tut.pori.dao; 017 018import javax.sql.DataSource; 019 020import org.springframework.beans.factory.annotation.Autowired; 021import org.springframework.jdbc.core.JdbcTemplate; 022import org.springframework.jdbc.datasource.DataSourceTransactionManager; 023import org.springframework.transaction.support.TransactionTemplate; 024 025/** 026 * Base class for SQL DAOs. 027 * 028 * http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-spring-config.html 029 * http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-spring-config-jdbctemplate.html 030 * 031 * Subclassing this class will automatically add the new class to DAOHandler, and will be retrievable 032 * run-time from ServiceInitializer.getDAOHandler().getSQLDAO(...) 033 */ 034public abstract class SQLDAO { 035 /* databases */ 036 /** default database schema */ 037 protected static final String DATABASE = "ca_frontend"; 038 /* common columns */ 039 /** COUNT(*) definition */ 040 protected static final String COLUMN_COUNT = "COUNT(*)"; 041 /** default column name for GUIDs */ 042 protected static final String COLUMN_GUID = "guid"; 043 /** default column name for row created */ 044 protected static final String COLUMN_ROW_CREATED = "row_created"; 045 /** default column name for row updated */ 046 protected static final String COLUMN_ROW_UPDATED = "row_updated"; 047 /** default column name for user id */ 048 protected static final String COLUMN_USER_ID = "user_id"; 049 /* sql strings */ 050 /** SQL string for retrieving the last generated row id */ 051 protected static final String SQL_SELECT_LAST_INSERT_ID = "SELECT LAST_INSERT_ID()"; 052 /* member variables */ 053 private JdbcTemplate _jdbcTemplate = null; 054 private TransactionTemplate _transactionTemplate = null; 055 056 /** 057 * 058 * @param dataSource 059 */ 060 @Autowired 061 public void setDataSource(DataSource dataSource){ 062 _jdbcTemplate = new JdbcTemplate(dataSource); 063 } 064 065 /** 066 * 067 * @param tnxManager 068 */ 069 @Autowired 070 public void setTnxManager(DataSourceTransactionManager tnxManager){ 071 _transactionTemplate = new TransactionTemplate(tnxManager); 072 } 073 074 /** 075 * @return the jdbcTemplate 076 */ 077 protected JdbcTemplate getJdbcTemplate() { 078 return _jdbcTemplate; 079 } 080 081 /** 082 * Note: the template is shared amongst all instances that subclass SQLDAO. It is thread-safe, though you should not 083 * change template settings if you are not absolutely sure what you are doing. If a more specific configuration instance 084 * is required, you should instantiate new TransactionTemplate. 085 * 086 * @return the transactionTemplate 087 */ 088 public TransactionTemplate getTransactionTemplate() { 089 return _transactionTemplate; 090 } 091 092 /** 093 * Helper method for checking if the given column is a COUNT(*) column 094 * 095 * @param columnName 096 * @param value 097 * @return the value of the count column or < 0 if not an count column 098 */ 099 protected long checkCountColumn(String columnName, Object value){ 100 if(!COLUMN_COUNT.equalsIgnoreCase(columnName)){ 101 return -1; 102 } 103 return (Long)value; 104 } 105}