use of org.hisp.dhis.program.ProgramStatus 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.program.ProgramStatus in project dhis2-core by dhis2.
the class EnrollmentTrackerConverterService method from.
private ProgramInstance from(TrackerPreheat preheat, Enrollment enrollment, ProgramInstance programInstance) {
OrganisationUnit organisationUnit = preheat.get(OrganisationUnit.class, enrollment.getOrgUnit());
checkNotNull(organisationUnit, TrackerImporterAssertErrors.ORGANISATION_UNIT_CANT_BE_NULL);
Program program = preheat.get(Program.class, enrollment.getProgram());
checkNotNull(program, TrackerImporterAssertErrors.PROGRAM_CANT_BE_NULL);
TrackedEntityInstance trackedEntityInstance = preheat.getTrackedEntity(TrackerIdScheme.UID, enrollment.getTrackedEntity());
Date now = new Date();
if (isNewEntity(programInstance)) {
programInstance = new ProgramInstance();
programInstance.setUid(!StringUtils.isEmpty(enrollment.getEnrollment()) ? enrollment.getEnrollment() : enrollment.getUid());
programInstance.setCreated(now);
programInstance.setStoredBy(enrollment.getStoredBy());
programInstance.setCreatedByUserInfo(UserInfoSnapshot.from(preheat.getUser()));
}
programInstance.setLastUpdated(now);
programInstance.setLastUpdatedByUserInfo(UserInfoSnapshot.from(preheat.getUser()));
programInstance.setDeleted(false);
programInstance.setCreatedAtClient(DateUtils.fromInstant(enrollment.getCreatedAtClient()));
programInstance.setLastUpdatedAtClient(DateUtils.fromInstant(enrollment.getUpdatedAtClient()));
Date enrollmentDate = DateUtils.fromInstant(enrollment.getEnrolledAt());
Date incidentDate = DateUtils.fromInstant(enrollment.getOccurredAt());
programInstance.setEnrollmentDate(enrollmentDate);
programInstance.setIncidentDate(incidentDate != null ? incidentDate : enrollmentDate);
programInstance.setOrganisationUnit(organisationUnit);
programInstance.setProgram(program);
programInstance.setEntityInstance(trackedEntityInstance);
programInstance.setFollowup(enrollment.isFollowUp());
programInstance.setGeometry(enrollment.getGeometry());
if (enrollment.getStatus() == null) {
enrollment.setStatus(EnrollmentStatus.ACTIVE);
}
programInstance.setEndDate(DateUtils.fromInstant(enrollment.getCompletedAt()));
programInstance.setCompletedBy(enrollment.getCompletedBy());
ProgramStatus previousStatus = programInstance.getStatus();
programInstance.setStatus(enrollment.getStatus().getProgramStatus());
if (!Objects.equal(previousStatus, programInstance.getStatus())) {
if (programInstance.isCompleted()) {
programInstance.setEndDate(enrollment.getCompletedAt() != null ? DateUtils.fromInstant(enrollment.getCompletedAt()) : new Date());
programInstance.setCompletedBy(enrollment.getCompletedBy() != null ? enrollment.getCompletedBy() : preheat.getUsername());
} else if (programInstance.getStatus().equals(ProgramStatus.CANCELLED)) {
programInstance.setEndDate(enrollment.getCompletedAt() != null ? DateUtils.fromInstant(enrollment.getCompletedAt()) : new Date());
}
}
if (isNotEmpty(enrollment.getNotes())) {
programInstance.getComments().addAll(notesConverterService.from(preheat, enrollment.getNotes()));
}
return programInstance;
}
use of org.hisp.dhis.program.ProgramStatus in project dhis2-core by dhis2.
the class EnrollmentController method getEnrollments.
// -------------------------------------------------------------------------
// READ
// -------------------------------------------------------------------------
@RequestMapping(value = "", method = RequestMethod.GET)
@PreAuthorize("hasRole('ALL') or hasRole('F_PROGRAM_ENROLLMENT') or hasRole('F_PROGRAM_UNENROLLMENT') or hasRole('F_PROGRAM_ENROLLMENT_READ')")
@ResponseBody
public RootNode getEnrollments(@RequestParam(required = false) String ou, @RequestParam(required = false) OrganisationUnitSelectionMode ouMode, @RequestParam(required = false) String program, @RequestParam(required = false) ProgramStatus programStatus, @RequestParam(required = false) Boolean followUp, @RequestParam(required = false) Date lastUpdated, @RequestParam(required = false) Date programStartDate, @RequestParam(required = false) Date programEndDate, @RequestParam(required = false) String trackedEntity, @RequestParam(required = false) String trackedEntityInstance, @RequestParam(required = false) String enrollment, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) boolean totalPages, @RequestParam(required = false) boolean skipPaging) {
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
if (fields.isEmpty()) {
fields.add("enrollment,created,lastUpdated,trackedEntity,trackedEntityInstance,program,status,orgUnit,orgUnitName,enrollmentDate,incidentDate,followup");
}
Set<String> orgUnits = TextUtils.splitToArray(ou, TextUtils.SEMICOLON);
List<Enrollment> enrollments;
if (enrollment == null) {
ProgramInstanceQueryParams params = programInstanceService.getFromUrl(orgUnits, ouMode, lastUpdated, program, programStatus, programStartDate, programEndDate, trackedEntity, trackedEntityInstance, followUp, page, pageSize, totalPages, skipPaging);
enrollments = new ArrayList<>(enrollmentService.getEnrollments(programInstanceService.getProgramInstances(params)));
} else {
Set<String> enrollmentIds = TextUtils.splitToArray(enrollment, TextUtils.SEMICOLON);
enrollments = enrollmentIds != null ? enrollmentIds.stream().map(enrollmentId -> enrollmentService.getEnrollment(enrollmentId)).collect(Collectors.toList()) : null;
}
RootNode rootNode = NodeUtils.createMetadata();
rootNode.addChild(fieldFilterService.filter(Enrollment.class, enrollments, fields));
return rootNode;
}
use of org.hisp.dhis.program.ProgramStatus in project dhis2-core by dhis2.
the class TrackedEntityInstanceController method getTrackedEntityInstances.
// -------------------------------------------------------------------------
// READ
// -------------------------------------------------------------------------
@RequestMapping(value = "", method = RequestMethod.GET)
@ResponseBody
public RootNode getTrackedEntityInstances(@RequestParam(required = false) String query, @RequestParam(required = false) Set<String> attribute, @RequestParam(required = false) Set<String> filter, @RequestParam(required = false) String ou, @RequestParam(required = false) OrganisationUnitSelectionMode ouMode, @RequestParam(required = false) String program, @RequestParam(required = false) ProgramStatus programStatus, @RequestParam(required = false) Boolean followUp, @RequestParam(required = false) Date lastUpdatedStartDate, @RequestParam(required = false) Date lastUpdatedEndDate, @RequestParam(required = false) Date programStartDate, @RequestParam(required = false) Date programEnrollmentStartDate, @RequestParam(required = false) Date programEndDate, @RequestParam(required = false) Date programEnrollmentEndDate, @RequestParam(required = false) Date programIncidentStartDate, @RequestParam(required = false) Date programIncidentEndDate, @RequestParam(required = false) String trackedEntity, @RequestParam(required = false) String trackedEntityInstance, @RequestParam(required = false) EventStatus eventStatus, @RequestParam(required = false) Date eventStartDate, @RequestParam(required = false) Date eventEndDate, @RequestParam(required = false) boolean skipMeta, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) boolean totalPages, @RequestParam(required = false) boolean skipPaging, @RequestParam(required = false) boolean includeDeleted, @RequestParam(required = false) String order) throws Exception {
programEnrollmentStartDate = ObjectUtils.firstNonNull(programEnrollmentStartDate, programStartDate);
programEnrollmentEndDate = ObjectUtils.firstNonNull(programEnrollmentEndDate, programEndDate);
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
if (fields.isEmpty()) {
fields.add(":all");
}
Set<String> orgUnits = TextUtils.splitToArray(ou, TextUtils.SEMICOLON);
RootNode rootNode = NodeUtils.createMetadata();
List<TrackedEntityInstance> trackedEntityInstances;
TrackedEntityInstanceQueryParams queryParams = instanceService.getFromUrl(query, attribute, filter, orgUnits, ouMode, program, programStatus, followUp, lastUpdatedStartDate, lastUpdatedEndDate, programEnrollmentStartDate, programEnrollmentEndDate, programIncidentStartDate, programIncidentEndDate, trackedEntity, eventStatus, eventStartDate, eventEndDate, skipMeta, page, pageSize, totalPages, skipPaging, includeDeleted, getOrderParams(order));
if (trackedEntityInstance == null) {
trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, getTrackedEntityInstanceParams(fields));
} else {
Set<String> trackedEntityInstanceIds = TextUtils.splitToArray(trackedEntityInstance, TextUtils.SEMICOLON);
trackedEntityInstances = trackedEntityInstanceIds != null ? trackedEntityInstanceIds.stream().map(id -> trackedEntityInstanceService.getTrackedEntityInstance(id, getTrackedEntityInstanceParams(fields))).collect(Collectors.toList()) : null;
}
if (queryParams.isPaging() && queryParams.isTotalPages()) {
int count = trackedEntityInstanceService.getTrackedEntityInstanceCount(queryParams);
Pager pager = new Pager(queryParams.getPageWithDefault(), count, queryParams.getPageSizeWithDefault());
rootNode.addChild(NodeUtils.createPager(pager));
}
rootNode.addChild(fieldFilterService.filter(TrackedEntityInstance.class, trackedEntityInstances, fields));
return rootNode;
}
Aggregations