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.address.service;
35  
36  import fr.paris.lutece.plugins.address.business.jaxb.Adresse;
37  import fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresses;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  import fr.paris.lutece.portal.service.util.AppPathService;
40  import fr.paris.lutece.portal.service.util.AppPropertiesService;
41  import fr.paris.lutece.util.ReferenceList;
42  
43  import java.io.FileInputStream;
44  import java.io.StringReader;
45  
46  import java.nio.ByteBuffer;
47  import java.nio.channels.FileChannel;
48  
49  import java.rmi.RemoteException;
50  
51  import java.util.List;
52  
53  import javax.servlet.http.HttpServletRequest;
54  
55  import javax.xml.bind.JAXBContext;
56  import javax.xml.bind.JAXBException;
57  import javax.xml.bind.Unmarshaller;
58  import javax.xml.transform.stream.StreamSource;
59  
60  
61  /**
62   *
63   */
64  public class DummyAddressService implements IAddressService
65  {
66      //jaxb context
67      private static final String JAXB_CONTEXT_WS_FICHE_ADDRESS = "fr.paris.lutece.plugins.address.business.jaxb.wsFicheAdresse";
68      private static final String JAXB_CONTEXT_WS_SEARCH_ADDRESS = "fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse";
69  
70      //properties
71      private static final String PROPERTY_XML_PATH_DIRECTORY = "address.xml.file.test.directory";
72      private static final String PROPERTY_XML_FILE_SEARCH_ADDRESS = "address.xml.file.searchAddress";
73      private static final String PROPERTY_XML_FILE_ADDRESS_INFO = "address.xml.file.addressInfo";
74      private String _strUrlWS;
75      private String _strDefaultCity;
76      private String _strDateSearch;
77      private String _strUserName;
78      private String _strPassword;
79      private String _strTimeOut;
80      private Adresses _listAdresses;
81  
82      /**
83      * @param request Request
84      * @param labeladresse the  label adress
85      * @return the XML flux of all adress corresponding
86      *
87      */
88      public ReferenceList searchAddress( HttpServletRequest request, String labeladresse )
89      {
90          String strFluxAddress = null;
91  
92          String strFilePath = AppPathService.getPath( PROPERTY_XML_PATH_DIRECTORY );
93          String strFileName = AppPropertiesService.getProperty( PROPERTY_XML_FILE_SEARCH_ADDRESS );
94  
95          byte[] out = new byte[128];
96  
97          try
98          {
99              out = read( strFilePath + strFileName );
100         }
101         catch ( Exception e )
102         {
103             AppLogService.error( e.getMessage(  ), e );
104         }
105 
106         strFluxAddress = new String( out );
107 
108         //traitement du flux xml		
109         Adresses adresses = null;
110 
111         JAXBContext jc;
112 
113         try
114         {
115             jc = JAXBContext.newInstance( JAXB_CONTEXT_WS_SEARCH_ADDRESS );
116 
117             Unmarshaller u = jc.createUnmarshaller(  );
118             StringBuffer xmlStr = new StringBuffer( strFluxAddress );
119             adresses = (Adresses) u.unmarshal( new StreamSource( new StringReader( xmlStr.toString(  ) ) ) );
120         }
121         catch ( JAXBException e )
122         {
123             AppLogService.error( e.getMessage(  ), e );
124         }
125 
126         List<fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse> listAdresses = adresses.getAdresse(  );
127 
128         ReferenceList refList = null;
129 
130         //build the list choice
131         if ( ( listAdresses != null ) && !listAdresses.isEmpty(  ) )
132         {
133             refList = new ReferenceList(  );
134 
135             for ( fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse currentAdresse : listAdresses )
136             {
137                 String suffixe = "";
138 
139                 if ( currentAdresse.getSuffixe(  ) != null )
140                 {
141                     suffixe = currentAdresse.getSuffixe(  );
142                 }
143 
144                 String strCurrentAdresse = currentAdresse.getNumero(  ) + " " + suffixe + " " +
145                     currentAdresse.getTypeVoie(  ) + " " + currentAdresse.getNomVoie(  ) + " " +
146                     currentAdresse.getCommune(  );
147 
148                 String strIdAdresse = currentAdresse.getIdentifiant(  ).toString(  );
149 
150                 refList.addItem( strIdAdresse, strCurrentAdresse );
151             }
152 
153             _listAdresses = adresses;
154         }
155 
156         return refList;
157     }
158 
159     /**
160      * @param request Request
161      * @param labeladresse the  label adress
162      * @param strArrondissement Arrondissement
163      * @return the XML flux of all adress corresponding
164      *
165      */
166     public ReferenceList searchAddress( HttpServletRequest request, String labeladresse, String strArrondissement )
167     {
168         String strFluxAddress = null;
169 
170         String strFilePath = AppPathService.getPath( PROPERTY_XML_PATH_DIRECTORY );
171         String strFileName = AppPropertiesService.getProperty( PROPERTY_XML_FILE_SEARCH_ADDRESS );
172 
173         byte[] out = new byte[128];
174 
175         try
176         {
177             out = read( strFilePath + strFileName );
178         }
179         catch ( Exception e )
180         {
181             AppLogService.error( e.getMessage(  ), e );
182         }
183 
184         strFluxAddress = new String( out );
185 
186         //traitement du flux xml		
187         Adresses adresses = null;
188 
189         JAXBContext jc;
190 
191         try
192         {
193             jc = JAXBContext.newInstance( JAXB_CONTEXT_WS_SEARCH_ADDRESS );
194 
195             Unmarshaller u = jc.createUnmarshaller(  );
196             StringBuffer xmlStr = new StringBuffer( strFluxAddress );
197             adresses = (Adresses) u.unmarshal( new StreamSource( new StringReader( xmlStr.toString(  ) ) ) );
198         }
199         catch ( JAXBException e )
200         {
201             AppLogService.error( e.getMessage(  ), e );
202         }
203 
204         List<fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse> listAdresses = adresses.getAdresse(  );
205 
206         ReferenceList refList = null;
207 
208         //build the list choice
209         if ( ( listAdresses != null ) && !listAdresses.isEmpty(  ) )
210         {
211             refList = new ReferenceList(  );
212 
213             for ( fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse currentAdresse : listAdresses )
214             {
215                 String suffixe = "";
216 
217                 if ( currentAdresse.getSuffixe(  ) != null )
218                 {
219                     suffixe = currentAdresse.getSuffixe(  );
220                 }
221 
222                 String strCurrentAdresse = currentAdresse.getNumero(  ) + " " + suffixe + " " +
223                     currentAdresse.getTypeVoie(  ) + " " + currentAdresse.getNomVoie(  ) + " " +
224                     currentAdresse.getCommune(  );
225 
226                 String strIdAdresse = currentAdresse.getIdentifiant(  ).toString(  );
227 
228                 refList.addItem( strIdAdresse, strCurrentAdresse );
229             }
230 
231             _listAdresses = adresses;
232         }
233 
234         return refList;
235     }
236 
237     /**
238      * @throws RemoteException the RemoteExecption
239      * @param request Request
240      * @param id the adress id
241      * @param bIsTest if true test connect at web service, if false search an adress
242      * @return the XML flux of an adress
243      *
244      */
245     public Adresse getGeolocalisation( HttpServletRequest request, long id, String strAddress, String strDate,
246         boolean bIsTest )
247     {
248         String strFluxAddress = null;
249 
250         String strFilePath = AppPathService.getPath( PROPERTY_XML_PATH_DIRECTORY );
251         String strFileName = AppPropertiesService.getProperty( PROPERTY_XML_FILE_ADDRESS_INFO );
252 
253         byte[] out = new byte[128];
254 
255         try
256         {
257             out = read( strFilePath + strFileName );
258         }
259         catch ( Exception e )
260         {
261             AppLogService.error( e.getMessage(  ), e );
262         }
263 
264         strFluxAddress = new String( out );
265 
266         //traitement du flux xml
267         fr.paris.lutece.plugins.address.business.jaxb.wsFicheAdresse.Adresse adresse = null;
268 
269         JAXBContext jc;
270 
271         try
272         {
273             jc = JAXBContext.newInstance( JAXB_CONTEXT_WS_FICHE_ADDRESS );
274 
275             Unmarshaller u = jc.createUnmarshaller(  );
276             StringBuffer xmlStr = new StringBuffer( strFluxAddress );
277             adresse = (fr.paris.lutece.plugins.address.business.jaxb.wsFicheAdresse.Adresse) u.unmarshal( new StreamSource( 
278                         new StringReader( xmlStr.toString(  ) ) ) );
279         }
280         catch ( JAXBException e )
281         {
282             AppLogService.error( e.getMessage(  ), e );
283         }
284 
285         Adresse adresseReturn = new Adresse(  );
286 
287         adresseReturn.setIadresse( adresse.getIdentifiant(  ) );
288         adresseReturn.setDunumero( adresse.getNumero(  ) );
289         adresseReturn.setDubis( adresse.getSuffixe1(  ) );
290         adresseReturn.setCodeCommune( adresse.getCodeInsee(  ).toString(  ) );
291 
292         String responseWebService = adresse.getGeometry(  );
293         responseWebService = responseWebService.substring( responseWebService.lastIndexOf( "(" ) + 1,
294                 responseWebService.length(  ) - 1 );
295 
296         adresseReturn.setGeoX( Float.parseFloat( responseWebService.substring( 0, responseWebService.lastIndexOf( " " ) ) ) );
297         adresseReturn.setGeoY( Float.parseFloat( responseWebService.substring( responseWebService.lastIndexOf( " " ),
298                     responseWebService.length(  ) ) ) );
299 
300         if ( !bIsTest )
301         {
302             List<fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse> listAddress = _listAdresses.getAdresse(  );
303 
304             for ( fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse currentAdresse : listAddress )
305             {
306                 if ( String.valueOf( currentAdresse.getIdentifiant(  ) ).equals( String.valueOf( id ) ) )
307                 {
308                     adresseReturn.setTypeVoie( currentAdresse.getTypeVoie(  ) );
309                     adresseReturn.setLibelleVoie( currentAdresse.getNomVoie(  ) );
310 
311                     break;
312                 }
313             }
314         }
315 
316         return adresseReturn;
317     }
318 
319     /**
320     * @param request Request
321     * @param id the adress id
322     * @param bIsTest if true test connect at web service, if false search an adress
323     * @return the XML flux of an adress
324     *
325     */
326     public Adresse getAdresseInfo( HttpServletRequest request, long id, boolean bIsTest )
327     {
328         String strFluxAddress = null;
329 
330         String strFilePath = AppPathService.getPath( PROPERTY_XML_PATH_DIRECTORY );
331         String strFileName = AppPropertiesService.getProperty( PROPERTY_XML_FILE_ADDRESS_INFO );
332 
333         byte[] out = new byte[128];
334 
335         try
336         {
337             out = read( strFilePath + strFileName );
338         }
339         catch ( Exception e )
340         {
341             AppLogService.error( e.getMessage(  ), e );
342         }
343 
344         strFluxAddress = new String( out );
345 
346         //traitement du flux xml
347         fr.paris.lutece.plugins.address.business.jaxb.wsFicheAdresse.Adresse adresse = null;
348 
349         JAXBContext jc;
350 
351         try
352         {
353             jc = JAXBContext.newInstance( JAXB_CONTEXT_WS_FICHE_ADDRESS );
354 
355             Unmarshaller u = jc.createUnmarshaller(  );
356             StringBuffer xmlStr = new StringBuffer( strFluxAddress );
357             adresse = (fr.paris.lutece.plugins.address.business.jaxb.wsFicheAdresse.Adresse) u.unmarshal( new StreamSource( 
358                         new StringReader( xmlStr.toString(  ) ) ) );
359         }
360         catch ( JAXBException e )
361         {
362             AppLogService.error( e.getMessage(  ), e );
363         }
364 
365         Adresse adresseReturn = new Adresse(  );
366 
367         adresseReturn.setIadresse( adresse.getIdentifiant(  ) );
368         adresseReturn.setDunumero( adresse.getNumero(  ) );
369         adresseReturn.setDubis( adresse.getSuffixe1(  ) );
370         adresseReturn.setCodeCommune( adresse.getCodeInsee(  ).toString(  ) );
371 
372         if ( !bIsTest )
373         {
374             List<fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse> listAddress = _listAdresses.getAdresse(  );
375 
376             for ( fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse currentAdresse : listAddress )
377             {
378                 if ( String.valueOf( currentAdresse.getIdentifiant(  ) ).equals( String.valueOf( id ) ) )
379                 {
380                     adresseReturn.setTypeVoie( currentAdresse.getTypeVoie(  ) );
381                     adresseReturn.setLibelleVoie( currentAdresse.getNomVoie(  ) );
382 
383                     break;
384                 }
385             }
386         }
387 
388         return adresseReturn;
389     }
390 
391     /**
392      * @throws Exception the execption
393      * @param file the file
394      * @return the byte array of file
395      *
396      */
397     private byte[] read( String file ) throws Exception // lots of exceptions
398     {
399         FileInputStream fis = new FileInputStream( file );
400 
401         FileChannel fc = fis.getChannel(  );
402         byte[] data = new byte[(int) fc.size(  )]; // fc.size returns the size
403         ByteBuffer bb = ByteBuffer.wrap( data );
404         fc.read( bb );
405 
406         return data;
407     }
408 
409     /**
410     *
411     * @return the date for parameter methodes of web service
412     */
413     public String getDateSearch(  )
414     {
415         return _strDateSearch;
416     }
417 
418     /**
419      *
420      * @param strDateSearch the new date search
421      */
422     public void setDateSearch( String strDateSearch )
423     {
424         _strDateSearch = strDateSearch;
425     }
426 
427     /**
428      *
429      * @return the default city for parameter methodes of web service
430      */
431     public String getDefaultCity(  )
432     {
433         return _strDefaultCity;
434     }
435 
436     /**
437      *
438      * @param strDefaultCity the new default city
439      */
440     public void setDefaultCity( String strDefaultCity )
441     {
442         _strDefaultCity = strDefaultCity;
443     }
444 
445     /**
446      *
447      * @return the url of the web service
448      */
449     public String getUrlWS(  )
450     {
451         return _strUrlWS;
452     }
453 
454     /**
455      *
456      * @param strUrlWS the new web service url
457      */
458     public void setUrlWS( String strUrlWS )
459     {
460         _strUrlWS = strUrlWS;
461     }
462 
463     /**
464      *
465      * @return the password
466      */
467     public String getPassword(  )
468     {
469         return _strPassword;
470     }
471 
472     /**
473      *
474      * @param password the password
475      */
476     public void setPassword( String password )
477     {
478         _strPassword = password;
479     }
480 
481     /**
482      *
483      * @return the user name
484      */
485     public String getUserName(  )
486     {
487         return _strUserName;
488     }
489 
490     /**
491      *
492      * @param userName the user name
493      */
494     public void setUserName( String userName )
495     {
496         _strUserName = userName;
497     }
498 
499     /**
500     *
501     * @return the timeout
502     */
503     public String getTimeOut(  )
504     {
505         return _strTimeOut;
506     }
507 
508     /**
509      *
510      * @param timeOut the timeout
511      */
512     public void setTimeOut( String timeOut )
513     {
514         _strTimeOut = timeOut;
515     }
516 
517     public Adresse getGeolocalisation( HttpServletRequest request, String addresse, String date, boolean bIsTest )
518         throws RemoteException
519     {
520         return getGeolocalisation( request, 0, addresse, date, bIsTest );
521     }
522 
523     /**
524      *
525      *{@inheritDoc}
526      */
527     public ReferenceList searchAddress( HttpServletRequest request, String labeladresse, String strSRID,
528         String strArrondissement ) throws RemoteException
529     {
530         return searchAddress( request, labeladresse, strArrondissement );
531     }
532 }