use of org.hisp.dhis.smscompression.models.SmsEvent in project dhis2-core by dhis2.
the class EnrollmentSMSListener method postProcess.
@Override
protected SmsResponse postProcess(IncomingSms sms, SmsSubmission submission) throws SMSProcessingException {
EnrollmentSmsSubmission subm = (EnrollmentSmsSubmission) submission;
Date enrollmentDate = subm.getEnrollmentDate();
Date incidentDate = subm.getIncidentDate();
Uid teiUid = subm.getTrackedEntityInstance();
Uid progid = subm.getTrackerProgram();
Uid tetid = subm.getTrackedEntityType();
Uid ouid = subm.getOrgUnit();
Uid enrollmentid = subm.getEnrollment();
OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit(ouid.getUid());
Program program = programService.getProgram(progid.getUid());
if (program == null) {
throw new SMSProcessingException(SmsResponse.INVALID_PROGRAM.set(progid));
}
TrackedEntityType entityType = trackedEntityTypeService.getTrackedEntityType(tetid.getUid());
if (entityType == null) {
throw new SMSProcessingException(SmsResponse.INVALID_TETYPE.set(tetid));
}
if (!programService.hasOrgUnit(program, orgUnit)) {
throw new SMSProcessingException(SmsResponse.OU_NOTIN_PROGRAM.set(ouid, progid));
}
TrackedEntityInstance entityInstance;
boolean teiExists = teiService.trackedEntityInstanceExists(teiUid.getUid());
if (teiExists) {
log.info(String.format("Given TEI [%s] exists. Updating...", teiUid));
entityInstance = teiService.getTrackedEntityInstance(teiUid.getUid());
} else {
log.info(String.format("Given TEI [%s] does not exist. Creating...", teiUid));
entityInstance = new TrackedEntityInstance();
entityInstance.setUid(teiUid.getUid());
entityInstance.setOrganisationUnit(orgUnit);
entityInstance.setTrackedEntityType(entityType);
}
Set<TrackedEntityAttributeValue> attributeValues = getSMSAttributeValues(subm, entityInstance);
if (teiExists) {
updateAttributeValues(attributeValues, entityInstance.getTrackedEntityAttributeValues());
entityInstance.setTrackedEntityAttributeValues(attributeValues);
teiService.updateTrackedEntityInstance(entityInstance);
} else {
teiService.createTrackedEntityInstance(entityInstance, attributeValues);
}
TrackedEntityInstance tei = teiService.getTrackedEntityInstance(teiUid.getUid());
// TODO: Unsure about this handling for enrollments, this needs to be
// checked closely
ProgramInstance enrollment;
boolean enrollmentExists = programInstanceService.programInstanceExists(enrollmentid.getUid());
if (enrollmentExists) {
enrollment = programInstanceService.getProgramInstance(enrollmentid.getUid());
// Update these dates in case they've changed
enrollment.setEnrollmentDate(enrollmentDate);
enrollment.setIncidentDate(incidentDate);
} else {
enrollment = programInstanceService.enrollTrackedEntityInstance(tei, program, enrollmentDate, incidentDate, orgUnit, enrollmentid.getUid());
}
if (enrollment == null) {
throw new SMSProcessingException(SmsResponse.ENROLL_FAILED.set(teiUid, progid));
}
enrollment.setStatus(getCoreProgramStatus(subm.getEnrollmentStatus()));
enrollment.setGeometry(convertGeoPointToGeometry(subm.getCoordinates()));
programInstanceService.updateProgramInstance(enrollment);
// We now check if the enrollment has events to process
User user = userService.getUser(subm.getUserId().getUid());
List<Object> errorUIDs = new ArrayList<>();
if (subm.getEvents() != null) {
for (SmsEvent event : subm.getEvents()) {
errorUIDs.addAll(processEvent(event, user, enrollment, sms));
}
}
enrollment.setStatus(getCoreProgramStatus(subm.getEnrollmentStatus()));
enrollment.setGeometry(convertGeoPointToGeometry(subm.getCoordinates()));
programInstanceService.updateProgramInstance(enrollment);
if (!errorUIDs.isEmpty()) {
return SmsResponse.WARN_DVERR.setList(errorUIDs);
}
if (attributeValues == null || attributeValues.isEmpty()) {
// TODO: Is this correct handling?
return SmsResponse.WARN_AVEMPTY;
}
return SmsResponse.SUCCESS;
}
use of org.hisp.dhis.smscompression.models.SmsEvent in project dhis2-core by dhis2.
the class EnrollmentSMSListenerTest method createEnrollmentSubmissionWithEvents.
private EnrollmentSmsSubmission createEnrollmentSubmissionWithEvents() {
EnrollmentSmsSubmission subm = createEnrollmentSubmissionNoEvents();
ArrayList<SmsEvent> events = new ArrayList<>();
events.add(createEvent());
subm.setEvents(events);
return subm;
}
use of org.hisp.dhis.smscompression.models.SmsEvent in project dhis2-core by dhis2.
the class EnrollmentSMSListenerTest method createEnrollmentSubmissionEventNoValues.
private EnrollmentSmsSubmission createEnrollmentSubmissionEventNoValues() {
EnrollmentSmsSubmission subm = createEnrollmentSubmissionNoEvents();
SmsEvent event = createEvent();
event.setValues(null);
ArrayList<SmsEvent> events = new ArrayList<>();
events.add(event);
subm.setEvents(events);
return subm;
}
use of org.hisp.dhis.smscompression.models.SmsEvent in project dhis2-core by dhis2.
the class EnrollmentSMSListenerTest method createEnrollmentSubmissionEventWithNulls.
private EnrollmentSmsSubmission createEnrollmentSubmissionEventWithNulls() {
EnrollmentSmsSubmission subm = createEnrollmentSubmissionNoEvents();
SmsEvent event = createEvent();
event.setEventDate(null);
event.setDueDate(null);
event.setCoordinates(null);
ArrayList<SmsEvent> events = new ArrayList<>();
events.add(event);
subm.setEvents(events);
return subm;
}
use of org.hisp.dhis.smscompression.models.SmsEvent in project dhis2-core by dhis2.
the class EnrollmentSMSListenerTest method createEvent.
private SmsEvent createEvent() {
SmsEvent event = new SmsEvent();
event.setOrgUnit(organisationUnit.getUid());
event.setProgramStage(programStage.getUid());
event.setAttributeOptionCombo(categoryOptionCombo.getUid());
event.setEvent(programStageInstance.getUid());
event.setEventStatus(SmsEventStatus.COMPLETED);
event.setEventDate(new Date());
event.setDueDate(new Date());
event.setCoordinates(new GeoPoint(59.9399586f, 10.7195609f));
ArrayList<SmsDataValue> eventValues = new ArrayList<>();
eventValues.add(new SmsDataValue(categoryOptionCombo.getUid(), dataElement.getUid(), "10"));
event.setValues(eventValues);
return event;
}
Aggregations