Search in sources :

Example 6 with Constant

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

the class ProgramIndicatorServiceTest method setUpTest.

@Override
public void setUpTest() {
    OrganisationUnit organisationUnit = createOrganisationUnit('A');
    organisationUnitService.addOrganisationUnit(organisationUnit);
    // ---------------------------------------------------------------------
    // Program
    // ---------------------------------------------------------------------
    programA = createProgram('A', new HashSet<>(), organisationUnit);
    programService.addProgram(programA);
    psA = new ProgramStage("StageA", programA);
    psA.setSortOrder(1);
    programStageService.saveProgramStage(psA);
    psB = new ProgramStage("StageB", programA);
    psB.setSortOrder(2);
    programStageService.saveProgramStage(psB);
    Set<ProgramStage> programStages = new HashSet<>();
    programStages.add(psA);
    programStages.add(psB);
    programA.setProgramStages(programStages);
    programService.updateProgram(programA);
    programB = createProgram('B', new HashSet<>(), organisationUnit);
    programService.addProgram(programB);
    // ---------------------------------------------------------------------
    // Program Stage DE
    // ---------------------------------------------------------------------
    deA = createDataElement('A');
    deA.setDomainType(DataElementDomain.TRACKER);
    deB = createDataElement('B');
    deB.setDomainType(DataElementDomain.TRACKER);
    dataElementService.addDataElement(deA);
    dataElementService.addDataElement(deB);
    ProgramStageDataElement stageDataElementA = new ProgramStageDataElement(psA, deA, false, 1);
    ProgramStageDataElement stageDataElementB = new ProgramStageDataElement(psA, deB, false, 2);
    ProgramStageDataElement stageDataElementC = new ProgramStageDataElement(psB, deA, false, 1);
    ProgramStageDataElement stageDataElementD = new ProgramStageDataElement(psB, deB, false, 2);
    programStageDataElementService.addProgramStageDataElement(stageDataElementA);
    programStageDataElementService.addProgramStageDataElement(stageDataElementB);
    programStageDataElementService.addProgramStageDataElement(stageDataElementC);
    programStageDataElementService.addProgramStageDataElement(stageDataElementD);
    // ---------------------------------------------------------------------
    // TrackedEntityInstance & Enrollment
    // ---------------------------------------------------------------------
    TrackedEntityInstance entityInstance = createTrackedEntityInstance('A', organisationUnit);
    entityInstanceService.addTrackedEntityInstance(entityInstance);
    incidentDate = DateUtils.getMediumDate("2014-10-22");
    enrollmentDate = DateUtils.getMediumDate("2014-12-31");
    programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, programA, enrollmentDate, incidentDate, organisationUnit);
    incidentDate = DateUtils.getMediumDate("2014-10-22");
    enrollmentDate = DateUtils.getMediumDate("2014-12-31");
    programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, programA, enrollmentDate, incidentDate, organisationUnit);
    // TODO enroll twice?
    // ---------------------------------------------------------------------
    // TrackedEntityAttribute
    // ---------------------------------------------------------------------
    atA = createTrackedEntityAttribute('A', ValueType.NUMBER);
    atB = createTrackedEntityAttribute('B', ValueType.NUMBER);
    attributeService.addTrackedEntityAttribute(atA);
    attributeService.addTrackedEntityAttribute(atB);
    TrackedEntityAttributeValue attributeValueA = new TrackedEntityAttributeValue(atA, entityInstance, "1");
    TrackedEntityAttributeValue attributeValueB = new TrackedEntityAttributeValue(atB, entityInstance, "2");
    attributeValueService.addTrackedEntityAttributeValue(attributeValueA);
    attributeValueService.addTrackedEntityAttributeValue(attributeValueB);
    // ---------------------------------------------------------------------
    // TrackedEntityDataValue
    // ---------------------------------------------------------------------
    ProgramStageInstance stageInstanceA = programStageInstanceService.createProgramStageInstance(programInstance, psA, enrollmentDate, incidentDate, organisationUnit);
    ProgramStageInstance stageInstanceB = programStageInstanceService.createProgramStageInstance(programInstance, psB, enrollmentDate, incidentDate, organisationUnit);
    Set<ProgramStageInstance> programStageInstances = new HashSet<>();
    programStageInstances.add(stageInstanceA);
    programStageInstances.add(stageInstanceB);
    programInstance.setProgramStageInstances(programStageInstances);
    programInstance.setProgram(programA);
    TrackedEntityDataValue dataValueA = new TrackedEntityDataValue(stageInstanceA, deA, "3");
    TrackedEntityDataValue dataValueB = new TrackedEntityDataValue(stageInstanceA, deB, "1");
    TrackedEntityDataValue dataValueC = new TrackedEntityDataValue(stageInstanceB, deA, "5");
    TrackedEntityDataValue dataValueD = new TrackedEntityDataValue(stageInstanceB, deB, "7");
    dataValueService.saveTrackedEntityDataValue(dataValueA);
    dataValueService.saveTrackedEntityDataValue(dataValueB);
    dataValueService.saveTrackedEntityDataValue(dataValueC);
    dataValueService.saveTrackedEntityDataValue(dataValueD);
    // ---------------------------------------------------------------------
    // Constant
    // ---------------------------------------------------------------------
    Constant constantA = createConstant('A', 7.0);
    constantService.saveConstant(constantA);
    // ---------------------------------------------------------------------
    // ProgramIndicator
    // ---------------------------------------------------------------------
    String expressionA = "( d2:daysBetween(" + KEY_PROGRAM_VARIABLE + "{" + ProgramIndicator.VAR_ENROLLMENT_DATE + "}, " + KEY_PROGRAM_VARIABLE + "{" + ProgramIndicator.VAR_INCIDENT_DATE + "}) )  / " + ProgramIndicator.KEY_CONSTANT + "{" + constantA.getUid() + "}";
    indicatorA = createProgramIndicator('A', programA, expressionA, null);
    programA.getProgramIndicators().add(indicatorA);
    indicatorB = createProgramIndicator('B', programA, "70", null);
    programA.getProgramIndicators().add(indicatorB);
    indicatorC = createProgramIndicator('C', programA, "0", null);
    programA.getProgramIndicators().add(indicatorC);
    String expressionD = "0 + A + 4 + " + ProgramIndicator.KEY_PROGRAM_VARIABLE + "{" + ProgramIndicator.VAR_INCIDENT_DATE + "}";
    indicatorD = createProgramIndicator('D', programB, expressionD, null);
    String expressionE = KEY_DATAELEMENT + "{" + psA.getUid() + "." + deA.getUid() + "} + " + KEY_DATAELEMENT + "{" + psB.getUid() + "." + deA.getUid() + "} - " + KEY_ATTRIBUTE + "{" + atA.getUid() + "} + " + KEY_ATTRIBUTE + "{" + atB.getUid() + "}";
    String filterE = KEY_DATAELEMENT + "{" + psA.getUid() + "." + deA.getUid() + "} + " + KEY_ATTRIBUTE + "{" + atA.getUid() + "} > 10";
    indicatorE = createProgramIndicator('E', programB, expressionE, filterE);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Constant(org.hisp.dhis.constant.Constant) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) HashSet(java.util.HashSet)

Example 7 with Constant

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

the class DhisConvenienceTest method createConstant.

/**
     * @param uniqueCharacter A unique character to identify the object.
     * @param value           The value for constant
     * @return a constant instance
     */
protected static Constant createConstant(char uniqueCharacter, double value) {
    Constant constant = new Constant();
    constant.setAutoFields();
    constant.setName("Constant" + uniqueCharacter);
    constant.setValue(value);
    return constant;
}
Also used : Constant(org.hisp.dhis.constant.Constant)

Example 8 with Constant

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

the class DefaultProgramIndicatorService method getSubstitutedElementsAnalyticsSql.

private String getSubstitutedElementsAnalyticsSql(String expression, boolean ignoreMissingValues, AnalyticsType analyticsType) {
    if (expression == null) {
        return null;
    }
    StringBuffer buffer = new StringBuffer();
    Matcher matcher = ProgramIndicator.EXPRESSION_EQUALSZEROOREMPTY_PATTERN.matcher(expression);
    while (matcher.find()) {
        String key = matcher.group(1);
        String el1 = matcher.group(2);
        String el2 = matcher.group(3);
        boolean equalsZero = matcher.group(4) != null && matcher.group(4).matches(ProgramIndicator.EQUALSZERO);
        boolean equalsEmpty = matcher.group(4) != null && matcher.group(4).matches(ProgramIndicator.EQUALSEMPTY);
        if (ProgramIndicator.KEY_DATAELEMENT.equals(key) || ProgramIndicator.KEY_ATTRIBUTE.equals(key)) {
            String columnName;
            if (ProgramIndicator.KEY_DATAELEMENT.equals(key)) {
                columnName = AnalyticsType.ENROLLMENT == analyticsType ? statementBuilder.columnQuote(el1 + ProgramIndicator.DB_SEPARATOR_ID + el2) : statementBuilder.columnQuote(el2);
            } else // ProgramIndicator.KEY_ATTRIBUTE
            {
                columnName = statementBuilder.columnQuote(el1);
            }
            if (equalsZero) {
                columnName = getNumericIgnoreNullSql(columnName) + " == 0 ";
            } else if (equalsEmpty) {
                columnName = getTextIgnoreNullSql(columnName) + " == '' ";
            } else if (ignoreMissingValues) {
                columnName = getNumericIgnoreNullSql(columnName);
            }
            matcher.appendReplacement(buffer, columnName);
        } else if (ProgramIndicator.KEY_CONSTANT.equals(key)) {
            Constant constant = constantService.getConstant(el1);
            if (constant != null) {
                matcher.appendReplacement(buffer, String.valueOf(constant.getValue()));
            }
        }
    }
    return TextUtils.appendTail(matcher, buffer);
}
Also used : Matcher(java.util.regex.Matcher) Constant(org.hisp.dhis.constant.Constant)

Example 9 with Constant

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

the class DefaultProgramIndicatorService method getSubstitutedExpression.

/**
     * Generates an expression where all items are substituted with a sample value
     * in order to maintain a valid expression syntax.
     *
     * @param expression the expression.
     */
private String getSubstitutedExpression(String expression) {
    StringBuffer expr = 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 sample = ValidationUtils.getSubstitutionValue(dataElement.getValueType());
                matcher.appendReplacement(expr, sample);
            } else {
                return ProgramIndicator.INVALID_IDENTIFIERS_IN_EXPRESSION;
            }
        } else if (ProgramIndicator.KEY_ATTRIBUTE.equals(key)) {
            TrackedEntityAttribute attribute = attributeService.getTrackedEntityAttribute(uid);
            if (attribute != null) {
                String sample = ValidationUtils.getSubstitutionValue(attribute.getValueType());
                matcher.appendReplacement(expr, sample);
            } else {
                return ProgramIndicator.INVALID_IDENTIFIERS_IN_EXPRESSION;
            }
        } else if (ProgramIndicator.KEY_CONSTANT.equals(key)) {
            Constant constant = constantService.getConstant(uid);
            if (constant != null) {
                matcher.appendReplacement(expr, String.valueOf(constant.getValue()));
            } else {
                return ProgramIndicator.INVALID_IDENTIFIERS_IN_EXPRESSION;
            }
        } else if (ProgramIndicator.KEY_PROGRAM_VARIABLE.equals(key)) {
            String sampleValue = VARIABLE_SAMPLE_VALUE_MAP.get(uid);
            if (sampleValue != null) {
                matcher.appendReplacement(expr, sampleValue);
            } else {
                return ProgramIndicator.UNKNOWN_VARIABLE;
            }
        }
    }
    matcher.appendTail(expr);
    return expr.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)

Example 10 with Constant

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

the class ExpressionUpgrader method upgradeExpression.

private String upgradeExpression(String expression) {
    if (expression == null || expression.trim().isEmpty()) {
        return null;
    }
    boolean changes = false;
    StringBuffer sb = new StringBuffer();
    try {
        // -----------------------------------------------------------------
        // Constants
        // -----------------------------------------------------------------
        Matcher matcher = OLD_CONSTANT_PATTERN.matcher(expression);
        while (matcher.find()) {
            Constant constant = constantService.getConstant(Integer.parseInt(matcher.group(1)));
            String replacement = "C{" + constant.getUid() + "}";
            matcher.appendReplacement(sb, replacement);
            changes = true;
        }
        matcher.appendTail(sb);
        expression = sb.toString();
        // -----------------------------------------------------------------
        // Operands
        // -----------------------------------------------------------------
        matcher = OLD_OPERAND_PATTERN.matcher(expression);
        sb = new StringBuffer();
        while (matcher.find()) {
            DataElement de = dataElementService.getDataElement(Integer.parseInt(matcher.group(1)));
            String replacement = "#{" + de.getUid();
            if (matcher.groupCount() == 2 && matcher.group(2) != null && !matcher.group(2).trim().isEmpty()) {
                DataElementCategoryOptionCombo coc = categoryService.getDataElementCategoryOptionCombo(Integer.parseInt(matcher.group(2)));
                replacement += "." + coc.getUid();
            }
            replacement += "}";
            matcher.appendReplacement(sb, replacement);
            changes = true;
        }
        matcher.appendTail(sb);
        expression = sb.toString();
    } catch (Exception ex) {
        log.error("Failed to upgrade expression: " + expression);
        // Log and continue
        log.error(ex);
    }
    if (changes) {
        log.info("Upgraded expression: " + expression);
    }
    return changes ? expression : null;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Matcher(java.util.regex.Matcher) Constant(org.hisp.dhis.constant.Constant) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

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