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.ArrayList;
26  import java.util.List;
27  
28  import org.apache.struts2.ServletActionContext;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.beans.factory.annotation.Required;
32  
33  import com.gisgraphy.domain.geoloc.entity.Country;
34  import com.gisgraphy.domain.repository.CountryDao;
35  import com.gisgraphy.domain.repository.IAlternateNameDao;
36  import com.gisgraphy.domain.valueobject.GisgraphyServiceType;
37  import com.gisgraphy.domain.valueobject.Output;
38  import com.gisgraphy.domain.valueobject.Output.OutputStyle;
39  import com.gisgraphy.fulltext.FulltextQuery;
40  import com.gisgraphy.fulltext.FulltextQueryHttpBuilder;
41  import com.gisgraphy.fulltext.FulltextResultsDto;
42  import com.gisgraphy.fulltext.IFullTextSearchEngine;
43  import com.gisgraphy.fulltext.SolrResponseDto;
44  import com.gisgraphy.fulltext.spell.SpellCheckerConfig;
45  import com.gisgraphy.helper.GeolocHelper;
46  import com.gisgraphy.helper.OutputFormatHelper;
47  import com.gisgraphy.helper.URLUtils;
48  import com.gisgraphy.serializer.common.OutputFormat;
49  
50  /**
51   * fulltext search Action
52   * 
53   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
54   */
55  public class FulltextSearchAction extends SearchAction {
56  
57      private static final long serialVersionUID = -9018894533914543310L;
58  
59      private static Logger logger = LoggerFactory
60  	    .getLogger(FulltextSearchAction.class);
61  
62      private IFullTextSearchEngine fullTextSearchEngine;
63  
64      private IAlternateNameDao alternateNameDao;
65  
66      private CountryDao countryDao;
67  
68      private FulltextResultsDto responseDTO;
69  
70      private String placetype;
71  
72      private boolean allwordsrequired = FulltextQuery.ALL_WORDS_REQUIRED_DEFAULT_OPTION;
73      
74      /**
75       * force admin mode to false
76       */
77      private Boolean admin=false;
78  
79      // form parameters
80  
81      private String country;
82      private String lang;
83  
84      private String style;
85      private String q;
86      private boolean spellchecking = SpellCheckerConfig.activeByDefault;
87      
88      private static List<String> cachedUsedLanguageCode = new ArrayList<String>();
89      
90      private static String syncToken = "";
91  
92     
93  
94      /**
95       * @return Wether the search has been done and the results should be
96       *         displayed
97       */
98      public boolean isDisplayResults() {
99  	return this.responseDTO != null;
100     }
101 
102     private void executeQuery() {
103 	try {
104 	    FulltextQuery fulltextQuery = FulltextQueryHttpBuilder.getInstance().buildFromRequest(
105 	    		ServletActionContext.getRequest());
106 	    setFrom(fulltextQuery.getFirstPaginationIndex());
107 	    setTo(fulltextQuery.getLastPaginationIndex());
108 	    this.responseDTO = fullTextSearchEngine.executeQuery(fulltextQuery);
109 	    //manually add the urls
110 	    for (SolrResponseDto dto :responseDTO.getResults()){
111 	    	dto.setYahoo_map_url(URLUtils.createYahooMapUrl(GeolocHelper.createPoint(dto.getLng(), dto.getLat())));
112 	    	dto.setGoogle_map_url(URLUtils.createGoogleMapUrl(GeolocHelper.createPoint(dto.getLng(), dto.getLat())));
113 	    	dto.setOpenstreetmap_map_url(URLUtils.createOpenstreetmapMapUrl((GeolocHelper.createPoint(dto.getLng(), dto.getLat()))));
114 	    }
115 	} catch (RuntimeException e) {
116 	    String exceptionMessage = "";
117 	    if (e.getCause() != null && e.getCause().getCause() != null) {
118 		exceptionMessage = e.getCause().getCause().toString();
119 		logger.error("An error occured during search : "
120 			+ exceptionMessage,e);
121 	    } else {
122 		exceptionMessage = e.getMessage();
123 		logger.error("An error occured during search : "
124 			+ e.getMessage(),e);
125 	    }
126 	    this.errorMessage = exceptionMessage == null? getText("errorPage.heading"):exceptionMessage;
127 	}
128     }
129 
130     /**
131      * Execute a fulltextSearch from the request parameters
132      * 
133      * @return SUCCESS if the search is successfull
134      * @throws Exception
135      *                 in case of errors
136      */
137     public String search() throws Exception {
138 	executeQuery();
139 	return SUCCESS;
140 
141     }
142 
143     /**
144      * Execute a fulltextSearch from the request parameters
145      * 
146      * @return POPUPVIEW if the search is successfull The view will not be
147      *         decorated by sitemesh (see decorators.xml)
148      * @throws Exception
149      *                 in case of errors
150      */
151     public String searchpopup() throws Exception {
152 	executeQuery();
153 	return POPUP_VIEW;
154     }
155 
156     /**
157      * @return the languages
158      */
159     public List<String> getLanguages() {
160 	synchronized (syncToken) {
161 	    if (cachedUsedLanguageCode.size()==0){
162 		    cachedUsedLanguageCode = alternateNameDao.getUsedLanguagesCodes();
163 		}
164 	}
165 	return cachedUsedLanguageCode;
166     }
167 
168     /**
169      * @return the available countries
170      */
171     public List<Country> getCountries() {
172 	return countryDao.getAllSortedByName();
173     }
174 
175     /**
176      * @return the available
177      */
178     public OutputStyle[] getVerbosityModes() {
179 	return Output.OutputStyle.values();
180     }
181 
182     /**
183      * @return the available formats for fulltext search
184      */
185     public OutputFormat[] getFormats() {
186 	return OutputFormatHelper.listFormatByService(GisgraphyServiceType.FULLTEXT);
187     }
188 
189     /**
190      * @param fullTextSearchEngine
191      *                the fullTextSearchEngine to set
192      */
193     public void setFullTextSearchEngine(
194 	    IFullTextSearchEngine fullTextSearchEngine) {
195 	this.fullTextSearchEngine = fullTextSearchEngine;
196     }
197 
198     /**
199      * @param country
200      *                the country parameter to set
201      */
202     public void setCountry(String country) {
203 	this.country = country;
204     }
205 
206     /**
207      * @param lang
208      *                the lang parameter to set
209      */
210     public void setLang(String lang) {
211 	this.lang = lang;
212     }
213 
214     /**
215      * @param style
216      *                the style parameter to set
217      */
218     public void setStyle(String style) {
219 	this.style = style;
220     }
221 
222     /**
223      * @param q
224      *                The q parameter to set
225      */
226     public void setQ(String q) {
227 	this.q = q;
228     }
229 
230     /**
231      * @return the country
232      */
233     public String getCountry() {
234 	return this.country;
235     }
236 
237     /**
238      * @return the lang
239      */
240     public String getLang() {
241 	return this.lang;
242     }
243 
244     /**
245      * @return the style
246      */
247     public String getStyle() {
248 	return this.style == null ? OutputStyle.getDefault().toString()
249 		: this.style;
250     }
251 
252     /**
253      * @return the q
254      */
255     public String getQ() {
256 	return this.q;
257     }
258 
259     /**
260      * @param placetype
261      *                the placetype to set
262      */
263     public void setPlacetype(String placetype) {
264 	this.placetype = placetype;
265     }
266 
267     /**
268      * @return the placetype
269      */
270     public String getPlacetype() {
271 	return placetype;
272     }
273 
274     /**
275      * @param countryDao
276      *                the countryDao to set
277      */
278     @Required
279     public void setCountryDao(CountryDao countryDao) {
280 	this.countryDao = countryDao;
281     }
282 
283     /**
284      * @param alternateNameDao
285      *                the alternateNameDao to set
286      */
287     public void setAlternateNameDao(IAlternateNameDao alternateNameDao) {
288 	this.alternateNameDao = alternateNameDao;
289     }
290 
291     /**
292      * @return the response
293      */
294     public FulltextResultsDto getResponseDTO() {
295 	return this.responseDTO;
296     }
297     
298     /**
299      * @return the spellchecking
300      */
301     public boolean isSpellchecking() {
302         return spellchecking;
303     }
304 
305     /**
306      * @param spellchecking the spellchecking to set
307      */
308     public void setSpellchecking(boolean spellchecking) {
309         this.spellchecking = spellchecking;
310     }
311 
312 	/**
313 	 * @return the allwordsrequired
314 	 */
315 	public boolean isAllwordsrequired() {
316 		return allwordsrequired;
317 	}
318 
319 	/**
320 	 * @param allwordsrequired the allwordsrequired to set
321 	 */
322 	public void setAllwordsrequired(boolean allwordsrequired) {
323 		this.allwordsrequired = allwordsrequired;
324 	}
325 
326 	 public Boolean isAdmin() {
327 		    return this.admin;
328 		}
329 	    
330 	  
331 
332 	
333 
334 	
335 }