Search in sources :

Example 1 with Uid

use of org.hisp.dhis.smscompression.models.Uid in project dhis2-core by dhis2.

the class EnrollmentSMSListener method processEvent.

protected List<Object> processEvent(SmsEvent event, User user, ProgramInstance programInstance, IncomingSms sms) {
    Uid stageid = event.getProgramStage();
    Uid aocid = event.getAttributeOptionCombo();
    Uid orgunitid = event.getOrgUnit();
    OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit(orgunitid.getUid());
    if (orgUnit == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_ORGUNIT.set(orgunitid));
    }
    ProgramStage programStage = programStageService.getProgramStage(stageid.getUid());
    if (programStage == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_STAGE.set(stageid));
    }
    CategoryOptionCombo aoc = categoryService.getCategoryOptionCombo(aocid.getUid());
    if (aoc == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_AOC.set(aocid));
    }
    List<Object> errorUIDs = saveNewEvent(event.getEvent().getUid(), orgUnit, programStage, programInstance, sms, aoc, user, event.getValues(), event.getEventStatus(), event.getEventDate(), event.getDueDate(), event.getCoordinates());
    return errorUIDs;
}
Also used : Uid(org.hisp.dhis.smscompression.models.Uid) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramStage(org.hisp.dhis.program.ProgramStage) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 2 with Uid

use of org.hisp.dhis.smscompression.models.Uid 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 3 with Uid

use of org.hisp.dhis.smscompression.models.Uid in project dhis2-core by dhis2.

the class EnrollmentSMSListener method createTrackedEntityValue.

protected TrackedEntityAttributeValue createTrackedEntityValue(SmsAttributeValue SMSAttributeValue, TrackedEntityInstance tei) {
    Uid attribUid = SMSAttributeValue.getAttribute();
    String val = SMSAttributeValue.getValue();
    TrackedEntityAttribute attribute = trackedEntityAttributeService.getTrackedEntityAttribute(attribUid.getUid());
    if (attribute == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_ATTRIB.set(attribUid));
    } else if (val == null) {
        // TODO: Is this an error we can't recover from?
        throw new SMSProcessingException(SmsResponse.NULL_ATTRIBVAL.set(attribUid));
    }
    TrackedEntityAttributeValue trackedEntityAttributeValue = new TrackedEntityAttributeValue();
    trackedEntityAttributeValue.setAttribute(attribute);
    trackedEntityAttributeValue.setEntityInstance(tei);
    trackedEntityAttributeValue.setValue(val);
    return trackedEntityAttributeValue;
}
Also used : Uid(org.hisp.dhis.smscompression.models.Uid) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)

Example 4 with Uid

use of org.hisp.dhis.smscompression.models.Uid in project dhis2-core by dhis2.

the class TrackerEventSMSListener method postProcess.

@Override
protected SmsResponse postProcess(IncomingSms sms, SmsSubmission submission) throws SMSProcessingException {
    TrackerEventSmsSubmission subm = (TrackerEventSmsSubmission) submission;
    Uid ouid = subm.getOrgUnit();
    Uid stageid = subm.getProgramStage();
    Uid enrolmentid = subm.getEnrollment();
    Uid aocid = subm.getAttributeOptionCombo();
    OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit(ouid.getUid());
    User user = userService.getUser(subm.getUserId().getUid());
    ProgramInstance programInstance = programInstanceService.getProgramInstance(enrolmentid.getUid());
    if (programInstance == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_ENROLL.set(enrolmentid));
    }
    ProgramStage programStage = programStageService.getProgramStage(stageid.getUid());
    if (programStage == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_STAGE.set(stageid));
    }
    CategoryOptionCombo aoc = categoryService.getCategoryOptionCombo(aocid.getUid());
    if (aoc == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_AOC.set(aocid));
    }
    List<Object> errorUIDs = saveNewEvent(subm.getEvent().getUid(), orgUnit, programStage, programInstance, sms, aoc, user, subm.getValues(), subm.getEventStatus(), subm.getEventDate(), subm.getDueDate(), subm.getCoordinates());
    if (!errorUIDs.isEmpty()) {
        return SmsResponse.WARN_DVERR.setList(errorUIDs);
    } else if (subm.getValues() == null || subm.getValues().isEmpty()) {
        // TODO: Should we save the event if there are no data values?
        return SmsResponse.WARN_DVEMPTY;
    }
    return SmsResponse.SUCCESS;
}
Also used : Uid(org.hisp.dhis.smscompression.models.Uid) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackerEventSmsSubmission(org.hisp.dhis.smscompression.models.TrackerEventSmsSubmission) ProgramStage(org.hisp.dhis.program.ProgramStage) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 5 with Uid

use of org.hisp.dhis.smscompression.models.Uid in project dhis2-core by dhis2.

the class RelationshipSMSListener method postProcess.

@Override
protected SmsResponse postProcess(IncomingSms sms, SmsSubmission submission) throws SMSProcessingException {
    RelationshipSmsSubmission subm = (RelationshipSmsSubmission) submission;
    Uid fromid = subm.getFrom();
    Uid toid = subm.getTo();
    Uid typeid = subm.getRelationshipType();
    RelationshipType relType = relationshipTypeService.getRelationshipType(typeid.getUid());
    if (relType == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_RELTYPE.set(typeid));
    }
    RelationshipItem fromItem = createRelationshipItem(relType, RelationshipDir.FROM, fromid);
    RelationshipItem toItem = createRelationshipItem(relType, RelationshipDir.TO, toid);
    Relationship rel = new Relationship();
    // auto-generated
    if (subm.getRelationship() != null) {
        rel.setUid(subm.getRelationship().getUid());
    }
    rel.setRelationshipType(relType);
    rel.setFrom(fromItem);
    rel.setTo(toItem);
    rel.setCreated(new Date());
    rel.setLastUpdated(new Date());
    // TODO: Are there values we need to account for in relationships?
    relationshipService.addRelationship(rel);
    return SmsResponse.SUCCESS;
}
Also used : Uid(org.hisp.dhis.smscompression.models.Uid) RelationshipSmsSubmission(org.hisp.dhis.smscompression.models.RelationshipSmsSubmission) Relationship(org.hisp.dhis.relationship.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) Date(java.util.Date)

Aggregations

Uid (org.hisp.dhis.smscompression.models.Uid)11 Date (java.util.Date)6 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 User (org.hisp.dhis.user.User)5 ArrayList (java.util.ArrayList)4 ProgramInstance (org.hisp.dhis.program.ProgramInstance)3 ProgramStage (org.hisp.dhis.program.ProgramStage)3 DataElement (org.hisp.dhis.dataelement.DataElement)2 Program (org.hisp.dhis.program.Program)2 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)2 SmsDataValue (org.hisp.dhis.smscompression.models.SmsDataValue)2 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)2 HashMap (java.util.HashMap)1 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)1 CompleteDataSetRegistration (org.hisp.dhis.dataset.CompleteDataSetRegistration)1 DataSet (org.hisp.dhis.dataset.DataSet)1 DataValue (org.hisp.dhis.datavalue.DataValue)1 EventDataValue (org.hisp.dhis.eventdatavalue.EventDataValue)1 Period (org.hisp.dhis.period.Period)1