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.List;
26  
27  import org.apache.struts2.ServletActionContext;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.beans.factory.annotation.Required;
31  
32  import com.gisgraphy.addressparser.AddressQuery;
33  import com.gisgraphy.addressparser.AddressResultsDto;
34  import com.gisgraphy.addressparser.web.AddressQueryHttpBuilder;
35  import com.gisgraphy.domain.geoloc.entity.Country;
36  import com.gisgraphy.domain.repository.CountryDao;
37  import com.gisgraphy.domain.valueobject.GisgraphyServiceType;
38  import com.gisgraphy.domain.valueobject.Output.OutputStyle;
39  import com.gisgraphy.geocoding.IGeocodingService;
40  import com.gisgraphy.helper.OutputFormatHelper;
41  import com.gisgraphy.serializer.common.OutputFormat;
42  
43  /**
44   * fulltext search Action
45   * 
46   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
47   */
48  public class GeocodingByTextAction extends SearchAction {
49      
50      private boolean autosubmit = false;
51  
52      private static final long serialVersionUID = -9018894533914543310L;
53  
54      private static Logger logger = LoggerFactory
55  	    .getLogger(GeocodingByTextAction.class);
56  
57      private IGeocodingService geocodingService;
58  
59      private CountryDao countryDao;
60  
61      private  AddressResultsDto addressResultsDto;
62  
63      // form parameters
64  
65      private String country;
66      private String style;
67      private String address;
68      private Boolean structured = false;
69      
70       
71      /**
72       * @return Wether the search has been done and the results should be
73       *         displayed
74       */
75      public boolean isDisplayResults() {
76  	return this.addressResultsDto != null;
77      }
78  
79      private void executeQuery() {
80  	try {
81  	   AddressQuery query = AddressQueryHttpBuilder.getInstance().buildFromRequest(ServletActionContext.getRequest());
82  	    
83  	    this.addressResultsDto = geocodingService.geocode(query);
84  	} catch (RuntimeException e) {
85  	    String exceptionMessage = "";
86  	    if (e.getCause() != null && e.getCause().getCause() != null) {
87  		exceptionMessage = e.getCause().getCause().toString();
88  		logger.error("An error occured during search : "
89  			+ exceptionMessage,e);
90  	    } else {
91  		exceptionMessage = e.getMessage();
92  		logger.error("An error occured during search : "
93  			+ e.getMessage(),e);
94  	    }
95  	    this.errorMessage = exceptionMessage == null? getText("errorPage.heading"):exceptionMessage;
96  	}
97      }
98  
99      /**
100      * Execute a fulltextSearch from the request parameters
101      * 
102      * @return SUCCESS if the search is successfull
103      * @throws Exception
104      *                 in case of errors
105      */
106     public String search() throws Exception {
107 	executeQuery();
108 	return SUCCESS;
109 
110     }
111 
112     /**
113      * Execute a fulltextSearch from the request parameters
114      * 
115      * @return POPUPVIEW if the search is successfull The view will not be
116      *         decorated by sitemesh (see decorators.xml)
117      * @throws Exception
118      *                 in case of errors
119      */
120     public String searchpopup() throws Exception {
121 	executeQuery();
122 	return POPUP_VIEW;
123     }
124 
125    
126     /**
127      * @return the available countries
128      */
129     public List<Country> getCountries() {
130 	return countryDao.getAllSortedByName();
131     }
132 
133     
134     /**
135      * @return the available formats for fulltext search
136      */
137     public OutputFormat[] getFormats() {
138 	return OutputFormatHelper.listFormatByService(GisgraphyServiceType.GEOCODING);
139     }
140 
141     
142 
143     /**
144      * @param country
145      *                the country parameter to set
146      */
147     public void setCountry(String country) {
148 	this.country = country;
149     }
150 
151     
152     
153 
154     /**
155      * @param address the address to set
156      */
157     public void setAdress(String address) {
158 	this.address = address;
159     }
160 
161     /**
162      * @return the country
163      */
164     public String getCountry() {
165 	return this.country;
166     }
167 
168 
169     /**
170      * @return the style
171      */
172     public String getStyle() {
173 	return this.style == null ? OutputStyle.getDefault().toString()
174 		: this.style;
175     }
176 
177     /**
178      * @return the address to parse
179      */
180     public String getAddress() {
181 	return this.address;
182     }
183 
184    
185 
186     /**
187      * @param countryDao
188      *                the countryDao to set
189      */
190     @Required
191     public void setCountryDao(CountryDao countryDao) {
192 	this.countryDao = countryDao;
193     }
194 
195 
196     /**
197      * @return the response
198      */
199     public AddressResultsDto getAddressResultsDto() {
200 	return this.addressResultsDto;
201     }
202     
203 
204     /**
205      * @return the autosubmit
206      */
207     public boolean isAutosubmit() {
208         return autosubmit;
209     }
210 
211     /**
212      * @param autosubmit the autosubmit to set
213      */
214     public void setAutosubmit(boolean autosubmit) {
215         this.autosubmit = autosubmit;
216     }
217 
218     @Required
219     public void setGeocodingService(IGeocodingService geocodingService) {
220         this.geocodingService = geocodingService;
221     }
222 
223 	public Boolean getStructured() {
224 		return structured;
225 	}
226 
227 	public void setStructured(Boolean structured) {
228 		this.structured = structured;
229 	}
230   
231 
232 }