use of org.hisp.dhis.dxf2.events.enrollment.Enrollments 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.Enrollments in project dhis2-core by dhis2.
the class EnrollmentController method postEnrollmentJson.
// -------------------------------------------------------------------------
// CREATE
// -------------------------------------------------------------------------
@PostMapping(value = "", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseBody
public WebMessage postEnrollmentJson(@RequestParam(defaultValue = "CREATE_AND_UPDATE") ImportStrategy strategy, ImportOptions importOptions, HttpServletRequest request) throws IOException {
importOptions.setStrategy(strategy);
InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream());
if (!importOptions.isAsync()) {
ImportSummaries importSummaries = enrollmentService.addEnrollmentsJson(inputStream, importOptions);
importSummaries.setImportOptions(importOptions);
importSummaries.getImportSummaries().stream().filter(importSummary -> !importOptions.isDryRun() && !importSummary.getStatus().equals(ImportStatus.ERROR) && !importOptions.getImportStrategy().isDelete() && (!importOptions.getImportStrategy().isSync() || importSummary.getImportCount().getDeleted() == 0)).forEach(importSummary -> importSummary.setHref(ContextUtils.getRootPath(request) + RESOURCE_PATH + "/" + importSummary.getReference()));
if (importSummaries.getImportSummaries().size() == 1) {
ImportSummary importSummary = importSummaries.getImportSummaries().get(0);
importSummary.setImportOptions(importOptions);
if (!importSummary.getStatus().equals(ImportStatus.ERROR)) {
importSummaries(importSummaries).setHttpStatus(HttpStatus.CREATED).setLocation("/api/" + "enrollments" + "/" + importSummary.getReference());
}
}
return importSummaries(importSummaries).setHttpStatus(HttpStatus.CREATED);
}
return startAsyncImport(importOptions, enrollmentService.getEnrollmentsJson(inputStream));
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.
the class TrackedEntityInstanceSupportService method getTrackedEntityInstanceParams.
public TrackedEntityInstanceParams getTrackedEntityInstanceParams(List<String> fields) {
String joined = Joiner.on("").join(fields);
if (joined.contains("*")) {
return TrackedEntityInstanceParams.TRUE;
}
TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
if (joined.contains("relationships")) {
params.setIncludeRelationships(true);
}
if (joined.contains("enrollments")) {
params.setIncludeEnrollments(true);
}
if (joined.contains("events")) {
params.setIncludeEvents(true);
}
if (joined.contains("programOwners")) {
params.setIncludeProgramOwners(true);
}
return params;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.
the class TrackedEntityInstanceController method getTrackedEntityInstanceParams.
private TrackedEntityInstanceParams getTrackedEntityInstanceParams(List<String> fields) {
String joined = Joiner.on("").join(fields);
if (joined.contains("*")) {
return TrackedEntityInstanceParams.TRUE;
}
TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
if (joined.contains("relationships")) {
params.setIncludeRelationships(true);
}
if (joined.contains("enrollments")) {
params.setIncludeEnrollments(true);
}
if (joined.contains("events")) {
params.setIncludeEvents(true);
}
if (joined.contains("programOwners")) {
params.setIncludeProgramOwners(true);
}
return params;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.
the class AbstractEnrollmentService method addEnrollment.
@Override
public ImportSummary addEnrollment(Enrollment enrollment, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
TrackedEntityInstance trackedEntityInstance = trackedEntityInstanceService.getTrackedEntityInstance(entityInstance);
Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
ProgramInstanceQueryParams params = new ProgramInstanceQueryParams();
params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
params.setSkipPaging(true);
params.setProgram(program);
params.setTrackedEntityInstance(entityInstance);
params.setProgramStatus(ProgramStatus.ACTIVE);
List<Enrollment> enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
if (!enrollments.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " already have an active enrollment in program " + program.getUid());
importSummary.incrementIgnored();
return importSummary;
}
if (program.getOnlyEnrollOnce()) {
params.setProgramStatus(ProgramStatus.COMPLETED);
enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
if (!enrollments.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " already have a completed enrollment in program " + program.getUid() + ", and this program is" + " configured to only allow enrolling one time.");
importSummary.incrementIgnored();
return importSummary;
}
}
Set<ImportConflict> importConflicts = new HashSet<>();
importConflicts.addAll(checkAttributes(enrollment, importOptions));
importSummary.setConflicts(importConflicts);
if (!importConflicts.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.incrementIgnored();
return importSummary;
}
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), enrollment.getOrgUnit());
ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, program, enrollment.getEnrollmentDate(), enrollment.getIncidentDate(), organisationUnit, enrollment.getEnrollment());
if (programInstance == null) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("Could not enroll TrackedEntityInstance " + enrollment.getTrackedEntityInstance() + " into program " + enrollment.getProgram());
importSummary.incrementIgnored();
return importSummary;
}
if (program.getDisplayIncidentDate() && programInstance.getIncidentDate() == null) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("DisplayIncidentDate is true but IncidentDate is null ");
importSummary.incrementIgnored();
return importSummary;
}
if (program.getCaptureCoordinates()) {
if (enrollment.getCoordinate() != null && enrollment.getCoordinate().isValid()) {
programInstance.setLatitude(enrollment.getCoordinate().getLatitude());
programInstance.setLongitude(enrollment.getCoordinate().getLongitude());
} else {
programInstance.setLatitude(null);
programInstance.setLongitude(null);
}
}
updateAttributeValues(enrollment, importOptions);
updateDateFields(enrollment, programInstance);
programInstance.setFollowup(enrollment.getFollowup());
programInstanceService.updateProgramInstance(programInstance);
manager.update(programInstance.getEntityInstance());
saveTrackedEntityComment(programInstance, enrollment);
importSummary.setReference(programInstance.getUid());
importSummary.getImportCount().incrementImported();
importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
importSummary.setEvents(handleEvents(enrollment, programInstance, importOptions));
return importSummary;
}
Aggregations