PageRenderTime 43ms CodeModel.GetById 18ms app.highlight 19ms RepoModel.GetById 2ms app.codeStats 0ms

/alaspatial/src/main/java/org/ala/spatial/dao/SpeciesDAOImpl.java

http://alageospatialportal.googlecode.com/
Java | 255 lines | 129 code | 36 blank | 90 comment | 11 complexity | 8e7c11400ed9e037781f153c5774bc2b MD5 | raw file
  1package org.ala.spatial.dao;
  2
  3import java.util.Hashtable;
  4import java.util.List;
  5import java.util.Map;
  6import java.util.Vector;
  7import org.ala.spatial.model.CommonName;
  8import org.ala.spatial.model.Species;
  9import org.ala.spatial.model.TaxonNames;
 10import org.ala.spatial.model.ValidTaxonName;
 11import org.hibernate.Query;
 12import org.hibernate.Session;
 13import org.hibernate.SessionFactory;
 14import org.springframework.beans.factory.annotation.Autowired;
 15import org.springframework.beans.factory.annotation.Qualifier;
 16import org.springframework.orm.hibernate3.HibernateCallback;
 17import org.springframework.orm.hibernate3.HibernateTemplate;
 18import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 19import org.springframework.stereotype.Service;
 20
 21/**
 22 * Implementation class for any of the species search.
 23 * Currently, all species seach is combined into a single class
 24 * for easier management
 25 *
 26 * @author ajayr
 27 * @version 1.0
 28 * 
 29 */
 30@Service(value="speciesDAO")
 31public class SpeciesDAOImpl extends HibernateDaoSupport implements SpeciesDAO {
 32
 33    /*
 34    private SessionFactory sessionFactory;
 35
 36    public void setSessionFactory(SessionFactory sessionFactory) {
 37    this.sessionFactory = sessionFactory;
 38    }
 39     */
 40    /**
 41    private HibernateTemplate hibernateTemplate;
 42
 43    public void setSessionFactory(SessionFactory sessionFactory) {
 44        //System.out.println("Setting session factory!!");
 45        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
 46    }
 47     **/
 48
 49    private HibernateTemplate hibernateTemplate;
 50
 51    @Autowired
 52    public SpeciesDAOImpl(@Qualifier("sessionFactory") SessionFactory sessionFactory) {
 53        this.setSessionFactory(sessionFactory);
 54        this.hibernateTemplate = new HibernateTemplate(sessionFactory); 
 55    }
 56
 57    /*
 58     * Test method to get some species quickly
 59     *
 60     * @return     List of species
 61     */
 62    // TODO maybe remove this class eventually?
 63    @Override
 64    public List<Species> getSpecies() {
 65        Vector tmpSpecies = new Vector();
 66
 67        Species s = new Species();
 68        s.setSpecies("Test name 1");
 69        tmpSpecies.add(s);
 70
 71        Species s2 = new Species();
 72        s2.setSpecies("Test name 2");
 73        tmpSpecies.add(s2);
 74
 75        return tmpSpecies;
 76    }
 77
 78    /*
 79     * Get all records for a given taxon name.
 80     *
 81     * @param name Taxon name
 82     * @return     List of species
 83     */
 84    @Override
 85    public List<Species> getRecordsByNameLevel(String name, String level) { //  throws DataAccessException
 86        //return this.sessionFactory.getCurrentSession().createQuery("from Species where scientificname = ? ").setParameter(0, name).list();
 87        level = level.trim().toLowerCase();
 88        if (level.equalsIgnoreCase("g") || level.equalsIgnoreCase("genus")) {
 89            level = "genus";
 90        } else if (level.equalsIgnoreCase("sp") || level.equalsIgnoreCase("species")) {
 91            level = "species";
 92        } else if (level.equalsIgnoreCase("sn")) {
 93            level = "scientificname";
 94        } else if (level.equalsIgnoreCase("f")) {
 95            level = "family";
 96        } else if (level.equalsIgnoreCase("cn")) {
 97            level = "commonname";
 98        }
 99
100        //Object[] params = {level, name};
101        //return hibernateTemplate.find("from Species where ? = ? ", params);
102        /*
103        Session session = getSession(false);
104        try {
105        Query query = session.createQuery("from Species where scientificname = ?");
106        query.setString(0, name);
107        List result = query.list();
108        if (result == null) {
109        //throw new MyException("No search results.");
110        }
111        return result;
112        } catch (HibernateException ex) {
113        throw convertHibernateAccessException(ex);
114        }
115         */
116
117
118        
119        final String theName = name; //.toLowerCase();
120        final String theLevel = level.toLowerCase();
121
122        return (List) this.hibernateTemplate.execute(new HibernateCallback() {
123
124            @Override
125            public List<Species> doInHibernate(Session session) {
126                Query query = session.createQuery("from Species where "+theLevel+" = :name AND (longitude <> '' or longitude <> '') "); // (:level)
127                //query.setParameter(0, theLevel);
128                //query.setParameter(1, theName);
129                //query.setString("level", theLevel);
130                query.setString("name", theName); 
131                return query.list();
132            }
133        });
134    }
135
136    /*
137     * Get all records for a given taxon id
138     *
139     * @param name Taxon id
140     */
141    @Override
142    public List<Species> getRecordsById(String id) {
143        //throw new UnsupportedOperationException("Not supported yet.");
144
145        final String lsid = id + "%";
146        return (List) this.hibernateTemplate.execute(new HibernateCallback() {
147
148            @Override
149            public List<Species> doInHibernate(Session session) {
150                //Query query = session.createQuery("from Species where taxonconceptid = :lsid AND (longitude <> '' or longitude <> '') "); // (:level)
151                //Query query = session.createQuery("from Species where scientificname like :lsid AND (longitude <> '' or longitude <> '') "); // (:level)
152                Query query = session.createQuery("from Species where scientificname like :id AND (longitude is not null or longitude is not null) "); // (:level)
153                //query.setParameter(0, theLevel);
154                //query.setParameter(1, theName);
155                //query.setString("level", theLevel);
156                query.setString("id", lsid);
157                return query.list();
158            }
159        });
160    }
161
162    /*
163     * Test methond to get taxon names as Master Names List.
164     *
165     * @return     List of names
166     */
167    @Override
168    public List<TaxonNames> getNames() {
169        Vector tmpSpecies = new Vector();
170
171        TaxonNames s = new TaxonNames();
172        s.setTname("Test name 1");
173        s.setTlevel("species");
174        tmpSpecies.add(s);
175
176        TaxonNames s2 = new TaxonNames();
177        s2.setTname("Test name 2");
178        s2.setTlevel("species");
179        tmpSpecies.add(s2);
180
181        TaxonNames s3 = new TaxonNames();
182        s3.setTname("Test name 3");
183        s3.setTlevel("genus");
184        tmpSpecies.add(s3);
185
186        TaxonNames s4 = new TaxonNames();
187        s4.setTname("Test name 4");
188        s4.setTlevel("phylum");
189        tmpSpecies.add(s4);
190
191        return tmpSpecies;
192
193    }
194
195    /*
196     * Get all taxon names list.
197     *
198     * @param name Taxon name
199     * @return     List of names
200     */
201    @Override
202    public List<TaxonNames> findByName(String name) {
203        return hibernateTemplate.find("from TaxonNames where lower(tname) like ? ", (name.toLowerCase() + "%"));
204    }
205
206    /*
207     * Get all taxon names list, compatible with paging. 
208     *
209     * @param name Taxon name
210     * @param limit limit factor for paging
211     * @param offset offset factor for paging 
212     * @return     Map of names and the total count available
213     */
214    @Override
215    public Map findByName(final String name, final int limit, final int offset) {  // throws DataAccessException
216        //Object[] mnldetails = {(name.toLowerCase() + "%"), limit, offset};
217        //return hibernateTemplate.find("from TaxonNames where lower(tname) like ? LIMIT ? OFFSET ? ", mnldetails);
218
219        int namesCount = ((Long) hibernateTemplate.find("select count(*) from TaxonNames where lower(tname) like ? ", (name.toLowerCase() + "%")).get(0)).intValue();
220
221        List namesList = (List) this.hibernateTemplate.execute(new HibernateCallback() {
222
223            @Override
224            public List<TaxonNames> doInHibernate(Session session) {
225                Query query = session.createQuery("from TaxonNames where lower(tname) LIKE ? ");
226                query.setParameter(0, (name.toLowerCase() + "%"));
227                query.setFirstResult(offset);
228                query.setMaxResults(limit);
229                return query.list();
230            }
231        });
232
233        Hashtable<String, Object> htNames = new Hashtable();
234        htNames.put("totalCount", namesCount);
235        htNames.put("taxanames", namesList);
236
237        return htNames;
238    }
239
240    @Override
241    public List<CommonName> getCommonNames(String name) {
242        //return hibernateTemplate.find("from CommonName where lower(commonname) like ? ", "%" + (name.toLowerCase() + "%"));
243        
244        //to allow easy removal of scientific name duplicates
245        return hibernateTemplate.find("from CommonName where lower(commonname) like ? order by scientificname", "%" + (name.toLowerCase() + "%"));
246    }
247
248    @Override
249    public List<ValidTaxonName> findById(String lsid) {
250        System.out.println("findById: " + lsid.toLowerCase());
251        List l = hibernateTemplate.find("from ValidTaxonName where lower(guid) = ? ", (lsid.toLowerCase()));
252        System.out.println("returning " + l.size() + " items.");
253        return l;
254    }
255}