ValidationErrorUtil.java

  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.util.beanvalidation;

  35. import java.util.Locale;
  36. import java.util.Map;
  37. import java.util.Map.Entry;

  38. import javax.validation.ConstraintViolation;

  39. import fr.paris.lutece.portal.service.i18n.I18nService;

  40. /**
  41.  * ValidationError Utils
  42.  */
  43. public final class ValidationErrorUtil
  44. {
  45.     /**
  46.      * Private constructor
  47.      */
  48.     private ValidationErrorUtil( )
  49.     {
  50.         // Nothing to do
  51.     }

  52.     /**
  53.      * Return the attribute's value to set as the value#1 of the message
  54.      *
  55.      * @param constraintViolation
  56.      *            The Constraint violation
  57.      * @param config
  58.      *            The config
  59.      * @return The value
  60.      */
  61.     public static String getValue1( ConstraintViolation constraintViolation, ValidationErrorConfig config )
  62.     {
  63.         return getValue( constraintViolation, config.getValue1Attributes( ) );
  64.     }

  65.     /**
  66.      * Return the attribute's value to set as the value#2 of the message
  67.      *
  68.      * @param constraintViolation
  69.      *            The Constraint violation
  70.      * @param config
  71.      *            The config
  72.      * @return The value
  73.      */
  74.     public static String getValue2( ConstraintViolation constraintViolation, ValidationErrorConfig config )
  75.     {
  76.         return getValue( constraintViolation, config.getValue2Attributes( ) );
  77.     }

  78.     /**
  79.      * Return the field name as it will appear in the message
  80.      *
  81.      * @param constraintViolation
  82.      *            The Constraint violation
  83.      * @param config
  84.      *            The config
  85.      * @param locale
  86.      *            The locale
  87.      * @return The field name
  88.      */
  89.     public static String getFieldname( ConstraintViolation constraintViolation, ValidationErrorConfig config, Locale locale )
  90.     {
  91.         String strField = constraintViolation.getPropertyPath( ).toString( );

  92.         // remove the variable prefix
  93.         String [ ] prefix = config.getVariablesPrefix( );

  94.         for ( int i = 0; i < prefix.length; i++ )
  95.         {
  96.             strField = removePrefix( strField, prefix [i] );
  97.         }

  98.         // set first letter in lower case
  99.         strField = strField.substring( 0, 1 ).toLowerCase( ) + strField.substring( 1 );

  100.         String strKey = config.getFieldKeysPrefix( ) + strField;

  101.         String strFieldName = I18nService.getLocalizedString( strKey, locale );

  102.         // if the key isn't found
  103.         if ( strFieldName.equals( "" ) )
  104.         {
  105.             // display the missing key as the field name
  106.             strFieldName = "[" + strKey + "]";
  107.         }

  108.         strFieldName = config.getFieldWrapperBegin( ) + strFieldName + config.getFieldWrapperEnd( );

  109.         return strFieldName;
  110.     }

  111.     /**
  112.      * Remove the variable prefix
  113.      *
  114.      * @param strSource
  115.      *            The source
  116.      * @param strPrefix
  117.      *            The prefix
  118.      * @return The string with the prefix removed
  119.      */
  120.     private static String removePrefix( String strSource, String strPrefix )
  121.     {
  122.         String strReturn = strSource;

  123.         if ( strSource.startsWith( strPrefix ) )
  124.         {
  125.             strReturn = strSource.substring( strPrefix.length( ) );
  126.         }

  127.         return strReturn;
  128.     }

  129.     /**
  130.      * Return the attribute's value to set as value the of the message
  131.      *
  132.      * @param constraintViolation
  133.      *            The Constraint violation
  134.      * @param strAttributes
  135.      *            The attributes names list
  136.      * @return The value
  137.      */
  138.     private static String getValue( ConstraintViolation constraintViolation, String strAttributes )
  139.     {
  140.         String strValue = "";

  141.         Map<String, Object> mapAttributes = constraintViolation.getConstraintDescriptor( ).getAttributes( );

  142.         for ( Entry<String, Object> entry : mapAttributes.entrySet( ) )
  143.         {
  144.             if ( strAttributes.contains( entry.getKey( ) ) )
  145.             {
  146.                 strValue = getValue( entry.getValue( ) );
  147.             }
  148.         }

  149.         return strValue;
  150.     }

  151.     /**
  152.      * Convert an unkown type value to a String value
  153.      *
  154.      * @param value
  155.      *            The valus
  156.      * @return The value as a String
  157.      */
  158.     private static String getValue( Object value )
  159.     {
  160.         if ( value instanceof Integer )
  161.         {
  162.             return Integer.toString( (Integer) value );
  163.         }

  164.         if ( value instanceof Long )
  165.         {
  166.             return Long.toString( (Long) value );
  167.         }

  168.         return (String) value;
  169.     }
  170.    
  171.     /**
  172.      * Return the attribute name as field identifier
  173.      *
  174.      * @param constraintViolation
  175.      *            The Constraint violation
  176.      * @param config
  177.      *            The config
  178.      * @param locale
  179.      *            The locale
  180.      * @return The field name
  181.      */
  182.     public static String getFieldId( ConstraintViolation constraintViolation, ValidationErrorConfig config, Locale locale )
  183.     {
  184.         String strField = constraintViolation.getPropertyPath( ).toString( );

  185.         // remove the variable prefix
  186.         String [ ] prefix = config.getVariablesPrefix( );

  187.         for ( int i = 0; i < prefix.length; i++ )
  188.         {
  189.             strField = removePrefix( strField, prefix [i] );
  190.         }

  191.         // set first letter in lower case
  192.         strField = strField.substring( 0, 1 ).toLowerCase( ) + strField.substring( 1 );


  193.         return strField;
  194.     }
  195. }