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.commons.lang.StringUtils;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.beans.factory.annotation.Required;
31  
32  import com.gisgraphy.domain.geoloc.entity.GisFeature;
33  import com.gisgraphy.domain.repository.GisFeatureDao;
34  import com.gisgraphy.domain.repository.IGisFeatureDao;
35  import com.gisgraphy.domain.valueobject.Output;
36  import com.gisgraphy.domain.valueobject.Output.OutputStyle;
37  import com.gisgraphy.fulltext.FullTextFields;
38  import com.gisgraphy.fulltext.FulltextQuery;
39  import com.gisgraphy.fulltext.FulltextResultsDto;
40  import com.gisgraphy.fulltext.IFullTextSearchEngine;
41  import com.gisgraphy.fulltext.SolrResponseDto;
42  import com.opensymphony.xwork2.ActionSupport;
43  
44  /**
45   * DisplayFeature Action
46   * 
47   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
48   */
49  public class DisplayFeatureAction extends ActionSupport {
50  
51      /**
52       * The reference in the localized file for the error for the fact featureId
53       * is required
54       */
55      public static final String ERROR_REF_REQUIRED_FEATURE_ID = "required.featureId";
56  
57      /**
58       * The reference in the localized file for the error for the fact that the
59       * specified featureId is not a numeric value
60       */
61      public static final String ERROR_REF_NON_NUMERIC_FEATUREID = "displayfeature.featureid.numeric";
62  
63      /**
64       * The reference in the localized file for the error for the fact that more
65       * than one features were founds for the specified featureId
66       */
67      public static final String ERROR_REF_NON_UNIQUE_RESULT = "result.nouniqueresult";
68  
69      /**
70       * The reference in the localized file for the error for the fact that no
71       * gid were found for the specified featureId
72       */
73      public static final String ERROR_REF_NORESULT = "result.feature.noresult";
74  
75      /**
76       * The reference in the localized file for general error
77       */
78      static final String ERROR_REF_GENERAL_ERROR = "display.error";
79  
80      /**
81       * Default Generated serial Id
82       */
83      private static final long serialVersionUID = 2940477008022016677L;
84  
85      private static final Output FULL_OUTPUT = Output.withDefaultFormat().withLanguageCode(Output.DEFAULT_LANGUAGE_CODE)
86  	    .withStyle(OutputStyle.FULL);
87  
88      private static Logger logger = LoggerFactory
89  	    .getLogger(DisplayFeatureAction.class);
90  
91      private IFullTextSearchEngine fullTextSearchEngine;
92      
93      private IGisFeatureDao gisFeatureDao;
94  
95      private String featureId;
96  
97      private SolrResponseDto result = null;
98      
99      private String shape = null;
100 
101     public static final String ERROR = "error";
102 
103     /**
104      * @return the fullyqualified name if exists, or the name
105      */
106     public String getPreferedName() {
107 	if (result == null) {
108 	    return "";
109 	} else {
110 	    String fully_qualified_name = result.getFully_qualified_name();
111 	    return !StringUtils.isEmpty(fully_qualified_name) ? fully_qualified_name
112 		    : result.getName();
113 	}
114     }
115 
116     private boolean isNumeric(String number) {
117 	try {
118 	    Integer.parseInt(number);
119 	    return true;
120 	} catch (NumberFormatException e) {
121 	    return false;
122 	}
123     }
124 
125     /**
126      * The error message reference in the localized properties file
127      */
128     private String errorRef = "";
129 
130     private String errorMessage = "";
131 
132     /*
133      * (non-Javadoc)
134      * 
135      * @see com.opensymphony.xwork2.ActionSupport#execute()
136      */
137     @Override
138     public String execute() throws Exception {
139 	try {
140 	    if (StringUtils.isEmpty(featureId)) {
141 		errorRef = ERROR_REF_REQUIRED_FEATURE_ID;
142 		return ERROR;
143 	    }
144 	    if (!isNumeric(featureId)) {
145 		errorRef = ERROR_REF_NON_NUMERIC_FEATUREID;
146 		return ERROR;
147 	    }
148 	    FulltextQuery fulltextQuery = (FulltextQuery) new FulltextQuery(
149 		    FullTextFields.FEATUREID.getValue()+":"+featureId).withOutput(FULL_OUTPUT);
150 	    FulltextResultsDto responseDTO = fullTextSearchEngine
151 		    .executeQuery(fulltextQuery);
152 
153 	    List<SolrResponseDto> results = responseDTO.getResults();
154 	    int resultSize = results.size();
155 	    if (resultSize == 0) {
156 		errorRef = ERROR_REF_NORESULT;
157 		return ERROR;
158 	    } else if (resultSize > 1) {
159 		errorRef = ERROR_REF_NON_UNIQUE_RESULT;
160 		return ERROR;
161 	    } else {
162 		result = results.get(0);
163 		this.shape = retrieveShape(result.getFeature_id());
164 	    }
165 	} catch (RuntimeException e) {
166 	    if (e.getCause() != null) {
167 		logger.warn("An error occured during search : "
168 			+ e.getCause().getMessage());
169 	    } else {
170 		logger.warn("An error occured during search : "
171 			+ e.getMessage());
172 	    }
173 	    this.errorRef = ERROR_REF_GENERAL_ERROR;
174 	    this.errorMessage = e.getMessage();
175 	    return ERROR;
176 	}
177 	return SUCCESS;
178     }
179 
180     protected String retrieveShape(Long featureId) {
181     	if (featureId!=null){
182     		return gisFeatureDao.getShapeAsWKTByFeatureId(featureId);
183     	}
184     	return null;
185 		
186 	}
187 
188 	/**
189      * @param fullTextSearchEngine
190      *                the fullTextSearchEngine to set
191      */
192     @Required
193     public void setFullTextSearchEngine(
194 	    IFullTextSearchEngine fullTextSearchEngine) {
195 	this.fullTextSearchEngine = fullTextSearchEngine;
196     }
197     
198     @Required
199     public void setGisFeatureDao(IGisFeatureDao gisFeatureDao) {
200 		this.gisFeatureDao = gisFeatureDao;
201 	}
202     
203     
204 
205     /**
206      * @param featureId
207      *                the featureId to set
208      */
209     public void setFeatureId(String featureId) {
210 	this.featureId = featureId;
211     }
212 
213     /**
214      * @return the errorRef
215      */
216     public String getErrorRef() {
217 	return errorRef;
218     }
219 
220     /**
221      * @return the result
222      */
223     public SolrResponseDto getResult() {
224 	return result;
225     }
226 
227     /**
228      * @return the errorMessage
229      */
230     public String getErrorMessage() {
231 	return errorMessage;
232     }
233 
234 	public String getShape() {
235 		return shape;
236 	}
237 
238 	
239 
240 }