Search in sources :

Example 41 with Enrollment

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());
}
Also used : Enrollment(org.hisp.dhis.tracker.domain.Enrollment) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 42 with Enrollment

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;
}
Also used : Enrollment(org.hisp.dhis.tracker.domain.Enrollment)

Example 43 with 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);
    }
}
Also used : java.util(java.util) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Collectors(java.util.stream.Collectors) EnrollmentStatus(org.hisp.dhis.tracker.domain.EnrollmentStatus) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) E1015(org.hisp.dhis.tracker.report.TrackerErrorCode.E1015) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) TrackerImporterAssertErrors(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors) E1016(org.hisp.dhis.tracker.report.TrackerErrorCode.E1016) org.hisp.dhis.program(org.hisp.dhis.program) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 44 with Enrollment

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);
}
Also used : ValidationUtils.addIssuesToReporter(org.hisp.dhis.tracker.validation.hooks.ValidationUtils.addIssuesToReporter) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) List(java.util.List) Component(org.springframework.stereotype.Component) Lists(com.google.common.collect.Lists) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) Autowired(org.springframework.beans.factory.annotation.Autowired) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Collectors(java.util.stream.Collectors) RuleActionImplementer(org.hisp.dhis.tracker.programrule.RuleActionImplementer) ProgramRuleIssue(org.hisp.dhis.tracker.programrule.ProgramRuleIssue) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) ProgramRuleIssue(org.hisp.dhis.tracker.programrule.ProgramRuleIssue)

Example 45 with Enrollment

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());
    }
}
Also used : Attribute(org.hisp.dhis.tracker.domain.Attribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Program(org.hisp.dhis.program.Program) E1019(org.hisp.dhis.tracker.report.TrackerErrorCode.E1019) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) E1018(org.hisp.dhis.tracker.report.TrackerErrorCode.E1018) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) Map(java.util.Map) TrackerIdScheme(org.hisp.dhis.tracker.TrackerIdScheme) ReferenceTrackerEntity(org.hisp.dhis.tracker.preheat.ReferenceTrackerEntity) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) E1076(org.hisp.dhis.tracker.report.TrackerErrorCode.E1076) Set(java.util.Set) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) TrackedAttributeValidationService(org.hisp.dhis.tracker.validation.service.attribute.TrackedAttributeValidationService) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) E1075(org.hisp.dhis.tracker.report.TrackerErrorCode.E1075) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Component(org.springframework.stereotype.Component) E1006(org.hisp.dhis.tracker.report.TrackerErrorCode.E1006) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) Optional(java.util.Optional) Collections(java.util.Collections) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute)

Aggregations

Enrollment (org.hisp.dhis.tracker.domain.Enrollment)88 Test (org.junit.jupiter.api.Test)67 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)53 Event (org.hisp.dhis.tracker.domain.Event)25 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)25 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)24 Program (org.hisp.dhis.program.Program)21 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)19 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)17 List (java.util.List)16 Optional (java.util.Optional)15 ProgramInstance (org.hisp.dhis.program.ProgramInstance)15 TrackerPreheat (org.hisp.dhis.tracker.preheat.TrackerPreheat)15 Sets (com.google.common.collect.Sets)14 Collectors (java.util.stream.Collectors)14 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)14 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)14 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)14 BeforeEach (org.junit.jupiter.api.BeforeEach)14 Mockito.when (org.mockito.Mockito.when)14