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.IAddressParserService;
35  import com.gisgraphy.addressparser.web.AddressQueryHttpBuilder;
36  import com.gisgraphy.domain.geoloc.entity.Country;
37  import com.gisgraphy.domain.repository.CountryDao;
38  import com.gisgraphy.domain.valueobject.GisgraphyServiceType;
39  import com.gisgraphy.domain.valueobject.Output.OutputStyle;
40  import com.gisgraphy.helper.OutputFormatHelper;
41  import com.gisgraphy.serializer.common.OutputFormat;
42  
43  /**
44   * geocoding by text action
45   * 
46   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
47   */
48  public class AddressParserAction 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(AddressParserAction.class);
56  
57      private IAddressParserService addressParser;
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      
69       
70      /**
71       * @return Wether the search has been done and the results should be
72       *         displayed
73       */
74      public boolean isDisplayResults() {
75  	return this.addressResultsDto != null;
76      }
77  
78      private void executeQuery() {
79  	try {
80  	   AddressQuery query = AddressQueryHttpBuilder.getInstance().buildFromRequest(ServletActionContext.getRequest());
81  	    
82  	    this.addressResultsDto = addressParser.execute(query);
83  	} catch (RuntimeException e) {
84  	    String exceptionMessage = "";
85  	    if (e.getCause() != null && e.getCause().getCause() != null) {
86  		exceptionMessage = e.getCause().getCause().toString();
87  		logger.error("An error occured during search : "
88  			+ exceptionMessage,e);
89  	    } else {
90  		exceptionMessage = e.getMessage();
91  		logger.error("An error occured during search : "
92  			+ e.getMessage(),e);
93  	    }
94  	    this.errorMessage = exceptionMessage == null? getText("errorPage.heading"):exceptionMessage;
95  	}
96      }
97  
98      /**
99       * Execute a fulltextSearch from the request parameters
100      * 
101      * @return SUCCESS if the search is successfull
102      * @throws Exception
103      *                 in case of errors
104      */
105     public String search() throws Exception {
106 	executeQuery();
107 	return SUCCESS;
108 
109     }
110 
111     /**
112      * Execute a fulltextSearch from the request parameters
113      * 
114      * @return POPUPVIEW if the search is successfull The view will not be
115      *         decorated by sitemesh (see decorators.xml)
116      * @throws Exception
117      *                 in case of errors
118      */
119     public String searchpopup() throws Exception {
120 	executeQuery();
121 	return POPUP_VIEW;
122     }
123 
124    
125     /**
126      * @return the available countries
127      */
128     public List<Country> getCountries() {
129 	return countryDao.getAllSortedByName();
130     }
131 
132     
133     /**
134      * @return the available formats for fulltext search
135      */
136     public OutputFormat[] getFormats() {
137 	return OutputFormatHelper.listFormatByService(GisgraphyServiceType.GEOCODING);
138     }
139 
140     
141 
142     /**
143      * @param country
144      *                the country parameter to set
145      */
146     public void setCountry(String country) {
147 	this.country = country;
148     }
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 	/**
219 	 * @param addressParser the addressParser to set
220 	 */
221     @Required
222 	public void setAddressParser(IAddressParserService addressParser) {
223 		this.addressParser = addressParser;
224 	}
225 
226   
227 
228    
229   
230 
231 }