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 java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.servlet.http.HttpServletRequest;
29  
30  import org.apache.struts2.ServletActionContext;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import com.gisgraphy.domain.valueobject.GisgraphyServiceType;
35  import com.gisgraphy.domain.valueobject.StreetSearchResultsDto;
36  import com.gisgraphy.helper.OutputFormatHelper;
37  import com.gisgraphy.serializer.common.OutputFormat;
38  import com.gisgraphy.street.IStreetSearchEngine;
39  import com.gisgraphy.street.StreetSearchQuery;
40  import com.gisgraphy.street.StreetSearchQueryHttpBuilder;
41  import com.gisgraphy.street.StreetType;
42  
43  /**
44   * Street search Action
45   * 
46   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
47   */
48  public class StreetSearchAction extends SearchAction {
49      
50      private boolean autosubmit = false;
51  
52      /**
53       * 
54       */
55      private static final long serialVersionUID = -9018894533914543310L;
56  
57      private static Logger logger = LoggerFactory
58  	    .getLogger(StreetSearchAction.class);
59  
60      private IStreetSearchEngine streetSearchEngine;
61  
62      private StreetSearchResultsDto streetSearchResultsDto = null;
63  
64      public String lat;
65  
66      public String lng;
67  
68      public String radius;
69  
70      private String streetType;
71      
72      private String name;
73      
74      private boolean distance = true;
75  
76      public boolean isDistance() {
77          return distance;
78      }
79  
80      public void setDistance(boolean distance) {
81          this.distance = distance;
82      }
83  
84      /**
85       * @return Wether the search has been done and the results should be
86       *         displayed
87       */
88      public boolean isDisplayResults() {
89  	return getStreetSearchResultsDto() != null;
90      }
91  
92      private void executeQuery() {
93  	try {
94  	    StreetSearchQuery streetSearchQuery = StreetSearchQueryHttpBuilder.getInstance().buildFromHttpRequest(getRequest());
95  	    this.streetSearchResultsDto = streetSearchEngine.executeQuery(streetSearchQuery);
96  	    setFrom(streetSearchQuery.getFirstPaginationIndex());
97  	    setTo(streetSearchQuery.getLastPaginationIndex());
98  	} catch (RuntimeException e) {
99  	    if (e.getCause() != null) {
100 		logger.error("An error occured during search : "
101 			+ e.getCause().getMessage());
102 	    } else {
103 		logger.error("An error occured during search : "
104 			+ e.getMessage());
105 	    }
106 	    this.errorMessage = e.getMessage() == null? getText("errorPage.heading"):e.getMessage();
107 	}
108     }
109 
110     /**
111      * @return the request
112      */
113     protected HttpServletRequest getRequest() {
114 	return ServletActionContext
115 	    .getRequest();
116     }
117 
118     /**
119      * Execute a StreetSearch from the request parameters
120      * 
121      * @return SUCCESS if the search is successfull
122      * @throws Exception
123      *                 in case of errors
124      */
125     public String search() throws Exception {
126 	executeQuery();
127 	return SUCCESS;
128     }
129 
130     public StreetType[] getStreetTypes(){
131 	return StreetType.values();
132     }
133     
134     /**
135      * Execute a streetSearch from the request parameters
136      * 
137      * @return POPUPVIEW if the search is successfull The view will not be
138      *         decorated by sitemesh (see decorators.xml)
139      * @throws Exception
140      *                 in case of errors
141      */
142     public String searchpopup() throws Exception {
143 	executeQuery();
144 	return POPUP_VIEW;
145     }
146 
147     /**
148      * @return the available formats for fulltext
149      */
150     public OutputFormat[] getFormats() {
151 	return OutputFormatHelper.listFormatByService(GisgraphyServiceType.STREET);
152     }
153 
154    public Map<String, String> getNameOptions(){
155        HashMap<String, String> nameOptions = new HashMap<String, String>();
156        nameOptions.put("", getText("search.street.includeNoNameStreet"));
157        nameOptions.put("%", getText("search.street.dont.includeNoNameStreet"));
158        return nameOptions;
159    }
160 
161 
162 
163     /**
164      * @param streetSearchEngine the streetSearchEngine to set
165      */
166     public void setStreetSearchEngine(IStreetSearchEngine streetSearchEngine) {
167         this.streetSearchEngine = streetSearchEngine;
168     }
169 
170     /**
171      * @return the lat
172      */
173     public String getLat() {
174 	return lat;
175     }
176 
177     /**
178      * @param lat
179      *                the lat to set
180      */
181     public void setLat(String lat) {
182 	this.lat = lat;
183     }
184 
185     /**
186      * @return the lng
187      */
188     public String getLng() {
189 	return lng;
190     }
191 
192     /**
193      * @param lng
194      *                the lng to set
195      */
196     public void setLng(String lng) {
197 	this.lng = lng;
198     }
199 
200     /**
201      * @return the radius
202      */
203     public String getRadius() {
204 	return radius;
205     }
206 
207     /**
208      * @param radius
209      *                the radius to set
210      */
211     public void setRadius(String radius) {
212 	this.radius = radius;
213     }
214 
215     /**
216      * @param streetType
217      *                the streettype to set
218      */
219     public void setStreetType(String streetType) {
220 	this.streetType = streetType;
221     }
222 
223     /**
224      * @return the placetype
225      */
226     public String getStreetType() {
227 	  return streetType;
228 	}
229 
230     /**
231      * @return the response
232      */
233     public StreetSearchResultsDto getStreetSearchResultsDto() {
234 	return this.streetSearchResultsDto;
235     }
236 
237     /**
238      * @return the autosubmit
239      */
240     public boolean isAutosubmit() {
241         return autosubmit;
242     }
243 
244     /**
245      * @param autosubmit the autosubmit to set
246      */
247     public void setAutosubmit(boolean autosubmit) {
248         this.autosubmit = autosubmit;
249     }
250 
251     /**
252      * @return the name
253      */
254     public String getName() {
255         return name;
256     }
257 
258     /**
259      * @param name the name to set
260      */
261     public void setName(String name) {
262         this.name = name;
263     }
264     
265   
266 
267 }