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.List;
26  
27  import javax.persistence.PersistenceException;
28  
29  import org.hibernate.Query;
30  import org.hibernate.Session;
31  import org.springframework.orm.hibernate3.HibernateCallback;
32  import org.springframework.stereotype.Repository;
33  import org.springframework.util.Assert;
34  
35  import com.gisgraphy.domain.geoloc.entity.GisFeature;
36  import com.gisgraphy.domain.valueobject.GisFeatureDistance;
37  import com.vividsolutions.jts.geom.Point;
38  
39  /**
40   * A data access object for {@link GisFeature}
41   * 
42   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
43   */
44  @Repository
45  public class GisFeatureDao extends GenericGisDao<GisFeature> implements
46  	IGisFeatureDao {
47  
48      /**
49       * Default Constructor
50       */
51      public GisFeatureDao() {
52  	super(GisFeature.class);
53      }
54  
55      /*
56       * (non-Javadoc)
57       * 
58       * @see com.gisgraphy.domain.repository.IGisFeatureDao#listAllFeaturesFromText(java.lang.String,
59       *      boolean)
60       */
61      public List<GisFeature> listAllFeaturesFromText(String name,
62  	    boolean includeAlternateNames) {
63  	return listFromText(name, includeAlternateNames, null);
64      }
65  
66      /*
67       * (non-Javadoc)
68       * 
69       * @see com.gisgraphy.domain.repository.IGisFeatureDao#getNearestAndDistanceFrom(com.gisgraphy.domain.geoloc.entity.GisFeature,
70       *      double, int, int, java.lang.Class)
71       */
72      public List<GisFeatureDistance> getNearestAndDistanceFromGisFeature(
73  	    GisFeature gisFeature, double distance, int firstResult,
74  	    int maxResults,
75  	    boolean includeDistanceField,
76  	    Class<? extends GisFeature> requiredClass, boolean isMunicipality) {
77  	Assert.notNull(gisFeature, "can not get nearest for a null gisFeature");
78  	return getNearestAndDistanceFrom(gisFeature.getLocation(), gisFeature
79  		.getId(), distance, firstResult, maxResults, includeDistanceField, requiredClass, isMunicipality);
80      }
81  
82      /*
83       * (non-Javadoc)
84       * 
85       * @see com.gisgraphy.domain.repository.IGisFeatureDao#getNearestAndDistanceFrom(com.gisgraphy.domain.geoloc.entity.GisFeature,
86       *      double, java.lang.Class)
87       */
88      public List<GisFeatureDistance> getNearestAndDistanceFromGisFeature(
89  	    GisFeature gisFeature, double distance,
90  	    boolean includeDistanceField,
91  	    Class<? extends GisFeature> requiredClass) {
92  	Assert.notNull(gisFeature, "can not get nearest for a null gisFeature");
93  	return getNearestAndDistanceFromGisFeature(gisFeature, distance, -1,
94  		-1, includeDistanceField,requiredClass, false);
95      }
96  
97      /*
98       * (non-Javadoc)
99       * 
100      * @see com.gisgraphy.domain.repository.IGisFeatureDao#getNearestAndDistanceFrom(com.vividsolutions.jts.geom.Point,
101      *      double, int, int, java.lang.Class)
102      */
103     public List<GisFeatureDistance> getNearestAndDistanceFrom(Point point,
104 	    double distance, int firstResult, int maxResults,
105 	    boolean includeDistanceField,
106 	    Class<? extends GisFeature> requiredClass) {
107 	return getNearestAndDistanceFrom(point, 0L, distance, firstResult,
108 		maxResults, includeDistanceField, requiredClass, false);
109     }
110 
111     /*
112      * (non-Javadoc)
113      * 
114      * @see com.gisgraphy.domain.repository.IGisFeatureDao#getNearestAndDistanceFrom(com.vividsolutions.jts.geom.Point,
115      *      double, java.lang.Class)
116      */
117     public List<GisFeatureDistance> getNearestAndDistanceFrom(Point point,
118 	    double distance, boolean includeDistanceField, Class<? extends GisFeature> requiredClass) {
119 	return getNearestAndDistanceFrom(point, distance, -1, -1, includeDistanceField, requiredClass);
120     }
121 
122     /*
123      * (non-Javadoc)
124      * 
125      * @see com.gisgraphy.domain.repository.IGisFeatureDao#deleteAllExceptAdms()
126      */
127     public int deleteAllExceptAdmsAndCountries() {
128 	return ((Integer) this.getHibernateTemplate().execute(
129 		new HibernateCallback() {
130 
131 		    public Object doInHibernate(Session session)
132 			    throws PersistenceException {
133 			String queryString = "delete from "
134 				+ persistentClass.getSimpleName()
135 				+ " as g where (g.featureCode NOT IN ('ADM1','ADM2','ADM3','ADM4') OR g.featureCode is null) AND g.featureId NOT IN (select featureId from Country)  ";
136 
137 			Query qry = session.createQuery(queryString);
138 			// Need to flush to avoid optimisticLock exception
139 			session.flush();
140 			session.clear();
141 			qry.setCacheable(false);
142 
143 			return Integer.valueOf(qry.executeUpdate());
144 
145 		    }
146 		})).intValue();
147 
148     }
149     
150   
151     /* (non-Javadoc)
152      * @see com.gisgraphy.domain.repository.IGisFeatureDao#getMaxFeatureId()
153      */
154     public long getMaxFeatureId(){
155 	return (Long) this.getHibernateTemplate().execute(
156 		new HibernateCallback() {
157 
158 		    public Object doInHibernate(Session session)
159 			    throws PersistenceException {
160 			String queryString = "select max(featureId) from "
161 				+ GisFeature.class.getSimpleName();
162 
163 			Query qry = session.createQuery(queryString);
164 			qry.setCacheable(true);
165 			Long count = (Long) qry.uniqueResult();
166 			return count==null?0:count;
167 		    }
168 		});
169     }
170 
171 }