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