Search in sources :

Example 21 with Enrollment

use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.

the class AbstractEnrollmentService method updateEnrollment.

private ImportSummary updateEnrollment(Enrollment enrollment, ImportOptions importOptions, boolean handleEvents) {
    importOptions = updateImportOptions(importOptions);
    if (enrollment == null || StringUtils.isEmpty(enrollment.getEnrollment())) {
        return new ImportSummary(ImportStatus.ERROR, "No enrollment or enrollment ID was supplied").incrementIgnored();
    }
    ProgramInstance programInstance = programInstanceService.getProgramInstance(enrollment.getEnrollment());
    List<String> errors = trackerAccessManager.canUpdate(importOptions.getUser(), programInstance, false);
    if (programInstance == null) {
        return new ImportSummary(ImportStatus.ERROR, "ID " + enrollment.getEnrollment() + " doesn't point to a valid enrollment.").incrementIgnored();
    }
    if (!errors.isEmpty()) {
        return new ImportSummary(ImportStatus.ERROR, errors.toString()).incrementIgnored();
    }
    ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
    checkAttributes(programInstance.getEntityInstance(), enrollment, importOptions, importSummary);
    if (importSummary.hasConflicts()) {
        importSummary.setStatus(ImportStatus.ERROR).incrementIgnored();
        return importSummary;
    }
    Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
    if (!program.isRegistration()) {
        String descMsg = "Provided program " + program.getUid() + " is a program without registration. An enrollment cannot be created into program without registration.";
        return new ImportSummary(ImportStatus.ERROR, descMsg).incrementIgnored();
    }
    programInstance.setProgram(program);
    if (enrollment.getIncidentDate() != null) {
        programInstance.setIncidentDate(enrollment.getIncidentDate());
    }
    if (enrollment.getEnrollmentDate() != null) {
        programInstance.setEnrollmentDate(enrollment.getEnrollmentDate());
    }
    if (enrollment.getOrgUnit() != null) {
        OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), enrollment.getOrgUnit());
        programInstance.setOrganisationUnit(organisationUnit);
    }
    programInstance.setFollowup(enrollment.getFollowup());
    if (program.getDisplayIncidentDate() && programInstance.getIncidentDate() == null) {
        return new ImportSummary(ImportStatus.ERROR, "DisplayIncidentDate is true but IncidentDate is null").incrementIgnored();
    }
    updateFeatureType(program, enrollment, programInstance);
    if (EnrollmentStatus.fromProgramStatus(programInstance.getStatus()) != enrollment.getStatus()) {
        Date endDate = enrollment.getCompletedDate();
        String user = enrollment.getCompletedBy();
        if (enrollment.getCompletedDate() == null) {
            endDate = new Date();
        }
        if (user == null) {
            user = importOptions.getUser().getUsername();
        }
        if (EnrollmentStatus.CANCELLED == enrollment.getStatus()) {
            programInstance.setEndDate(endDate);
            programInstanceService.cancelProgramInstanceStatus(programInstance);
        } else if (EnrollmentStatus.COMPLETED == enrollment.getStatus()) {
            programInstance.setEndDate(endDate);
            programInstance.setCompletedBy(user);
            programInstanceService.completeProgramInstanceStatus(programInstance);
        } else if (EnrollmentStatus.ACTIVE == enrollment.getStatus()) {
            programInstanceService.incompleteProgramInstanceStatus(programInstance);
        }
    }
    validateProgramInstance(program, programInstance, enrollment, importSummary);
    if (importSummary.getStatus() != ImportStatus.SUCCESS) {
        return importSummary;
    }
    updateAttributeValues(enrollment, importOptions);
    updateDateFields(enrollment, programInstance);
    programInstanceService.updateProgramInstance(programInstance, importOptions.getUser());
    teiService.updateTrackedEntityInstance(programInstance.getEntityInstance(), importOptions.getUser());
    saveTrackedEntityComment(programInstance, enrollment, importOptions.getUser());
    importSummary = new ImportSummary(enrollment.getEnrollment()).incrementUpdated();
    importSummary.setReference(enrollment.getEnrollment());
    if (handleEvents) {
        importSummary.setEvents(handleEvents(enrollment, programInstance, importOptions));
    } else {
        for (Event event : enrollment.getEvents()) {
            event.setEnrollment(enrollment.getEnrollment());
            event.setProgram(programInstance.getProgram().getUid());
            event.setTrackedEntityInstance(enrollment.getTrackedEntityInstance());
        }
    }
    return importSummary;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramInstance(org.hisp.dhis.program.ProgramInstance) EnrollmentEvaluationEvent(org.hisp.dhis.programrule.engine.EnrollmentEvaluationEvent) Event(org.hisp.dhis.dxf2.events.event.Event) TrackerEnrollmentWebHookEvent(org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent) ProgramEnrollmentNotificationEvent(org.hisp.dhis.program.notification.event.ProgramEnrollmentNotificationEvent) Date(java.util.Date)

Example 22 with Enrollment

use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.

the class AbstractEnrollmentService method deleteEnrollments.

@Override
public ImportSummaries deleteEnrollments(List<Enrollment> enrollments, ImportOptions importOptions, boolean clearSession) {
    ImportSummaries importSummaries = new ImportSummaries();
    importOptions = updateImportOptions(importOptions);
    int counter = 0;
    for (Enrollment enrollment : enrollments) {
        importSummaries.addImportSummary(deleteEnrollment(enrollment.getEnrollment(), enrollment, importOptions));
        if (clearSession && counter % FLUSH_FREQUENCY == 0) {
            clearSession();
        }
        counter++;
    }
    return importSummaries;
}
Also used : ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Example 23 with Enrollment

use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.

the class AbstractEnrollmentService method handleEvents.

private ImportSummaries handleEvents(Enrollment enrollment, ProgramInstance programInstance, ImportOptions importOptions) {
    List<Event> create = new ArrayList<>();
    List<Event> update = new ArrayList<>();
    List<String> delete = new ArrayList<>();
    for (Event event : enrollment.getEvents()) {
        event.setEnrollment(enrollment.getEnrollment());
        event.setProgram(programInstance.getProgram().getUid());
        event.setTrackedEntityInstance(enrollment.getTrackedEntityInstance());
        if (importOptions.getImportStrategy().isSync() && event.isDeleted()) {
            delete.add(event.getEvent());
        } else if (!programStageInstanceService.programStageInstanceExists(event.getEvent())) {
            create.add(event);
        } else {
            update.add(event);
        }
    }
    ImportSummaries importSummaries = new ImportSummaries();
    importSummaries.addImportSummaries(eventService.deleteEvents(delete, false));
    importSummaries.addImportSummaries(eventService.updateEvents(update, importOptions, false, false));
    importSummaries.addImportSummaries(eventService.addEvents(create, importOptions, false));
    return importSummaries;
}
Also used : ArrayList(java.util.ArrayList) EnrollmentEvaluationEvent(org.hisp.dhis.programrule.engine.EnrollmentEvaluationEvent) Event(org.hisp.dhis.dxf2.events.event.Event) TrackerEnrollmentWebHookEvent(org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent) ProgramEnrollmentNotificationEvent(org.hisp.dhis.program.notification.event.ProgramEnrollmentNotificationEvent) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Example 24 with Enrollment

use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.

the class AbstractEnrollmentService method checkForExistingEnrollmentsIncludingDeleted.

private List<String> checkForExistingEnrollmentsIncludingDeleted(List<Enrollment> enrollments, ImportSummaries importSummaries) {
    List<String> foundEnrollments = programInstanceService.getProgramInstancesUidsIncludingDeleted(enrollments.stream().map(Enrollment::getEnrollment).collect(toList()));
    for (String foundEnrollmentUid : foundEnrollments) {
        ImportSummary is = new ImportSummary(ImportStatus.ERROR, "Enrollment " + foundEnrollmentUid + " already exists or was deleted earlier").setReference(foundEnrollmentUid).incrementIgnored();
        importSummaries.addImportSummary(is);
    }
    return foundEnrollments;
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary)

Example 25 with Enrollment

use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.

the class RegistrationMultiEventsServiceTest method testSaveRepeatableStageWithoutEventIdShouldCreateNewEvent.

@Test
void testSaveRepeatableStageWithoutEventIdShouldCreateNewEvent() {
    Enrollment enrollment = createEnrollment(programA.getUid(), trackedEntityInstanceMaleA.getTrackedEntityInstance());
    ImportSummary importSummary = enrollmentService.addEnrollment(enrollment, null, null);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    Event event = createEvent(programA.getUid(), programStageA.getUid(), organisationUnitA.getUid(), trackedEntityInstanceMaleA.getTrackedEntityInstance(), dataElementA.getUid());
    importSummary = eventService.addEvent(event, null, false);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    event = createEvent(programA.getUid(), programStageB.getUid(), organisationUnitA.getUid(), trackedEntityInstanceMaleA.getTrackedEntityInstance(), dataElementB.getUid());
    importSummary = eventService.addEvent(event, null, false);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    EventSearchParams params = new EventSearchParams();
    params.setProgram(programA);
    params.setOrgUnit(organisationUnitA);
    params.setOrgUnitSelectionMode(OrganisationUnitSelectionMode.SELECTED);
    assertEquals(2, eventService.getEvents(params).getEvents().size());
    event = createEvent(programA.getUid(), programStageB.getUid(), organisationUnitA.getUid(), trackedEntityInstanceMaleA.getTrackedEntityInstance(), dataElementB.getUid());
    importSummary = eventService.addEvent(event, null, false);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    assertEquals(3, eventService.getEvents(params).getEvents().size());
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) Event(org.hisp.dhis.dxf2.events.event.Event) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)46 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)44 Test (org.junit.jupiter.api.Test)35 Event (org.hisp.dhis.dxf2.events.event.Event)31 Date (java.util.Date)28 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)23 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)20 ArrayList (java.util.ArrayList)18 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)18 ProgramInstance (org.hisp.dhis.program.ProgramInstance)18 List (java.util.List)17 TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)17 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)14 Program (org.hisp.dhis.program.Program)14 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)13 Map (java.util.Map)12 Set (java.util.Set)12 Collectors (java.util.stream.Collectors)12 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)12 Lists (com.google.common.collect.Lists)11