View Javadoc
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      {
64      }
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          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          boolean bReturn = false;
84  
85          String strListValues = AppPropertiesService.getProperty( PROPERTY_VALUES );
86          String strSeparator = AppPropertiesService.getProperty( PROPERTY_SEPARATOR );
87  
88          if ( ( strListValues == null ) || ( strSeparator == null ) )
89          {
90              return bReturn;
91          }
92  
93          String[] arrayValues = strListValues.split( strSeparator );
94  
95          for ( String strCurrentValue : arrayValues )
96          {
97              if ( ( strTypeVoie != null ) &&
98                      strTypeVoie.substring( strTypeVoie.length(  ) - 2 ).equalsIgnoreCase( strCurrentValue ) )
99              {
100                 bReturn = true;
101             }
102         }
103 
104         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         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             return Long.parseLong( strValue );
128         }
129         catch ( NumberFormatException nfe )
130         {
131             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         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             return Integer.parseInt( strValue );
156         }
157         catch ( NumberFormatException nfe )
158         {
159             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         if ( StringUtils.isNotBlank( strGeometry ) && strGeometry.matches( VALID_GEOMETRY_REGEX ) )
173         {
174             String strCleanedGeometry = strGeometry.substring( strGeometry.lastIndexOf( CONSTANT_OPEN_PARENTHESIS ) +
175                     1, strGeometry.length(  ) - 1 );
176 
177             try
178             {
179                 adresse.setGeoX( Float.parseFloat( strCleanedGeometry.substring( 0,
180                             strCleanedGeometry.lastIndexOf( CONSTANT_ONE_SPACE ) ) ) );
181                 adresse.setGeoY( Float.parseFloat( strCleanedGeometry.substring( strCleanedGeometry.lastIndexOf( 
182                                 CONSTANT_ONE_SPACE ), strCleanedGeometry.length(  ) ) ) );
183             }
184             catch ( NumberFormatException nfe )
185             {
186                 // set to 0
187                 AppLogService.error( "LibraryAddressUtils.fillAddressGeolocation failed for " + strGeometry + " " +
188                     nfe.getLocalizedMessage(  ) );
189                 adresse.setGeoX( 0 );
190                 adresse.setGeoY( 0 );
191             }
192         }
193     }
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         StringBuilder sbAddress = new StringBuilder(  );
203 
204         sbAddress.append( ObjectUtils.toString( adresse.getDunumero(  ) ) );
205         sbAddress.append( CONSTANT_ONE_SPACE );
206 
207         if ( StringUtils.isNotBlank( adresse.getDubis(  ) ) )
208         {
209             sbAddress.append( ObjectUtils.toString( adresse.getDubis(  ) ) );
210             sbAddress.append( CONSTANT_ONE_SPACE );
211         }
212 
213         sbAddress.append( ObjectUtils.toString( adresse.getTypeVoie(  ) ) );
214 
215         if ( !LibraryAddressUtils.isTerminateByApostrophe( adresse.getTypeVoie(  ) ) )
216         {
217             sbAddress.append( CONSTANT_ONE_SPACE );
218         }
219 
220         sbAddress.append( ObjectUtils.toString( adresse.getLibelleVoie(  ) ) );
221         sbAddress.append( CONSTANT_COMA );
222         sbAddress.append( CONSTANT_ONE_SPACE );
223 
224         if ( StringUtils.isNotBlank( adresse.getComplement1Adresse(  ) ) )
225         {
226             sbAddress.append( adresse.getComplement1Adresse(  ) );
227             sbAddress.append( CONSTANT_COMA );
228             sbAddress.append( CONSTANT_ONE_SPACE );
229         }
230 
231         if ( StringUtils.isNotBlank( adresse.getComplement2Adresse(  ) ) )
232         {
233             sbAddress.append( adresse.getComplement2Adresse(  ) );
234             sbAddress.append( CONSTANT_COMA );
235             sbAddress.append( CONSTANT_ONE_SPACE );
236         }
237 
238         sbAddress.append( ObjectUtils.toString( adresse.getVille(  ) ) );
239 
240         return sbAddress.toString(  );
241     }
242 }