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;
023
024import java.util.Collection;
025import java.util.HashMap;
026import java.util.TreeSet;
027import uk.ac.roslin.ensembl.model.core.CollectionOfSpecies;
028import uk.ac.roslin.ensembl.model.core.CollectionSpecies;
029import uk.ac.roslin.ensembl.model.database.CollectionDatabase;
030import uk.ac.roslin.ensembl.model.database.DatabaseType;
031
032public class DBCollection implements CollectionOfSpecies  {
033
034    private String collectionName="";
035    private String dBversion="";
036    private String schemaVersion="";
037    private TreeSet<DBCollectionSpecies> species = new TreeSet<DBCollectionSpecies>();
038    private DBCollectionCoreDatabase coreDatabase;
039    private TreeSet<DBCollectionDatabase> databases = new TreeSet<DBCollectionDatabase>();
040
041    public DBCollection() {
042    }
043
044    public DBCollection(DBCollectionCoreDatabase database) {
045        this.setPrimaryCoreDatabase(database);
046    }
047
048    public void setPrimaryCoreDatabase(DBCollectionCoreDatabase database) {
049        this.coreDatabase = database;
050        if (this.coreDatabase != null) {
051            this.databases.add(database);
052        }
053        collectionName=this.coreDatabase!=null ? coreDatabase.getCollectionName() : null;
054        schemaVersion = this.coreDatabase!=null ? coreDatabase.getSchemaVersion() : null;
055        dBversion = this.coreDatabase != null ? coreDatabase.getDBVersion() : null ;
056    }
057
058
059    @Override
060    public void setProperty(String id, HashMap row) {
061    }
062
063    @Override
064    public String getDBVersion() {
065        return dBversion;
066    }
067
068    @Override
069    public String getCollectionName() {
070        return collectionName;
071    }
072
073    @Override
074    public TreeSet<DBCollectionDatabase> getDatabases() {
075        return databases;
076    }
077
078    @Override
079    public void addDatabases(TreeSet<? extends CollectionDatabase> databases) {
080
081        for (CollectionDatabase d : databases) {
082            try {
083                this.databases.add((DBCollectionDatabase) d);
084            } catch (Exception e) {
085            }
086        }
087    }
088
089    @Override
090    public TreeSet<DBCollectionSpecies> getSpecies() {
091        return this.species;
092    }
093
094    @Override
095    public void addSpecies(Collection<? extends CollectionSpecies> spp) {
096        for (CollectionSpecies cs : spp) {
097                this.addSpecies(cs);
098        }
099    }
100    
101    @Override
102    public void addSpecies(CollectionSpecies sp) {
103            try {
104                this.species.add((DBCollectionSpecies) sp);
105            } catch (Exception e) {
106            }
107    }
108
109    @Override
110    public DBCollectionCoreDatabase getCoreDatabase() {
111        return coreDatabase;
112    }
113
114    @Override
115    public String getSchemaVersion() {
116        return schemaVersion;
117    }
118
119    @Override
120    public TreeSet<DBCollectionDatabase> getDatabasesByType(DatabaseType type) {
121        TreeSet<DBCollectionDatabase> out = new TreeSet<DBCollectionDatabase>();
122        for (DBCollectionDatabase d : databases) {
123            if (d.getType()==type) {
124                out.add(d);
125            }
126        }
127        return out;
128    }
129
130    @Override
131    public String toString() {
132        return this.collectionName;
133    }
134
135    /**
136     * Finalization method to be called after all species have been added to all collections.
137     * This sets all the species to have the most recent dbstyle name, and all the TreeSets 
138     * in the collections to be remade - because they will sort on this.
139     */
140    public void resortSpecies() {
141
142        TreeSet<DBCollectionSpecies> temp = new TreeSet<DBCollectionSpecies>();
143        
144        for (DBCollectionSpecies sp: species) {
145            
146            if (sp.getDatabaseStyleSpeciesName(sp.getHighestDBRelease())!=null) {
147                sp.setDatabaseStyleSpeciesName(sp.getDatabaseStyleSpeciesName(sp.getHighestDBRelease()));
148            }
149            temp.add(sp);
150        }
151        
152        species = temp;
153    }
154    
155    
156}