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  package com.gisgraphy.domain.repository;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import javax.persistence.PersistenceException;
29  
30  import org.hibernate.Query;
31  import org.hibernate.Session;
32  import org.springframework.orm.hibernate3.HibernateCallback;
33  import org.springframework.stereotype.Repository;
34  import org.springframework.util.Assert;
35  
36  import com.gisgraphy.domain.geoloc.entity.City;
37  import com.gisgraphy.domain.valueobject.SRID;
38  import com.vividsolutions.jts.geom.Point;
39  
40  /**
41   * A data access object for {@link City}
42   * 
43   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
44   */
45  @Repository
46  public class CityDao extends GenericGisDao<City> implements ICityDao {
47  
48      /**
49       * Default constructor
50       */
51      public CityDao() {
52  	super(City.class);
53      }
54  
55      /*
56       * (non-Javadoc)
57       * 
58       * @see com.gisgraphy.domain.repository.ICityDao#ListByZipCode(int,
59       *      java.lang.String)
60       */
61      @SuppressWarnings("unchecked")
62      public List<City> listByZipCode(final String zipcode, final String countrycode) {
63  	return (List<City>) this.getHibernateTemplate().execute(
64  		new HibernateCallback() {
65  
66  		    public Object doInHibernate(Session session)
67  			    throws PersistenceException {
68  			String queryString = "from "
69  				+ City.class.getSimpleName()
70  				+ " as c left outer join c.zipCodes z where z.code = ?";
71  			if (countrycode != null) {
72  			    queryString = queryString + " and c.countryCode=?";
73  			}
74  
75  			Query qry = session.createQuery(queryString);
76  			qry.setCacheable(true);
77  
78  			qry.setParameter(0, zipcode);
79  			if (countrycode != null) {
80  			    qry.setParameter(1, countrycode.toUpperCase());
81  			}
82  
83  			List<City> results = (List<City>) qry.list();
84  			if (results == null) {
85  			    results = new ArrayList<City>();
86  			}
87  			return results;
88  		    }
89  		});
90      }
91      
92     
93      
94  
95  	public City getByShape(final Point location,final String countryCode,final boolean filterMunicipality) {
96  		Assert.notNull(location);
97  		return (City) this.getHibernateTemplate().execute(new HibernateCallback() {
98  
99  		    public Object doInHibernate(Session session)
100 			    throws PersistenceException {
101 		    	//select name,municipality,source,openstreetmapid from city c 
102 		    	//where st_contains(c.shape,ST_GeometryFromText('POINT(2.349 48.868)',4326))=true limit 1
103 		    String pointAsString = "ST_GeometryFromText('POINT("+location.getX()+" "+location.getY()+")',"+SRID.WGS84_SRID.getSRID()+")";
104 			String queryString = "from " + persistentClass.getSimpleName()
105 				+ " as c where st_contains(c.shape,"+pointAsString+")=true ";
106 			if (filterMunicipality){
107 				queryString+=" and c.municipality=true";
108 			}
109 			if (countryCode!=null ){
110 				queryString+=" and c.countryCode='"+countryCode+"'";
111 			}
112 			queryString = queryString+ " order by st_area(c.shape)";
113 			//we need to sort by distance due to error in osm data 
114 			//eg : if we search for the nearest city of http://www.openstreetmap.org/way/27904415
115 			// we can have 2 cities : http://www.openstreetmap.org/way/75509282 vs http://www.openstreetmap.org/relation/388250
116 			//cause there is the city and the district
117 
118 			Query qry = session.createQuery(queryString).setMaxResults(1);
119 
120 			//qry.setParameter("point2", location);
121 			City result = (City) qry.uniqueResult();
122 
123 			return result;
124 		    }
125 		});
126 	}
127 
128 	
129 }