Search in sources :

Example 1 with TrackerEnrollmentWebHookEvent

use of org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent in project dhis2-core by dhis2.

the class AbstractEnrollmentService method addEnrollment.

private ImportSummary addEnrollment(Enrollment enrollment, ImportOptions importOptions, org.hisp.dhis.trackedentity.TrackedEntityInstance daoTrackedEntityInstance, boolean handleEvents) {
    importOptions = updateImportOptions(importOptions);
    String storedBy = !StringUtils.isEmpty(enrollment.getStoredBy()) && enrollment.getStoredBy().length() < 31 ? enrollment.getStoredBy() : (importOptions.getUser() == null || StringUtils.isEmpty(importOptions.getUser().getUsername()) ? "system-process" : importOptions.getUser().getUsername());
    if (daoTrackedEntityInstance == null) {
        daoTrackedEntityInstance = getTrackedEntityInstance(enrollment.getTrackedEntityInstance(), importOptions.getUser());
    }
    Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), enrollment.getOrgUnit());
    ImportSummary importSummary = validateRequest(program, daoTrackedEntityInstance, enrollment, organisationUnit, importOptions);
    if (importSummary.getStatus() != ImportStatus.SUCCESS) {
        return importSummary;
    }
    List<String> errors = trackerAccessManager.canCreate(importOptions.getUser(), new ProgramInstance(program, daoTrackedEntityInstance, organisationUnit), false);
    if (!errors.isEmpty()) {
        return new ImportSummary(ImportStatus.ERROR, errors.toString()).incrementIgnored();
    }
    if (enrollment.getStatus() == null) {
        enrollment.setStatus(EnrollmentStatus.ACTIVE);
    }
    ProgramStatus programStatus = enrollment.getStatus() == EnrollmentStatus.ACTIVE ? ProgramStatus.ACTIVE : enrollment.getStatus() == EnrollmentStatus.COMPLETED ? ProgramStatus.COMPLETED : ProgramStatus.CANCELLED;
    ProgramInstance programInstance = programInstanceService.prepareProgramInstance(daoTrackedEntityInstance, program, programStatus, enrollment.getEnrollmentDate(), enrollment.getIncidentDate(), organisationUnit, enrollment.getEnrollment());
    if (programStatus == ProgramStatus.COMPLETED || programStatus == ProgramStatus.CANCELLED) {
        Date date = enrollment.getCompletedDate();
        if (date == null) {
            date = new Date();
        }
        String user = enrollment.getCompletedBy();
        if (user == null) {
            user = importOptions.getUser().getUsername();
        }
        programInstance.setCompletedBy(user);
        programInstance.setEndDate(date);
    }
    programInstanceService.addProgramInstance(programInstance, importOptions.getUser());
    importSummary = validateProgramInstance(program, programInstance, enrollment, importSummary);
    if (importSummary.getStatus() != ImportStatus.SUCCESS) {
        return importSummary;
    }
    // -----------------------------------------------------------------
    // Send enrollment notifications (if any)
    // -----------------------------------------------------------------
    eventPublisher.publishEvent(new ProgramEnrollmentNotificationEvent(this, programInstance.getId()));
    eventPublisher.publishEvent(new EnrollmentEvaluationEvent(this, programInstance.getId()));
    eventPublisher.publishEvent(new TrackerEnrollmentWebHookEvent(this, programInstance.getUid()));
    updateFeatureType(program, enrollment, programInstance);
    updateAttributeValues(enrollment, importOptions);
    updateDateFields(enrollment, programInstance);
    programInstance.setFollowup(enrollment.getFollowup());
    programInstance.setStoredBy(storedBy);
    programInstanceService.updateProgramInstance(programInstance, importOptions.getUser());
    trackerOwnershipAccessManager.assignOwnership(daoTrackedEntityInstance, program, organisationUnit, true, true);
    saveTrackedEntityComment(programInstance, enrollment, importOptions.getUser());
    importSummary.setReference(programInstance.getUid());
    enrollment.setEnrollment(programInstance.getUid());
    importSummary.getImportCount().incrementImported();
    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) EnrollmentEvaluationEvent(org.hisp.dhis.programrule.engine.EnrollmentEvaluationEvent) TrackerEnrollmentWebHookEvent(org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramEnrollmentNotificationEvent(org.hisp.dhis.program.notification.event.ProgramEnrollmentNotificationEvent) 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) ProgramStatus(org.hisp.dhis.program.ProgramStatus)

Aggregations

Date (java.util.Date)1 Event (org.hisp.dhis.dxf2.events.event.Event)1 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)1 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)1 Program (org.hisp.dhis.program.Program)1 ProgramInstance (org.hisp.dhis.program.ProgramInstance)1 ProgramStatus (org.hisp.dhis.program.ProgramStatus)1 ProgramEnrollmentNotificationEvent (org.hisp.dhis.program.notification.event.ProgramEnrollmentNotificationEvent)1 EnrollmentEvaluationEvent (org.hisp.dhis.programrule.engine.EnrollmentEvaluationEvent)1 TrackerEnrollmentWebHookEvent (org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent)1