Search in sources :

Example 6 with Uid

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

the class CompressionSMSListener method checkUser.

private void checkUser(SmsSubmission subm) {
    Uid userid = subm.getUserId();
    User user = userService.getUser(userid.getUid());
    if (user == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_USER.set(userid));
    }
}
Also used : Uid(org.hisp.dhis.smscompression.models.Uid) User(org.hisp.dhis.user.User)

Example 7 with Uid

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

the class CompressionSMSListener method saveNewEvent.

protected List<Object> saveNewEvent(String eventUid, OrganisationUnit orgUnit, ProgramStage programStage, ProgramInstance programInstance, IncomingSms sms, CategoryOptionCombo aoc, User user, List<SmsDataValue> values, SmsEventStatus eventStatus, Date eventDate, Date dueDate, GeoPoint coordinates) {
    ArrayList<Object> errorUids = new ArrayList<>();
    ProgramStageInstance programStageInstance;
    if (programStageInstanceService.programStageInstanceExists(eventUid)) {
        programStageInstance = programStageInstanceService.getProgramStageInstance(eventUid);
    } else {
        programStageInstance = new ProgramStageInstance();
        programStageInstance.setUid(eventUid);
    }
    programStageInstance.setOrganisationUnit(orgUnit);
    programStageInstance.setProgramStage(programStage);
    programStageInstance.setProgramInstance(programInstance);
    programStageInstance.setExecutionDate(eventDate);
    programStageInstance.setDueDate(dueDate);
    programStageInstance.setAttributeOptionCombo(aoc);
    programStageInstance.setStoredBy(user.getUsername());
    UserInfoSnapshot currentUserInfo = UserInfoSnapshot.from(user);
    programStageInstance.setCreatedByUserInfo(currentUserInfo);
    programStageInstance.setLastUpdatedByUserInfo(currentUserInfo);
    programStageInstance.setStatus(getCoreEventStatus(eventStatus));
    programStageInstance.setGeometry(convertGeoPointToGeometry(coordinates));
    if (eventStatus.equals(SmsEventStatus.COMPLETED)) {
        programStageInstance.setCompletedBy(user.getUsername());
        programStageInstance.setCompletedDate(new Date());
    }
    Map<DataElement, EventDataValue> dataElementsAndEventDataValues = new HashMap<>();
    if (values != null) {
        for (SmsDataValue dv : values) {
            Uid deid = dv.getDataElement();
            String val = dv.getValue();
            DataElement de = dataElementService.getDataElement(deid.getUid());
            // TODO: Is this the correct way of handling errors here?
            if (de == null) {
                log.warn(String.format("Given data element [%s] could not be found. Continuing with submission...", deid));
                errorUids.add(deid);
                continue;
            } else if (val == null || StringUtils.isEmpty(val)) {
                log.warn(String.format("Value for atttribute [%s] is null or empty. Continuing with submission...", deid));
                continue;
            }
            EventDataValue eventDataValue = new EventDataValue(deid.getUid(), dv.getValue(), currentUserInfo);
            eventDataValue.setAutoFields();
            dataElementsAndEventDataValues.put(de, eventDataValue);
        }
    }
    programStageInstanceService.saveEventDataValuesAndSaveProgramStageInstance(programStageInstance, dataElementsAndEventDataValues);
    return errorUids;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.util.Date) DataElement(org.hisp.dhis.dataelement.DataElement) Uid(org.hisp.dhis.smscompression.models.Uid) SmsDataValue(org.hisp.dhis.smscompression.models.SmsDataValue) UserInfoSnapshot(org.hisp.dhis.program.UserInfoSnapshot) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 8 with Uid

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

the class SimpleEventSMSListener method postProcess.

@Override
protected SmsResponse postProcess(IncomingSms sms, SmsSubmission submission) throws SMSProcessingException {
    SimpleEventSmsSubmission subm = (SimpleEventSmsSubmission) submission;
    Uid ouid = subm.getOrgUnit();
    Uid aocid = subm.getAttributeOptionCombo();
    Uid progid = subm.getEventProgram();
    OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit(ouid.getUid());
    User user = userService.getUser(subm.getUserId().getUid());
    Program program = programService.getProgram(subm.getEventProgram().getUid());
    if (program == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_PROGRAM.set(progid));
    }
    CategoryOptionCombo aoc = categoryService.getCategoryOptionCombo(aocid.getUid());
    if (aoc == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_AOC.set(aocid));
    }
    if (!programService.hasOrgUnit(program, orgUnit)) {
        throw new SMSProcessingException(SmsResponse.OU_NOTIN_PROGRAM.set(ouid, progid));
    }
    List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(program, ProgramStatus.ACTIVE));
    // If it doesn't exist, this is the first event, we can create it here
    if (programInstances.isEmpty()) {
        ProgramInstance pi = new ProgramInstance();
        pi.setEnrollmentDate(new Date());
        pi.setIncidentDate(new Date());
        pi.setProgram(program);
        pi.setStatus(ProgramStatus.ACTIVE);
        programInstanceService.addProgramInstance(pi);
        programInstances.add(pi);
    } else if (programInstances.size() > 1) {
        // TODO: Are we sure this is a problem we can't recover from?
        throw new SMSProcessingException(SmsResponse.MULTI_PROGRAMS.set(progid));
    }
    ProgramInstance programInstance = programInstances.get(0);
    Set<ProgramStage> programStages = programInstance.getProgram().getProgramStages();
    if (programStages.size() > 1) {
        throw new SMSProcessingException(SmsResponse.MULTI_STAGES.set(progid));
    }
    ProgramStage programStage = programStages.iterator().next();
    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 : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ArrayList(java.util.ArrayList) Date(java.util.Date) Uid(org.hisp.dhis.smscompression.models.Uid) SimpleEventSmsSubmission(org.hisp.dhis.smscompression.models.SimpleEventSmsSubmission) ProgramStage(org.hisp.dhis.program.ProgramStage) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 9 with Uid

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

the class DeleteEventSMSListener method postProcess.

@Override
protected SmsResponse postProcess(IncomingSms sms, SmsSubmission submission) throws SMSProcessingException {
    DeleteSmsSubmission subm = (DeleteSmsSubmission) submission;
    Uid eventid = subm.getEvent();
    ProgramStageInstance psi = programStageInstanceService.getProgramStageInstance(eventid.getUid());
    if (psi == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_EVENT.set(eventid));
    }
    programStageInstanceService.deleteProgramStageInstance(psi);
    return SmsResponse.SUCCESS;
}
Also used : Uid(org.hisp.dhis.smscompression.models.Uid) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) DeleteSmsSubmission(org.hisp.dhis.smscompression.models.DeleteSmsSubmission)

Example 10 with Uid

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

the class AggregateDataSetSMSListener method submitDataValues.

private List<Object> submitDataValues(List<SmsDataValue> values, Period period, OrganisationUnit orgUnit, CategoryOptionCombo aoc, User user) {
    ArrayList<Object> errorElems = new ArrayList<>();
    if (values == null) {
        return errorElems;
    }
    for (SmsDataValue smsdv : values) {
        Uid deid = smsdv.getDataElement();
        Uid cocid = smsdv.getCategoryOptionCombo();
        String combid = deid + "-" + cocid;
        DataElement de = dataElementService.getDataElement(deid.getUid());
        if (de == null) {
            log.warn(String.format("Data element [%s] does not exist. Continuing with submission...", deid));
            errorElems.add(combid);
            continue;
        }
        CategoryOptionCombo coc = categoryService.getCategoryOptionCombo(cocid.getUid());
        if (coc == null) {
            log.warn(String.format("Category Option Combo [%s] does not exist. Continuing with submission...", cocid));
            errorElems.add(combid);
            continue;
        }
        String val = smsdv.getValue();
        if (val == null || StringUtils.isEmpty(val)) {
            log.warn(String.format("Value for [%s]  is null or empty. Continuing with submission...", combid));
            continue;
        }
        DataValue dv = dataValueService.getDataValue(de, period, orgUnit, coc, aoc);
        boolean newDataValue = false;
        if (dv == null) {
            dv = new DataValue();
            dv.setCategoryOptionCombo(coc);
            dv.setSource(orgUnit);
            dv.setDataElement(de);
            dv.setPeriod(period);
            dv.setComment("");
            newDataValue = true;
        }
        dv.setValue(val);
        dv.setLastUpdated(new java.util.Date());
        dv.setStoredBy(user.getUsername());
        if (newDataValue) {
            boolean addedDataValue = dataValueService.addDataValue(dv);
            if (!addedDataValue) {
                log.warn(String.format("Failed to submit data value [%s]. Continuing with submission...", combid));
                errorElems.add(combid);
            }
        } else {
            dataValueService.updateDataValue(dv);
        }
    }
    return errorElems;
}
Also used : Uid(org.hisp.dhis.smscompression.models.Uid) DataElement(org.hisp.dhis.dataelement.DataElement) SmsDataValue(org.hisp.dhis.smscompression.models.SmsDataValue) DataValue(org.hisp.dhis.datavalue.DataValue) SmsDataValue(org.hisp.dhis.smscompression.models.SmsDataValue) Date(java.util.Date) ArrayList(java.util.ArrayList) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

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