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 package com.gisgraphy.hibernate.criterion;
24
25 import org.hibernate.Criteria;
26 import org.hibernate.HibernateException;
27 import org.hibernate.criterion.CriteriaQuery;
28 import org.hibernate.criterion.Order;
29
30 import com.gisgraphy.domain.valueobject.SRID;
31 import com.vividsolutions.jts.geom.Point;
32
33 /**
34 * A criteria that sort by distance for better performance you can use
35 * {@link ProjectionOrder} if a projection for distance calculation have been
36 * added
37 *
38 * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
39 */
40 public class DistanceOrder extends Order {
41
42 /**
43 * Default serial id
44 */
45 private static final long serialVersionUID = 7382013976844760232L;
46
47 private Point point;
48 private boolean ascending = true;
49
50 /**
51 * @param point
52 * The point from which we calculate the distance
53 * @param ascending
54 * Whether we sort Ascending
55 */
56 public DistanceOrder(Point point, boolean ascending) {
57 super(null, ascending);
58 this.point = point;
59 this.ascending = ascending;
60 }
61
62 /**
63 * @param point
64 * the point from which we calculate the distance, default
65 * ascending is true
66 */
67 public DistanceOrder(Point point) {
68 super(null, true);
69 this.point = point;
70 }
71
72 /*
73 * (non-Javadoc)
74 *
75 * @see org.hibernate.criterion.Order#toSqlString(org.hibernate.Criteria,
76 * org.hibernate.criterion.CriteriaQuery)
77 */
78 @Override
79 public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
80 throws HibernateException {
81 StringBuilder fragment = new StringBuilder();
82 fragment.append(" distance_sphere(");
83 fragment.append(criteriaQuery.getSQLAlias(criteria));
84 fragment.append(".location,geometryfromtext('");
85 fragment.append(point.toText());
86 fragment.append("',");
87 fragment.append(SRID.WGS84_SRID.getSRID());
88 fragment.append(ascending ? ")) asc" : ")) desc");
89 return fragment.toString();
90 }
91
92 }