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.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              // "Normal" range
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              // "Wrapped" range
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  }