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