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.HashMap;
025import java.util.List;
026import org.apache.ibatis.session.SqlSession;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029import uk.ac.roslin.ensembl.dao.AnalysisDAO;
030import uk.ac.roslin.ensembl.dao.factory.DAOFactory;
031import uk.ac.roslin.ensembl.datasourceaware.DAAnalysis;
032import uk.ac.roslin.ensembl.exception.DAOException;
033import uk.ac.roslin.ensembl.mapper.AnalysisMapper;
034
035
036public class DBAnalysisDAO extends DBBaseDAO implements AnalysisDAO {
037    
038    final static Logger LOGGER = LoggerFactory.getLogger(DBAnalysisDAO.class);
039
040    public DBAnalysisDAO() {
041        super();
042    }
043
044    public DBAnalysisDAO(DAOFactory factory) throws DAOException {
045        super(factory);
046    }
047    
048
049    @Override
050    public HashMap<Integer, DAAnalysis> getAnalyses() throws DAOException {
051        
052        HashMap<Integer, DAAnalysis> out = new HashMap<Integer, DAAnalysis>();
053        List<DAAnalysis> result = null;
054        SqlSession session = null;
055        String databaseName ="'unavailable  database'";
056
057        try {
058            databaseName = this.getFactory().getDatabaseName();
059            session = this.getFactory().getNewSqlSession();
060            AnalysisMapper mapper = session.getMapper(AnalysisMapper.class);
061            result= mapper.getAnalyses();
062        } catch (Exception e) {
063            throw new DAOException("Failed to call getAnalyses("+databaseName+") on AnalysisDAO", e);
064        } finally {
065            if (session != null) {
066                session.close();
067            }
068        }
069        
070        if (result != null && !result.isEmpty()) {
071            for (DAAnalysis anal: result) {
072                anal.setDaoFactory(daoFactory);
073                out.put(anal.getId(), anal);
074            }
075        }
076        
077        return out;
078    }
079    
080}