View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.promotego.dao.hibernate;
20  
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.promotego.api.geocoder.beans.Geolocation;
25  import org.promotego.api.geocoder.util.DistanceUtil;
26  import org.promotego.dao.hibernate.misc.DistanceTool;
27  import org.promotego.dao.hibernate.misc.RadiusWhereClauseBuilder;
28  import org.springframework.orm.hibernate3.HibernateTemplate;
29  
30  public abstract class HibernateRadiusSearchDaoSupport<T>
31                                              extends HibernateDaoSupport<T>
32  {
33      private DistanceTool<T> m_distanceTool;
34      
35      protected HibernateRadiusSearchDaoSupport(DistanceTool<T> distanceTool)
36      {
37          m_distanceTool = distanceTool;
38      }
39  
40      /***
41       * Find all locations within the radius in miles specified of the given
42       * location.
43       */
44      public List<T> findNear(Geolocation theTee, double radius)
45      {
46          // Get the locations from the database
47          List<T> retval = findInRange(theTee, radius);
48      
49          // perform distance calculation and remove all locations outside radius
50          Iterator<T> iterator = retval.iterator();
51          while (iterator.hasNext())
52          {
53              T thisTee = iterator.next();
54              if (DistanceUtil.calculateDistance(theTee,
55                                      m_distanceTool.getGeolocation(thisTee))
56                      > radius)
57              {
58                  iterator.remove();
59              }
60          }
61          
62          return retval;
63      }
64  
65      protected List<T> findInRange(Geolocation geolocation, double radius)
66      {
67          // retrieve a list of candidates based on the range
68          HibernateTemplate ht = getHibernateTemplate();
69          
70          String whereClause = RadiusWhereClauseBuilder.buildWhereClause(m_distanceTool.getPathToGeolocation(), geolocation, radius);
71          
72          @SuppressWarnings("unchecked")
73              List<T> retval = (List<T>)ht.find(m_distanceTool.getBaseQuery() + " " + whereClause);
74      
75          return retval;
76      }
77  
78  }