Search in sources :

Example 1 with CommonExpressionVisitor

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

the class DefaultExpressionService method getExpressionDescription.

@Override
public String getExpressionDescription(String expression, ParseType parseType, DataType dataType) {
    if (isEmpty(expression)) {
        return "";
    }
    CommonExpressionVisitor visitor = newVisitor(ITEM_GET_DESCRIPTIONS, ExpressionParams.builder().expression(expression).parseType(parseType).dataType(dataType).missingValueStrategy(NEVER_SKIP).build());
    visit(expression, dataType, visitor, false);
    Map<String, String> itemDescriptions = visitor.getItemDescriptions();
    String description = expression;
    for (Map.Entry<String, String> entry : itemDescriptions.entrySet()) {
        description = description.replace(entry.getKey(), entry.getValue());
    }
    return description;
}
Also used : CommonExpressionVisitor(org.hisp.dhis.parser.expression.CommonExpressionVisitor) AntlrParserUtils.castString(org.hisp.dhis.antlr.AntlrParserUtils.castString) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) CachingMap(org.hisp.dhis.commons.collection.CachingMap)

Example 2 with CommonExpressionVisitor

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

the class ProgramSqlGeneratorFunctionsTest method test.

private Object test(String expression, AntlrExprLiteral exprLiteral, ExpressionItemMethod itemMethod) {
    Set<String> dataElementsAndAttributesIdentifiers = new LinkedHashSet<>();
    dataElementsAndAttributesIdentifiers.add(BASE_UID + "a");
    dataElementsAndAttributesIdentifiers.add(BASE_UID + "b");
    dataElementsAndAttributesIdentifiers.add(BASE_UID + "c");
    ProgramExpressionParams params = ProgramExpressionParams.builder().programIndicator(programIndicator).reportingStartDate(startDate).reportingEndDate(endDate).dataElementAndAttributeIdentifiers(dataElementsAndAttributesIdentifiers).build();
    CommonExpressionVisitor visitor = CommonExpressionVisitor.builder().idObjectManager(idObjectManager).dimensionService(dimensionService).programIndicatorService(programIndicatorService).programStageService(programStageService).statementBuilder(statementBuilder).i18n(new I18n(null, null)).itemMap(PROGRAM_INDICATOR_ITEMS).itemMethod(itemMethod).progParams(params).build();
    visitor.setExpressionLiteral(exprLiteral);
    return Parser.visit(expression, visitor);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProgramExpressionParams(org.hisp.dhis.parser.expression.ProgramExpressionParams) CommonExpressionVisitor(org.hisp.dhis.parser.expression.CommonExpressionVisitor) AntlrParserUtils.castString(org.hisp.dhis.antlr.AntlrParserUtils.castString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) I18n(org.hisp.dhis.i18n.I18n)

Example 3 with CommonExpressionVisitor

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

the class DefaultProgramIndicatorService method _getAnalyticsSql.

private String _getAnalyticsSql(String expression, ProgramIndicator programIndicator, Date startDate, Date endDate, String tableAlias) {
    Set<String> uids = getDataElementAndAttributeIdentifiers(expression, programIndicator.getAnalyticsType());
    ProgramExpressionParams params = ProgramExpressionParams.builder().programIndicator(programIndicator).reportingStartDate(startDate).reportingEndDate(endDate).dataElementAndAttributeIdentifiers(uids).build();
    CommonExpressionVisitor visitor = newVisitor(ITEM_GET_SQL, params);
    visitor.setExpressionLiteral(new SqlLiteral());
    String sql = castString(Parser.visit(expression, visitor));
    return (tableAlias != null ? sql.replaceAll(ANALYTICS_TBL_ALIAS + "\\.", tableAlias + "\\.") : sql);
}
Also used : ProgramExpressionParams(org.hisp.dhis.parser.expression.ProgramExpressionParams) CommonExpressionVisitor(org.hisp.dhis.parser.expression.CommonExpressionVisitor) AntlrParserUtils.castString(org.hisp.dhis.antlr.AntlrParserUtils.castString) SqlLiteral(org.hisp.dhis.parser.expression.literal.SqlLiteral)

Example 4 with CommonExpressionVisitor

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

the class ProgramSqlGeneratorItemsTest method test.

private Object test(String expression, AntlrExprLiteral exprLiteral, ExpressionItemMethod itemMethod) {
    Set<String> dataElementsAndAttributesIdentifiers = new LinkedHashSet<>();
    dataElementsAndAttributesIdentifiers.add(BASE_UID + "a");
    dataElementsAndAttributesIdentifiers.add(BASE_UID + "b");
    dataElementsAndAttributesIdentifiers.add(BASE_UID + "c");
    ProgramExpressionParams params = ProgramExpressionParams.builder().programIndicator(programIndicator).reportingStartDate(startDate).reportingEndDate(endDate).dataElementAndAttributeIdentifiers(dataElementsAndAttributesIdentifiers).build();
    CommonExpressionVisitor visitor = CommonExpressionVisitor.builder().idObjectManager(idObjectManager).dimensionService(dimensionService).programIndicatorService(programIndicatorService).programStageService(programStageService).statementBuilder(statementBuilder).i18n(new I18n(null, null)).constantMap(constantMap).itemMap(PROGRAM_INDICATOR_ITEMS).itemMethod(itemMethod).progParams(params).build();
    visitor.setExpressionLiteral(exprLiteral);
    return Parser.visit(expression, visitor);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProgramExpressionParams(org.hisp.dhis.parser.expression.ProgramExpressionParams) CommonExpressionVisitor(org.hisp.dhis.parser.expression.CommonExpressionVisitor) AntlrParserUtils.castString(org.hisp.dhis.antlr.AntlrParserUtils.castString) I18n(org.hisp.dhis.i18n.I18n)

Example 5 with CommonExpressionVisitor

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

the class DefaultExpressionService method getExpressionInfo.

/**
 * Gets the information that we need from an expression
 */
private ExpressionInfo getExpressionInfo(ExpressionParams params) {
    CommonExpressionVisitor visitor = newVisitor(ITEM_GET_EXPRESSION_INFO, params);
    visit(params.getExpression(), params.getDataType(), visitor, true);
    return visitor.getInfo();
}
Also used : CommonExpressionVisitor(org.hisp.dhis.parser.expression.CommonExpressionVisitor)

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