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.geoloc;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import com.gisgraphy.domain.geoloc.entity.GisFeature;
28  import com.gisgraphy.domain.valueobject.GisgraphyConfig;
29  import com.gisgraphy.domain.valueobject.GisgraphyServiceType;
30  import com.gisgraphy.domain.valueobject.Output;
31  import com.gisgraphy.domain.valueobject.Pagination;
32  import com.gisgraphy.fulltext.FulltextQuery;
33  import com.gisgraphy.helper.GeolocHelper;
34  import com.gisgraphy.helper.GisHelper;
35  import com.gisgraphy.helper.OutputFormatHelper;
36  import com.gisgraphy.serializer.common.OutputFormat;
37  import com.gisgraphy.service.AbstractGisQuery;
38  import com.gisgraphy.servlet.FulltextServlet;
39  import com.gisgraphy.servlet.GisgraphyServlet;
40  import com.vividsolutions.jts.geom.Point;
41  
42  /**
43   * A GeolocQuery Query builder. it build geolocQuery from HTTP Request
44   * 
45   * @see Pagination
46   * @see Output
47   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
48   */
49  public class GeolocQueryHttpBuilder {
50  	
51      
52    
53  	private static GeolocQueryHttpBuilder instance = new GeolocQueryHttpBuilder();
54  	
55  	public static GeolocQueryHttpBuilder getInstance() {
56  		return instance;
57  	}
58  
59      
60      /**
61       * @param req
62       *                an HttpServletRequest to construct a {@link GeolocQuery}
63       */
64      public GeolocQuery buildFromHttpRequest(HttpServletRequest req) {
65      
66  	// point
67  	Float latitude=null;
68  	Float longitude=null;
69  	// lat
70  	try {
71  			String latParameter = req.getParameter(GeolocQuery.LAT_PARAMETER);
72  				if (latParameter!=null){
73  					latitude = GeolocHelper.parseInternationalDouble(latParameter);
74  				} else if(isPointRequired()) {
75  					throw new GeolocSearchException("latitude is empty");
76  				} 
77  		} catch (Exception e) {
78  			throw new GeolocSearchException("latitude is not correct");
79  		}
80  
81  	// long
82  	try {
83  	    String longParameter = req
84  		    .getParameter(GeolocQuery.LONG_PARAMETER);
85  	    if (longParameter!=null){
86  	    	longitude = GeolocHelper.parseInternationalDouble(longParameter);
87  	    } else if(isPointRequired()){
88  	    		throw new GeolocSearchException(
89  	    		"longitude is empty");
90  	    	}
91  	} catch (Exception e) {
92  	    throw new GeolocSearchException(
93  		    "longitude is not correct ");
94  	}
95  	
96  	
97  	// point
98  	
99  	Point point = null ;
100 	try {
101 		if (latitude!=null && longitude!=null ){
102 	    point = GeolocHelper.createPoint(longitude, latitude);
103 		} 
104 	} catch (RuntimeException e1) {
105 	    	throw new GeolocSearchException("can not determine Point");
106 	}
107 	
108 	// radius
109 	double radius;
110 	try {
111 	    radius = GeolocHelper.parseInternationalDouble(req
112 		    .getParameter(GeolocQuery.RADIUS_PARAMETER));
113 	} catch (Exception e) {
114 	    radius = GeolocQuery.DEFAULT_RADIUS;
115 	}
116 	
117 	GeolocQuery geolocQuery = constructMinimalQuery(point, radius);
118 
119 	// pagination
120 	Pagination pagination = null;
121 	int from;
122 	int to;
123 	try {
124 	    from = Integer.valueOf(
125 		    req.getParameter(GisgraphyServlet.FROM_PARAMETER)).intValue();
126 	} catch (Exception e) {
127 	    from = Pagination.DEFAULT_FROM;
128 	}
129 
130 	try {
131 	    to = Integer
132 		    .valueOf(req.getParameter(FulltextServlet.TO_PARAMETER))
133 		    .intValue();
134 	} catch (NumberFormatException e) {
135 	    to = from+AbstractGisQuery.DEFAULT_NB_RESULTS-1;
136 	}
137 
138 	pagination = Pagination.paginateWithMaxResults(getMaxResults()).from(from).to(to)
139 		.limitNumberOfResults(getMaxResults());
140 	// output format
141 	OutputFormat format = OutputFormat.getFromString(req
142 		.getParameter(GisgraphyServlet.FORMAT_PARAMETER));
143 	format = OutputFormatHelper.getDefaultForServiceIfNotSupported(format, GisgraphyServiceType.GEOLOC);
144 	Output output = Output.withFormat(format);
145 
146 	// indent
147 	if ("true".equalsIgnoreCase(req
148 		.getParameter(GisgraphyServlet.INDENT_PARAMETER))
149 		|| "on".equalsIgnoreCase(req
150 			.getParameter(GisgraphyServlet.INDENT_PARAMETER))) {
151 	    output.withIndentation();
152 	}
153 	
154 	// municipality
155 		if ("true".equalsIgnoreCase(req
156 			.getParameter(GeolocQuery.MUNICIPALITY_PARAMETER))
157 			|| "on".equalsIgnoreCase(req
158 				.getParameter(GeolocQuery.MUNICIPALITY_PARAMETER))) {
159 		    geolocQuery.withMunicipalityFilter(true);
160 		}
161 
162 	//placetype
163 	Class<? extends GisFeature> clazz = GisHelper
164 		.getClassEntityFromString(req
165 			.getParameter(GeolocQuery.PLACETYPE_PARAMETER));
166 
167 	//distance field
168 	if ("false".equalsIgnoreCase(req
169 		.getParameter(GeolocQuery.DISTANCE_PARAMETER))
170 		|| "off".equalsIgnoreCase(req
171 			.getParameter(GeolocQuery.DISTANCE_PARAMETER))) {
172 	    geolocQuery.withDistanceField(false);
173 	}
174 	
175 	// apiKey
176 	String apiKey = req.getParameter(GisgraphyServlet.APIKEY_PARAMETER);
177 	geolocQuery.setApikey(apiKey);
178 	
179 	String CallBackParameter = req.getParameter(GeolocQuery.CALLBACK_PARAMETER);
180 	if (CallBackParameter!=null){
181 	    geolocQuery.withCallback(CallBackParameter);
182 	}
183 	
184 	geolocQuery.withPagination(pagination);
185 	if(clazz == null){
186 		geolocQuery.withPlaceType(GisgraphyConfig.defaultGeolocSearchPlaceTypeClass);
187 		}else {
188 			geolocQuery.withPlaceType(clazz);
189     }
190 	geolocQuery.withOutput(output);
191 	return geolocQuery;
192 
193 }
194 
195 
196 	protected int getMaxResults() {
197 		return GeolocQuery.DEFAULT_MAX_RESULTS;
198 	}
199 
200 	/**
201 	 * Create a basic GeolocQuery. this method must be overide 
202 	 * if we need to create inheritance object
203 	 * 
204 	 * @param point the JTS point to create the query
205 	 * @param radius the radius to search around
206 	 */
207 	protected GeolocQuery constructMinimalQuery(Point point, double radius) {
208 		GeolocQuery geolocQuery = new GeolocQuery(point,radius);
209 		return geolocQuery;
210 	}
211 	
212 	/**
213 	 * @return true if the point is required
214 	 */
215 	protected boolean isPointRequired(){
216 		return  true;
217 	    }
218 }