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.config;
023
024import java.util.Properties;
025import java.util.Enumeration;
026import java.util.Locale;
027import java.util.ResourceBundle;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030import uk.ac.roslin.ensembl.exception.ConfigurationException;
031
032/**
033 *
034 * @author paterson
035 */
036public class SchemaVersion {
037
038    final static Logger LOGGER = LoggerFactory.getLogger(SchemaVersion.class);
039
040    private Properties schemaVersionProperties = null;
041    private String currentEnsemblVersion = null;
042    private String currentGenomesVersion = null;
043    private String[] registeredSchemaVersions = null;
044    private String[] registeredGenomesReleases = null;
045
046    public SchemaVersion() throws ConfigurationException {
047        schemaVersionProperties = this.readResource("uk.ac.roslin.ensembl.configfiles.schema_version_mappings");
048        initialize();
049    }
050
051    public SchemaVersion(Properties localMappingRules) throws ConfigurationException {
052        schemaVersionProperties = localMappingRules;
053        if (schemaVersionProperties == null || schemaVersionProperties.isEmpty()) {
054            throw new ConfigurationException("No Schema Mapping properties given");
055        }
056        initialize();
057    }
058
059    private void initialize() {
060        currentEnsemblVersion = (schemaVersionProperties != null) ? schemaVersionProperties.getProperty("current_ensembl_release") : null;
061        currentGenomesVersion = (schemaVersionProperties != null) ? schemaVersionProperties.getProperty("current_genomes_release") : null;
062        registeredSchemaVersions = (schemaVersionProperties != null
063                && schemaVersionProperties.getProperty("known_ensembl_schemas") != null)
064                ? schemaVersionProperties.getProperty("known_ensembl_schemas").split(" ")
065                : null;
066        registeredGenomesReleases = (schemaVersionProperties != null
067                && schemaVersionProperties.getProperty("known_genomes_releases") != null)
068                ? schemaVersionProperties.getProperty("known_genomes_releases").split(" ")
069                : null;
070    }
071
072    private Properties readResource(String id) throws ConfigurationException {
073        Properties p = null;
074        try {
075            //NB: need to pass in the classloader to get this to work in a test environment!
076            ResourceBundle rb = ResourceBundle.getBundle(id, Locale.getDefault(),
077                this.getClass().getClassLoader());
078            p = new Properties();
079            for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
080                final String key = (String) keys.nextElement();
081                final String value = rb.getString(key);
082                p.put(key, value);
083            }
084
085        } catch (Exception ex) {
086            throw new ConfigurationException("System can't read the configuration file: " + id, ex);
087        }
088
089        if (p == null || p.isEmpty()) {
090            throw new ConfigurationException("No Schema Mapping properties read from " + id);
091        }
092        return p;
093    }
094
095    public String getMybatisSchemaPath(String database, String version) {
096        String out = null;
097        String location = null;
098        String sc = null;
099
100        location = (schemaVersionProperties != null) ? schemaVersionProperties.getProperty("schema_location") : null;
101        sc = (schemaVersionProperties != null) ? schemaVersionProperties.getProperty(database + "_" + version + "_schema") : null;
102
103        out = (location != null && sc != null) ? location + sc + "Configuration.xml" : null;
104        return out;
105    }
106
107    public Properties getConfigurationProperties() {
108        return schemaVersionProperties;
109    }
110
111    public String getCurrentEnsemblVersion() {
112        return currentEnsemblVersion;
113
114    }
115
116    public String getCurrentGenomesVersion() {
117        return currentGenomesVersion;
118    }
119
120    public String[] getRegisteredSchemas() {
121        return registeredSchemaVersions;
122    }
123    
124    public String[] getKnownGenomesReleases() {
125        return registeredGenomesReleases;
126    }
127
128    public String getBaseMybatis() {
129        return schemaVersionProperties.getProperty("base_mybatis");
130    }
131}