Search in sources :

Example 11 with Constant

use of org.hisp.dhis.constant.Constant in project dhis2-core by dhis2.

the class DefaultProgramIndicatorService method getExpressionDescription.

@Override
@Transactional
public String getExpressionDescription(String expression) {
    if (expression == null) {
        return null;
    }
    I18n i18n = i18nManager.getI18n();
    StringBuffer description = new StringBuffer();
    Matcher matcher = ProgramIndicator.EXPRESSION_PATTERN.matcher(expression);
    while (matcher.find()) {
        String key = matcher.group(1);
        String uid = matcher.group(2);
        if (ProgramIndicator.KEY_DATAELEMENT.equals(key)) {
            String de = matcher.group(3);
            ProgramStage programStage = programStageService.getProgramStage(uid);
            DataElement dataElement = dataElementService.getDataElement(de);
            if (programStage != null && dataElement != null) {
                String programStageName = programStage.getDisplayName();
                String dataelementName = dataElement.getDisplayName();
                matcher.appendReplacement(description, programStageName + ProgramIndicator.SEPARATOR_ID + dataelementName);
            }
        } else if (ProgramIndicator.KEY_ATTRIBUTE.equals(key)) {
            TrackedEntityAttribute attribute = attributeService.getTrackedEntityAttribute(uid);
            if (attribute != null) {
                matcher.appendReplacement(description, attribute.getDisplayName());
            }
        } else if (ProgramIndicator.KEY_CONSTANT.equals(key)) {
            Constant constant = constantService.getConstant(uid);
            if (constant != null) {
                matcher.appendReplacement(description, constant.getDisplayName());
            }
        } else if (ProgramIndicator.KEY_PROGRAM_VARIABLE.equals(key)) {
            String varName = i18n.getString(uid);
            if (varName != null) {
                matcher.appendReplacement(description, varName);
            }
        }
    }
    matcher.appendTail(description);
    return description.toString();
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Matcher(java.util.regex.Matcher) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Constant(org.hisp.dhis.constant.Constant) I18n(org.hisp.dhis.i18n.I18n) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with Constant

use of org.hisp.dhis.constant.Constant in project dhis2-core by dhis2.

the class DefaultExpressionService method getExpressionDescription.

@Override
@Transactional
public String getExpressionDescription(String expression) {
    if (expression == null || expression.isEmpty()) {
        return null;
    }
    // ---------------------------------------------------------------------
    // Operands
    // ---------------------------------------------------------------------
    StringBuffer sb = new StringBuffer();
    Matcher matcher = VARIABLE_PATTERN.matcher(expression);
    while (matcher.find()) {
        String dimensionItem = matcher.group(GROUP_ID);
        DimensionalItemObject dimensionItemObject = dimensionService.getDataDimensionalItemObject(dimensionItem);
        if (dimensionItemObject == null) {
            throw new InvalidIdentifierReferenceException("Identifier does not reference a dimensional item object: " + dimensionItem);
        }
        matcher.appendReplacement(sb, Matcher.quoteReplacement(dimensionItemObject.getDisplayName()));
    }
    expression = TextUtils.appendTail(matcher, sb);
    // ---------------------------------------------------------------------
    // Constants
    // ---------------------------------------------------------------------
    sb = new StringBuffer();
    matcher = CONSTANT_PATTERN.matcher(expression);
    while (matcher.find()) {
        String co = matcher.group(GROUP_ID);
        Constant constant = constantService.getConstant(co);
        if (constant == null) {
            throw new InvalidIdentifierReferenceException("Identifier does not reference a constant: " + co);
        }
        matcher.appendReplacement(sb, Matcher.quoteReplacement(constant.getDisplayName()));
    }
    expression = TextUtils.appendTail(matcher, sb);
    // ---------------------------------------------------------------------
    // Org unit groups
    // ---------------------------------------------------------------------
    sb = new StringBuffer();
    matcher = OU_GROUP_PATTERN.matcher(expression);
    while (matcher.find()) {
        String oug = matcher.group(GROUP_ID);
        OrganisationUnitGroup group = organisationUnitGroupService.getOrganisationUnitGroup(oug);
        if (group == null) {
            throw new InvalidIdentifierReferenceException("Identifier does not reference an organisation unit group: " + oug);
        }
        matcher.appendReplacement(sb, Matcher.quoteReplacement(group.getDisplayName()));
    }
    expression = TextUtils.appendTail(matcher, sb);
    // ---------------------------------------------------------------------
    // Days
    // ---------------------------------------------------------------------
    sb = new StringBuffer();
    matcher = DAYS_PATTERN.matcher(expression);
    while (matcher.find()) {
        matcher.appendReplacement(sb, DAYS_DESCRIPTION);
    }
    expression = TextUtils.appendTail(matcher, sb);
    return expression;
}
Also used : OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Matcher(java.util.regex.Matcher) Constant(org.hisp.dhis.constant.Constant) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with Constant

use of org.hisp.dhis.constant.Constant in project dhis2-core by dhis2.

the class DefaultExpressionService method substituteExpressions.

@Override
@Transactional
public void substituteExpressions(Collection<Indicator> indicators, Integer days) {
    if (indicators != null && !indicators.isEmpty()) {
        Map<String, Constant> constants = new CachingMap<String, Constant>().load(idObjectManager.getAllNoAcl(Constant.class), c -> c.getUid());
        Map<String, OrganisationUnitGroup> orgUnitGroups = new CachingMap<String, OrganisationUnitGroup>().load(idObjectManager.getAllNoAcl(OrganisationUnitGroup.class), g -> g.getUid());
        for (Indicator indicator : indicators) {
            indicator.setExplodedNumerator(substituteExpression(indicator.getNumerator(), constants, orgUnitGroups, days));
            indicator.setExplodedDenominator(substituteExpression(indicator.getDenominator(), constants, orgUnitGroups, days));
        }
    }
}
Also used : OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) Constant(org.hisp.dhis.constant.Constant) Indicator(org.hisp.dhis.indicator.Indicator) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Constant (org.hisp.dhis.constant.Constant)13 Matcher (java.util.regex.Matcher)8 DataElement (org.hisp.dhis.dataelement.DataElement)5 OrganisationUnitGroup (org.hisp.dhis.organisationunit.OrganisationUnitGroup)5 Transactional (org.springframework.transaction.annotation.Transactional)5 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)3 Indicator (org.hisp.dhis.indicator.Indicator)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)2 InvalidIdentifierReferenceException (org.hisp.dhis.common.exception.InvalidIdentifierReferenceException)2 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 Period (org.hisp.dhis.period.Period)2 ProgramDataElementDimensionItem (org.hisp.dhis.program.ProgramDataElementDimensionItem)2 Sets (com.google.common.collect.Sets)1 Collection (java.util.Collection)1 List (java.util.List)1 Set (java.util.Set)1 Function (java.util.function.Function)1