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; 022 023 import java.io.InputStreamReader; 024 import java.io.Reader; 025 import java.util.Properties; 026 import org.apache.ibatis.session.SqlSession; 027 import org.apache.ibatis.session.SqlSessionFactory; 028 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 029 import uk.ac.roslin.ensembl.model.database.Database; 030 import uk.ac.roslin.ensembl.dao.database.factory.DBDAOFactory; 031 import uk.ac.roslin.ensembl.exception.ConfigurationException; 032 import uk.ac.roslin.ensembl.config.EnsemblDBType; 033 import uk.ac.roslin.ensembl.exception.DAOException; 034 import uk.ac.roslin.ensembl.model.database.Registry; 035 036 /** 037 * 038 * @author paterson 039 */ 040 public abstract class DBDatabase implements Database { 041 042 EnsemblDBType type = null; 043 String dBName = null; 044 String dbVersion = null ; 045 int intDBVersion ; 046 String schemaVersion = null ; 047 int intSchemaVersion ; 048 String build = null; 049 DBDAOFactory factory = null; 050 Registry registry = null; 051 052 public DBDatabase() { 053 054 } 055 056 public DBDatabase (String db_name, EnsemblDBType type, Registry registry) throws ConfigurationException { 057 this.setdBName(db_name); 058 this.setType(type); 059 this.setRegistry(registry); 060 061 } 062 063 @Override 064 public String getdBName() { 065 return dBName; 066 } 067 068 public void setdBName(String dBName) { 069 this.dBName = dBName; 070 } 071 072 @Override 073 public EnsemblDBType getType() { 074 return type; 075 } 076 077 public void setType(EnsemblDBType type) { 078 this.type = type; 079 } 080 081 @Override 082 public String getDBVersion() { 083 return dbVersion; 084 } 085 086 public void setDBVersion(String version) { 087 this.dbVersion = version; 088 } 089 090 @Override 091 public int getIntDBVersion() { 092 return intDBVersion; 093 } 094 095 public void setIntDBVersion(int intVersion) { 096 this.intDBVersion = intVersion; 097 } 098 099 @Override 100 public String getSchemaVersion() { 101 return schemaVersion; 102 } 103 104 public void setSchemaVersion(String ensemblSchemaVersion) { 105 this.schemaVersion = ensemblSchemaVersion; 106 } 107 108 @Override 109 public int getIntSchemaVersion() { 110 return intSchemaVersion; 111 } 112 113 public void setIntSchemaVersion(int intEnsemblSchemaVersion) { 114 this.intSchemaVersion = intEnsemblSchemaVersion; 115 } 116 117 @Override 118 public String getBuild() { 119 return build; 120 } 121 122 public void setBuild(String build) { 123 this.build = build; 124 } 125 126 @Override 127 public int compareTo(Database other) { 128 if (this.getdBName()!=null && other != null && other.getdBName()!=null) { 129 return this.getdBName().compareTo(other.getdBName()); 130 } else { 131 if (this.getdBName()!=null) { 132 return 1; 133 } else { 134 return -1; 135 } 136 } 137 } 138 139 140 @Override 141 public Registry getRegistry() { 142 return registry; 143 } 144 145 public void setRegistry(Registry r) { 146 this.registry=r; 147 } 148 149 @Override 150 public String toString() { 151 return "release: "+this.getDBVersion()+" [genome build: "+this.getBuild()+"]"; 152 } 153 154 155 //************************************************************************* 156 157 158 private SqlSessionFactory sqlSessionFactory = null; 159 160 private SqlSessionFactory getSessionFactory() throws DAOException { 161 162 if (sqlSessionFactory == null) { 163 sqlSessionFactory = createSessionFactory(); 164 } 165 return sqlSessionFactory; 166 } 167 168 @Override 169 public SqlSession getNewSqlSession() throws DAOException { 170 try { 171 return getSessionFactory().openSession(); 172 } catch (Exception e) { 173 throw new DAOException("Failed to retrieve an SqlSession from the Factory", e); 174 } 175 } 176 177 private SqlSessionFactory createSessionFactory() throws DAOException { 178 179 SqlSessionFactory out = null; 180 Reader reader = null; 181 182 Properties configuration = (Properties) registry.getConfigProperties().clone(); 183 String thisDBUrl = configuration.getProperty("url").concat("/").concat(this.getdBName() + "?zeroDateTimeBehavior=convertToNull"); 184 String mybatisSchemaFilePath = registry.findMybatisSchemaForSchemaVersion(this.getType(), this.getSchemaVersion()); 185 186 configuration.setProperty("url", thisDBUrl); 187 configuration.setProperty("ensembl_release", this.getSchemaVersion()); 188 configuration.setProperty("mybatis_file", mybatisSchemaFilePath); 189 190 191 try { 192 193 String resource = mybatisSchemaFilePath; 194 reader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(resource)); 195 196 if (reader == null) { 197 /* "belt and braces" check, as nothing is impossible */ 198 throw new DAOException("Failed to read Configuration.xml"); 199 } 200 201 out = (new SqlSessionFactoryBuilder()).build(reader, "current", configuration); 202 203 204 if (out == null) { 205 /* also unlikely, but again a "belt and braces" check */ 206 throw new DAOException("Failed to build SqlMapClient"); 207 } 208 209 reader.close(); 210 211 } catch (Exception e) { 212 213 throw new DAOException(e); 214 } 215 216 217 return out; 218 } 219 220 }