use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class AbstractEnrollmentService method validateRequest.
private ImportSummary validateRequest(Program program, org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance, Enrollment enrollment, OrganisationUnit organisationUnit, ImportOptions importOptions) {
ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
String error = validateProgramForEnrollment(program, enrollment, organisationUnit, importOptions);
if (!StringUtils.isEmpty(error)) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription(error);
importSummary.incrementIgnored();
return importSummary;
}
ProgramInstanceQueryParams params = new ProgramInstanceQueryParams();
params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
params.setSkipPaging(true);
params.setProgram(program);
params.setTrackedEntityInstanceUid(entityInstance.getUid());
// endpoint is used for bulk import and sync purposes as well
if (enrollment.getStatus() != EnrollmentStatus.CANCELLED) {
List<Enrollment> enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
Set<Enrollment> activeEnrollments = enrollments.stream().filter(e -> e.getStatus() == EnrollmentStatus.ACTIVE).collect(Collectors.toSet());
// needed
if (!activeEnrollments.isEmpty() && enrollment.getStatus() == EnrollmentStatus.ACTIVE) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("TrackedEntityInstance " + entityInstance.getUid() + " already has an active enrollment in program " + program.getUid());
importSummary.incrementIgnored();
return importSummary;
}
// imported enrollment has a state other than CANCELLED
if (program.getOnlyEnrollOnce()) {
Set<Enrollment> activeOrCompletedEnrollments = enrollments.stream().filter(e -> e.getStatus() == EnrollmentStatus.ACTIVE || e.getStatus() == EnrollmentStatus.COMPLETED).collect(Collectors.toSet());
if (!activeOrCompletedEnrollments.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("TrackedEntityInstance " + entityInstance.getUid() + " already has an active or completed enrollment in program " + program.getUid() + ", and this program only allows enrolling one time");
importSummary.incrementIgnored();
return importSummary;
}
}
}
checkAttributes(entityInstance, enrollment, importOptions, importSummary);
if (importSummary.hasConflicts()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.incrementIgnored();
}
return importSummary;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class AbstractEnrollmentService method deleteEnrollment.
private ImportSummary deleteEnrollment(String uid, Enrollment enrollment, ImportOptions importOptions) {
ImportSummary importSummary = new ImportSummary();
importOptions = updateImportOptions(importOptions);
boolean existsEnrollment = programInstanceService.programInstanceExists(uid);
if (existsEnrollment) {
ProgramInstance programInstance = programInstanceService.getProgramInstance(uid);
if (enrollment != null) {
importSummary.setReference(uid);
importSummary.setEvents(handleEvents(enrollment, programInstance, importOptions));
}
if (importOptions.getUser() != null) {
isAllowedToDelete(importOptions.getUser(), programInstance, importSummary);
if (importSummary.hasConflicts()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setReference(programInstance.getUid());
importSummary.incrementIgnored();
return importSummary;
}
}
programInstanceService.deleteProgramInstance(programInstance);
teiService.updateTrackedEntityInstance(programInstance.getEntityInstance());
importSummary.setReference(uid);
importSummary.setStatus(ImportStatus.SUCCESS);
importSummary.setDescription("Deletion of enrollment " + uid + " was successful");
return importSummary.incrementDeleted();
} else {
// If I am here, it means that the item is either already deleted or
// it is not present in the system at all.
importSummary.setStatus(ImportStatus.SUCCESS);
importSummary.setDescription("Enrollment " + uid + " cannot be deleted as it is not present in the system");
return importSummary.incrementIgnored();
}
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class AbstractEnrollmentService method updateEnrollments.
// -------------------------------------------------------------------------
// UPDATE
// -------------------------------------------------------------------------
@Override
public ImportSummaries updateEnrollments(List<Enrollment> enrollments, ImportOptions importOptions, boolean clearSession) {
List<List<Enrollment>> partitions = Lists.partition(enrollments, FLUSH_FREQUENCY);
importOptions = updateImportOptions(importOptions);
ImportSummaries importSummaries = new ImportSummaries();
List<Event> events = new ArrayList<>();
for (List<Enrollment> _enrollments : partitions) {
reloadUser(importOptions);
prepareCaches(_enrollments, importOptions.getUser());
for (Enrollment enrollment : _enrollments) {
ImportSummary importSummary = updateEnrollment(enrollment, importOptions, false);
importSummaries.addImportSummary(importSummary);
if (importSummary.isStatus(ImportStatus.SUCCESS)) {
List<Event> enrollmentEvents = enrollment.getEvents();
enrollmentEvents.forEach(e -> e.setEnrollment(enrollment.getEnrollment()));
events.addAll(enrollmentEvents);
}
}
if (clearSession && enrollments.size() >= FLUSH_FREQUENCY) {
clearSession();
}
}
ImportSummaries eventImportSummaries = eventService.processEventImport(events, importOptions, null);
linkEventSummaries(importSummaries, eventImportSummaries, events);
return importSummaries;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment 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;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class AbstractEnrollmentService method updateEnrollmentForNote.
@Override
public ImportSummary updateEnrollmentForNote(Enrollment enrollment) {
if (enrollment == null || enrollment.getEnrollment() == null) {
return new ImportSummary(ImportStatus.ERROR, "No enrollment or enrollment ID was supplied").incrementIgnored();
}
ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
ProgramInstance programInstance = programInstanceService.getProgramInstance(enrollment.getEnrollment());
if (programInstance == null) {
return new ImportSummary(ImportStatus.ERROR, "Enrollment ID was not valid.").incrementIgnored();
}
saveTrackedEntityComment(programInstance, enrollment, currentUserService.getCurrentUser());
importSummary.setReference(enrollment.getEnrollment());
importSummary.getImportCount().incrementUpdated();
return importSummary;
}
Aggregations