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.dao.database.coreaccess;
021    
022    import java.util.ArrayList;
023    import uk.ac.roslin.ensembl.datasourceaware.core.DACoordinateSystem;
024    import java.util.HashMap;
025    import java.util.List;
026    import org.apache.ibatis.session.SqlSession;
027    import uk.ac.roslin.ensembl.dao.coreaccess.ChromosomeDAO;
028    import uk.ac.roslin.ensembl.datasourceaware.core.DAChromosome;
029    import uk.ac.roslin.ensembl.dao.database.factory.DBDAOCollectionCoreFactory;
030    import uk.ac.roslin.ensembl.dao.database.factory.DBDAOSingleSpeciesCoreFactory;
031    import uk.ac.roslin.ensembl.dao.factory.DAOCollectionCoreFactory;
032    import uk.ac.roslin.ensembl.dao.factory.DAOSingleSpeciesCoreFactory;
033    import uk.ac.roslin.ensembl.mapper.core.ChromosomeMapper;
034    import uk.ac.roslin.ensembl.exception.DAOException;
035    import uk.ac.roslin.ensembl.model.core.CoordinateSystem;
036    
037    /**
038     *
039     * @author paterson
040     */
041    public class DBChromosomeDAO extends DBCoreObjectDAO implements ChromosomeDAO {
042    
043        public DBChromosomeDAO() {
044            super();
045        }
046    
047        public DBChromosomeDAO(DAOSingleSpeciesCoreFactory factory) {
048            super(factory);
049        }
050    
051        public DBChromosomeDAO(DAOCollectionCoreFactory factory)  {
052            super(factory);
053        }
054    
055        @Override
056        public DAChromosome getChromosomeByName(String name) throws DAOException {
057    
058            if (name==null || name.isEmpty()) {
059                return null;
060            }
061    
062            DAChromosome out = null;
063            String chrName = name;
064            CoordinateSystem chrCS = null;
065    
066            if (singleSpecies) {
067                chrCS = ssFactory.getDatabase().getChromosomeLevelCoordSystem();
068            } else {
069                chrCS = collFactory.getDatabase().getChromosomeLevelCS(species);
070            }
071    
072            if (chrCS==null || chrCS.getId()==null  ) {
073                throw new DAOException("Failed to call to retrieve the CoordinateSystem for Chromosomes");
074            }
075    
076            Integer chrCSID = chrCS.getId();
077    
078            HashMap parameters = new HashMap();
079            parameters.put("chrName",chrName);
080            parameters.put("coordSysID", chrCSID);
081    
082            SqlSession session = null;
083    
084            try {
085                session = this.getFactory().getNewSqlSession();
086                ChromosomeMapper mapper = session.getMapper(ChromosomeMapper.class);
087                out = mapper.getChromosomeByName_CoordSysID(parameters);
088            } catch (Exception e) {
089                throw new DAOException("Failed to call getChromsomeByName_CoordSysID", e);
090            } finally {
091                if (session != null) {
092                    session.close();
093                }
094            }
095    
096            if (out != null) {
097                out.setDaoFactory(daoFactory);
098                out.setCoordSystem(chrCS);
099                out.setSpecies(species);
100            }
101    
102            return out;
103        }
104    
105        @Override
106        public List<DAChromosome> getChromosomes() throws DAOException {
107    
108            List<DAChromosome> out = new ArrayList<DAChromosome>();
109            CoordinateSystem chrCS = null;
110    
111            if (singleSpecies) {
112                chrCS = ssFactory.getDatabase().getChromosomeLevelCoordSystem();
113            } else {
114                chrCS = collFactory.getDatabase().getChromosomeLevelCS(species);
115            }
116    
117            if (chrCS==null || chrCS.getId()==null  ) {
118                throw new DAOException("Failed to call to retrieve the CoordinateSystem for Chromosomes");
119            }
120    
121            Integer chrCSID = chrCS.getId();
122    
123            HashMap parameters = new HashMap();
124            parameters.put("coordSysID", chrCSID);
125    
126            SqlSession session = null;
127    
128            try {
129                session = this.getFactory().getNewSqlSession();
130                ChromosomeMapper mapper = session.getMapper(ChromosomeMapper.class);
131                out = mapper.getChromosomesByCoordSysID(parameters);
132            } catch (Exception e) {
133                throw new DAOException("Failed to call getChromsomes", e);
134            } finally {
135                if (session != null) {
136                    session.close();
137                }
138            }
139            for (DAChromosome c : out) {
140                c.setDaoFactory(daoFactory);
141                c.setCoordSystem(chrCS);
142                c.setSpecies(species);
143            }
144    
145            return out;
146        }
147    }
148    
149