1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
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
83
84
85
86
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
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
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
161
162
163
164
165
166
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
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
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
241
242
243
244
245
246
247
248
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
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
324
325
326
327
328
329
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
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
399
400
401
402
403
404
405 private byte [ ] read( String file ) throws Exception
406 {
407 FileInputStream fis = new FileInputStream( file );
408
409 FileChannel fc = fis.getChannel( );
410 byte [ ] data = new byte [ (int) fc.size( )];
411 ByteBuffer bb = ByteBuffer.wrap( data );
412 fc.read( bb );
413
414 return data;
415 }
416
417
418
419
420
421 public String getDateSearch( )
422 {
423 return _strDateSearch;
424 }
425
426
427
428
429
430
431 public void setDateSearch( String strDateSearch )
432 {
433 _strDateSearch = strDateSearch;
434 }
435
436
437
438
439
440 public String getDefaultCity( )
441 {
442 return _strDefaultCity;
443 }
444
445
446
447
448
449
450 public void setDefaultCity( String strDefaultCity )
451 {
452 _strDefaultCity = strDefaultCity;
453 }
454
455
456
457
458
459 public String getUrlWS( )
460 {
461 return _strUrlWS;
462 }
463
464
465
466
467
468
469 public void setUrlWS( String strUrlWS )
470 {
471 _strUrlWS = strUrlWS;
472 }
473
474
475
476
477
478 public String getPassword( )
479 {
480 return _strPassword;
481 }
482
483
484
485
486
487
488 public void setPassword( String password )
489 {
490 _strPassword = password;
491 }
492
493
494
495
496
497 public String getUserName( )
498 {
499 return _strUserName;
500 }
501
502
503
504
505
506
507 public void setUserName( String userName )
508 {
509 _strUserName = userName;
510 }
511
512
513
514
515
516 public String getTimeOut( )
517 {
518 return _strTimeOut;
519 }
520
521
522
523
524
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
539
540 public ReferenceList searchAddress( HttpServletRequest request, String labeladresse, String strSRID, String strArrondissement ) throws RemoteException
541 {
542 return searchAddress( request, labeladresse, strArrondissement );
543 }
544 }