Coverage Report - fr.paris.lutece.plugins.address.util.LibraryAddressUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
LibraryAddressUtils
0 %
0/58
0 %
0/22
2,889
 
 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.util;
 35  
 
 36  
 import fr.paris.lutece.plugins.address.business.jaxb.Adresse;
 37  
 import fr.paris.lutece.portal.service.util.AppLogService;
 38  
 import fr.paris.lutece.portal.service.util.AppPropertiesService;
 39  
 
 40  
 import org.apache.commons.lang.ObjectUtils;
 41  
 import org.apache.commons.lang.StringUtils;
 42  
 
 43  
 
 44  
 /**
 45  
  * Utils methods for library address
 46  
  */
 47  
 public final class LibraryAddressUtils
 48  
 {
 49  
     public static final String CONSTANT_COMA = ",";
 50  
     public static final String CONSTANT_MULTI_SPACE = "\\s+";
 51  
     public static final String CONSTANT_ONE_SPACE = " ";
 52  
     public static final String CONSTANT_OPEN_PARENTHESIS = "(";
 53  
     public static final String CONSTANT_CLOSE_PARENTHESIS = ")";
 54  
     public static final String VALID_GEOMETRY_REGEX = ".*\\(.+ .+\\)";
 55  
     private static final String PROPERTY_VALUES = "address.endTypeVoie.values";
 56  
     private static final String PROPERTY_SEPARATOR = "address.endTypeVoie.separator";
 57  
 
 58  
     /**
 59  
      *
 60  
      *
 61  
      */
 62  
     private LibraryAddressUtils(  )
 63  0
     {
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Replace a sequence of space by one space
 68  
      * @param strChaine the chaine
 69  
      * @return the chaine with one space beetween words
 70  
      */
 71  
     public static String removeMultiSpace( String strChaine )
 72  
     {
 73  0
         return strChaine.replaceAll( CONSTANT_MULTI_SPACE, CONSTANT_ONE_SPACE );
 74  
     }
 75  
 
 76  
     /**
 77  
      * Test strTypeVoie is termitate by  a value of list strListValuesTerminator
 78  
      * @param strTypeVoie the type voie to test
 79  
      * @return true if a value of strTypeVoie is terminate by a value of list strListValuesTerminator
 80  
      */
 81  
     public static boolean isTerminateByApostrophe( String strTypeVoie )
 82  
     {
 83  0
         boolean bReturn = false;
 84  
 
 85  0
         String strListValues = AppPropertiesService.getProperty( PROPERTY_VALUES );
 86  0
         String strSeparator = AppPropertiesService.getProperty( PROPERTY_SEPARATOR );
 87  
 
 88  0
         if ( ( strListValues == null ) || ( strSeparator == null ) )
 89  
         {
 90  0
             return bReturn;
 91  
         }
 92  
 
 93  0
         String[] arrayValues = strListValues.split( strSeparator );
 94  
 
 95  0
         for ( String strCurrentValue : arrayValues )
 96  
         {
 97  0
             if ( ( strTypeVoie != null ) &&
 98  0
                     strTypeVoie.substring( strTypeVoie.length(  ) - 2 ).equalsIgnoreCase( strCurrentValue ) )
 99  
             {
 100  0
                 bReturn = true;
 101  
             }
 102  
         }
 103  
 
 104  0
         return bReturn;
 105  
     }
 106  
 
 107  
     /**
 108  
      * Parse a long
 109  
      * @param strValue the value to parse
 110  
      * @return the long value of <code>strValue</code>, <code>-1</code> otherwise.
 111  
      */
 112  
     public static long parseLong( String strValue )
 113  
     {
 114  0
         return parseLong( strValue, -1 );
 115  
     }
 116  
 
 117  
     /**
 118  
      * Parse a long
 119  
      * @param strValue the value to parse
 120  
      * @param nDefaultValue the default value
 121  
      * @return the long value of <code>strValue</code>, <code>nDefaultValue</code> otherwise.
 122  
      */
 123  
     public static long parseLong( String strValue, long nDefaultValue )
 124  
     {
 125  
         try
 126  
         {
 127  0
             return Long.parseLong( strValue );
 128  
         }
 129  0
         catch ( NumberFormatException nfe )
 130  
         {
 131  0
             return nDefaultValue;
 132  
         }
 133  
     }
 134  
 
 135  
     /**
 136  
      * Parse a long
 137  
      * @param strValue the value to parse
 138  
      * @return the int value of <code>strValue</code>, <code>-1</code> otherwise.
 139  
      */
 140  
     public static int parseInt( String strValue )
 141  
     {
 142  0
         return parseInt( strValue, -1 );
 143  
     }
 144  
 
 145  
     /**
 146  
      * Parse a long
 147  
      * @param strValue the value to parse
 148  
      * @param nDefaultValue the default value
 149  
      * @return the int value of <code>strValue</code>, <code>nDefaultValue</code> otherwise.
 150  
      */
 151  
     public static int parseInt( String strValue, int nDefaultValue )
 152  
     {
 153  
         try
 154  
         {
 155  0
             return Integer.parseInt( strValue );
 156  
         }
 157  0
         catch ( NumberFormatException nfe )
 158  
         {
 159  0
             return nDefaultValue;
 160  
         }
 161  
     }
 162  
 
 163  
     /**
 164  
      * Fills the address with x and y geolocation using strGeometry.
 165  
      * <code>POINT (123.456789 987.654321)</code> will give <code>x = 123.456789</code> and <code>y = 987.654321</code>.
 166  
      * Set x and y to 0 if x or y is not a number.
 167  
      * @param adresse the address to fill
 168  
      * @param strGeometry the geometry string
 169  
      */
 170  
     public static void fillAddressGeolocation( Adresse adresse, String strGeometry )
 171  
     {
 172  0
         if ( StringUtils.isNotBlank( strGeometry ) && strGeometry.matches( VALID_GEOMETRY_REGEX ) )
 173  
         {
 174  0
             String strCleanedGeometry = strGeometry.substring( strGeometry.lastIndexOf( CONSTANT_OPEN_PARENTHESIS ) +
 175  0
                     1, strGeometry.length(  ) - 1 );
 176  
 
 177  
             try
 178  
             {
 179  0
                 adresse.setGeoX( Float.parseFloat( strCleanedGeometry.substring( 0,
 180  0
                             strCleanedGeometry.lastIndexOf( CONSTANT_ONE_SPACE ) ) ) );
 181  0
                 adresse.setGeoY( Float.parseFloat( strCleanedGeometry.substring( strCleanedGeometry.lastIndexOf( 
 182  0
                                 CONSTANT_ONE_SPACE ), strCleanedGeometry.length(  ) ) ) );
 183  
             }
 184  0
             catch ( NumberFormatException nfe )
 185  
             {
 186  
                 // set to 0
 187  0
                 AppLogService.error( "LibraryAddressUtils.fillAddressGeolocation failed for " + strGeometry + " " +
 188  0
                     nfe.getLocalizedMessage(  ) );
 189  0
                 adresse.setGeoX( 0 );
 190  0
                 adresse.setGeoY( 0 );
 191  0
             }
 192  
         }
 193  0
     }
 194  
 
 195  
     /**
 196  
      * String representation of the adresse
 197  
      * @param adresse the adresse
 198  
      * @return the string
 199  
      */
 200  
     public static String normalizeAddress( Adresse adresse )
 201  
     {
 202  0
         StringBuilder sbAddress = new StringBuilder(  );
 203  
 
 204  0
         sbAddress.append( ObjectUtils.toString( adresse.getDunumero(  ) ) );
 205  0
         sbAddress.append( CONSTANT_ONE_SPACE );
 206  
 
 207  0
         if ( StringUtils.isNotBlank( adresse.getDubis(  ) ) )
 208  
         {
 209  0
             sbAddress.append( ObjectUtils.toString( adresse.getDubis(  ) ) );
 210  0
             sbAddress.append( CONSTANT_ONE_SPACE );
 211  
         }
 212  
 
 213  0
         sbAddress.append( ObjectUtils.toString( adresse.getTypeVoie(  ) ) );
 214  
 
 215  0
         if ( !LibraryAddressUtils.isTerminateByApostrophe( adresse.getTypeVoie(  ) ) )
 216  
         {
 217  0
             sbAddress.append( CONSTANT_ONE_SPACE );
 218  
         }
 219  
 
 220  0
         sbAddress.append( ObjectUtils.toString( adresse.getLibelleVoie(  ) ) );
 221  0
         sbAddress.append( CONSTANT_COMA );
 222  0
         sbAddress.append( CONSTANT_ONE_SPACE );
 223  
 
 224  0
         if ( StringUtils.isNotBlank( adresse.getComplement1Adresse(  ) ) )
 225  
         {
 226  0
             sbAddress.append( adresse.getComplement1Adresse(  ) );
 227  0
             sbAddress.append( CONSTANT_COMA );
 228  0
             sbAddress.append( CONSTANT_ONE_SPACE );
 229  
         }
 230  
 
 231  0
         if ( StringUtils.isNotBlank( adresse.getComplement2Adresse(  ) ) )
 232  
         {
 233  0
             sbAddress.append( adresse.getComplement2Adresse(  ) );
 234  0
             sbAddress.append( CONSTANT_COMA );
 235  0
             sbAddress.append( CONSTANT_ONE_SPACE );
 236  
         }
 237  
 
 238  0
         sbAddress.append( ObjectUtils.toString( adresse.getVille(  ) ) );
 239  
 
 240  0
         return sbAddress.toString(  );
 241  
     }
 242  
 }