1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
47 List<T> retval = findInRange(theTee, radius);
48
49
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
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 }