001 /** 002 * Copyright (C) 2010 The Roslin Institute <contact andy.law@roslin.ed.ac.uk> 003 * 004 * This file is part of the Ensembl Java API demonstration project developed by the 005 * Bioinformatics Group at The Roslin Institute, The Royal (Dick) School of 006 * Veterinary Studies, University of Edinburgh. 007 * 008 * This is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (version 3) as published by 010 * the Free Software Foundation. 011 * 012 * This software is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * in this software distribution. If not, see <http://www.gnu.org/licenses/gpl-3.0.html/>. 019 */ 020 021 package uk.ac.roslin.ensembl.dao.database.factory; 022 023 import uk.ac.roslin.ensembl.model.database.DatabaseType; 024 import uk.ac.roslin.ensembl.exception.DAOException; 025 import java.util.Properties; 026 import java.io.InputStreamReader; 027 import java.io.Reader; 028 import org.apache.ibatis.session.SqlSession; 029 import org.apache.ibatis.session.SqlSessionFactory; 030 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 031 import uk.ac.roslin.ensembl.dao.factory.DAOFactory; 032 import uk.ac.roslin.ensembl.model.database.Database; 033 import uk.ac.roslin.ensembl.model.database.Registry; 034 035 public abstract class DBDAOFactory implements DAOFactory { 036 037 protected Properties configuration; 038 protected String databaseType; 039 protected String ensemblSchema; 040 protected String dbVersion; 041 protected String schemaVersion; 042 protected String ibatisSchemaFilePath; 043 protected String mybatisSchemaFilePath; 044 protected String databaseName; 045 protected Registry registry; 046 protected String thisDBUrl; 047 048 protected DatabaseType dBType; 049 protected Database database; 050 051 protected SqlSessionFactory sqlSessionFactory = null; 052 053 public DBDAOFactory() { 054 } 055 056 public DBDAOFactory(Database database) throws DAOException { 057 this.setDatabase(database); 058 //to reduce overhead only make this when first need it 059 //sqlSessionFactory = createSessionFactory(); 060 } 061 062 public final void setDatabase(Database db) throws DAOException { 063 try { 064 this.database = db; 065 this.dbVersion = this.database.getDBVersion(); 066 this.setDatabaseName(this.database.getdBName()); 067 this.registry = database.getRegistry(); 068 this.setDBType(database.getType()); 069 this.configuration = (Properties) registry.getConfigProperties().clone(); 070 this.setEnsemblSchemaVersion(database.getSchemaVersion()); 071 this.setMybatisSchemaFilePath(registry.findMybatisSchemaForSchemaVersion(this.dBType, this.ensemblSchema)); 072 thisDBUrl = configuration.getProperty("url").concat("/").concat(database.getdBName() + "?zeroDateTimeBehavior=convertToNull"); 073 configuration.setProperty("url", thisDBUrl); 074 configuration.setProperty("ensembl_release", ensemblSchema); 075 configuration.setProperty("mybatis_file", mybatisSchemaFilePath); 076 } catch (Exception e) { 077 throw new DAOException("Fail to set Database on a DBDAOFactory", e); 078 } 079 } 080 081 082 @Override 083 public Properties getConfiguration() { 084 return configuration; 085 } 086 087 @Override 088 public String getEnsemblSchemaVersion() { 089 return ensemblSchema; 090 } 091 092 @Override 093 public String getDBVersion() { 094 return dbVersion; 095 } 096 097 @Override 098 public void setEnsemblSchemaVersion(String schemaVersion) { 099 this.ensemblSchema = schemaVersion; 100 } 101 102 @Override 103 public void setDBVersion(String dbversion) { 104 this.dbVersion = dbversion; 105 } 106 107 @Override 108 public Registry getRegistry() { 109 return registry; 110 } 111 112 @Override 113 public void setDBType(DatabaseType type) { 114 dBType = type; 115 } 116 117 @Override 118 public DatabaseType getDBType() { 119 return dBType; 120 } 121 122 123 @Override 124 public void setMybatisSchemaFilePath(String schema) { 125 this.mybatisSchemaFilePath = schema; 126 } 127 128 @Override 129 public String getMybatisSchemaFilePath() { 130 return this.mybatisSchemaFilePath; 131 } 132 133 @Override 134 public String getDatabaseName() { 135 return this.databaseName; 136 } 137 138 @Override 139 public void setDatabaseName(String database) { 140 this.databaseName = database; 141 } 142 143 @Override 144 public Database getDatabase() { 145 return this.database; 146 } 147 148 //************************************************************************* 149 150 @Override 151 public SqlSession getNewSqlSession() throws DAOException { 152 if (database == null) { 153 throw new DAOException("No Database set for Factory"); 154 } 155 return (SqlSession) database.getNewSqlSession(); 156 } 157 158 // private SqlSessionFactory getSessionFactory() throws DAOException { 159 // 160 // if (sqlSessionFactory == null) { 161 // sqlSessionFactory = createSessionFactory(); 162 // } 163 // return sqlSessionFactory; 164 // } 165 // 166 // @Override 167 // public SqlSession getNewSqlSession() throws DAOException { 168 // try { 169 // return getSessionFactory().openSession(); 170 // } catch (Exception e) { 171 // throw new DAOException("Failed to retrieve an SqlSession from the Factory", e); 172 // } 173 // } 174 // 175 // private SqlSessionFactory createSessionFactory() throws DAOException { 176 // 177 // SqlSessionFactory out = null; 178 // Reader reader = null; 179 // 180 // 181 // try { 182 // 183 // String resource = this.mybatisSchemaFilePath; 184 // reader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(resource)); 185 // 186 // if (reader == null) { 187 // /* "belt and braces" check, as nothing is impossible */ 188 // throw new DAOException("Failed to read Configuration.xml"); 189 // } 190 // 191 // out = (new SqlSessionFactoryBuilder()).build(reader, "current", configuration); 192 // 193 // 194 // if (out == null) { 195 // /* also unlikely, but again a "belt and braces" check */ 196 // throw new DAOException("Failed to build SqlMapClient"); 197 // } 198 // 199 // reader.close(); 200 // 201 // } catch (Exception e) { 202 // 203 // throw new DAOException(e); 204 // } 205 // 206 // 207 // return out; 208 // } 209 210 }