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

org.hisp.dhis.rules.Utils Maven / Gradle / Ivy

package org.hisp.dhis.rules;

import org.hisp.dhis.rules.models.RuleDataValue;
import org.hisp.dhis.rules.models.RuleEvent;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Nonnull;

public final class Utils
{

    public static final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd", Locale.US );
    static final String VARIABLE_PATTERN = "[#]\\{([\\w -_.]+)\\}";
    static final Pattern VARIABLE_PATTERN_COMPILED = Pattern.compile( VARIABLE_PATTERN );

    private Utils()
    {
        // no instances
    }

    public static List values( List ruleDataValues )
    {
        List values = new ArrayList<>( ruleDataValues.size() );
        for ( RuleDataValue ruleDataValue : ruleDataValues )
        {
            values.add( ruleDataValue.value() );
        }
        return Collections.unmodifiableList( values );
    }

    public static String getLastUpdateDateForPrevious( List ruleDataValues,
        RuleEvent ruleEvent )
    {
        List dates = new ArrayList<>();
        for ( RuleDataValue date : ruleDataValues )
        {
            Date d = date.eventDate();
            if ( d.before( ruleEvent.eventDate() ) )
            {
                dates.add( d );
            }
        }

        return dateFormat.format( Collections.max( dates ) );
    }

    public static String getLastUpdateDate( List ruleDataValues )
    {
        List dates = new ArrayList<>();
        for ( RuleDataValue date : ruleDataValues )
        {
            Date d = date.eventDate();
            dates.add( d );
        }

        return dateFormat.format( Collections.max( dates ) );
    }

    @Nonnull
    static String unwrapVariableName( @Nonnull String variable )
    {
        Matcher variableNameMatcher = VARIABLE_PATTERN_COMPILED.matcher( variable );

        // extract variable name
        if ( variableNameMatcher.find() )
        {
            return variableNameMatcher.group( 1 );
        }

        throw new IllegalArgumentException( "Malformed variable: " + variable );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy