Search in sources :

Example 1 with ParserException

use of org.hisp.dhis.antlr.ParserException in project dhis2-core by dhis2.

the class DataIntegrityServiceTest method testInvalidProgramIndicatorExpression.

@Test
void testInvalidProgramIndicatorExpression() {
    ProgramIndicator programIndicator = new ProgramIndicator();
    programIndicator.setName("Test-PI");
    programIndicator.setExpression("A{someuid} + 1");
    when(programIndicatorService.expressionIsValid(anyString())).thenReturn(false);
    when(programIndicatorService.getAllProgramIndicators()).thenReturn(List.of(programIndicator));
    when(expressionService.getExpressionDescription(anyString(), any())).thenThrow(new ParserException(INVALID_EXPRESSION));
    List<DataIntegrityIssue> issues = subject.getInvalidProgramIndicatorExpressions();
    assertNotNull(issues);
    assertEquals(1, issues.size());
    DataIntegrityIssue issue = issues.get(0);
    assertEquals(issueName(programIndicator), issue.getName());
    assertEquals(INVALID_EXPRESSION, issue.getComment());
}
Also used : ParserException(org.hisp.dhis.antlr.ParserException) DataIntegrityIssue(org.hisp.dhis.dataintegrity.DataIntegrityDetails.DataIntegrityIssue) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) Test(org.junit.jupiter.api.Test)

Example 2 with ParserException

use of org.hisp.dhis.antlr.ParserException in project dhis2-core by dhis2.

the class ExpressionServiceTest method eval.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
 * Evaluates a test expression, against getExpressionDimensionalItemObjects
 * and getExpressionValue. Returns a string containing first the returned
 * value from getExpressionValue, and then the items returned from
 * getExpressionDimensionalItemObjects, if any, separated by spaces.
 *
 * @param expr expression to evaluate
 * @param parseType type of expression to parse
 * @param missingValueStrategy strategy to use if item value is missing
 * @param dataType data type that the expression should return
 * @return result from testing the expression
 */
private String eval(String expr, ParseType parseType, MissingValueStrategy missingValueStrategy, DataType dataType) {
    try {
        expressionService.getExpressionDescription(expr, parseType, dataType);
    } catch (ParserException ex) {
        return ex.getMessage();
    }
    Map<DimensionalItemId, DimensionalItemObject> itemMap = new HashMap<>();
    expressionService.getExpressionDimensionalItemMaps(expr, parseType, dataType, itemMap, itemMap);
    Object value = expressionService.getExpressionValue(ExpressionParams.builder().expression(expr).parseType(parseType).dataType(dataType).itemMap(itemMap).valueMap(valueMap).orgUnitCountMap(ORG_UNIT_COUNT_MAP).days(DAYS).missingValueStrategy(missingValueStrategy).samplePeriods(TEST_SAMPLE_PERIODS).periodValueMap(samples).build());
    return result(value, itemMap.values());
}
Also used : ParserException(org.hisp.dhis.antlr.ParserException) DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) HashMap(java.util.HashMap) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 3 with ParserException

use of org.hisp.dhis.antlr.ParserException in project dhis2-core by dhis2.

the class ProgramItemStageElement method getDescription.

@Override
public Object getDescription(ExprContext ctx, CommonExpressionVisitor visitor) {
    assumeStageElementSyntax(ctx);
    String programStageId = ctx.uid0.getText();
    String dataElementId = ctx.uid1.getText();
    ProgramStageService stageService = visitor.getProgramStageService();
    ProgramStage programStage = stageService.getProgramStage(programStageId);
    DataElement dataElement = visitor.getIdObjectManager().get(DataElement.class, dataElementId);
    if (programStage == null) {
        throw new ParserExceptionWithoutContext("Program stage " + programStageId + " not found");
    }
    if (dataElement == null) {
        throw new ParserExceptionWithoutContext("Data element " + dataElementId + " not found");
    }
    if (isNonDefaultStageOffset(visitor.getState().getStageOffset()) && !isRepeatableStage(stageService, programStageId)) {
        throw new ParserException(getErrorMessage(programStageId));
    }
    String description = programStage.getDisplayName() + ProgramIndicator.SEPARATOR_ID + dataElement.getDisplayName();
    visitor.getItemDescriptions().put(ctx.getText(), description);
    return ValidationUtils.getNullReplacementValue(dataElement.getValueType());
}
Also used : ParserExceptionWithoutContext(org.hisp.dhis.antlr.ParserExceptionWithoutContext) DataElement(org.hisp.dhis.dataelement.DataElement) ParserException(org.hisp.dhis.antlr.ParserException) ProgramStageService(org.hisp.dhis.program.ProgramStageService) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 4 with ParserException

use of org.hisp.dhis.antlr.ParserException in project dhis2-core by dhis2.

the class ProgramItemStageElement method getSql.

@Override
public Object getSql(ExprContext ctx, CommonExpressionVisitor visitor) {
    assumeStageElementSyntax(ctx);
    String programStageId = ctx.uid0.getText();
    String dataElementId = ctx.uid1.getText();
    ProgramExpressionParams params = visitor.getProgParams();
    int stageOffset = visitor.getState().getStageOffset();
    String column;
    if (isNonDefaultStageOffset(stageOffset)) {
        if (isRepeatableStage(visitor.getProgramStageService(), programStageId)) {
            column = visitor.getStatementBuilder().getProgramIndicatorEventColumnSql(programStageId, Integer.valueOf(stageOffset).toString(), SqlUtils.quote(dataElementId), params.getReportingStartDate(), params.getReportingEndDate(), params.getProgramIndicator());
        } else {
            throw new ParserException(getErrorMessage(programStageId));
        }
    } else {
        column = visitor.getStatementBuilder().getProgramIndicatorDataValueSelectSql(programStageId, dataElementId, params.getReportingStartDate(), params.getReportingEndDate(), params.getProgramIndicator());
    }
    if (visitor.getState().isReplaceNulls()) {
        DataElement dataElement = visitor.getIdObjectManager().get(DataElement.class, dataElementId);
        if (dataElement == null) {
            throw new ParserExceptionWithoutContext("Data element " + dataElementId + " not found during SQL generation.");
        }
        column = replaceNullSqlValues(column, dataElement.getValueType());
    }
    return column;
}
Also used : ParserExceptionWithoutContext(org.hisp.dhis.antlr.ParserExceptionWithoutContext) ProgramExpressionParams(org.hisp.dhis.parser.expression.ProgramExpressionParams) ParserException(org.hisp.dhis.antlr.ParserException) DataElement(org.hisp.dhis.dataelement.DataElement)

Example 5 with ParserException

use of org.hisp.dhis.antlr.ParserException in project dhis2-core by dhis2.

the class DataIntegrityServiceTest method testInvalidProgramIndicatorFilter.

@Test
void testInvalidProgramIndicatorFilter() {
    ProgramIndicator programIndicator = new ProgramIndicator();
    programIndicator.setName("Test-PI");
    programIndicator.setFilter("A{someuid} + 1");
    when(programIndicatorService.filterIsValid(anyString())).thenReturn(false);
    when(programIndicatorService.getAllProgramIndicators()).thenReturn(List.of(programIndicator));
    when(expressionService.getExpressionDescription(anyString(), any())).thenThrow(new ParserException(INVALID_EXPRESSION));
    List<DataIntegrityIssue> issues = subject.getInvalidProgramIndicatorFilters();
    assertEquals(1, issues.size(), 1);
    DataIntegrityIssue issue = issues.get(0);
    assertEquals(issueName(programIndicator), issue.getName());
    assertEquals(INVALID_EXPRESSION, issue.getComment());
}
Also used : ParserException(org.hisp.dhis.antlr.ParserException) DataIntegrityIssue(org.hisp.dhis.dataintegrity.DataIntegrityDetails.DataIntegrityIssue) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) Test(org.junit.jupiter.api.Test)

Aggregations

ParserException (org.hisp.dhis.antlr.ParserException)6 ParserExceptionWithoutContext (org.hisp.dhis.antlr.ParserExceptionWithoutContext)2 DataElement (org.hisp.dhis.dataelement.DataElement)2 DataIntegrityIssue (org.hisp.dhis.dataintegrity.DataIntegrityDetails.DataIntegrityIssue)2 ProgramIndicator (org.hisp.dhis.program.ProgramIndicator)2 Test (org.junit.jupiter.api.Test)2 HashMap (java.util.HashMap)1 AntlrParserUtils.castString (org.hisp.dhis.antlr.AntlrParserUtils.castString)1 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)1 DimensionalItemId (org.hisp.dhis.common.DimensionalItemId)1 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)1 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)1 ProgramExpressionParams (org.hisp.dhis.parser.expression.ProgramExpressionParams)1 ProgramStage (org.hisp.dhis.program.ProgramStage)1 ProgramStageService (org.hisp.dhis.program.ProgramStageService)1 Transactional (org.springframework.transaction.annotation.Transactional)1