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.vividsolutions.jts.geom.Polygon;
34  
35  /**
36   * An implementation of the <code>Criterion</code> interface that implements
37   *  restriction for a psql intersects restriction
38   * 
39   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
40   */
41  public class IntersectsRestriction implements Criterion {
42  
43  
44      private Polygon polygon;
45  
46      private String columnName;
47  
48     
49  
50      private static final long serialVersionUID = 1L;
51  
52  
53      /**
54       * @param columnName the name of the column that hold GIS information (typically shape column)
55       * @param polygon The shape we want to see if the column data intersects
56       */
57      public IntersectsRestriction(String columnName, Polygon polygon) {
58  	if (columnName == null){
59  	    throw new IllegalArgumentException("collumname is required  for IntersectsRestriction");
60  	}
61  	if (polygon == null){
62  	    throw new IllegalArgumentException("polygon is required  for IntersectsRestriction");
63  	}
64  	this.columnName = columnName;
65  	this.polygon = polygon;
66      }
67      
68  
69  
70      /*
71       * (non-Javadoc)
72       * 
73       * @see org.hibernate.criterion.Criterion#getTypedValues(org.hibernate.Criteria,
74       *      org.hibernate.criterion.CriteriaQuery)
75       */
76      public TypedValue[] getTypedValues(Criteria criteria,
77  	    CriteriaQuery criteriaQuery) throws HibernateException {
78  	return new TypedValue[] { criteriaQuery.getTypedValue(criteria,
79  		GisFeature.LOCATION_COLUMN_NAME, polygon) };
80  
81      }
82  
83      /*
84       * (non-Javadoc)
85       * 
86       * @see org.hibernate.criterion.Criterion#toSqlString(org.hibernate.Criteria,
87       *      org.hibernate.criterion.CriteriaQuery)
88       */
89      public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
90  	    throws HibernateException {
91  	String columnName = criteriaQuery.getColumn(criteria,
92  		this.columnName);
93  	StringBuffer result = new StringBuffer(columnName).append(
94  		" && ").append(" ? ");
95  	return result.toString();
96  	    }
97  
98  }