001/** 002 * Copyright (C) 2010-2015 The Roslin Institute <contact andy.law@roslin.ed.ac.uk> 003 * 004 * This file is part of JEnsembl: a Java API to Ensembl data sources developed by the 005 * Bioinformatics Group at The Roslin Institute, The Royal (Dick) School of 006 * Veterinary Studies, University of Edinburgh. 007 * 008 * Project hosted at: http://jensembl.sourceforge.net 009 * 010 * This is free software: you can redistribute it and/or modify 011 * it under the terms of the GNU General Public License (version 3) as published by 012 * the Free Software Foundation. 013 * 014 * This software is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * in this software distribution. If not, see: http://opensource.org/licenses/gpl-3.0.html 021 */ 022package uk.ac.roslin.ensembl.dao.database; 023 024import java.io.InputStreamReader; 025import java.io.Reader; 026import java.util.*; 027import org.apache.ibatis.session.SqlSession; 028import org.apache.ibatis.session.SqlSessionFactory; 029import org.apache.ibatis.session.SqlSessionFactoryBuilder; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032import uk.ac.roslin.ensembl.config.EnsemblDBType; 033import uk.ac.roslin.ensembl.dao.database.factory.DBDAOFactory; 034import uk.ac.roslin.ensembl.dao.factory.DAOFactory; 035import uk.ac.roslin.ensembl.exception.ConfigurationException; 036import uk.ac.roslin.ensembl.exception.DAOException; 037import uk.ac.roslin.ensembl.model.database.Database; 038import uk.ac.roslin.ensembl.model.database.Registry; 039 040/** 041 * 042 * @author paterson 043 */ 044public abstract class DBDatabase implements Database { 045 046 EnsemblDBType type = null; 047 String dBName = null; 048 String dBClassifier = null; 049 String dbVersion = null ; 050 int intDBVersion ; 051 String schemaVersion = null ; 052 int intSchemaVersion ; 053 String build = null; 054 DBDAOFactory factory = null; 055 Registry registry = null; 056 final static Logger LOGGER = LoggerFactory.getLogger(DBDatabase.class); 057 058 public DBDatabase() { 059 060 } 061 062 public DBDatabase (String db_name, EnsemblDBType type, Registry registry) throws ConfigurationException { 063 this.setdBName(db_name); 064 this.setType(type); 065 this.setRegistry(registry); 066 067 } 068 069 @Override 070 public String getdBName() { 071 return dBName; 072 } 073 074 public final void setdBName(String dBName) { 075 this.dBName = dBName; 076 } 077 078 @Override 079 public EnsemblDBType getType() { 080 return type; 081 } 082 083 public void setType(EnsemblDBType type) { 084 this.type = type; 085 } 086 087 @Override 088 public String getDBVersion() { 089 return dbVersion; 090 } 091 092 public final void setDBVersion(String version) { 093 this.dbVersion = version; 094 } 095 096 @Override 097 public int getIntDBVersion() { 098 return intDBVersion; 099 } 100 101 public void setIntDBVersion(int intVersion) { 102 this.intDBVersion = intVersion; 103 } 104 105 @Override 106 public String getSchemaVersion() { 107 return schemaVersion; 108 } 109 110 public void setSchemaVersion(String ensemblSchemaVersion) { 111 this.schemaVersion = ensemblSchemaVersion; 112 } 113 114 @Override 115 public int getIntSchemaVersion() { 116 return intSchemaVersion; 117 } 118 119 public void setIntSchemaVersion(int intEnsemblSchemaVersion) { 120 this.intSchemaVersion = intEnsemblSchemaVersion; 121 } 122 123 @Override 124 public String getBuild() { 125 return build; 126 } 127 128 public void setBuild(String build) { 129 this.build = build; 130 } 131 132 @Override 133 public int compareTo(Database other) { 134 if (this.getdBName()!=null && other != null && other.getdBName()!=null) { 135 return this.getdBName().compareTo(other.getdBName()); 136 } else { 137 if (this.getdBName()!=null) { 138 return 1; 139 } else { 140 return -1; 141 } 142 } 143 } 144 145 146 @Override 147 public Registry getRegistry() { 148 return registry; 149 } 150 151 public final void setRegistry(Registry r) { 152 this.registry=r; 153 } 154 155 @Override 156 public String toString() { 157 return "release: "+this.getDBVersion()+" [genome build: "+this.getBuild()+"]"; 158 } 159 160 @Override 161 public String getdBClassifier() { 162 return dBClassifier; 163 } 164 165 //************************************************************************* 166 167 168 private SqlSessionFactory sqlSessionFactory = null; 169 170 protected SqlSessionFactory getSessionFactory() throws DAOException { 171 172 if (sqlSessionFactory == null) { 173 sqlSessionFactory = createSessionFactory(); 174 } 175 return sqlSessionFactory; 176 } 177 178 @Override 179 public SqlSession getNewSqlSession() throws DAOException { 180 try { 181 return getSessionFactory().openSession(); 182 } catch (Exception e) { 183 throw new DAOException("Failed to retrieve an SqlSession from the Factory", e); 184 } 185 } 186 187 private SqlSessionFactory createSessionFactory() throws DAOException { 188 189 SqlSessionFactory out = null; 190 Reader reader = null; 191 192 Properties configuration = (Properties) registry.getConfigProperties().clone(); 193 String thisDBUrl = configuration.getProperty("url").concat("/").concat(this.getdBName() + "?zeroDateTimeBehavior=convertToNull"); 194 String mybatisSchemaFilePath = registry.findMybatisSchemaForSchemaVersion(this.getType(), this.getSchemaVersion()); 195 196 configuration.setProperty("url", thisDBUrl); 197 configuration.setProperty("ensembl_release", this.getSchemaVersion()); 198 configuration.setProperty("mybatis_file", mybatisSchemaFilePath); 199 200 201 try { 202 203 String resource = mybatisSchemaFilePath; 204 reader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(resource)); 205 206 if (reader == null) { 207 /* "belt and braces" check, as nothing is impossible */ 208 throw new DAOException("Failed to read Configuration.xml"); 209 } 210 211 out = (new SqlSessionFactoryBuilder()).build(reader, "current", configuration); 212 213 214 if (out == null) { 215 /* also unlikely, but again a "belt and braces" check */ 216 throw new DAOException("Failed to build SqlMapClient"); 217 } 218 219 reader.close(); 220 221 } catch (Exception e) { 222 223 throw new DAOException(e); 224 } 225 226 227 return out; 228 } 229 230}