All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hisp.dhis.rules.gs1.GS1DataMatrixValueFormatter Maven / Gradle / Ivy

package org.hisp.dhis.rules.gs1;

import java.util.HashMap;
import java.util.Map;

public class GS1DataMatrixValueFormatter extends GS1BaseValueFormatter
{
    private Map dataMap;

    public String formatValue( String value, GS1Elements valueToReturn )
    {
        dataMap = new HashMap<>();
        String[] gs1Groups = removeGS1Identifier( value ).split( GS1Elements.GS1_GROUP_SEPARATOR.getElement() );
        for ( String gs1Group : gs1Groups )
        {
            handleGroupData( gs1Group );
        }
        if ( dataMap.containsKey( valueToReturn.getElement() ) )
        {
            return dataMap.get( valueToReturn.getElement() );
        }
        else
        {
            throw new IllegalArgumentException( "Required key does not exist for provided value" );
        }
    }

    private void handleGroupData( String gs1Group )
    {
        if ( !gs1Group.isEmpty() )
        {
            int gs1GroupLength = gs1Group.length();
            String ai = GS1Elements.getApplicationIdentifier( gs1Group );
            Integer nextValueLength = GS1Table.aiFixedLengthMap().get( ai.substring(0, 2) );
            if ( nextValueLength == null )
                nextValueLength = gs1GroupLength;
            dataMap.put( ai, gs1Group.substring( ai.length(), nextValueLength ) );
            handleGroupData( gs1Group.substring( nextValueLength ) );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy