1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.promotego.dao.hibernate.misc;
20
21 import org.promotego.api.geocoder.beans.Geolocation;
22 import org.promotego.api.geocoder.util.GeolocationRange;
23 import org.promotego.api.geocoder.util.Range;
24 import org.promotego.api.geocoder.util.RangeUtil;
25
26 public class RadiusWhereClauseBuilder
27 {
28 public static String buildWhereClause(String geolocationPath,
29 Geolocation searchBase, double radius)
30 {
31 GeolocationRange theRange = RangeUtil.getGeolocationRange(searchBase, radius);
32
33 StringBuilder whereClause = new StringBuilder("where ");
34
35 Range latitudeRange = theRange.getLatitudeRange();
36 assert latitudeRange.getMinimum() < latitudeRange.getMaximum()
37 : "Latitude ranges are not allowed to wrap";
38
39 whereClause.append(geolocationPath).append(".latitudeRadians >= ")
40 .append(latitudeRange.getMinimum()).append(" and ")
41 .append(geolocationPath).append(".latitudeRadians <= ")
42 .append(latitudeRange.getMaximum());
43
44 Range longitudeRange = theRange.getLongitudeRange();
45 assert Math.abs(longitudeRange.getMinimum()) <= Math.PI : "Longitute minimum out of range";
46 assert Math.abs(longitudeRange.getMaximum()) <= Math.PI : "Longitute maximum out of range";
47
48 if (longitudeRange.getMinimum() <= longitudeRange.getMaximum())
49 {
50
51 whereClause.append(" and ").append(geolocationPath)
52 .append(".longitudeRadians >= ")
53 .append(longitudeRange.getMinimum()).append(" and ")
54 .append(geolocationPath).append(".longitudeRadians <= ")
55 .append(longitudeRange.getMaximum());
56 }
57 else
58 {
59
60 whereClause.append(" and ( ").append(geolocationPath)
61 .append(".longitudeRadians <= ")
62 .append(longitudeRange.getMinimum()).append(" or ")
63 .append(geolocationPath).append(".longitudeRadians >= ")
64 .append(longitudeRange.getMaximum())
65 .append(" )");
66 }
67
68 return whereClause.toString();
69 }
70 }