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;
}
Aggregations