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.webapp.action;
24  
25  import org.apache.struts2.ServletActionContext;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.beans.factory.annotation.Required;
29  
30  import com.gisgraphy.domain.valueobject.GisgraphyConfig;
31  import com.gisgraphy.domain.valueobject.GisgraphyServiceType;
32  import com.gisgraphy.geocoloc.IGeolocSearchEngine;
33  import com.gisgraphy.geoloc.GeolocQuery;
34  import com.gisgraphy.geoloc.GeolocQueryHttpBuilder;
35  import com.gisgraphy.geoloc.GeolocResultsDto;
36  import com.gisgraphy.helper.OutputFormatHelper;
37  import com.gisgraphy.serializer.common.OutputFormat;
38  
39  /**
40   * Geolocalisation search Action
41   * 
42   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
43   */
44  public class GeolocSearchAction extends SearchAction {
45  
46      /**
47       * 
48       */
49      private static final long serialVersionUID = -9018894533914543310L;
50  
51      private static Logger logger = LoggerFactory
52  	    .getLogger(GeolocSearchAction.class);
53  
54      private IGeolocSearchEngine geolocSearchEngine;
55  
56      private GeolocResultsDto responseDTO;
57  
58      public String lat;
59  
60      public String lng;
61  
62      public String radius;
63  
64      private String placetype;
65      
66      private boolean distance = true;
67  
68      public boolean isDistance() {
69          return distance;
70      }
71  
72      public void setDistance(boolean distance) {
73          this.distance = distance;
74      }
75  
76      /**
77       * @return Wether the search has been done and the results should be
78       *         displayed
79       */
80      public boolean isDisplayResults() {
81  	return getResponseDTO() != null;
82      }
83  
84      private void executeQuery() {
85  	try {
86  	    GeolocQuery geolocQuery = GeolocQueryHttpBuilder.getInstance().buildFromHttpRequest(ServletActionContext
87  		    .getRequest());
88  	    this.responseDTO = geolocSearchEngine.executeQuery(geolocQuery);
89  	    setFrom(geolocQuery.getFirstPaginationIndex());
90  	    setTo(geolocQuery.getLastPaginationIndex());
91  	} catch (RuntimeException e) {
92  	    if (e.getCause() != null) {
93  		logger.error("An error occured during search : "
94  			+ e.getCause().getMessage());
95  	    } else {
96  		logger.error("An error occured during search : "
97  			+ e.getMessage());
98  	    }
99  	    this.errorMessage = e.getMessage() == null? getText("errorPage.heading"):e.getMessage();
100 	}
101     }
102 
103     /**
104      * Execute a GeolocSearch from the request parameters
105      * 
106      * @return SUCCESS if the search is successful
107      * @throws Exception
108      *                 in case of errors
109      */
110     public String search() throws Exception {
111 	executeQuery();
112 	return SUCCESS;
113     }
114 
115     /**
116      * Execute a geolocSearch from the request parameters
117      * 
118      * @return POPUPVIEW if the search is successfull The view will not be
119      *         decorated by sitemesh (see decorators.xml)
120      * @throws Exception
121      *                 in case of errors
122      */
123     public String searchpopup() throws Exception {
124 	executeQuery();
125 	return POPUP_VIEW;
126     }
127 
128     /**
129      * @return the available formats for fulltext
130      */
131     public OutputFormat[] getFormats() {
132 	return OutputFormatHelper.listFormatByService(GisgraphyServiceType.GEOLOC);
133     }
134 
135     /**
136      * @param geolocSearchEngine
137      *                the geolocSearchEngine to set
138      */
139     @Required
140     public void setGeolocSearchEngine(IGeolocSearchEngine geolocSearchEngine) {
141 	this.geolocSearchEngine = geolocSearchEngine;
142     }
143 
144     /**
145      * @return the lat
146      */
147     public String getLat() {
148 	return lat;
149     }
150 
151     /**
152      * @param lat
153      *                the lat to set
154      */
155     public void setLat(String lat) {
156 	this.lat = lat;
157     }
158 
159     /**
160      * @return the lng
161      */
162     public String getLng() {
163 	return lng;
164     }
165 
166     /**
167      * @param lng
168      *                the lng to set
169      */
170     public void setLng(String lng) {
171 	this.lng = lng;
172     }
173 
174     /**
175      * @return the radius
176      */
177     public String getRadius() {
178 	return radius;
179     }
180 
181     /**
182      * @param radius
183      *                the radius to set
184      */
185     public void setRadius(String radius) {
186 	this.radius = radius;
187     }
188 
189     /**
190      * @param placetype
191      *                the placetype to set
192      */
193     public void setPlacetype(String placetype) {
194 	this.placetype = placetype;
195     }
196 
197     /**
198      * @return the placetype
199      */
200     public String getPlacetype() {
201 	if (placetype == null
202 		&& GisgraphyConfig.defaultGeolocSearchPlaceTypeClass != null) {
203 	    return GisgraphyConfig.defaultGeolocSearchPlaceTypeClass
204 		    .getSimpleName().toLowerCase();
205 	} else {
206 	    return placetype;
207 	}
208     }
209 
210     /**
211      * @return the response
212      */
213     public GeolocResultsDto getResponseDTO() {
214 	return this.responseDTO;
215     }
216 
217 }