use of org.hisp.dhis.tracker.domain.Enrollment in project dhis2-core by dhis2.
the class PreCheckExistenceValidationHookTest method verifyEnrollmentValidationSuccessWhenIsCreateAndEnrollmentIsNotPresent.
@Test
void verifyEnrollmentValidationSuccessWhenIsCreateAndEnrollmentIsNotPresent() {
// given
Enrollment enrollment = Enrollment.builder().enrollment(NOT_PRESENT_ENROLLMENT_UID).build();
// when
when(ctx.getStrategy(enrollment)).thenReturn(TrackerImportStrategy.CREATE);
ValidationErrorReporter reporter = new ValidationErrorReporter(ctx);
validationHook.validateEnrollment(reporter, enrollment);
// then
assertFalse(reporter.hasErrors());
}
use of org.hisp.dhis.tracker.domain.Enrollment in project dhis2-core by dhis2.
the class EnrollmentInExistingValidationHook method getEnrollmentFromProgramInstance.
public Enrollment getEnrollmentFromProgramInstance(ProgramInstance programInstance) {
Enrollment enrollment = new Enrollment();
enrollment.setEnrollment(programInstance.getUid());
enrollment.setStatus(EnrollmentStatus.fromProgramStatus(programInstance.getStatus()));
return enrollment;
}
use of org.hisp.dhis.tracker.domain.Enrollment in project dhis2-core by dhis2.
the class EnrollmentInExistingValidationHook method validateTeiNotEnrolledAlready.
private void validateTeiNotEnrolledAlready(ValidationErrorReporter reporter, Enrollment enrollment, Program program) {
checkNotNull(enrollment.getTrackedEntity(), TRACKED_ENTITY_INSTANCE_CANT_BE_NULL);
TrackedEntityInstance tei = getTrackedEntityInstance(reporter, enrollment.getTrackedEntity());
Set<Enrollment> payloadEnrollment = reporter.getValidationContext().getBundle().getEnrollments().stream().filter(Objects::nonNull).filter(pi -> pi.getProgram().equals(program.getUid())).filter(pi -> pi.getTrackedEntity().equals(tei.getUid()) && !pi.getEnrollment().equals(enrollment.getEnrollment())).filter(pi -> EnrollmentStatus.ACTIVE == pi.getStatus() || EnrollmentStatus.COMPLETED == pi.getStatus()).collect(Collectors.toSet());
Set<Enrollment> dbEnrollment = reporter.getValidationContext().getBundle().getPreheat().getTrackedEntityToProgramInstanceMap().getOrDefault(enrollment.getTrackedEntity(), new ArrayList<>()).stream().filter(Objects::nonNull).filter(pi -> pi.getProgram().getUid().equals(program.getUid()) && !pi.getUid().equals(enrollment.getEnrollment())).filter(pi -> ProgramStatus.ACTIVE == pi.getStatus() || ProgramStatus.COMPLETED == pi.getStatus()).distinct().map(this::getEnrollmentFromProgramInstance).collect(Collectors.toSet());
// Priority to payload
Collection<Enrollment> mergedEnrollments = Stream.of(payloadEnrollment, dbEnrollment).flatMap(Set::stream).filter(e -> !Objects.equals(e.getEnrollment(), enrollment.getEnrollment())).collect(Collectors.toMap(Enrollment::getEnrollment, p -> p, (Enrollment x, Enrollment y) -> x)).values();
if (EnrollmentStatus.ACTIVE == enrollment.getStatus()) {
Set<Enrollment> activeOnly = mergedEnrollments.stream().filter(e -> EnrollmentStatus.ACTIVE == e.getStatus()).collect(Collectors.toSet());
if (!activeOnly.isEmpty()) {
reporter.addError(enrollment, E1015, tei, program);
}
}
if (Boolean.TRUE.equals(program.getOnlyEnrollOnce()) && !mergedEnrollments.isEmpty()) {
reporter.addError(enrollment, E1016, tei, program);
}
}
use of org.hisp.dhis.tracker.domain.Enrollment in project dhis2-core by dhis2.
the class EnrollmentRuleValidationHook method validateEnrollment.
@Override
public void validateEnrollment(ValidationErrorReporter reporter, Enrollment enrollment) {
TrackerImportValidationContext context = reporter.getValidationContext();
List<ProgramRuleIssue> programRuleIssues = validators.stream().flatMap(v -> v.validateEnrollments(context.getBundle()).getOrDefault(enrollment.getEnrollment(), Lists.newArrayList()).stream()).collect(Collectors.toList());
addIssuesToReporter(reporter, enrollment, programRuleIssues);
}
use of org.hisp.dhis.tracker.domain.Enrollment in project dhis2-core by dhis2.
the class EnrollmentAttributeValidationHook method validateRequiredProperties.
protected void validateRequiredProperties(ValidationErrorReporter reporter, Enrollment enrollment, Attribute attribute, Program program) {
reporter.addErrorIfNull(attribute.getAttribute(), enrollment, E1075, attribute);
Optional<ProgramTrackedEntityAttribute> optionalTrackedAttr = program.getProgramAttributes().stream().filter(pa -> pa.getAttribute().getUid().equals(attribute.getAttribute()) && pa.isMandatory()).findFirst();
if (optionalTrackedAttr.isPresent()) {
reporter.addErrorIfNull(attribute.getValue(), enrollment, E1076, TrackedEntityAttribute.class.getSimpleName(), attribute.getAttribute());
}
if (attribute.getAttribute() != null) {
TrackedEntityAttribute teAttribute = reporter.getValidationContext().getTrackedEntityAttribute(attribute.getAttribute());
reporter.addErrorIfNull(teAttribute, enrollment, E1006, attribute.getAttribute());
}
}
Aggregations