use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class AbstractEventService method getEvent.
@Transactional(readOnly = true)
@Override
public Event getEvent(ProgramStageInstance programStageInstance, boolean isSynchronizationQuery, boolean skipOwnershipCheck) {
if (programStageInstance == null) {
return null;
}
Event event = new Event();
event.setEvent(programStageInstance.getUid());
if (programStageInstance.getProgramInstance().getEntityInstance() != null) {
event.setTrackedEntityInstance(programStageInstance.getProgramInstance().getEntityInstance().getUid());
}
event.setFollowup(programStageInstance.getProgramInstance().getFollowup());
event.setEnrollmentStatus(EnrollmentStatus.fromProgramStatus(programStageInstance.getProgramInstance().getStatus()));
event.setStatus(programStageInstance.getStatus());
event.setEventDate(DateUtils.getIso8601NoTz(programStageInstance.getExecutionDate()));
event.setDueDate(DateUtils.getIso8601NoTz(programStageInstance.getDueDate()));
event.setStoredBy(programStageInstance.getStoredBy());
event.setCompletedBy(programStageInstance.getCompletedBy());
event.setCompletedDate(DateUtils.getIso8601NoTz(programStageInstance.getCompletedDate()));
event.setCreated(DateUtils.getIso8601NoTz(programStageInstance.getCreated()));
event.setCreatedByUserInfo(programStageInstance.getCreatedByUserInfo());
event.setLastUpdatedByUserInfo(programStageInstance.getLastUpdatedByUserInfo());
event.setCreatedAtClient(DateUtils.getIso8601NoTz(programStageInstance.getCreatedAtClient()));
event.setLastUpdated(DateUtils.getIso8601NoTz(programStageInstance.getLastUpdated()));
event.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(programStageInstance.getLastUpdatedAtClient()));
event.setGeometry(programStageInstance.getGeometry());
event.setDeleted(programStageInstance.isDeleted());
if (programStageInstance.getAssignedUser() != null) {
event.setAssignedUser(programStageInstance.getAssignedUser().getUid());
event.setAssignedUserUsername(programStageInstance.getAssignedUser().getUsername());
event.setAssignedUserDisplayName(programStageInstance.getAssignedUser().getName());
}
User user = currentUserService.getCurrentUser();
OrganisationUnit ou = programStageInstance.getOrganisationUnit();
List<String> errors = trackerAccessManager.canRead(user, programStageInstance, skipOwnershipCheck);
if (!errors.isEmpty()) {
throw new IllegalQueryException(errors.toString());
}
if (ou != null) {
event.setOrgUnit(ou.getUid());
event.setOrgUnitName(ou.getName());
}
Program program = programStageInstance.getProgramInstance().getProgram();
event.setProgram(program.getUid());
event.setEnrollment(programStageInstance.getProgramInstance().getUid());
event.setProgramStage(programStageInstance.getProgramStage().getUid());
CategoryOptionCombo attributeOptionCombo = programStageInstance.getAttributeOptionCombo();
if (attributeOptionCombo != null) {
event.setAttributeOptionCombo(attributeOptionCombo.getUid());
event.setAttributeCategoryOptions(String.join(";", attributeOptionCombo.getCategoryOptions().stream().map(CategoryOption::getUid).collect(Collectors.toList())));
}
if (programStageInstance.getProgramInstance().getEntityInstance() != null) {
event.setTrackedEntityInstance(programStageInstance.getProgramInstance().getEntityInstance().getUid());
}
Collection<EventDataValue> dataValues;
if (!isSynchronizationQuery) {
dataValues = programStageInstance.getEventDataValues();
} else {
Set<String> dataElementsToSync = programStageInstance.getProgramStage().getProgramStageDataElements().stream().filter(psde -> !psde.getSkipSynchronization()).map(psde -> psde.getDataElement().getUid()).collect(Collectors.toSet());
dataValues = programStageInstance.getEventDataValues().stream().filter(dv -> dataElementsToSync.contains(dv.getDataElement())).collect(Collectors.toSet());
}
for (EventDataValue dataValue : dataValues) {
if (getDataElement(user.getUid(), dataValue.getDataElement())) {
DataValue value = new DataValue();
value.setCreated(DateUtils.getIso8601NoTz(dataValue.getCreated()));
value.setCreatedByUserInfo(dataValue.getCreatedByUserInfo());
value.setLastUpdated(DateUtils.getIso8601NoTz(dataValue.getLastUpdated()));
value.setLastUpdatedByUserInfo(dataValue.getLastUpdatedByUserInfo());
value.setDataElement(dataValue.getDataElement());
value.setValue(dataValue.getValue());
value.setProvidedElsewhere(dataValue.getProvidedElsewhere());
value.setStoredBy(dataValue.getStoredBy());
event.getDataValues().add(value);
} else {
log.info("Can not find a Data Element having UID [" + dataValue.getDataElement() + "]");
}
}
event.getNotes().addAll(NoteHelper.convertNotes(programStageInstance.getComments()));
event.setRelationships(programStageInstance.getRelationshipItems().stream().filter(Objects::nonNull).map((r) -> relationshipService.getRelationship(r.getRelationship(), RelationshipParams.FALSE, user)).collect(Collectors.toSet()));
return event;
}
use of org.hisp.dhis.program.Program 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.program.Program 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.Program in project dhis2-core by dhis2.
the class AbstractEnrollmentService method checkAttributes.
private void checkAttributes(org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance, Enrollment enrollment, ImportOptions importOptions, ImportConflicts importConflicts) {
Map<TrackedEntityAttribute, Boolean> mandatoryMap = Maps.newHashMap();
Map<String, String> attributeValueMap = Maps.newHashMap();
Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
for (ProgramTrackedEntityAttribute programTrackedEntityAttribute : program.getProgramAttributes()) {
mandatoryMap.put(programTrackedEntityAttribute.getAttribute(), programTrackedEntityAttribute.isMandatory());
}
// ignore attributes which do not belong to this program
trackedEntityInstance.getTrackedEntityAttributeValues().stream().filter(value -> mandatoryMap.containsKey(value.getAttribute())).forEach(value -> attributeValueMap.put(value.getAttribute().getUid(), value.getValue()));
for (Attribute attribute : enrollment.getAttributes()) {
attributeValueMap.put(attribute.getAttribute(), attribute.getValue());
validateAttributeType(attribute, importOptions, importConflicts);
}
List<String> errors = trackerAccessManager.canRead(importOptions.getUser(), trackedEntityInstance);
if (!errors.isEmpty()) {
throw new IllegalQueryException(errors.toString());
}
checkAttributeForMandatoryMaxLengthAndUniqueness(trackedEntityInstance, importOptions, importConflicts, mandatoryMap, attributeValueMap);
if (!attributeValueMap.isEmpty()) {
importConflicts.addConflict(ATTRIBUTE_ATTRIBUTE, "Only program attributes is allowed for enrollment " + attributeValueMap);
}
if (!program.getSelectEnrollmentDatesInFuture()) {
if (Objects.nonNull(enrollment.getEnrollmentDate()) && enrollment.getEnrollmentDate().after(new Date())) {
importConflicts.addConflict("Enrollment.date", "Enrollment Date can't be future date :" + enrollment.getEnrollmentDate());
}
}
if (!program.getSelectIncidentDatesInFuture()) {
if (Objects.nonNull(enrollment.getIncidentDate()) && enrollment.getIncidentDate().after(new Date())) {
importConflicts.addConflict("Enrollment.incidentDate", "Incident Date can't be future date :" + enrollment.getIncidentDate());
}
}
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class EventManager method resolveImportableEvents.
/**
* Filters out Events which are already present in the database (regardless
* of the 'deleted' state) as well as duplicates within the payload (if
* stage is not repeatable)
*
* @param events Events to import
* @param importSummaries ImportSummaries used for import
* @return Events that is possible to import (pass validation)
*/
private List<Event> resolveImportableEvents(final List<Event> events, final ImportSummaries importSummaries, final WorkContext workContext) {
List<Event> importableEvents = new ArrayList<>();
Set<String> importableStageEvents = new HashSet<>();
final Set<String> existingProgramStageInstances = workContext.getProgramStageInstanceMap().keySet();
for (Event eventToImport : events) {
if (existingProgramStageInstances.contains(eventToImport.getUid())) {
final ImportSummary is = new ImportSummary(ERROR, "Event " + eventToImport.getUid() + " already exists or was deleted earlier").setReference(eventToImport.getUid()).incrementIgnored();
importSummaries.addImportSummary(is);
continue;
}
Program program = workContext.getProgramsMap().get(eventToImport.getProgram());
ProgramStage programStage = workContext.getProgramStage(IdScheme.UID, eventToImport.getProgramStage());
if (program != null && programStage != null && program.isRegistration() && !programStage.getRepeatable()) {
String eventContextId = programStage.getUid() + "-" + eventToImport.getEnrollment();
if (importableStageEvents.contains(eventContextId)) {
final ImportSummary is = new ImportSummary(ERROR, "ProgramStage " + eventToImport.getProgramStage() + " is not repeatable. Current payload contains duplicate event").setReference(eventToImport.getUid()).incrementIgnored();
importSummaries.addImportSummary(is);
} else {
importableEvents.add(eventToImport);
importableStageEvents.add(eventContextId);
}
} else {
importableEvents.add(eventToImport);
}
}
return importableEvents;
}
Aggregations