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.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Set;
028import org.apache.ibatis.session.SqlSession;
029import uk.ac.roslin.ensembl.config.EnsemblDBType;
030import uk.ac.roslin.ensembl.config.FeatureType;
031import uk.ac.roslin.ensembl.dao.database.factory.DBDAOSingleSpeciesFactory;
032import uk.ac.roslin.ensembl.dao.database.factory.DBDAOSingleSpeciesVariationFactory;
033import uk.ac.roslin.ensembl.dao.factory.DAOVariationFactory;
034import uk.ac.roslin.ensembl.datasourceaware.core.DACoordinateSystem;
035import uk.ac.roslin.ensembl.exception.ConfigurationException;
036import uk.ac.roslin.ensembl.exception.DAOException;
037import uk.ac.roslin.ensembl.mapper.variation.VariationMapper;
038import uk.ac.roslin.ensembl.model.ObjectType;
039import uk.ac.roslin.ensembl.model.core.CoordinateSystem;
040import uk.ac.roslin.ensembl.model.database.Registry;
041import uk.ac.roslin.ensembl.model.database.SingleSpeciesCoreDatabase;
042import uk.ac.roslin.ensembl.model.database.SingleSpeciesVariationDatabase;
043/**
044 *
045 * @author tpaterso
046 */
047public class DBSingleSpeciesVariationDatabase extends 
048        DBSingleSpeciesDatabase implements SingleSpeciesVariationDatabase  {
049    
050    private SingleSpeciesCoreDatabase coreDB = null;
051    private HashMap<FeatureType, HashMap<DACoordinateSystem, Integer>> featureCSHash = null;
052    private HashMap<DACoordinateSystem, List<FeatureType>> CSFeatureHash = null;    
053    
054    public DBSingleSpeciesVariationDatabase(String db_name, EnsemblDBType type, Registry registry) throws ConfigurationException {
055        super(db_name, type, registry);
056    }
057    
058    public DAOVariationFactory getVariationFactory() {
059        if (this.factory != null) {
060            return (DAOVariationFactory) this.factory;
061        } else {
062            try {
063                this.factory = DBDAOSingleSpeciesFactory.makeFactory(this);
064            } catch (Exception ex) {
065            }
066            return (DBDAOSingleSpeciesVariationFactory) this.factory;
067
068        }
069    }
070    
071    
072
073    public DBSingleSpeciesCoreDatabase getCoreDB() {
074        if (coreDB != null) {
075            return (DBSingleSpeciesCoreDatabase) coreDB;
076        } else if (this.getSpecies()!= null) {
077            coreDB = (DBSingleSpeciesCoreDatabase) this.getSpecies().getDatabaseByTypeAndVersion(EnsemblDBType.core, this.getDBVersion());
078        }
079        
080        return (DBSingleSpeciesCoreDatabase) coreDB;
081    }
082
083    public void setCoreDB(SingleSpeciesCoreDatabase coreDB) {
084        this.coreDB = coreDB;
085    }
086    
087    private void addFeatureCS(String featureType, Integer csID, Integer maxLength) {
088
089        FeatureType type = null;
090        DACoordinateSystem cs = null;
091
092        type = FeatureType.getFeatureType(featureType);
093        try {
094            cs = this.getCoreDB().getCSByID(csID);
095        } catch (DAOException ex) {
096        }
097
098        if (type == null || cs == null) {
099            return;
100        } else {
101            if (!this.featureCSHash.containsKey(type)) {
102                this.featureCSHash.put(type, new HashMap<DACoordinateSystem, Integer>());
103            }
104            this.featureCSHash.get(type).put(cs, maxLength);
105
106            if (!this.CSFeatureHash.containsKey(cs)) {
107                this.CSFeatureHash.put(cs, new ArrayList<FeatureType>());
108            }            
109            this.CSFeatureHash.get(cs).add(type);
110
111        }
112
113
114    }
115
116    private void setFeatureCS() throws DAOException {
117
118        if (this.getCoreDB()==null) {
119            return;
120        }
121        
122        featureCSHash = new HashMap<FeatureType, HashMap<DACoordinateSystem, Integer>> ();
123        CSFeatureHash  = new HashMap<DACoordinateSystem, List<FeatureType>>();
124        
125        List<HashMap> tempList = null;
126        SqlSession session = null;
127
128        try {
129            session = this.getVariationFactory().getNewSqlSession();
130            VariationMapper mapper = session.getMapper(VariationMapper.class);
131            tempList = mapper.setFeatureCS();
132        } catch (Exception e) {
133            throw new DAOException("Failed to call setFeaturesCS", e);
134        } finally {
135            if (session != null) {
136                session.close();
137            }
138        }
139
140        if (tempList != null && !tempList.isEmpty()) {
141            for (HashMap m : tempList) {
142
143                try {
144                    String feature = (String) m.get("feature_type");
145                    Integer max = (Integer) m.get("max_length");
146                    Integer csID = (Integer) m.get("cs_id");
147
148                    this.addFeatureCS(feature, csID, max);
149
150
151                } catch (Exception e) {
152                }
153            }
154        }
155
156    
157    }
158     
159    public Set<DACoordinateSystem> getCSForFeature(ObjectType featureType) throws DAOException {
160        
161        if (featureType == null || !FeatureType.getAllTypes().contains(featureType)) {
162            return null;
163        }
164
165        if (this.featureCSHash == null) {
166                this.setFeatureCS();
167        }
168
169        if (this.featureCSHash != null
170                && this.featureCSHash.containsKey(featureType)) {
171            return this.featureCSHash.get(featureType).keySet();
172        } else {
173
174            return null;
175        }
176    }
177
178
179    public List<FeatureType> getFeaturesForCS(CoordinateSystem coordSys) throws DAOException{
180        
181        if (coordSys==null) {
182            return null;
183        }
184
185        if (this.featureCSHash == null) {
186                this.setFeatureCS();
187        }
188
189        if (this.CSFeatureHash != null
190                && this.CSFeatureHash.containsKey(coordSys)) {
191            return this.CSFeatureHash.get(coordSys);
192        } else {
193            return null;
194        }
195    }
196
197    public Integer getMaxLengthForFeature(ObjectType featureType, CoordinateSystem cs) throws DAOException {
198
199        if ( featureType == null || cs == null ) {
200            return null;
201        }
202
203        if (this.featureCSHash == null) {
204                this.setFeatureCS();
205        }
206
207        if (this.featureCSHash != null
208                && this.featureCSHash.containsKey(featureType)
209                && this.featureCSHash.get(featureType).containsKey(cs)) {
210            return this.featureCSHash.get(featureType).get(cs);
211        } else {
212            return null;
213        }
214
215    }
216
217//    public void addHandler(Class<?> type, JdbcType  jdbcType, TypeHandler  handler) throws DAOException {
218//        this.getSessionFactory().getConfiguration().getTypeHandlerRegistry()
219//                .register(type, jdbcType, handler);
220//    }
221//    
222//    public void addMapper(String resource) throws DAOException {
223//
224//        if (resource != null) {
225//
226//            try {
227//                ErrorContext.instance().resource(resource);
228//                Reader reader = Resources.getResourceAsReader(resource);
229//                Configuration conf = this.getSessionFactory().getConfiguration();
230//                XMLMapperBuilder mapperParser = new XMLMapperBuilder(reader, conf, resource, new HashMap<String, XNode>());
231//                mapperParser.parse();
232//            } catch (Exception ex) {
233//                throw new DAOException("Failed to add a new SQL Mapper Resource", ex);
234//            }
235//        }
236//    }
237}