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    package uk.ac.roslin.ensembl.datasourceaware.core;
021    
022    import java.util.HashMap;
023    import uk.ac.roslin.ensembl.config.EnsemblComparaDivision;
024    import uk.ac.roslin.ensembl.config.EnsemblDBType;
025    import uk.ac.roslin.ensembl.dao.factory.DAOComparaFactory;
026    import uk.ac.roslin.ensembl.datasourceaware.DAObject;
027    import uk.ac.roslin.ensembl.dao.factory.DAOCoreFactory;
028    import uk.ac.roslin.ensembl.dao.factory.DAOSpeciesFactory;
029    import uk.ac.roslin.ensembl.model.core.CollectionSpecies;
030    import uk.ac.roslin.ensembl.model.core.CoreObject;
031    import uk.ac.roslin.ensembl.model.core.Species;
032    import uk.ac.roslin.ensembl.model.database.CollectionCoreDatabase;
033    import uk.ac.roslin.ensembl.model.database.CollectionDatabase;
034    import uk.ac.roslin.ensembl.model.database.SingleSpeciesCoreDatabase;
035    import uk.ac.roslin.ensembl.model.database.SingleSpeciesDatabase;
036    
037    public abstract class DACoreObject extends DAObject implements CoreObject {
038    
039        protected Species species = null;
040    //    protected DAOComparaFactory instantiatingComparaFactory = null;
041        protected HashMap<EnsemblComparaDivision, DAOComparaFactory> comparaFactories =
042                new HashMap<EnsemblComparaDivision, DAOComparaFactory>();
043    
044        public DACoreObject() {
045            super();
046        }
047    
048        public DACoreObject(DAOCoreFactory factory) {
049            super(factory);
050        }
051    
052        @Override
053        public DAOCoreFactory getDaoFactory() {
054    
055            //@start of snip to instantiate a coreFactory from species +version info
056            //if their is no corefactory set : see matching change in DBHomologyDAO svn 132
057    
058            //try and make a factory if there isn't one, but we do have set the db version and species
059            if (daoFactory == null && this.getDBVersion() != null
060                    && this.getSpecies() != null) {
061    
062                DAOCoreFactory f = null;
063    
064                try {
065                    //are we single or multispecies
066                    if (this.getSpecies() instanceof CollectionSpecies) {
067                        f = (DAOCoreFactory) ((CollectionCoreDatabase) ((CollectionSpecies) this.getSpecies()).getDatabaseByTypeAndVersion(
068                                EnsemblDBType.collection_core, this.getDBVersion())).getCoreFactory(this.getSpecies());
069                    } else {
070                        f = (DAOCoreFactory) ((SingleSpeciesCoreDatabase) this.getSpecies().getDatabaseByTypeAndVersion(
071                                EnsemblDBType.core, this.getDBVersion())).getCoreFactory();
072                    }
073                } catch (Exception e) {
074                    // LOGGER.warn("Failed to retrieve a Factory for new homologous gene object", e);
075                }
076                this.setDaoFactory(f);
077    
078            }
079            //@end snip change svn 132
080    
081            return (DAOCoreFactory) daoFactory;
082        }
083    
084        public Species getSpecies() {
085            if (species == null && this.daoFactory != null && this.daoFactory instanceof DAOSpeciesFactory) {
086                species = ((DAOSpeciesFactory) this.daoFactory).getSpecies();
087            }
088            return species;
089        }
090    
091        public void setSpecies(Species species) {
092            this.species = species;
093        }
094    
095        public DAOComparaFactory getComparaFactory(EnsemblComparaDivision comparaDivision) {
096    
097            if (comparaFactories.containsKey(comparaDivision)) {
098                return comparaFactories.get(comparaDivision);
099            } else if (this.getDaoFactory() != null) {
100                DAOComparaFactory f = this.getDaoFactory().getComparaFactory(comparaDivision);
101                comparaFactories.put(comparaDivision, f);
102                return f;
103            }
104            return null;
105        }
106    
107        public EnsemblComparaDivision getComparaDivision() {
108            if (this.getSpecies() != null) {
109                return this.getSpecies().getComparaDivision();
110            } else return null;
111        }
112    
113        public DAOComparaFactory getComparaFactory() {
114    
115            return this.getComparaFactory(this.getComparaDivision());
116    
117        }
118    
119    //   public DAOComparaFactory getInstantiatingComparaFactory() {
120    //        return instantiatingComparaFactory;
121    //   }
122    
123    //    public void setInstantiatingComparaFactory(DAOComparaFactory instantiatingComparaFactory) {
124    //        this.instantiatingComparaFactory = instantiatingComparaFactory;
125    //
126    //    }
127    
128        public void setDBVersion(String version) {
129            this.dbVersion = version;
130        }
131    
132        public String getAssembly() {
133    
134            if (this.getSpecies() == null || this.getDaoFactory() == null
135                    || this.getDaoFactory().getDatabase() == null) {
136                return null;
137            }
138    
139            try {
140    
141                if (this.getSpecies() instanceof CollectionSpecies) {
142                    return ((CollectionDatabase) this.getDaoFactory().getDatabase()).getAssembly(
143                            (CollectionSpecies) this.getSpecies());
144    
145                } else {
146                    return ((SingleSpeciesDatabase) this.getDaoFactory().getDatabase()).getAssembly();
147    
148                }
149            } catch (Exception e) {
150                return null;
151            }
152    
153        }
154    
155    }