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;
}
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());
}
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;
}
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;
}
}
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;
}
Aggregations