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.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
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
67
68
69
70
71
72 public static String removeMultiSpace( String strChaine )
73 {
74 return strChaine.replaceAll( CONSTANT_MULTI_SPACE, CONSTANT_ONE_SPACE );
75 }
76
77
78
79
80
81
82
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
111
112
113
114
115
116 public static long parseLong( String strValue )
117 {
118 return parseLong( strValue, -1 );
119 }
120
121
122
123
124
125
126
127
128
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
144
145
146
147
148
149 public static int parseInt( String strValue )
150 {
151 return parseInt( strValue, -1 );
152 }
153
154
155
156
157
158
159
160
161
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
177
178
179
180
181
182
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
199 AppLogService.error( "LibraryAddressUtils.fillAddressGeolocation failed for " + strGeometry + " " + nfe.getLocalizedMessage( ) );
200 adresse.setGeoX( 0 );
201 adresse.setGeoY( 0 );
202 }
203 }
204 }
205
206
207
208
209
210
211
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 }