Search in sources :

Example 11 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, 255));
            object.setInstruction(getSafe(values, 4, 255));
            object.setImportance(Importance.valueOf(getSafe(values, 5, Importance.MEDIUM.toString(), 255)));
            // Index 6 was rule type which has been removed from the data
            // model
            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, 255));
            leftSide.setDescription(getSafe(values, 10, 255));
            leftSide.setMissingValueStrategy(MissingValueStrategy.safeValueOf(getSafe(values, 11, MissingValueStrategy.NEVER_SKIP.toString(), 50)));
            rightSide.setExpression(getSafe(values, 12, 255));
            rightSide.setDescription(getSafe(values, 13, 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 12 with ValidationRule

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

the class ObjectBundleServiceTest method testCreateMetadataWithValidationRules.

@Test
void testCreateMetadataWithValidationRules() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/metadata_with_vr.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertFalse(validate.hasErrorReports());
    objectBundleService.commit(bundle);
    List<DataSet> dataSets = manager.getAll(DataSet.class);
    List<OrganisationUnit> organisationUnits = manager.getAll(OrganisationUnit.class);
    List<DataElement> dataElements = manager.getAll(DataElement.class);
    List<UserAuthorityGroup> userRoles = manager.getAll(UserAuthorityGroup.class);
    List<User> users = manager.getAll(User.class);
    List<ValidationRule> validationRules = manager.getAll(ValidationRule.class);
    assertFalse(dataSets.isEmpty());
    assertFalse(organisationUnits.isEmpty());
    assertFalse(dataElements.isEmpty());
    assertFalse(users.isEmpty());
    assertFalse(userRoles.isEmpty());
    assertEquals(2, validationRules.size());
    ValidationRule validationRule1 = manager.get(ValidationRule.class, "ztzsVjSIWg7");
    assertNotNull(validationRule1.getLeftSide());
    assertNotNull(validationRule1.getRightSide());
    ValidationRule validationRule2 = manager.get(ValidationRule.class, "TGvH4Hiyduc");
    assertNotNull(validationRule2.getLeftSide());
    assertNotNull(validationRule2.getRightSide());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) DataSet(org.hisp.dhis.dataset.DataSet) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ValidationRule(org.hisp.dhis.validation.ValidationRule) DataElement(org.hisp.dhis.dataelement.DataElement) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) List(java.util.List) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 13 with ValidationRule

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

the class DefaultDataIntegrityService method getInvalidValidationRuleExpressions.

private List<DataIntegrityIssue> getInvalidValidationRuleExpressions(Function<ValidationRule, Expression> getter) {
    List<DataIntegrityIssue> issues = new ArrayList<>();
    I18n i18n = i18nManager.getI18n();
    for (ValidationRule rule : validationRuleService.getAllValidationRules()) {
        ExpressionValidationOutcome result = expressionService.expressionIsValid(getter.apply(rule).getExpression(), VALIDATION_RULE_EXPRESSION);
        if (!result.isValid()) {
            issues.add(toIssue(rule, i18n.getString(result.getKey())));
        }
    }
    return issues;
}
Also used : DataIntegrityIssue(org.hisp.dhis.dataintegrity.DataIntegrityDetails.DataIntegrityIssue) ExpressionValidationOutcome(org.hisp.dhis.expression.ExpressionValidationOutcome) ArrayList(java.util.ArrayList) I18n(org.hisp.dhis.i18n.I18n) ValidationRule(org.hisp.dhis.validation.ValidationRule)

Example 14 with ValidationRule

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

the class MonitoringJob method execute.

@Override
@Transactional
public void execute(JobConfiguration jobConfiguration, JobProgress progress) {
    notifier.clear(jobConfiguration).notify(jobConfiguration, "Monitoring data");
    MonitoringJobParameters monitoringJobParameters = (MonitoringJobParameters) jobConfiguration.getJobParameters();
    try {
        List<Period> periods;
        Collection<ValidationRule> validationRules;
        List<String> groupUIDs = monitoringJobParameters.getValidationRuleGroups();
        if (groupUIDs.isEmpty()) {
            validationRules = validationRuleService.getValidationRulesWithNotificationTemplates();
        } else {
            validationRules = groupUIDs.stream().map(validationRuleService::getValidationRuleGroup).filter(Objects::nonNull).map(ValidationRuleGroup::getMembers).filter(Objects::nonNull).reduce(Sets.newHashSet(), SetUtils::union);
        }
        if (monitoringJobParameters.getRelativeStart() != 0 && monitoringJobParameters.getRelativeEnd() != 0) {
            Date startDate = DateUtils.getDateAfterAddition(new Date(), monitoringJobParameters.getRelativeStart());
            Date endDate = DateUtils.getDateAfterAddition(new Date(), monitoringJobParameters.getRelativeEnd());
            periods = periodService.getPeriodsBetweenDates(startDate, endDate);
            periods = ListUtils.union(periods, periodService.getIntersectionPeriods(periods));
        } else {
            periods = validationRules.stream().map(ValidationRule::getPeriodType).distinct().map((vr) -> Arrays.asList(vr.createPeriod(), vr.getPreviousPeriod(vr.createPeriod()))).reduce(Lists.newArrayList(), ListUtils::union);
        }
        ValidationAnalysisParams parameters = validationService.newParamsBuilder(validationRules, null, periods).withIncludeOrgUnitDescendants(true).withMaxResults(ValidationService.MAX_SCHEDULED_ALERTS).withSendNotifications(monitoringJobParameters.isSendNotifications()).withPersistResults(monitoringJobParameters.isPersistResults()).build();
        validationService.validationAnalysis(parameters);
        notifier.notify(jobConfiguration, INFO, "Monitoring process done", true);
    } catch (RuntimeException ex) {
        notifier.notify(jobConfiguration, ERROR, "Process failed: " + ex.getMessage(), true);
        messageService.sendSystemErrorNotification("Monitoring process failed", ex);
        throw ex;
    }
}
Also used : Objects(java.util.Objects) Period(org.hisp.dhis.period.Period) ValidationAnalysisParams(org.hisp.dhis.validation.ValidationAnalysisParams) Date(java.util.Date) MonitoringJobParameters(org.hisp.dhis.scheduling.parameters.MonitoringJobParameters) ValidationRule(org.hisp.dhis.validation.ValidationRule) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 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.
 * @param skipFormValidation Skip when validating forms.
 */
public static ValidationRule createValidationRule(String uniqueCharacter, Operator operator, Expression leftSide, Expression rightSide, PeriodType periodType, boolean skipFormValidation) {
    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);
    validationRule.setSkipFormValidation(skipFormValidation);
    return validationRule;
}
Also used : ValidationRule(org.hisp.dhis.validation.ValidationRule)

Aggregations

ValidationRule (org.hisp.dhis.validation.ValidationRule)15 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 DataElement (org.hisp.dhis.dataelement.DataElement)5 DataSet (org.hisp.dhis.dataset.DataSet)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)4 Period (org.hisp.dhis.period.Period)4 User (org.hisp.dhis.user.User)4 UserAuthorityGroup (org.hisp.dhis.user.UserAuthorityGroup)4 Test (org.junit.jupiter.api.Test)4 ClassPathResource (org.springframework.core.io.ClassPathResource)4 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)3 PeriodType (org.hisp.dhis.period.PeriodType)3 ValidationResult (org.hisp.dhis.validation.ValidationResult)3 Date (java.util.Date)2 Expression (org.hisp.dhis.expression.Expression)2 Program (org.hisp.dhis.program.Program)2