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