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.coreaccess;
023
024import uk.ac.roslin.ensembl.datasourceaware.core.DACoordinateSystem;
025import java.util.ArrayList;
026import java.util.HashMap;
027import java.util.List;
028import org.apache.ibatis.session.SqlSession;
029import uk.ac.roslin.ensembl.dao.coreaccess.CoordinateSystemDAO;
030import uk.ac.roslin.ensembl.dao.factory.DAOCollectionCoreFactory;
031import uk.ac.roslin.ensembl.dao.factory.DAOSingleSpeciesCoreFactory;
032import uk.ac.roslin.ensembl.mapper.core.SpeciesMapper;
033import uk.ac.roslin.ensembl.exception.DAOException;
034
035
036/**
037 *
038 * @author paterson
039 */
040public class DBCoordinateSystemDAO extends DBCoreObjectDAO implements CoordinateSystemDAO {
041
042    public DBCoordinateSystemDAO()  {
043        super();
044    }
045
046    public DBCoordinateSystemDAO(DAOSingleSpeciesCoreFactory factory)  {
047        super(factory);
048    }
049    
050    public DBCoordinateSystemDAO(DAOCollectionCoreFactory factory) {
051        super(factory);
052    }
053
054    @Override
055    public List<DACoordinateSystem> getCoordinateSystems() throws DAOException {
056
057        List<DACoordinateSystem> out = new ArrayList<DACoordinateSystem>();
058        SqlSession session = null;
059        HashMap<String, Object> parameters = new HashMap<String, Object>();
060
061        try {
062            //hack following :)
063            //old databases, and hence their factories) for species removed
064            //from current ensembl don't have species associated!
065            //cos their is no current species for them
066            if (species !=null) {
067                parameters.put("speciesID",
068                    species.getDBSpeciesID(this.getFactory().getDBVersion()));
069            } else {
070                parameters.put("speciesID", new Integer(1));
071            }
072            session = this.getFactory().getNewSqlSession();
073            SpeciesMapper mapper = session.getMapper(SpeciesMapper.class);
074            out = mapper.getCoordSystems(parameters);
075        } catch (Exception e) {
076            throw new DAOException("Failed to call getCoordinateSystems", e);
077        } finally {
078            if (session != null) {
079                session.close();
080            }
081        }
082
083        return out;
084    }
085
086    @Override
087    public void setFeatureCS() throws DAOException {
088
089        List<HashMap> tempList = null;
090        SqlSession session = null;
091
092        try {
093            HashMap<String, Object> parameters = new HashMap<String, Object>();
094            parameters.put("dbName", this.getFactory().getDatabase().getdBName());
095            //hack following :)
096            //old databases, and hence their factories) for species removed
097            //from current ensembl don't have species associated!
098            //cos their is no current species for them
099            if (species !=null) {
100                parameters.put("speciesID",
101                    species.getDBSpeciesID(this.getFactory().getDBVersion()));
102            } else {
103                parameters.put("speciesID", new Integer(1));
104            }
105            session = this.getFactory().getNewSqlSession();
106            SpeciesMapper mapper = session.getMapper(SpeciesMapper.class);
107            tempList = mapper.setFeatureCS(parameters);
108        } catch (Exception e) {
109            throw new DAOException("Failed to call setFeaturesCS", e);
110        } finally {
111            if (session != null) {
112                session.close();
113            }
114        }
115
116        if (tempList != null && !tempList.isEmpty()) {
117            for (HashMap m : tempList) {
118
119                try {
120                    String feature = (String) m.get("feature_type");
121                    Integer max = (Integer) m.get("max_length");
122                    Integer csID = (Integer) m.get("cs_id");
123
124                    if (singleSpecies) {
125                        ssFactory.getDatabase().addFeatureCS(feature, csID, max);
126                    } else {
127                        collFactory.getDatabase().addFeatureCS(feature, csID, max, species);
128                    }
129
130                } catch (Exception e) {
131                }
132            }
133        }
134
135    }
136}