use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.
the class AbstractEventService method addEvent.
protected ImportSummary addEvent(Event event, User user, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
Program program = getProgram(importOptions.getIdSchemes().getProgramIdScheme(), event.getProgram());
ProgramStage programStage = getProgramStage(importOptions.getIdSchemes().getProgramStageIdScheme(), event.getProgramStage());
ProgramInstance programInstance;
ProgramStageInstance programStageInstance = null;
if (program == null) {
return new ImportSummary(ImportStatus.ERROR, "Event.program does not point to a valid program: " + event.getProgram()).incrementIgnored();
}
if (programStage == null && program.isRegistration()) {
return new ImportSummary(ImportStatus.ERROR, "Event.programStage does not point to a valid programStage, and program is multi stage: " + event.getProgramStage()).incrementIgnored();
} else if (programStage == null) {
programStage = program.getProgramStageByStage(1);
}
Assert.notNull(programStage, "Program stage cannot be null");
if (!canAccess(program, user)) {
return new ImportSummary(ImportStatus.ERROR, "Current user does not have permission to access this program").incrementIgnored();
}
if (program.isRegistration()) {
if (event.getTrackedEntityInstance() == null) {
return new ImportSummary(ImportStatus.ERROR, "No Event.trackedEntityInstance was provided for registration based program").incrementIgnored();
}
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = entityInstanceService.getTrackedEntityInstance(event.getTrackedEntityInstance());
if (entityInstance == null) {
return new ImportSummary(ImportStatus.ERROR, "Event.trackedEntityInstance does not point to a valid tracked entity instance: " + event.getTrackedEntityInstance()).incrementIgnored();
}
List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(entityInstance, program, ProgramStatus.ACTIVE));
if (programInstances.isEmpty()) {
return new ImportSummary(ImportStatus.ERROR, "Tracked entity instance: " + entityInstance.getUid() + " is not enrolled in program: " + program.getUid()).incrementIgnored();
} else if (programInstances.size() > 1) {
return new ImportSummary(ImportStatus.ERROR, "Tracked entity instance: " + entityInstance.getUid() + " have multiple active enrollments in program: " + program.getUid()).incrementIgnored();
}
programInstance = programInstances.get(0);
if (!programStage.getRepeatable()) {
programStageInstance = programStageInstanceService.getProgramStageInstance(programInstance, programStage);
if (programStageInstance != null && !programStageInstance.getUid().equals(event.getEvent())) {
return new ImportSummary(ImportStatus.ERROR, "Program stage is not repeatable and an event already exists").incrementIgnored();
}
} else {
if (event.getEvent() != null) {
programStageInstance = manager.getObject(ProgramStageInstance.class, importOptions.getIdSchemes().getProgramStageInstanceIdScheme(), event.getEvent());
if (programStageInstance == null) {
if (!CodeGenerator.isValidUid(event.getEvent())) {
return new ImportSummary(ImportStatus.ERROR, "Event.event did not point to a valid event: " + event.getEvent()).incrementIgnored();
}
}
}
}
} else {
List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(program, ProgramStatus.ACTIVE));
if (programInstances.isEmpty()) {
// Create PI if it doesn't exist (should only be one)
ProgramInstance pi = new ProgramInstance();
pi.setEnrollmentDate(new Date());
pi.setIncidentDate(new Date());
pi.setProgram(program);
pi.setStatus(ProgramStatus.ACTIVE);
programInstanceService.addProgramInstance(pi);
programInstances.add(pi);
} else if (programInstances.size() > 1) {
return new ImportSummary(ImportStatus.ERROR, "Multiple active program instances exists for program: " + program.getUid()).incrementIgnored();
}
programInstance = programInstances.get(0);
if (event.getEvent() != null) {
programStageInstance = manager.getObject(ProgramStageInstance.class, importOptions.getIdSchemes().getProgramStageInstanceIdScheme(), event.getEvent());
if (programStageInstance == null) {
if (importOptions.getIdSchemes().getProgramStageInstanceIdScheme().equals(IdScheme.UID) && !CodeGenerator.isValidUid(event.getEvent())) {
return new ImportSummary(ImportStatus.ERROR, "Event.event did not point to a valid event: " + event.getEvent()).incrementIgnored();
}
}
}
}
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), event.getOrgUnit());
program = programInstance.getProgram();
if (programStageInstance != null) {
programStage = programStageInstance.getProgramStage();
}
if (organisationUnit == null) {
return new ImportSummary(ImportStatus.ERROR, "Event.orgUnit does not point to a valid organisation unit: " + event.getOrgUnit()).incrementIgnored();
}
if (!programInstance.getProgram().hasOrganisationUnit(organisationUnit)) {
return new ImportSummary(ImportStatus.ERROR, "Program is not assigned to this organisation unit: " + event.getOrgUnit()).incrementIgnored();
}
validateExpiryDays(event, program, null);
return saveEvent(program, programInstance, programStage, programStageInstance, organisationUnit, event, user, importOptions);
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.
the class AbstractEnrollmentService method updateEnrollments.
// -------------------------------------------------------------------------
// UPDATE
// -------------------------------------------------------------------------
@Override
public ImportSummaries updateEnrollments(List<Enrollment> enrollments, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummaries importSummaries = new ImportSummaries();
int counter = 0;
for (Enrollment enrollment : enrollments) {
importSummaries.addImportSummary(updateEnrollment(enrollment, importOptions));
if (counter % FLUSH_FREQUENCY == 0) {
clearSession();
}
counter++;
}
return importSummaries;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.
the class AbstractEnrollmentService method addEnrollments.
// -------------------------------------------------------------------------
// CREATE
// -------------------------------------------------------------------------
@Override
public ImportSummaries addEnrollments(List<Enrollment> enrollments, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummaries importSummaries = new ImportSummaries();
int counter = 0;
for (Enrollment enrollment : enrollments) {
importSummaries.addImportSummary(addEnrollment(enrollment, importOptions));
if (counter % FLUSH_FREQUENCY == 0) {
clearSession();
}
counter++;
}
return importSummaries;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollments 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.Enrollments 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;
}
Aggregations