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