View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  
24  package com.gisgraphy.hibernate.criterion;
25  
26  import org.hibernate.Criteria;
27  import org.hibernate.HibernateException;
28  import org.hibernate.criterion.CriteriaQuery;
29  import org.hibernate.criterion.Criterion;
30  import org.hibernate.engine.TypedValue;
31  
32  import com.gisgraphy.domain.geoloc.entity.GisFeature;
33  import com.gisgraphy.helper.GisHelper;
34  import com.vividsolutions.jts.geom.Point;
35  
36  /**
37   * An implementation of the <code>Criterion</code> interface that implements
38   * distance restriction
39   * 
40   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
41   */
42  public class DistanceRestriction implements Criterion {
43  
44      /**
45       * Point we have to calculate the distance from
46       */
47      private Point point = null;
48  
49      private boolean useIndex = true;
50  
51      /**
52       * The distance restriction
53       */
54      private double distance;
55  
56     
57  
58      private static final long serialVersionUID = 1L;
59  
60      /**
61       * @param point
62       *                Point we have to calculate the distance from
63       * @param distance
64       *                The distance restriction
65       * @param useIndex
66       *                Wether we must use index or not
67       */
68      public DistanceRestriction(Point point, double distance, boolean useIndex) {
69  	this.point = point;
70  	this.distance = distance;
71  	this.useIndex = useIndex;
72      }
73  
74      /**
75       * @param point
76       *                Point we have to calculate the distance from
77       * @param distance
78       *                The distance restriction by default use index
79       */
80      public DistanceRestriction(Point point, double distance) {
81  	this.point = point;
82  	this.distance = distance;
83      }
84  
85      /*
86       * (non-Javadoc)
87       * 
88       * @see org.hibernate.criterion.Criterion#getTypedValues(org.hibernate.Criteria,
89       *      org.hibernate.criterion.CriteriaQuery)
90       */
91      public TypedValue[] getTypedValues(Criteria criteria,
92  	    CriteriaQuery criteriaQuery) throws HibernateException {
93  	return new TypedValue[] { criteriaQuery.getTypedValue(criteria,
94  		GisFeature.LOCATION_COLUMN_NAME, point) };
95  
96      }
97  
98      /*
99       * (non-Javadoc)
100      * 
101      * @see org.hibernate.criterion.Criterion#toSqlString(org.hibernate.Criteria,
102      *      org.hibernate.criterion.CriteriaQuery)
103      */
104     public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
105 	    throws HibernateException {
106 	String columnName = criteriaQuery.getColumn(criteria,
107 		GisFeature.LOCATION_COLUMN_NAME);
108 	StringBuffer result = new StringBuffer("( st_distance_sphere(").append(
109 		columnName).append(", ?) <=").append(this.distance).append(")");
110 	return useIndex ? result.append(" AND ").append(
111 			GisHelper.getBoundingBox(criteriaQuery.getSQLAlias(criteria), this.point
112 			.getY(), this.point.getX(), distance)).toString()
113 		: result.toString();
114 
115     }
116 
117 }