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.util.HashMap;
024    import uk.ac.roslin.ensembl.model.database.CollectionDatabase;
025    import uk.ac.roslin.ensembl.exception.ConfigurationException;
026    import uk.ac.roslin.ensembl.model.core.CollectionOfSpecies;
027    import uk.ac.roslin.ensembl.config.EnsemblDBType;
028    import uk.ac.roslin.ensembl.exception.DAOException;
029    import uk.ac.roslin.ensembl.model.core.CollectionSpecies;
030    import uk.ac.roslin.ensembl.model.database.Registry;
031    
032    /**
033     *
034     * @author paterson
035     */
036    // will make this abstract once we fill in all the subtypes
037    
038    
039    public class DBCollectionDatabase extends DBDatabase  implements CollectionDatabase {
040    
041        protected String collectionName = null;
042        protected DBCollection collection = null;
043        protected HashMap<CollectionSpecies, String> assemblyMap = null;
044    
045        public DBCollectionDatabase(String db_name, EnsemblDBType type, Registry registry) throws ConfigurationException {
046            super(db_name, type, registry);
047            init();
048        }
049    
050        private void init() throws ConfigurationException {
051    
052            this.collectionName = dBName.substring(0, dBName.indexOf("_" + this.type.toString()));
053            String sub = dBName.replace(collectionName+"_"+type.toString()+"_","");
054            if (sub.contains("_")) {
055                this.dbVersion = sub.substring(0, sub.indexOf("_"));
056                intDBVersion = Integer.parseInt(dbVersion);
057                sub = sub.replace(dbVersion + "_", "");
058                if (sub.contains("_")) {
059                    this.schemaVersion = sub.substring(0, sub.indexOf("_"));
060                    intSchemaVersion = Integer.parseInt(schemaVersion);
061                    build = sub.replace(schemaVersion+"_", "");
062                } else {
063                    this.schemaVersion = sub;
064                    intSchemaVersion = Integer.parseInt(schemaVersion);
065                }
066    
067    
068            } else {
069                this.dbVersion = sub;
070                intDBVersion = Integer.parseInt(dbVersion);
071                schemaVersion = dbVersion;
072                intSchemaVersion = intDBVersion;
073            }
074    
075        }
076    
077        @Override
078        public String getCollectionName() {
079            return collectionName;
080        }
081    
082        @Override
083        public void setCollectionName(String collectionName) {
084            this.collectionName = collectionName;
085        }
086    
087    
088        @Override
089        public void setCollection(CollectionOfSpecies r) {
090            this.collection = (DBCollection) r;
091        }
092    
093        @Override
094        public DBCollection getCollection() {
095            return this.collection;
096        }
097    
098        @Override
099        public String getAssembly(CollectionSpecies species) {
100    
101                if (this.assemblyMap== null) {
102                    this.assemblyMap = new HashMap<CollectionSpecies, String>();
103                    try {
104                        this.lazyLoadAssemblies();
105                    } catch (DAOException e) {
106                        //probably should throw this
107                    }
108                }
109                return this.assemblyMap.get(species);
110    
111        }
112    
113        private void lazyLoadAssemblies() throws DAOException  {
114    
115            try {
116                this.assemblyMap = this.getRegistry().getAssemblies(this);
117            } catch (Exception e) {
118                throw new DAOException("Failure to getAssemblies for species of this collection",e);
119            }
120        }
121     
122    }