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());
}
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());
}
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());
}
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;
}
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());
}
Aggregations