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.datasourceaware.core;
023
024import java.util.Date;
025import java.util.HashMap;
026import uk.ac.roslin.ensembl.config.EnsemblComparaDivision;
027import uk.ac.roslin.ensembl.config.EnsemblDBType;
028import uk.ac.roslin.ensembl.dao.factory.DAOComparaFactory;
029import uk.ac.roslin.ensembl.dao.factory.DAOCoreFactory;
030import uk.ac.roslin.ensembl.dao.factory.DAOSpeciesFactory;
031import uk.ac.roslin.ensembl.datasourceaware.DAObject;
032import uk.ac.roslin.ensembl.model.StableID;
033import uk.ac.roslin.ensembl.model.core.CollectionSpecies;
034import uk.ac.roslin.ensembl.model.core.CoreObject;
035import uk.ac.roslin.ensembl.model.core.Species;
036import uk.ac.roslin.ensembl.model.database.CollectionCoreDatabase;
037import uk.ac.roslin.ensembl.model.database.CollectionDatabase;
038import uk.ac.roslin.ensembl.model.database.SingleSpeciesCoreDatabase;
039import uk.ac.roslin.ensembl.model.database.SingleSpeciesDatabase;
040
041public abstract class DACoreObject extends DAObject implements CoreObject, StableID {
042
043    protected Species species = null;
044    protected String stableID = null;
045    protected Integer version = null;
046    protected Date modificationDate = null;
047    protected Date creationDate = null;
048    
049    
050//    protected DAOComparaFactory instantiatingComparaFactory = null;
051    protected HashMap<EnsemblComparaDivision, DAOComparaFactory> comparaFactories =
052            new HashMap<EnsemblComparaDivision, DAOComparaFactory>();
053
054    public DACoreObject() {
055        super();
056    }
057
058    public DACoreObject(DAOCoreFactory factory) {
059        super(factory);
060    }
061
062    @Override
063    public DAOCoreFactory getDaoFactory() {
064
065        //@start of snip to instantiate a coreFactory from species +version info
066        //if their is no corefactory set : see matching change in DBHomologyDAO svn 132
067
068        //try and make a factory if there isn't one, but we do have set the db version and species
069        if (daoFactory == null && this.getDBVersion() != null
070                && this.getSpecies() != null) {
071
072            DAOCoreFactory f = null;
073
074            try {
075                //are we single or multispecies
076                if (this.getSpecies() instanceof CollectionSpecies) {
077                    f = (DAOCoreFactory) ((CollectionCoreDatabase) ((CollectionSpecies) this.getSpecies()).getDatabaseByTypeAndVersion(
078                            EnsemblDBType.collection_core, this.getDBVersion())).getCoreFactory(this.getSpecies());
079                } else {
080                    f = (DAOCoreFactory) ((SingleSpeciesCoreDatabase) this.getSpecies().getDatabaseByTypeAndVersion(
081                            EnsemblDBType.core, this.getDBVersion())).getCoreFactory();
082                }
083            } catch (Exception e) {
084                // LOGGER.warn("Failed to retrieve a Factory for new homologous gene object", e);
085            }
086            this.setDaoFactory(f);
087
088        }
089        //@end snip change svn 132
090
091        return (DAOCoreFactory) daoFactory;
092    }
093
094    public Species getSpecies() {
095        if (species == null && this.daoFactory != null && this.daoFactory instanceof DAOSpeciesFactory) {
096            species = ((DAOSpeciesFactory) this.daoFactory).getSpecies();
097        }
098        return species;
099    }
100
101    public void setSpecies(Species species) {
102        this.species = species;
103    }
104
105    public DAOComparaFactory getComparaFactory(EnsemblComparaDivision comparaDivision) {
106
107        if (comparaFactories.containsKey(comparaDivision)) {
108            return comparaFactories.get(comparaDivision);
109        } else if (this.getDaoFactory() != null) {
110            DAOComparaFactory f = this.getDaoFactory().getComparaFactory(comparaDivision);
111            comparaFactories.put(comparaDivision, f);
112            return f;
113        }
114        return null;
115    }
116
117    public EnsemblComparaDivision getComparaDivision() {
118        if (this.getSpecies() != null) {
119            return this.getSpecies().getComparaDivision();
120        } 
121        return null;
122    }
123
124    public DAOComparaFactory getComparaFactory() {
125
126        return this.getComparaFactory(this.getComparaDivision());
127
128    }
129
130//   public DAOComparaFactory getInstantiatingComparaFactory() {
131//        return instantiatingComparaFactory;
132//   }
133
134//    public void setInstantiatingComparaFactory(DAOComparaFactory instantiatingComparaFactory) {
135//        this.instantiatingComparaFactory = instantiatingComparaFactory;
136//
137//    }
138
139    public void setDBVersion(String version) {
140        this.dbVersion = version;
141    }
142
143    @Override
144    public String getAssembly() {
145
146        if (this.getSpecies() == null || this.getDaoFactory() == null
147                || this.getDaoFactory().getDatabase() == null) {
148            return null;
149        }
150
151        try {
152
153            if (this.getSpecies() instanceof CollectionSpecies) {
154                return ((CollectionCoreDatabase) this.getDaoFactory().getDatabase()).getAssemblyName(
155                        (CollectionSpecies) this.getSpecies());
156
157            } else {
158                return ((SingleSpeciesCoreDatabase) this.getDaoFactory().getDatabase()).getAssemblyName();
159
160            }
161        } catch (Exception e) {
162            return null;
163        }
164
165    }
166
167    @Override
168    public Date getCreationDate() {
169        return creationDate;
170    }
171
172    @Override
173    public void setCreationDate(Date creationDate) {
174        this.creationDate = creationDate;
175    }
176
177    @Override
178    public Date getModificationDate() {
179        return modificationDate;
180    }
181
182    @Override
183    public void setModificationDate(Date modificationDate) {
184        this.modificationDate = modificationDate;
185    }
186
187    @Override
188    public String getStableID() {
189        return stableID;
190    }
191
192    @Override
193    public void setStableID(String stableID) {
194        this.stableID = stableID;
195    }
196
197    @Override
198    public Integer getVersion() {
199        return version;
200    }
201
202    @Override
203    public void setVersion(Integer version) {
204        this.version = version;
205    }
206
207}