use of org.hisp.dhis.tracker.validation.TrackerImportValidationContext in project dhis2-core by dhis2.
the class RepeatedEventsValidationHook method validate.
@Override
public void validate(ValidationErrorReporter reporter, TrackerImportValidationContext context) {
TrackerBundle bundle = context.getBundle();
Map<Pair<String, String>, List<Event>> eventsByEnrollmentAndNotRepeatableProgramStage = bundle.getEvents().stream().filter(e -> !reporter.isInvalid(e)).filter(e -> !context.getStrategy(e).isDelete()).filter(e -> {
ProgramStage programStage = context.getProgramStage(e.getProgramStage());
return programStage.getProgram().isRegistration() && !programStage.getRepeatable();
}).collect(Collectors.groupingBy(e -> Pair.of(e.getProgramStage(), e.getEnrollment())));
for (Map.Entry<Pair<String, String>, List<Event>> mapEntry : eventsByEnrollmentAndNotRepeatableProgramStage.entrySet()) {
if (mapEntry.getValue().size() > 1) {
for (Event event : mapEntry.getValue()) {
reporter.addError(event, TrackerErrorCode.E1039, mapEntry.getKey().getLeft());
}
}
}
bundle.getEvents().forEach(e -> validateNotMultipleEvents(reporter, context, e));
}
use of org.hisp.dhis.tracker.validation.TrackerImportValidationContext in project dhis2-core by dhis2.
the class PreCheckDataRelationsValidationHook method validateRelationshipReference.
private void validateRelationshipReference(ValidationErrorReporter reporter, Relationship relationship, RelationshipItem item) {
Optional<String> uid = getUidFromRelationshipItem(item);
TrackerType trackerType = relationshipItemValueType(item);
TrackerImportValidationContext ctx = reporter.getValidationContext();
if (TRACKED_ENTITY.equals(trackerType)) {
if (uid.isPresent() && !ValidationUtils.trackedEntityInstanceExist(ctx, uid.get())) {
reporter.addError(relationship, E4012, trackerType.getName(), uid.get());
}
} else if (ENROLLMENT.equals(trackerType)) {
if (uid.isPresent() && !ValidationUtils.enrollmentExist(ctx, uid.get())) {
reporter.addError(relationship, E4012, trackerType.getName(), uid.get());
}
} else if (EVENT.equals(trackerType) && uid.isPresent() && !ValidationUtils.eventExist(ctx, uid.get())) {
reporter.addError(relationship, E4012, trackerType.getName(), uid.get());
}
}
use of org.hisp.dhis.tracker.validation.TrackerImportValidationContext in project dhis2-core by dhis2.
the class PreCheckMandatoryFieldsValidationHook method validateEvent.
@Override
public void validateEvent(ValidationErrorReporter reporter, Event event) {
reporter.addErrorIf(() -> StringUtils.isEmpty(event.getOrgUnit()), event, E1123, ORG_UNIT);
reporter.addErrorIf(() -> StringUtils.isEmpty(event.getProgramStage()), event, E1123, "programStage");
// TODO remove if once metadata import is fixed
TrackerImportValidationContext context = reporter.getValidationContext();
ProgramStage programStage = context.getProgramStage(event.getProgramStage());
if (programStage != null) {
// Program stages should always have a program! Due to how metadata
// import is currently implemented
// it's possible that users run into the edge case that a program
// stage does not have an associated
// program. Tell the user it's an issue with the metadata and not
// the event itself. This should be
// fixed in the metadata import. For more see
// https://jira.dhis2.org/browse/DHIS2-12123
reporter.addErrorIfNull(programStage.getProgram(), event, E1008, event.getProgramStage());
// the DB entry of the program stage and not the payload itself
return;
}
reporter.addErrorIf(() -> StringUtils.isEmpty(event.getProgram()), event, E1123, "program");
}
use of org.hisp.dhis.tracker.validation.TrackerImportValidationContext in project dhis2-core by dhis2.
the class AttributeValidationHook method validateAttrValueType.
protected void validateAttrValueType(ValidationErrorReporter errorReporter, TrackerDto dto, Attribute attr, TrackedEntityAttribute teAttr) {
checkNotNull(attr, ATTRIBUTE_CANT_BE_NULL);
checkNotNull(teAttr, TRACKED_ENTITY_ATTRIBUTE_CANT_BE_NULL);
ValueType valueType = teAttr.getValueType();
TrackerImportValidationContext context = errorReporter.getValidationContext();
String error;
if (valueType.equals(ValueType.ORGANISATION_UNIT)) {
error = context.getOrganisationUnit(attr.getValue()) == null ? " Value " + attr.getValue() + " is not a valid org unit value" : null;
} else if (valueType.equals(ValueType.USERNAME)) {
error = context.usernameExists(attr.getValue()) ? null : " Value " + attr.getValue() + " is not a valid username value";
} else {
// org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:945)
try {
error = teAttrService.validateValueType(teAttr, attr.getValue());
} catch (Exception e) {
error = e.getMessage();
}
}
if (error != null) {
TrackerBundle bundle = context.getBundle();
TrackerErrorReport err = TrackerErrorReport.builder().uid(dto.getUid()).trackerType(dto.getTrackerType()).errorCode(TrackerErrorCode.E1007).addArg(valueType.toString()).addArg(error).build(bundle);
errorReporter.addError(err);
}
}
use of org.hisp.dhis.tracker.validation.TrackerImportValidationContext in project dhis2-core by dhis2.
the class EnrollmentInExistingValidationHook method validateEnrollment.
@Override
public void validateEnrollment(ValidationErrorReporter reporter, Enrollment enrollment) {
checkNotNull(enrollment, ENROLLMENT_CANT_BE_NULL);
if (EnrollmentStatus.CANCELLED == enrollment.getStatus()) {
return;
}
TrackerImportValidationContext validationContext = reporter.getValidationContext();
Program program = validationContext.getProgram(enrollment.getProgram());
checkNotNull(program, PROGRAM_CANT_BE_NULL);
if ((EnrollmentStatus.COMPLETED == enrollment.getStatus() && Boolean.FALSE.equals(program.getOnlyEnrollOnce()))) {
return;
}
validateTeiNotEnrolledAlready(reporter, enrollment, program);
}
Aggregations