Search in sources :

Example 6 with ValidationRule

use of org.hisp.dhis.validation.ValidationRule in project dhis2-core by dhis2.

the class DefaultCsvImportService method validationRulesFromCsv.

private List<ValidationRule> validationRulesFromCsv(CsvReader reader) throws IOException {
    List<ValidationRule> list = new ArrayList<>();
    while (reader.readRecord()) {
        String[] values = reader.getValues();
        if (values != null && values.length > 0) {
            Expression leftSide = new Expression();
            Expression rightSide = new Expression();
            ValidationRule object = new ValidationRule();
            setIdentifiableObject(object, values);
            object.setDescription(getSafe(values, 3, null, 255));
            object.setInstruction(getSafe(values, 4, null, 255));
            object.setImportance(Importance.valueOf(getSafe(values, 5, Importance.MEDIUM.toString(), 255)));
            // Left here so nobody wonders what field 6 is for
            // object.setRuleType( RuleType.valueOf( getSafe( values, 6, RuleType.VALIDATION.toString(), 255 ) ) );
            object.setOperator(Operator.safeValueOf(getSafe(values, 7, Operator.equal_to.toString(), 255)));
            object.setPeriodType(PeriodType.getByNameIgnoreCase(getSafe(values, 8, MonthlyPeriodType.NAME, 255)));
            leftSide.setExpression(getSafe(values, 9, null, 255));
            leftSide.setDescription(getSafe(values, 10, null, 255));
            leftSide.setMissingValueStrategy(MissingValueStrategy.safeValueOf(getSafe(values, 11, MissingValueStrategy.NEVER_SKIP.toString(), 50)));
            rightSide.setExpression(getSafe(values, 12, null, 255));
            rightSide.setDescription(getSafe(values, 13, null, 255));
            rightSide.setMissingValueStrategy(MissingValueStrategy.safeValueOf(getSafe(values, 14, MissingValueStrategy.NEVER_SKIP.toString(), 50)));
            object.setLeftSide(leftSide);
            object.setRightSide(rightSide);
            object.setAutoFields();
            list.add(object);
        }
    }
    return list;
}
Also used : Expression(org.hisp.dhis.expression.Expression) ArrayList(java.util.ArrayList) ValidationRule(org.hisp.dhis.validation.ValidationRule)

Example 7 with ValidationRule

use of org.hisp.dhis.validation.ValidationRule in project dhis2-core by dhis2.

the class ValidationRuleObjectBundleHook method preCreate.

@Override
public void preCreate(IdentifiableObject object, ObjectBundle bundle) {
    if (!ValidationRule.class.isInstance(object)) {
        return;
    }
    ValidationRule validationRule = (ValidationRule) object;
    if (validationRule.getPeriodType() != null) {
        PeriodType periodType = bundle.getPreheat().getPeriodTypeMap().get(validationRule.getPeriodType().getName());
        validationRule.setPeriodType(periodType);
    }
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) ValidationRule(org.hisp.dhis.validation.ValidationRule)

Example 8 with ValidationRule

use of org.hisp.dhis.validation.ValidationRule in project dhis2-core by dhis2.

the class FormUtilsImpl method getValidationRuleViolations.

@Override
public List<String> getValidationRuleViolations(OrganisationUnit organisationUnit, DataSet dataSet, Period period) {
    List<ValidationResult> validationRuleResults = new ArrayList<>(validationService.startInteractiveValidationAnalysis(dataSet, period, organisationUnit, null));
    List<String> validationRuleViolations = new ArrayList<>(validationRuleResults.size());
    for (ValidationResult result : validationRuleResults) {
        ValidationRule rule = result.getValidationRule();
        StringBuilder sb = new StringBuilder();
        sb.append(expressionService.getExpressionDescription(rule.getLeftSide().getExpression()));
        sb.append(" ").append(rule.getOperator().getMathematicalOperator()).append(" ");
        sb.append(expressionService.getExpressionDescription(rule.getRightSide().getExpression()));
        validationRuleViolations.add(sb.toString());
    }
    return validationRuleViolations;
}
Also used : ValidationResult(org.hisp.dhis.validation.ValidationResult) ValidationRule(org.hisp.dhis.validation.ValidationRule)

Example 9 with ValidationRule

use of org.hisp.dhis.validation.ValidationRule in project dhis2-core by dhis2.

the class DhisConvenienceTest method createValidationRule.

/**
     * @param uniqueCharacter A unique character to identify the object.
     * @param operator        The operator.
     * @param leftSide        The left side expression.
     * @param rightSide       The right side expression.
     * @param periodType      The period-type.
     */
public static ValidationRule createValidationRule(String uniqueCharacter, Operator operator, Expression leftSide, Expression rightSide, PeriodType periodType) {
    Assert.notNull(leftSide, "Left side expression must be specified");
    Assert.notNull(rightSide, "Rigth side expression must be specified");
    ValidationRule validationRule = new ValidationRule();
    validationRule.setAutoFields();
    validationRule.setName("ValidationRule" + uniqueCharacter);
    validationRule.setDescription("Description" + uniqueCharacter);
    validationRule.setOperator(operator);
    validationRule.setLeftSide(leftSide);
    validationRule.setRightSide(rightSide);
    validationRule.setPeriodType(periodType);
    return validationRule;
}
Also used : ValidationRule(org.hisp.dhis.validation.ValidationRule)

Aggregations

ValidationRule (org.hisp.dhis.validation.ValidationRule)9 List (java.util.List)4 DhisSpringTest (org.hisp.dhis.DhisSpringTest)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 DataElement (org.hisp.dhis.dataelement.DataElement)4 DataSet (org.hisp.dhis.dataset.DataSet)4 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)4 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)4 User (org.hisp.dhis.user.User)4 UserAuthorityGroup (org.hisp.dhis.user.UserAuthorityGroup)4 Test (org.junit.Test)4 ClassPathResource (org.springframework.core.io.ClassPathResource)4 PeriodType (org.hisp.dhis.period.PeriodType)2 Program (org.hisp.dhis.program.Program)2 ProgramStage (org.hisp.dhis.program.ProgramStage)2 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)2 ArrayList (java.util.ArrayList)1 Expression (org.hisp.dhis.expression.Expression)1 ProgramStageSection (org.hisp.dhis.program.ProgramStageSection)1 ValidationResult (org.hisp.dhis.validation.ValidationResult)1