View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.genericattributes.service.entrytype;
35  
36  import java.util.List;
37  import java.util.Locale;
38  import java.util.stream.Collectors;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import org.apache.commons.lang3.StringUtils;
43  
44  import com.google.i18n.phonenumbers.NumberParseException;
45  import com.google.i18n.phonenumbers.PhoneNumberUtil;
46  import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
47  import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
48  
49  import fr.paris.lutece.plugins.genericattributes.business.Entry;
50  import fr.paris.lutece.plugins.genericattributes.business.Field;
51  import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
52  import fr.paris.lutece.plugins.genericattributes.business.MandatoryError;
53  import fr.paris.lutece.plugins.genericattributes.business.Response;
54  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
55  import fr.paris.lutece.portal.service.i18n.I18nService;
56  import fr.paris.lutece.portal.service.message.AdminMessage;
57  import fr.paris.lutece.portal.service.message.AdminMessageService;
58  import fr.paris.lutece.portal.service.util.AppPropertiesService;
59  import fr.paris.lutece.util.string.StringUtil;
60  
61  /**
62   * Abstract entry type for telephone number
63   */
64  public abstract class AbstractEntryTypeTelephoneNumber extends EntryTypeService
65  {
66  
67      private static final String PARAMETER_AUTOCOMPLETE = "autocomplete";
68      private static final String PARAMETER_DEFAULT_REGION = "defaultRegion";
69      private static final String FIELD_AUTOCOMPLETE = "autocomplete";
70      public static final String FIELD_DEFAULT_REGION = "defaultRegion";
71      private static final String MESSAGE_ERROR_PHONENUMBER = "genericattributes.message.error.phonenumber";
72      private static final String MESSAGE_UNKNOWN_DEFAULT_REGION = "genericattributes.message.error.unknown.default.region";
73      private static final String PROPERTY_DEFAULT_DEFAULT_REGION = "genericattributes.telephoneNumber.default.default.region";
74  
75      @Override
76      public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
77      {
78          initCommonRequestData( entry, request );
79          String strTitle = request.getParameter( PARAMETER_TITLE );
80          String strCode = request.getParameter( PARAMETER_ENTRY_CODE );
81          String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
82          String strComment = request.getParameter( PARAMETER_COMMENT );
83          String strMandatory = request.getParameter( PARAMETER_MANDATORY );
84          String strErrorMessage = request.getParameter( PARAMETER_ERROR_MESSAGE );
85          String strCSSClass = request.getParameter( PARAMETER_CSS_CLASS );
86          String strOnlyDisplayInBack = request.getParameter( PARAMETER_ONLY_DISPLAY_IN_BACK );
87          String strIndexed = request.getParameter( PARAMETER_INDEXED );
88          String strAutocomplete = request.getParameter( PARAMETER_AUTOCOMPLETE );
89          String strDefaultRegion = request.getParameter( PARAMETER_DEFAULT_REGION );
90          String strPlaceholder = request.getParameter( PARAMETER_PLACEHOLDER );
91  
92          String strFieldError = StringUtils.EMPTY;
93  
94          if ( StringUtils.isBlank( strTitle ) )
95          {
96              strFieldError = ERROR_FIELD_TITLE;
97          }
98  
99          if ( StringUtils.isNotBlank( strFieldError ) )
100         {
101             Object [ ] tabRequiredFields = {
102                     I18nService.getLocalizedString( strFieldError, locale )
103             };
104 
105             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
106         }
107 
108         entry.setCode( strCode );
109         entry.setTitle( strTitle );
110         entry.setHelpMessage( strHelpMessage );
111         entry.setComment( strComment );
112         entry.setCSSClass( strCSSClass );
113 
114         Field fieldAutocomplete = GenericAttributesUtils.createOrUpdateField( entry, FIELD_AUTOCOMPLETE, null, null );
115         if ( StringUtils.isNotBlank( strAutocomplete ) )
116         {
117             fieldAutocomplete.setValue( strAutocomplete.trim( ) );
118         }
119         Field fieldDefaultRegion = GenericAttributesUtils.createOrUpdateField( entry, FIELD_DEFAULT_REGION, null, null );
120         if ( StringUtils.isNotBlank( strDefaultRegion ) )
121         {
122             if ( !PhoneNumberUtil.getInstance( ).getSupportedRegions( ).contains( strDefaultRegion ) )
123             {
124                 return AdminMessageService.getMessageUrl( request, MESSAGE_UNKNOWN_DEFAULT_REGION, new Object [ ] {
125                         strDefaultRegion
126                 }, AdminMessage.TYPE_STOP );
127             }
128             fieldDefaultRegion.setValue( strDefaultRegion );
129         }
130         else
131         {
132             fieldDefaultRegion.setValue( getDefaultDefaultRegion( ) );
133         }
134 
135         GenericAttributesUtils.createOrUpdateField( entry, FIELD_PLACEHOLDER, null, strPlaceholder != null ? strPlaceholder : StringUtils.EMPTY );
136 
137         entry.setMandatory( strMandatory != null );
138         entry.setErrorMessage( strErrorMessage );
139         entry.setOnlyDisplayInBack( strOnlyDisplayInBack != null );
140         entry.setIndexed( strIndexed != null );
141 
142         return null;
143 
144     }
145 
146     @Override
147     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
148     {
149         String strValueEntry = StringUtils.defaultString( request.getParameter( PREFIX_ATTRIBUTE + entry.getIdEntry( ) ) ).trim( );
150         Response/genericattributes/business/Response.html#Response">Response response = new Response( );
151         response.setEntry( entry );
152         response.setIterationNumber( getResponseIterationValue( request ) );
153         listResponse.add( response );
154 
155         response.setResponseValue( strValueEntry );
156 
157         if ( StringUtils.isNotBlank( response.getResponseValue( ) ) )
158         {
159             PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance( );
160             PhoneNumber number;
161             try
162             {
163                 number = phoneUtil.parse( strValueEntry, entry.getFieldByCode( FIELD_DEFAULT_REGION ).getValue( ) );
164             }
165             catch( NumberParseException e )
166             {
167                 String strKeyErrorMessage;
168                 switch( e.getErrorType( ) )
169                 {
170                     case INVALID_COUNTRY_CODE:
171                     case NOT_A_NUMBER:
172                     case TOO_LONG:
173                     case TOO_SHORT_AFTER_IDD:
174                     case TOO_SHORT_NSN:
175                         strKeyErrorMessage = MESSAGE_ERROR_PHONENUMBER + "." + e.getErrorType( ).name( );
176                         break;
177                     default:
178                         strKeyErrorMessage = MESSAGE_ERROR_PHONENUMBER;
179                         break;
180                 }
181                 GenericAttributeErrortributes/business/GenericAttributeError.html#GenericAttributeError">GenericAttributeError error = new GenericAttributeError( );
182                 error.setMandatoryError( false );
183                 error.setTitleQuestion( entry.getTitle( ) );
184                 error.setErrorMessage( I18nService.getLocalizedString( strKeyErrorMessage, request.getLocale( ) ) );
185 
186                 return error;
187             }
188             if ( !phoneUtil.isValidNumber( number ) )
189             {
190                 GenericAttributeErrortributes/business/GenericAttributeError.html#GenericAttributeError">GenericAttributeError error = new GenericAttributeError( );
191                 error.setMandatoryError( false );
192                 error.setTitleQuestion( entry.getTitle( ) );
193                 error.setErrorMessage( I18nService.getLocalizedString( MESSAGE_ERROR_PHONENUMBER, request.getLocale( ) ) );
194 
195                 return error;
196             }
197 
198             response.setResponseValue( phoneUtil.format( number, PhoneNumberFormat.E164 ) ); // normalized
199                                                                                              // value
200             response.setToStringValueResponse( getResponseValueForRecap( entry, request, response, locale ) );
201         }
202         else
203         {
204             response.setToStringValueResponse( StringUtils.EMPTY );
205         }
206 
207         // Checks if the entry value contains XSS characters
208         if ( StringUtil.containsXssCharacters( strValueEntry ) )
209         {
210             GenericAttributeErrortributes/business/GenericAttributeError.html#GenericAttributeError">GenericAttributeError error = new GenericAttributeError( );
211             error.setMandatoryError( false );
212             error.setTitleQuestion( entry.getTitle( ) );
213             error.setErrorMessage( I18nService.getLocalizedString( MESSAGE_XSS_FIELD, request.getLocale( ) ) );
214 
215             return error;
216         }
217 
218         if ( entry.isMandatory( ) && StringUtils.isBlank( strValueEntry ) )
219         {
220             if ( StringUtils.isNotEmpty( entry.getErrorMessage( ) ) )
221             {
222                 GenericAttributeErrortributes/business/GenericAttributeError.html#GenericAttributeError">GenericAttributeError error = new GenericAttributeError( );
223                 error.setMandatoryError( true );
224                 error.setErrorMessage( entry.getErrorMessage( ) );
225 
226                 return error;
227             }
228 
229             return new MandatoryError( entry, locale );
230         }
231 
232         return null;
233 
234     }
235 
236     /**
237      * {@inheritDoc}
238      * 
239      * <p>
240      * Export the normalized value.
241      */
242     @Override
243     public String getResponseValueForExport( Entry entry, HttpServletRequest request, Response response, Locale locale )
244     {
245         // export the normalized value
246         return response.getResponseValue( );
247     }
248 
249     /**
250      * {@inheritDoc}
251      * 
252      * <p>
253      * If the phone number is in the default region, display in this region format. If not, display in international format.
254      */
255     @Override
256     public String getResponseValueForRecap( Entry entry, HttpServletRequest request, Response response, Locale locale )
257     {
258         PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance( );
259         PhoneNumber number;
260         try
261         {
262             number = phoneUtil.parse( response.getResponseValue( ), entry.getFieldByCode( FIELD_DEFAULT_REGION ).getValue( ) );
263             if ( "FR".equals( phoneUtil.getRegionCodeForNumber( number ) ) )
264             {
265                 return phoneUtil.format( number, PhoneNumberFormat.NATIONAL );
266             }
267             return phoneUtil.format( number, PhoneNumberFormat.INTERNATIONAL );
268         }
269         catch( NumberParseException e )
270         {
271             return null;
272         }
273     }
274 
275     /**
276      * Supported region codes for selection in admin interface
277      * 
278      * @return the support region codes set
279      */
280     public List<String> getSupportedRegionCodes( )
281     {
282         return PhoneNumberUtil.getInstance( ).getSupportedRegions( ).stream( ).sorted( ).collect( Collectors.toList( ) );
283     }
284 
285     /**
286      * Gets the default default region
287      * 
288      * @return the default default region
289      */
290     public String getDefaultDefaultRegion( )
291     {
292         return AppPropertiesService.getProperty( PROPERTY_DEFAULT_DEFAULT_REGION, "FR" );
293     }
294 }