Coverage Report - fr.paris.lutece.plugins.address.service.DummyAddressService
 
Classes in this File Line Coverage Branch Coverage Complexity
DummyAddressService
0 %
0/163
0 %
0/28
2,158
 
 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  0
 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  0
         String strFluxAddress = null;
 91  
 
 92  0
         String strFilePath = AppPathService.getPath( PROPERTY_XML_PATH_DIRECTORY );
 93  0
         String strFileName = AppPropertiesService.getProperty( PROPERTY_XML_FILE_SEARCH_ADDRESS );
 94  
 
 95  0
         byte[] out = new byte[128];
 96  
 
 97  
         try
 98  
         {
 99  0
             out = read( strFilePath + strFileName );
 100  
         }
 101  0
         catch ( Exception e )
 102  
         {
 103  0
             AppLogService.error( e.getMessage(  ), e );
 104  0
         }
 105  
 
 106  0
         strFluxAddress = new String( out );
 107  
 
 108  
         //traitement du flux xml                
 109  0
         Adresses adresses = null;
 110  
 
 111  
         JAXBContext jc;
 112  
 
 113  
         try
 114  
         {
 115  0
             jc = JAXBContext.newInstance( JAXB_CONTEXT_WS_SEARCH_ADDRESS );
 116  
 
 117  0
             Unmarshaller u = jc.createUnmarshaller(  );
 118  0
             StringBuffer xmlStr = new StringBuffer( strFluxAddress );
 119  0
             adresses = (Adresses) u.unmarshal( new StreamSource( new StringReader( xmlStr.toString(  ) ) ) );
 120  
         }
 121  0
         catch ( JAXBException e )
 122  
         {
 123  0
             AppLogService.error( e.getMessage(  ), e );
 124  0
         }
 125  
 
 126  0
         List<fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse> listAdresses = adresses.getAdresse(  );
 127  
 
 128  0
         ReferenceList refList = null;
 129  
 
 130  
         //build the list choice
 131  0
         if ( ( listAdresses != null ) && !listAdresses.isEmpty(  ) )
 132  
         {
 133  0
             refList = new ReferenceList(  );
 134  
 
 135  0
             for ( fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse currentAdresse : listAdresses )
 136  
             {
 137  0
                 String suffixe = "";
 138  
 
 139  0
                 if ( currentAdresse.getSuffixe(  ) != null )
 140  
                 {
 141  0
                     suffixe = currentAdresse.getSuffixe(  );
 142  
                 }
 143  
 
 144  0
                 String strCurrentAdresse = currentAdresse.getNumero(  ) + " " + suffixe + " " +
 145  0
                     currentAdresse.getTypeVoie(  ) + " " + currentAdresse.getNomVoie(  ) + " " +
 146  0
                     currentAdresse.getCommune(  );
 147  
 
 148  0
                 String strIdAdresse = currentAdresse.getIdentifiant(  ).toString(  );
 149  
 
 150  0
                 refList.addItem( strIdAdresse, strCurrentAdresse );
 151  0
             }
 152  
 
 153  0
             _listAdresses = adresses;
 154  
         }
 155  
 
 156  0
         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  0
         String strFluxAddress = null;
 169  
 
 170  0
         String strFilePath = AppPathService.getPath( PROPERTY_XML_PATH_DIRECTORY );
 171  0
         String strFileName = AppPropertiesService.getProperty( PROPERTY_XML_FILE_SEARCH_ADDRESS );
 172  
 
 173  0
         byte[] out = new byte[128];
 174  
 
 175  
         try
 176  
         {
 177  0
             out = read( strFilePath + strFileName );
 178  
         }
 179  0
         catch ( Exception e )
 180  
         {
 181  0
             AppLogService.error( e.getMessage(  ), e );
 182  0
         }
 183  
 
 184  0
         strFluxAddress = new String( out );
 185  
 
 186  
         //traitement du flux xml                
 187  0
         Adresses adresses = null;
 188  
 
 189  
         JAXBContext jc;
 190  
 
 191  
         try
 192  
         {
 193  0
             jc = JAXBContext.newInstance( JAXB_CONTEXT_WS_SEARCH_ADDRESS );
 194  
 
 195  0
             Unmarshaller u = jc.createUnmarshaller(  );
 196  0
             StringBuffer xmlStr = new StringBuffer( strFluxAddress );
 197  0
             adresses = (Adresses) u.unmarshal( new StreamSource( new StringReader( xmlStr.toString(  ) ) ) );
 198  
         }
 199  0
         catch ( JAXBException e )
 200  
         {
 201  0
             AppLogService.error( e.getMessage(  ), e );
 202  0
         }
 203  
 
 204  0
         List<fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse> listAdresses = adresses.getAdresse(  );
 205  
 
 206  0
         ReferenceList refList = null;
 207  
 
 208  
         //build the list choice
 209  0
         if ( ( listAdresses != null ) && !listAdresses.isEmpty(  ) )
 210  
         {
 211  0
             refList = new ReferenceList(  );
 212  
 
 213  0
             for ( fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse currentAdresse : listAdresses )
 214  
             {
 215  0
                 String suffixe = "";
 216  
 
 217  0
                 if ( currentAdresse.getSuffixe(  ) != null )
 218  
                 {
 219  0
                     suffixe = currentAdresse.getSuffixe(  );
 220  
                 }
 221  
 
 222  0
                 String strCurrentAdresse = currentAdresse.getNumero(  ) + " " + suffixe + " " +
 223  0
                     currentAdresse.getTypeVoie(  ) + " " + currentAdresse.getNomVoie(  ) + " " +
 224  0
                     currentAdresse.getCommune(  );
 225  
 
 226  0
                 String strIdAdresse = currentAdresse.getIdentifiant(  ).toString(  );
 227  
 
 228  0
                 refList.addItem( strIdAdresse, strCurrentAdresse );
 229  0
             }
 230  
 
 231  0
             _listAdresses = adresses;
 232  
         }
 233  
 
 234  0
         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  0
         String strFluxAddress = null;
 249  
 
 250  0
         String strFilePath = AppPathService.getPath( PROPERTY_XML_PATH_DIRECTORY );
 251  0
         String strFileName = AppPropertiesService.getProperty( PROPERTY_XML_FILE_ADDRESS_INFO );
 252  
 
 253  0
         byte[] out = new byte[128];
 254  
 
 255  
         try
 256  
         {
 257  0
             out = read( strFilePath + strFileName );
 258  
         }
 259  0
         catch ( Exception e )
 260  
         {
 261  0
             AppLogService.error( e.getMessage(  ), e );
 262  0
         }
 263  
 
 264  0
         strFluxAddress = new String( out );
 265  
 
 266  
         //traitement du flux xml
 267  0
         fr.paris.lutece.plugins.address.business.jaxb.wsFicheAdresse.Adresse adresse = null;
 268  
 
 269  
         JAXBContext jc;
 270  
 
 271  
         try
 272  
         {
 273  0
             jc = JAXBContext.newInstance( JAXB_CONTEXT_WS_FICHE_ADDRESS );
 274  
 
 275  0
             Unmarshaller u = jc.createUnmarshaller(  );
 276  0
             StringBuffer xmlStr = new StringBuffer( strFluxAddress );
 277  0
             adresse = (fr.paris.lutece.plugins.address.business.jaxb.wsFicheAdresse.Adresse) u.unmarshal( new StreamSource( 
 278  0
                         new StringReader( xmlStr.toString(  ) ) ) );
 279  
         }
 280  0
         catch ( JAXBException e )
 281  
         {
 282  0
             AppLogService.error( e.getMessage(  ), e );
 283  0
         }
 284  
 
 285  0
         Adresse adresseReturn = new Adresse(  );
 286  
 
 287  0
         adresseReturn.setIadresse( adresse.getIdentifiant(  ) );
 288  0
         adresseReturn.setDunumero( adresse.getNumero(  ) );
 289  0
         adresseReturn.setDubis( adresse.getSuffixe1(  ) );
 290  0
         adresseReturn.setCodeCommune( adresse.getCodeInsee(  ).toString(  ) );
 291  
 
 292  0
         String responseWebService = adresse.getGeometry(  );
 293  0
         responseWebService = responseWebService.substring( responseWebService.lastIndexOf( "(" ) + 1,
 294  0
                 responseWebService.length(  ) - 1 );
 295  
 
 296  0
         adresseReturn.setGeoX( Float.parseFloat( responseWebService.substring( 0, responseWebService.lastIndexOf( " " ) ) ) );
 297  0
         adresseReturn.setGeoY( Float.parseFloat( responseWebService.substring( responseWebService.lastIndexOf( " " ),
 298  0
                     responseWebService.length(  ) ) ) );
 299  
 
 300  0
         if ( !bIsTest )
 301  
         {
 302  0
             List<fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse> listAddress = _listAdresses.getAdresse(  );
 303  
 
 304  0
             for ( fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse currentAdresse : listAddress )
 305  
             {
 306  0
                 if ( String.valueOf( currentAdresse.getIdentifiant(  ) ).equals( String.valueOf( id ) ) )
 307  
                 {
 308  0
                     adresseReturn.setTypeVoie( currentAdresse.getTypeVoie(  ) );
 309  0
                     adresseReturn.setLibelleVoie( currentAdresse.getNomVoie(  ) );
 310  
 
 311  0
                     break;
 312  
                 }
 313  0
             }
 314  
         }
 315  
 
 316  0
         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  0
         String strFluxAddress = null;
 329  
 
 330  0
         String strFilePath = AppPathService.getPath( PROPERTY_XML_PATH_DIRECTORY );
 331  0
         String strFileName = AppPropertiesService.getProperty( PROPERTY_XML_FILE_ADDRESS_INFO );
 332  
 
 333  0
         byte[] out = new byte[128];
 334  
 
 335  
         try
 336  
         {
 337  0
             out = read( strFilePath + strFileName );
 338  
         }
 339  0
         catch ( Exception e )
 340  
         {
 341  0
             AppLogService.error( e.getMessage(  ), e );
 342  0
         }
 343  
 
 344  0
         strFluxAddress = new String( out );
 345  
 
 346  
         //traitement du flux xml
 347  0
         fr.paris.lutece.plugins.address.business.jaxb.wsFicheAdresse.Adresse adresse = null;
 348  
 
 349  
         JAXBContext jc;
 350  
 
 351  
         try
 352  
         {
 353  0
             jc = JAXBContext.newInstance( JAXB_CONTEXT_WS_FICHE_ADDRESS );
 354  
 
 355  0
             Unmarshaller u = jc.createUnmarshaller(  );
 356  0
             StringBuffer xmlStr = new StringBuffer( strFluxAddress );
 357  0
             adresse = (fr.paris.lutece.plugins.address.business.jaxb.wsFicheAdresse.Adresse) u.unmarshal( new StreamSource( 
 358  0
                         new StringReader( xmlStr.toString(  ) ) ) );
 359  
         }
 360  0
         catch ( JAXBException e )
 361  
         {
 362  0
             AppLogService.error( e.getMessage(  ), e );
 363  0
         }
 364  
 
 365  0
         Adresse adresseReturn = new Adresse(  );
 366  
 
 367  0
         adresseReturn.setIadresse( adresse.getIdentifiant(  ) );
 368  0
         adresseReturn.setDunumero( adresse.getNumero(  ) );
 369  0
         adresseReturn.setDubis( adresse.getSuffixe1(  ) );
 370  0
         adresseReturn.setCodeCommune( adresse.getCodeInsee(  ).toString(  ) );
 371  
 
 372  0
         if ( !bIsTest )
 373  
         {
 374  0
             List<fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse> listAddress = _listAdresses.getAdresse(  );
 375  
 
 376  0
             for ( fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse currentAdresse : listAddress )
 377  
             {
 378  0
                 if ( String.valueOf( currentAdresse.getIdentifiant(  ) ).equals( String.valueOf( id ) ) )
 379  
                 {
 380  0
                     adresseReturn.setTypeVoie( currentAdresse.getTypeVoie(  ) );
 381  0
                     adresseReturn.setLibelleVoie( currentAdresse.getNomVoie(  ) );
 382  
 
 383  0
                     break;
 384  
                 }
 385  0
             }
 386  
         }
 387  
 
 388  0
         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  0
         FileInputStream fis = new FileInputStream( file );
 400  
 
 401  0
         FileChannel fc = fis.getChannel(  );
 402  0
         byte[] data = new byte[(int) fc.size(  )]; // fc.size returns the size
 403  0
         ByteBuffer bb = ByteBuffer.wrap( data );
 404  0
         fc.read( bb );
 405  
 
 406  0
         return data;
 407  
     }
 408  
 
 409  
     /**
 410  
     *
 411  
     * @return the date for parameter methodes of web service
 412  
     */
 413  
     public String getDateSearch(  )
 414  
     {
 415  0
         return _strDateSearch;
 416  
     }
 417  
 
 418  
     /**
 419  
      *
 420  
      * @param strDateSearch the new date search
 421  
      */
 422  
     public void setDateSearch( String strDateSearch )
 423  
     {
 424  0
         _strDateSearch = strDateSearch;
 425  0
     }
 426  
 
 427  
     /**
 428  
      *
 429  
      * @return the default city for parameter methodes of web service
 430  
      */
 431  
     public String getDefaultCity(  )
 432  
     {
 433  0
         return _strDefaultCity;
 434  
     }
 435  
 
 436  
     /**
 437  
      *
 438  
      * @param strDefaultCity the new default city
 439  
      */
 440  
     public void setDefaultCity( String strDefaultCity )
 441  
     {
 442  0
         _strDefaultCity = strDefaultCity;
 443  0
     }
 444  
 
 445  
     /**
 446  
      *
 447  
      * @return the url of the web service
 448  
      */
 449  
     public String getUrlWS(  )
 450  
     {
 451  0
         return _strUrlWS;
 452  
     }
 453  
 
 454  
     /**
 455  
      *
 456  
      * @param strUrlWS the new web service url
 457  
      */
 458  
     public void setUrlWS( String strUrlWS )
 459  
     {
 460  0
         _strUrlWS = strUrlWS;
 461  0
     }
 462  
 
 463  
     /**
 464  
      *
 465  
      * @return the password
 466  
      */
 467  
     public String getPassword(  )
 468  
     {
 469  0
         return _strPassword;
 470  
     }
 471  
 
 472  
     /**
 473  
      *
 474  
      * @param password the password
 475  
      */
 476  
     public void setPassword( String password )
 477  
     {
 478  0
         _strPassword = password;
 479  0
     }
 480  
 
 481  
     /**
 482  
      *
 483  
      * @return the user name
 484  
      */
 485  
     public String getUserName(  )
 486  
     {
 487  0
         return _strUserName;
 488  
     }
 489  
 
 490  
     /**
 491  
      *
 492  
      * @param userName the user name
 493  
      */
 494  
     public void setUserName( String userName )
 495  
     {
 496  0
         _strUserName = userName;
 497  0
     }
 498  
 
 499  
     /**
 500  
     *
 501  
     * @return the timeout
 502  
     */
 503  
     public String getTimeOut(  )
 504  
     {
 505  0
         return _strTimeOut;
 506  
     }
 507  
 
 508  
     /**
 509  
      *
 510  
      * @param timeOut the timeout
 511  
      */
 512  
     public void setTimeOut( String timeOut )
 513  
     {
 514  0
         _strTimeOut = timeOut;
 515  0
     }
 516  
 
 517  
     public Adresse getGeolocalisation( HttpServletRequest request, String addresse, String date, boolean bIsTest )
 518  
         throws RemoteException
 519  
     {
 520  0
         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  0
         return searchAddress( request, labeladresse, strArrondissement );
 531  
     }
 532  
 }