Search in sources :

Example 1 with SmsEvent

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;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) User(org.hisp.dhis.user.User) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ArrayList(java.util.ArrayList) SmsEvent(org.hisp.dhis.smscompression.models.SmsEvent) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) Uid(org.hisp.dhis.smscompression.models.Uid) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) EnrollmentSmsSubmission(org.hisp.dhis.smscompression.models.EnrollmentSmsSubmission)

Example 2 with SmsEvent

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;
}
Also used : EnrollmentSmsSubmission(org.hisp.dhis.smscompression.models.EnrollmentSmsSubmission) SmsEvent(org.hisp.dhis.smscompression.models.SmsEvent) ArrayList(java.util.ArrayList)

Example 3 with SmsEvent

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;
}
Also used : EnrollmentSmsSubmission(org.hisp.dhis.smscompression.models.EnrollmentSmsSubmission) SmsEvent(org.hisp.dhis.smscompression.models.SmsEvent) ArrayList(java.util.ArrayList)

Example 4 with SmsEvent

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;
}
Also used : EnrollmentSmsSubmission(org.hisp.dhis.smscompression.models.EnrollmentSmsSubmission) SmsEvent(org.hisp.dhis.smscompression.models.SmsEvent) ArrayList(java.util.ArrayList)

Example 5 with SmsEvent

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;
}
Also used : GeoPoint(org.hisp.dhis.smscompression.models.GeoPoint) SmsDataValue(org.hisp.dhis.smscompression.models.SmsDataValue) SmsEvent(org.hisp.dhis.smscompression.models.SmsEvent) ArrayList(java.util.ArrayList) Date(java.util.Date)

Aggregations

ArrayList (java.util.ArrayList)5 SmsEvent (org.hisp.dhis.smscompression.models.SmsEvent)5 EnrollmentSmsSubmission (org.hisp.dhis.smscompression.models.EnrollmentSmsSubmission)4 Date (java.util.Date)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)1 Program (org.hisp.dhis.program.Program)1 ProgramInstance (org.hisp.dhis.program.ProgramInstance)1 GeoPoint (org.hisp.dhis.smscompression.models.GeoPoint)1 SmsDataValue (org.hisp.dhis.smscompression.models.SmsDataValue)1 Uid (org.hisp.dhis.smscompression.models.Uid)1 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)1 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)1 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)1 User (org.hisp.dhis.user.User)1