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.factory; 023 024import java.util.Properties; 025import org.apache.ibatis.session.SqlSession; 026import org.apache.ibatis.session.SqlSessionFactory; 027import uk.ac.roslin.ensembl.dao.factory.DAOFactory; 028import uk.ac.roslin.ensembl.exception.DAOException; 029import uk.ac.roslin.ensembl.model.database.Database; 030import uk.ac.roslin.ensembl.model.database.DatabaseType; 031import uk.ac.roslin.ensembl.model.database.Registry; 032 033public abstract class DBDAOFactory implements DAOFactory { 034 035 protected Properties configuration; 036 protected String databaseType; 037 protected String ensemblSchema; 038 protected String dbVersion; 039 protected String schemaVersion; 040 protected String ibatisSchemaFilePath; 041 protected String mybatisSchemaFilePath; 042 protected String databaseName; 043 protected Registry registry; 044 protected String thisDBUrl; 045 046 protected DatabaseType dBType; 047 protected Database database; 048 049 protected SqlSessionFactory sqlSessionFactory = null; 050 051 public DBDAOFactory() { 052 } 053 054 public DBDAOFactory(Database database) throws DAOException { 055 this.setDatabase(database); 056 //to reduce overhead only make this when first need it 057 //sqlSessionFactory = createSessionFactory(); 058 } 059 060 public final void setDatabase(Database db) throws DAOException { 061 try { 062 this.database = db; 063 this.dbVersion = this.database.getDBVersion(); 064 this.setDatabaseName(this.database.getdBName()); 065 this.registry = database.getRegistry(); 066 this.setDBType(database.getType()); 067 this.configuration = (Properties) registry.getConfigProperties().clone(); 068 this.setEnsemblSchemaVersion(database.getSchemaVersion()); 069 this.setMybatisSchemaFilePath(registry.findMybatisSchemaForSchemaVersion(this.dBType, this.ensemblSchema)); 070 thisDBUrl = configuration.getProperty("url").concat("/").concat(database.getdBName() + "?zeroDateTimeBehavior=convertToNull"); 071 configuration.setProperty("url", thisDBUrl); 072 configuration.setProperty("ensembl_release", ensemblSchema); 073 configuration.setProperty("mybatis_file", mybatisSchemaFilePath); 074 } catch (Exception e) { 075 throw new DAOException("Fail to set Database on a DBDAOFactory", e); 076 } 077 } 078 079 080 @Override 081 public Properties getConfiguration() { 082 return configuration; 083 } 084 085 @Override 086 public String getEnsemblSchemaVersion() { 087 return ensemblSchema; 088 } 089 090 @Override 091 public String getDBVersion() { 092 return dbVersion; 093 } 094 095 @Override 096 public void setEnsemblSchemaVersion(String schemaVersion) { 097 this.ensemblSchema = schemaVersion; 098 } 099 100 @Override 101 public void setDBVersion(String dbversion) { 102 this.dbVersion = dbversion; 103 } 104 105 @Override 106 public Registry getRegistry() { 107 return registry; 108 } 109 110 @Override 111 public void setDBType(DatabaseType type) { 112 dBType = type; 113 } 114 115 @Override 116 public DatabaseType getDBType() { 117 return dBType; 118 } 119 120 121 @Override 122 public void setMybatisSchemaFilePath(String schema) { 123 this.mybatisSchemaFilePath = schema; 124 } 125 126 @Override 127 public String getMybatisSchemaFilePath() { 128 return this.mybatisSchemaFilePath; 129 } 130 131 @Override 132 public String getDatabaseName() { 133 return this.databaseName; 134 } 135 136 @Override 137 public void setDatabaseName(String database) { 138 this.databaseName = database; 139 } 140 141 @Override 142 public Database getDatabase() { 143 return this.database; 144 } 145 146 //************************************************************************* 147 148 @Override 149 public SqlSession getNewSqlSession() throws DAOException { 150 if (database == null) { 151 throw new DAOException("No Database set for Factory"); 152 } 153 return (SqlSession) database.getNewSqlSession(); 154 } 155 156// private SqlSessionFactory getSessionFactory() throws DAOException { 157// 158// if (sqlSessionFactory == null) { 159// sqlSessionFactory = createSessionFactory(); 160// } 161// return sqlSessionFactory; 162// } 163// 164// @Override 165// public SqlSession getNewSqlSession() throws DAOException { 166// try { 167// return getSessionFactory().openSession(); 168// } catch (Exception e) { 169// throw new DAOException("Failed to retrieve an SqlSession from the Factory", e); 170// } 171// } 172// 173// private SqlSessionFactory createSessionFactory() throws DAOException { 174// 175// SqlSessionFactory out = null; 176// Reader reader = null; 177// 178// 179// try { 180// 181// String resource = this.mybatisSchemaFilePath; 182// reader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(resource)); 183// 184// if (reader == null) { 185// /* "belt and braces" check, as nothing is impossible */ 186// throw new DAOException("Failed to read Configuration.xml"); 187// } 188// 189// out = (new SqlSessionFactoryBuilder()).build(reader, "current", configuration); 190// 191// 192// if (out == null) { 193// /* also unlikely, but again a "belt and braces" check */ 194// throw new DAOException("Failed to build SqlMapClient"); 195// } 196// 197// reader.close(); 198// 199// } catch (Exception e) { 200// 201// throw new DAOException(e); 202// } 203// 204// 205// return out; 206// } 207 208}