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 org.apache.commons.lang.StringUtils;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import com.gisgraphy.domain.geoloc.entity.OpenStreetMap;
30  import com.gisgraphy.domain.repository.IOpenStreetMapDao;
31  import com.gisgraphy.domain.valueobject.GisgraphyConfig;
32  import com.opensymphony.xwork2.ActionSupport;
33  
34  /**
35   * DisplayStreet Action
36   * 
37   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
38   */
39  public class DisplayStreetAction extends ActionSupport implements GoogleMapApiKeyAware{
40      
41      /**
42       * The reference in the localized file for  the fact that the street has no name
43       */
44      public static final String GLOBAL_STREET_NONAME = "global.street.noname";
45  
46      /**
47       * The reference in the localized file for the error for the fact gid
48       * is required
49       */
50      public static final String ERROR_REF_REQUIRED_FEATURE_ID = "required.gid";
51  
52      /**
53       * The reference in the localized file for the error for the fact that the
54       * specified gid is not a numeric value
55       */
56      public static final String ERROR_REF_NON_NUMERIC_FEATUREID = "displayfeature.gid.numeric";
57  
58  
59      /**
60       * The reference in the localized file for the error for the fact that no
61       * features were found for the specified gid
62       */
63      public static final String ERROR_REF_NORESULT = "result.street.noresult";
64  
65      /**
66       * The reference in the localized file for general error
67       */
68      static final String ERROR_REF_GENERAL_ERROR = "display.error";
69  
70      /**
71       * Default Generated serial Id
72       */
73      private static final long serialVersionUID = 2940477476216677L;
74  
75  
76      private static Logger logger = LoggerFactory
77  	    .getLogger(DisplayStreetAction.class);
78  
79      private IOpenStreetMapDao openStreetMapDao;
80  
81      private String gid;
82      
83      private String shape = null;
84  
85      private OpenStreetMap result = null;
86      
87      private String lat;
88  
89      private String lng;
90  
91      public static final String ERROR = "error";
92      
93      /**
94       * @return the fullyqualified name if exists, or the name
95       */
96      public String getPreferedName() {
97  	if (result == null) {
98  	    return "";
99  	} else {
100 	    return !StringUtils.isEmpty(result.getName()) ? result.getName()
101 		    :getText(GLOBAL_STREET_NONAME);
102 	}
103     }
104 
105    
106     private boolean isNumeric(String number) {
107 	try {
108 	    Integer.parseInt(number);
109 	    return true;
110 	} catch (NumberFormatException e) {
111 	    return false;
112 	}
113     }
114 
115     /**
116      * The error message reference in the localized properties file
117      */
118     private String errorRef = "";
119 
120     private String errorMessage = "";
121 
122     /*
123      * (non-Javadoc)
124      * 
125      * @see com.opensymphony.xwork2.ActionSupport#execute()
126      */
127     @Override
128     public String execute() throws Exception {
129 	try {
130 	    if (StringUtils.isEmpty(gid)) {
131 		errorRef = ERROR_REF_REQUIRED_FEATURE_ID;
132 		return ERROR;
133 	    }
134 	    if (!isNumeric(gid)) {
135 		errorRef = ERROR_REF_NON_NUMERIC_FEATUREID;
136 		return ERROR;
137 	    }
138 	    result = openStreetMapDao.getByGid(Long.valueOf(gid));
139 	    if (result == null) {
140 		errorRef = ERROR_REF_NORESULT;
141 		return ERROR;
142 	    } else {
143 	    	this.shape= retrieveShape(result.getGid());
144 	    }
145 	} catch (RuntimeException e) {
146 	    if (e.getCause() != null) {
147 		logger.warn("An error occured during search : "
148 			+ e.getCause().getMessage());
149 	    } else {
150 		logger.warn("An error occured during search : "
151 			+ e.getMessage());
152 	    }
153 	    this.errorRef = ERROR_REF_GENERAL_ERROR;
154 	    this.errorMessage = e.getMessage();
155 	    return ERROR;
156 	}
157 	return SUCCESS;
158     }
159 
160 
161     protected String retrieveShape(Long gid) {
162     	if (gid!=null){
163     		return openStreetMapDao.getShapeAsWKTByGId(gid);
164     	}
165     	return null;
166 		
167 	}
168 
169 
170     /**
171      * @return the gid
172      */
173     public String getGid() {
174         return gid;
175     }
176 
177 
178 
179     /**
180      * @param gid the gid to set
181      */
182     public void setGid(String gid) {
183         this.gid = gid;
184     }
185 
186 
187     /**
188      * @param openStreetMapDao the openStreetMapDao to set
189      */
190     public void setOpenStreetMapDao(IOpenStreetMapDao openStreetMapDao) {
191         this.openStreetMapDao = openStreetMapDao;
192     }
193 
194 
195 
196 
197 
198     /**
199      * @return the errorRef
200      */
201     public String getErrorRef() {
202 	return errorRef;
203     }
204 
205     /**
206      * @return the result
207      */
208     public OpenStreetMap getResult() {
209 	return result;
210     }
211 
212     /**
213      * @return the errorMessage
214      */
215     public String getErrorMessage() {
216 	return errorMessage;
217     }
218     
219     /**
220      * @return the googleMapAPIKey
221      */
222     public String getGoogleMapAPIKey() {
223         return GisgraphyConfig.googleMapAPIKey == null ? "" : GisgraphyConfig.googleMapAPIKey;
224     }
225 
226 
227 	public String getShape() {
228 		return shape;
229 	}
230 
231 
232 	public String getLat() {
233 		return lat;
234 	}
235 
236 
237 	public void setLat(String lat) {
238 		this.lat = lat;
239 	}
240 
241 
242 	public String getLng() {
243 		return lng;
244 	}
245 
246 
247 	public void setLng(String lng) {
248 		this.lng = lng;
249 	}
250 
251 
252 }