View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.directory.modules.address.web;
35  
36  import java.rmi.RemoteException;
37  import java.util.HashMap;
38  import java.util.Locale;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang.StringUtils;
44  import org.json.JSONException;
45  import org.json.JSONObject;
46  
47  import fr.paris.lutece.plugins.address.business.axis.InvalidParameterException;
48  import fr.paris.lutece.plugins.address.business.jaxb.Adresse;
49  import fr.paris.lutece.plugins.address.service.AddressServiceProvider;
50  import fr.paris.lutece.plugins.address.util.LibraryAddressUtils;
51  import fr.paris.lutece.portal.service.i18n.I18nService;
52  import fr.paris.lutece.portal.service.template.AppTemplateService;
53  import fr.paris.lutece.portal.service.util.AppLogService;
54  import fr.paris.lutece.util.ReferenceItem;
55  import fr.paris.lutece.util.ReferenceList;
56  import fr.paris.lutece.util.html.HtmlTemplate;
57  
58  /**
59   * 
60   * DirectoryAddressJspBean : for AJAX addresses search.
61   *
62   */
63  public class DirectoryAddressJspBean 
64  {
65  	/**
66  	 * For error logs (pmd fix)
67  	 */
68  	private static final String CONSTANT_LOG_PREFIX = "DirectoryAddressJspBean : ";
69  
70  	/**
71  	 * Address parameter
72  	 */
73  	private static final String PARAMETER_ADDRESS = "address";
74  	/**
75  	 * Id address parameter
76  	 */
77  	private static final String PARAMETER_ID_ADDRESS = "id_address";
78  	/**
79  	 * Id entry parameter
80  	 */
81  	private static final String PARAMETER_ID_ENTRY = "id_entry";
82  	
83  	private static final String MESSAGE_REMOTE_EXCEPTION = "module.directory.address.message.remoteException";
84  	private static final String MESSAGE_SELECT_AN_ADDRESS = "module.directory.address.message.selectAddress";
85  	private static final String MESSAGE_ADDRESS_NOT_FOUND = "module.directory.address.message.addressNotFound";
86  	private static final String MESSAGE_TOO_MANY_ADDRESSES = "module.directory.address.message.tooManyAddresses";
87  	
88  	private static final String CONSTANT_DOT = ".";
89  	
90  	/**
91  	 * Finds the address
92  	 * @param request the request
93  	 * @return {@link SearchAddress}
94  	 * @see SearchAddress
95  	 * @see AddressServiceProvider#searchAddress(HttpServletRequest, String)
96  	 */
97  	public String searchAddress( HttpServletRequest request )
98  	{
99  		String strAddress = request.getParameter( PARAMETER_ADDRESS );
100 		String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
101 		SearchAddress searchAddress = new SearchAddress(  );
102 		
103 		searchAddress.setIdEntry( LibraryAddressUtils.parseInt( strIdEntry ) );
104 		
105 		if ( StringUtils.isBlank( strAddress ) )
106 		{
107 			searchAddress.setMessage(I18nService.getLocalizedString( MESSAGE_SELECT_AN_ADDRESS, request.getLocale(  ) ) );
108 			return searchAddress.toJSONString( request.getLocale(  ) );
109 		}
110 		
111 		try 
112 		{
113 			ReferenceList refList = AddressServiceProvider.searchAddress( request,  strAddress );
114 			if ( refList != null && refList.size(  ) > 0 )
115 			{
116 				// fill addresses
117 				searchAddress.setAddresses( refList );
118 				if ( refList.size(  ) == 1 )
119 				{
120 					setSingleAddressInfos(request, LibraryAddressUtils.parseLong( refList.get(0).getCode(  ) ), searchAddress);
121 				}
122 			}
123 			else
124 			{
125 				searchAddress.setMessage( I18nService.getLocalizedString( MESSAGE_ADDRESS_NOT_FOUND, request.getLocale(  ) ) );
126 			}
127 		} 
128 		catch ( InvalidParameterException e ) 
129 		{
130 			AppLogService.error( CONSTANT_LOG_PREFIX + e.getMessage(  ), e );
131 			
132 			// set an error message
133 			String strMessage = e.getFaultString(  );//I18nService.getLocalizedString( MESSAGE_REMOTE_EXCEPTION, request.getLocale(  ) );
134 			
135 			if ( StringUtils.isNotBlank(strMessage) && !strMessage.endsWith( CONSTANT_DOT ) )
136 			{
137 				strMessage += CONSTANT_DOT;
138 			}
139 			searchAddress.setMessage( strMessage + I18nService.getLocalizedString( MESSAGE_TOO_MANY_ADDRESSES, request.getLocale(  ) ) ); 
140 		}
141 		catch ( RemoteException e ) 
142 		{
143 			AppLogService.error( CONSTANT_LOG_PREFIX + e.getMessage(  ), e );
144 			
145 			// set an error message
146 			String strMessage = I18nService.getLocalizedString( MESSAGE_REMOTE_EXCEPTION, request.getLocale(  ) );
147 			searchAddress.setMessage( strMessage );
148 		}
149 		
150 		if ( AppLogService.isDebugEnabled(  ) )
151 		{
152 			AppLogService.debug( searchAddress.toJSONString( request.getLocale(  ) ) ); 
153 		}
154 		
155 		return searchAddress.toJSONString( request.getLocale(  ) );
156 	}
157 	
158 	/**
159 	 * Gets the address info
160 	 * @param request the request
161 	 * @return the JSON string of the address
162 	 */
163 	public String getAddressInfo( HttpServletRequest request )
164 	{
165 		SearchAddress searchAddress = new SearchAddress(  );
166 		
167 		String strIdAddress = request.getParameter( PARAMETER_ID_ADDRESS );
168 		
169 		if ( StringUtils.isBlank( strIdAddress ) )
170 		{
171 			searchAddress.setMessage(I18nService.getLocalizedString( MESSAGE_SELECT_AN_ADDRESS, request.getLocale(  ) ) );
172 		}
173 		
174 		long lIdAddress = LibraryAddressUtils.parseLong( strIdAddress );
175 		
176 		if ( lIdAddress == -1 )
177 		{
178 			searchAddress.setMessage(I18nService.getLocalizedString( MESSAGE_SELECT_AN_ADDRESS, request.getLocale(  ) ) );
179 		}
180 		
181 		try 
182 		{
183 			setSingleAddressInfos(request, lIdAddress, searchAddress);
184 		}
185 		catch ( InvalidParameterException e ) 
186 		{
187 			AppLogService.error( CONSTANT_LOG_PREFIX + e.getMessage(  ), e );
188 			
189 			// set an error message
190 			String strMessage = e.getFaultString(  );//I18nService.getLocalizedString( MESSAGE_REMOTE_EXCEPTION, request.getLocale(  ) );
191 			searchAddress.setMessage( strMessage );
192 		}
193 		catch (RemoteException e) 
194 		{
195 			AppLogService.error( CONSTANT_LOG_PREFIX + e.getMessage(  ), e );
196 			
197 			// set an error message
198 			String strMessage = I18nService.getLocalizedString( MESSAGE_REMOTE_EXCEPTION, request.getLocale(  ) );
199 			searchAddress.setMessage( strMessage );
200 		}
201 		
202 		if ( AppLogService.isDebugEnabled(  ) )
203 		{
204 			AppLogService.debug( searchAddress.toJSONString( request.getLocale(  ) ) ); 
205 		}
206 		
207 		return searchAddress.toJSONString( request.getLocale(  ) );
208 	}
209 	
210 	/**
211 	 * Sets the address info
212 	 * @param request the request
213 	 * @param lIdAddress the address id
214 	 * @param searchAddress the {@link SearchAddress} to fill
215 	 * @throws RemoteException when an error occurs with the webservice
216 	 */
217 	private void setSingleAddressInfos( HttpServletRequest request, long lIdAddress, SearchAddress searchAddress ) throws RemoteException
218 	{
219 		Adresse adresse = AddressServiceProvider.getAdresseInfo( request, lIdAddress, false );
220 		if ( adresse == null )
221 		{
222 			searchAddress.setMessage(I18nService.getLocalizedString( MESSAGE_ADDRESS_NOT_FOUND, request.getLocale(  ) ) );
223 		}
224 		else
225 		{
226 			searchAddress.setX( adresse.getGeoX(  ) );
227 			searchAddress.setY( adresse.getGeoY(  ) );
228 							
229 			ReferenceItem refItem = new ReferenceItem(  );
230 			refItem.setCode( Long.toString( lIdAddress ) );
231 			refItem.setName( LibraryAddressUtils.normalizeAddress( adresse ) );
232 			
233 			searchAddress.setAddressLabel( refItem.getName(  ) );
234 			
235 			ReferenceList refList = new ReferenceList(  );
236 			refList.add( refItem );
237 			
238 			searchAddress.setAddresses( refList );
239 		}
240 	}
241 	
242 	/**
243 	 * SearchAddress. Use for JSON  compatibility :
244 	 * <br>
245 	 * <pre>
246 	 * {
247 	 *  "noResult" : "false",
248 	 *  "singleResult" : "false",
249 	 *  "listAddresses" : [{"12345", "217 rue de Bercy"},
250 	 *                 {"98765", "216, rue de Bercy"}],
251 	 *  "message" : "",
252 	 *  "error" : "false",
253 	 *  "address" : "",
254 	 *  "html" : "[...]"
255 	 * }
256 	 * </pre>
257 	 * @see {@link http://www.json.org/}
258 	 */
259 	public static class SearchAddress
260 	{
261 		private static final String TEMPLATE_ADDRESS_LIST = "admin/plugins/directory/modules/address/ajax/AddressList.html";
262 		private static final String TEMPLATE_SINGLE_ADDRESS = "admin/plugins/directory/modules/address/ajax/SingleAddress.html";
263 		private static final String TEMPLATE_NO_RESULT = "admin/plugins/directory/modules/address/ajax/NoResult.html";
264 		
265 		private static final String MARK_ADDRESS = "address";
266 		private static final String MARK_LIST_ADDRESSES = "listAddresses";
267 		private static final String MARK_ID_ENTRY = "id_entry";
268 		
269 		private static final String JSON_HTML = "html";
270 		private static final String JSON_X = "x";
271 		private static final String JSON_Y = "y";
272 		private static final String JSON_NO_RESULT = "noResult";
273 		private static final String JSON_SINGLE_RESULT = "singleResult";
274 		private static final String JSON_MESSAGE = "message";
275 		private static final String JSON_ERROR = "error";
276 		private static final String JSON_ADDRESS_LABEL = "addressLabel";
277 		
278 		private ReferenceList _refListAddresses;
279 		private String _strAddressLabel;
280 		private String _strMessage;
281 		private float _fX;
282 		private float _fY;
283 		private int _nIdEntry;
284 		
285 		/**
286 		 *  address
287 		 * @return  address
288 		 */
289 		public String getAddressLabel(  )
290 		{
291 			return _strAddressLabel;
292 		}
293 		
294 		/**
295 		 * Sets the address
296 		 * @param strAddressLabel the new address
297 		 */
298 		public void setAddressLabel( String strAddressLabel )
299 		{
300 			_strAddressLabel = strAddressLabel;
301 		}
302 		
303 		/**
304 		 * Entry id
305 		 * @return the entry id
306 		 */
307 		public int getIdEntry(  )
308 		{
309 			return _nIdEntry;
310 		}
311 		
312 		/**
313 		 * Sets the entry id
314 		 * @param nIdEntry the new entry id
315 		 */
316 		public void setIdEntry( int nIdEntry )
317 		{
318 			_nIdEntry = nIdEntry;
319 		}
320 		
321 		/**
322 		 * <code>true</code> if no result, <code>false</code> otherwise
323 		 * @return <code>true</code> if no result, <code>false</code> otherwise
324 		 */
325 		public boolean isNoResult(  ) 
326 		{
327 			return _refListAddresses == null || ( _refListAddresses.size(  ) == 0 );
328 		}
329 		
330 		/**
331 		 * <code>true</code> if there is one and only one result , <code>false</code> otherwise
332 		 * @return <code>true</code> if there is one and only one result, <code>false</code> otherwise
333 		 */
334 		public boolean isSingleResult(  )
335 		{
336 			return ( _refListAddresses != null ) && ( _refListAddresses.size(  ) == 1 );
337 		}
338 			
339 		/**
340 		 * Sets the addresses
341 		 * @param refListAddresses the addresses
342 		 */
343 		public void setAddresses( ReferenceList refListAddresses )
344 		{
345 			_refListAddresses = refListAddresses;
346 		}
347 		
348 		/**
349 		 * Return matching addresses
350 		 * @return the addresses
351 		 */
352 		public ReferenceList getAddresses(  )
353 		{
354 			return _refListAddresses;
355 		}
356 		
357 		/**
358 		 * Sets an error message
359 		 * @param strMessage an error message
360 		 */
361 		public void setMessage( String strMessage )
362 		{
363 			_strMessage = strMessage;
364 		}
365 		
366 		/**
367 		 * Returns the error message
368 		 * @return the error message, <code>null</code> otherwise.
369 		 */
370 		public String getMessage(  )
371 		{
372 			return _strMessage;
373 		}
374 		
375 		/**
376 		 * <code>true</code> if the error message is not blank, <code>false</code> otherwise
377 		 * @return <code>true</code> if the error message is not blank, <code>false</code> otherwise
378 		 */
379 		public boolean isError(  )
380 		{
381 			return StringUtils.isNotBlank( _strMessage );
382 		}
383 		
384 		/**
385 		 * X
386 		 * @return x
387 		 */
388 		public float getX(  )
389 		{
390 			return _fX;
391 		}
392 		
393 		/**
394 		 * Sets the x
395 		 * @param fX the x
396 		 */
397 		public void setX( float fX )
398 		{
399 			_fX = fX;
400 		}
401 		
402 		/**
403 		 * Sets the y
404 		 * @param fY the y
405 		 */
406 		public void setY( float fY )
407 		{
408 			_fY = fY;
409 		}
410 
411 		/**
412 		 * Y
413 		 * @return y
414 		 */
415 		public float getY(  )
416 		{
417 			return _fY;
418 		}
419 		
420 		/**
421 		 * 
422 		 * {@inheritDoc}
423 		 */
424 		public String toJSONString( Locale locale )
425 		{
426 			JSONObject jsonObject = new JSONObject(  );
427 			
428 			try {
429 				jsonObject.put( JSON_X, getX(  ) );
430 				jsonObject.put( JSON_Y, getY(  ) );
431 				jsonObject.put( JSON_ERROR, isError(  ) );
432 				jsonObject.put( JSON_MESSAGE, getMessage(  ) );
433 				jsonObject.put( JSON_SINGLE_RESULT, isSingleResult(  ) );
434 				jsonObject.put( JSON_NO_RESULT, isNoResult(  ) );
435 				jsonObject.put( JSON_ADDRESS_LABEL,getAddressLabel(  ) );
436 				
437 				HtmlTemplate template;
438 				if ( isNoResult(  ) )
439 				{
440 					template = AppTemplateService.getTemplate( TEMPLATE_NO_RESULT, locale );
441 				}
442 				else if ( isSingleResult(  ) )
443 				{
444 					Map<String, Object> model = new HashMap<String, Object>(  );
445 					model.put( MARK_ADDRESS, getAddresses(  ).get( 0 ) );
446 					model.put( MARK_ID_ENTRY, getIdEntry(  ) );
447 					template = AppTemplateService.getTemplate( TEMPLATE_SINGLE_ADDRESS, locale, model );
448 				}
449 				else
450 				{
451 					Map<String, Object> model = new HashMap<String, Object>(  );
452 					model.put( MARK_LIST_ADDRESSES, getAddresses(  ) );
453 					model.put( MARK_ID_ENTRY, getIdEntry(  ) );
454 					template = AppTemplateService.getTemplate( TEMPLATE_ADDRESS_LIST, locale, model );
455 				}
456 				
457 				jsonObject.put( JSON_HTML, template.getHtml(  ) );
458 				
459 			} 
460 			catch (JSONException e)
461 			{
462 				AppLogService.error( e.getMessage(  ), e );
463 			}
464 			return jsonObject.toString(  );
465 		}
466 	}
467 }