Search in sources :

Example 6 with CommonExpressionVisitor

use of org.hisp.dhis.parser.expression.CommonExpressionVisitor in project dhis2-core by dhis2.

the class DefaultExpressionService method getExpressionValue.

// -------------------------------------------------------------------------
// Compute the value of the expression
// -------------------------------------------------------------------------
@Override
public Object getExpressionValue(ExpressionParams params) {
    if (isEmpty(params.getExpression())) {
        return null;
    }
    CommonExpressionVisitor visitor = newVisitor(ITEM_EVALUATE, params);
    Object value = visit(params.getExpression(), params.getDataType(), visitor, true);
    ExpressionState state = visitor.getState();
    int itemsFound = state.getItemsFound();
    int itemValuesFound = state.getItemValuesFound();
    if (state.isUnprotectedNullDateFound()) {
        return null;
    }
    switch(params.getMissingValueStrategy()) {
        case SKIP_IF_ANY_VALUE_MISSING:
            if (itemValuesFound < itemsFound) {
                return null;
            }
        case SKIP_IF_ALL_VALUES_MISSING:
            if (itemsFound != 0 && itemValuesFound == 0) {
                return null;
            }
        case NEVER_SKIP:
            if (value == null) {
                switch(params.getDataType()) {
                    case NUMERIC:
                        return 0d;
                    case BOOLEAN:
                        return FALSE;
                    case TEXT:
                        return "";
                }
            }
    }
    return value;
}
Also used : ExpressionState(org.hisp.dhis.parser.expression.ExpressionState) CommonExpressionVisitor(org.hisp.dhis.parser.expression.CommonExpressionVisitor) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject)

Example 7 with CommonExpressionVisitor

use of org.hisp.dhis.parser.expression.CommonExpressionVisitor in project dhis2-core by dhis2.

the class DefaultProgramIndicatorService method validate.

@Override
@Transactional(readOnly = true)
public void validate(String expression, Class<?> clazz, Map<String, String> itemDescriptions) {
    CommonExpressionVisitor visitor = newVisitor(ITEM_GET_DESCRIPTIONS, null);
    castClass(clazz, Parser.visit(expression, visitor));
    itemDescriptions.putAll(visitor.getItemDescriptions());
}
Also used : CommonExpressionVisitor(org.hisp.dhis.parser.expression.CommonExpressionVisitor) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

CommonExpressionVisitor (org.hisp.dhis.parser.expression.CommonExpressionVisitor)7 AntlrParserUtils.castString (org.hisp.dhis.antlr.AntlrParserUtils.castString)4 ProgramExpressionParams (org.hisp.dhis.parser.expression.ProgramExpressionParams)3 LinkedHashSet (java.util.LinkedHashSet)2 I18n (org.hisp.dhis.i18n.I18n)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)1 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)1 CachingMap (org.hisp.dhis.commons.collection.CachingMap)1 ExpressionState (org.hisp.dhis.parser.expression.ExpressionState)1 SqlLiteral (org.hisp.dhis.parser.expression.literal.SqlLiteral)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 Transactional (org.springframework.transaction.annotation.Transactional)1