IdentityAttributeComparatorService.java

package fr.paris.lutece.plugins.identitystore.service.attribute;

import fr.paris.lutece.plugins.identitystore.v3.web.rs.util.Constants;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

public class IdentityAttributeComparatorService {

    private static IdentityAttributeComparatorService _instance;

    public static IdentityAttributeComparatorService instance( )
    {
        if( _instance == null )
        {
            _instance = new IdentityAttributeComparatorService( );
        }
        return _instance;
    }

    public boolean attributeValuesAreEqualAccordingToIndexMapping( final String key, final String attributeValue, final String searchedValue )
    {
        if( key != null )
        {
            final String value = this.defaultValueFormatter( attributeValue );
            final String searched = this.defaultValueFormatter( searchedValue );
            switch ( key )
            {
                case Constants.PARAM_FIRST_NAME:
                    return Objects.equals( this.formatFirstName( value ), this.formatFirstName( searched ) );
                case Constants.PARAM_FAMILY_NAME:
                case Constants.PARAM_PREFERRED_USERNAME:
                    return Objects.equals( this.formatLastName( value ), this.formatLastName( searched ) );
                case Constants.PARAM_BIRTH_PLACE:
                case Constants.PARAM_BIRTH_COUNTRY:
                    return Objects.equals( this.formatPlace( value ), this.formatPlace( searched ) );
                case Constants.PARAM_BIRTH_DATE:
                    return Objects.equals(this.formatBirthDate( value ), this.formatBirthDate( searched ) );
                case Constants.PARAM_ID_CUSTOMER:
                case Constants.PARAM_ID_CONNECTION:
                    return Objects.equals(this.formatId( value ), this.formatId( searched ) );
                default:
                    return Objects.equals( value, searched );
            }
        }
        return false;
    }

    public String formatFirstName( final String value )
    {
        return this.replaceBlanksWithBlank( this.replaceComasWithBlank( value ) );
    }

    public String formatLastName( final String value )
    {
        return this.replaceBlanksWithBlank( this.replaceComasWithBlank( this.replaceHyphensWithBlank( value ) ) );
    }

    public String formatPlace( final String value )
    {
        return this.replaceBlanksWithBlank( this.replaceComasWithBlank( this.replaceHyphensWithBlank( value ) ) );
    }

    public String formatBirthDate( final String value )
    {
        return value != null ? value.trim( ) : null;
    }

    public String formatId( final String value )
    {
        return value != null ? value.trim( ).toLowerCase( ) : null;
    }

    public String defaultValueFormatter( final String value )
    {
        return StringUtils.stripAccents( value != null ? value.toLowerCase( ).trim( ) : null );
    }

    public String replaceBlanksWithBlank( final String value )
    {
        return value != null ? RegExUtils.replaceAll( value, "( +)", " " ) : null;
    }

    public String replaceComasWithBlank( final String value )
    {
        return value != null ? RegExUtils.replaceAll( value, "(,+)", " " ) : null;
    }

    public String replaceHyphensWithBlank( final String value )
    {
        return value != null ? RegExUtils.replaceAll( value, "(-+)", " " ) : null;
    }

    public String replaceHyphensWithHyphen( final String value )
    {
        return value != null ? RegExUtils.replaceAll( value, "(-+)", "-" ) : null;
    }
}