Search in sources :

Example 6 with SMSCode

use of org.hisp.dhis.sms.command.code.SMSCode in project dhis2-core by dhis2.

the class SMSCommandAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    if (selectedCommandID > -1) {
        smsCommand = smsCommandService.getSMSCommand(selectedCommandID);
    }
    if (smsCommand != null && smsCommand.getCodes() != null) {
        for (SMSCode smsCode : smsCommand.getCodes()) {
            if (smsCommand.getParserType() == ParserType.TRACKED_ENTITY_REGISTRATION_PARSER && smsCode.getTrackedEntityAttribute() != null) {
                codes.put("" + smsCode.getTrackedEntityAttribute().getId(), smsCode.getCode());
            } else if (smsCode.getDataElement() != null) {
                codes.put("" + smsCode.getDataElement().getId() + smsCode.getOptionId(), smsCode.getCode());
            }
            if (smsCode.getFormula() != null) {
                formulas.put("" + smsCode.getDataElement().getId() + smsCode.getOptionId(), smsCode.getFormula());
            }
            if (smsCommand.getParserType().equals(ParserType.EVENT_REGISTRATION_PARSER)) {
                codes.put("" + smsCode.getDataElement().getId(), smsCode.getCode());
            }
        }
    }
    userGroupList = new ArrayList<>(userGroupService.getAllUserGroups());
    programList = new ArrayList<>(programService.getPrograms(ProgramType.WITH_REGISTRATION));
    programWithoutRegistration = new ArrayList<>(programService.getPrograms(ProgramType.WITHOUT_REGISTRATION));
    return SUCCESS;
}
Also used : SMSCode(org.hisp.dhis.sms.command.code.SMSCode)

Example 7 with SMSCode

use of org.hisp.dhis.sms.command.code.SMSCode in project dhis2-core by dhis2.

the class DataValueSMSListener method markCompleteDataSet.

private void markCompleteDataSet(String sender, OrganisationUnit orgunit, Map<String, String> parsedMessage, SMSCommand command, Date date) {
    Period period = null;
    int numberOfEmptyValue = 0;
    for (SMSCode code : command.getCodes()) {
        DataElementCategoryOptionCombo optionCombo = dataElementCategoryService.getDataElementCategoryOptionCombo(code.getOptionId());
        period = getPeriod(command, date);
        DataValue dv = dataValueService.getDataValue(code.getDataElement(), period, orgunit, optionCombo);
        if (dv == null && !StringUtils.isEmpty(code.getCode())) {
            numberOfEmptyValue++;
        }
    }
    // Check completeness method
    if (command.getCompletenessMethod() == SMSCommand.RECEIVE_ALL_DATAVALUE) {
        if (numberOfEmptyValue > 0) {
            return;
        }
    } else if (command.getCompletenessMethod() == SMSCommand.RECEIVE_AT_LEAST_ONE_DATAVALUE) {
        if (numberOfEmptyValue == command.getCodes().size()) {
            return;
        }
    } else if (command.getCompletenessMethod() == SMSCommand.DO_NOT_MARK_COMPLETE) {
        return;
    }
    // Go through the complete process
    String storedBy = SmsUtils.getUser(sender, command, userService.getUsersByPhoneNumber(sender)).getUsername();
    if (StringUtils.isBlank(storedBy)) {
        storedBy = "[unknown] from [" + sender + "]";
    }
    // If new values are submitted re-register as complete
    deregisterCompleteDataSet(command.getDataset(), period, orgunit);
    registerCompleteDataSet(command.getDataset(), period, orgunit, storedBy);
}
Also used : DataValue(org.hisp.dhis.datavalue.DataValue) Period(org.hisp.dhis.period.Period) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 8 with SMSCode

use of org.hisp.dhis.sms.command.code.SMSCode in project dhis2-core by dhis2.

the class SingleEventListener method registerEvent.

// -------------------------------------------------------------------------
// Supportive Methods
// -------------------------------------------------------------------------
private void registerEvent(Map<String, String> commandValuePairs, SMSCommand smsCommand, IncomingSms sms) {
    OrganisationUnit orgUnit = getOrganisationUnits(sms).iterator().next();
    List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(smsCommand.getProgram(), ProgramStatus.ACTIVE));
    if (programInstances.isEmpty()) {
        ProgramInstance pi = new ProgramInstance();
        pi.setEnrollmentDate(new Date());
        pi.setIncidentDate(new Date());
        pi.setProgram(smsCommand.getProgram());
        pi.setStatus(ProgramStatus.ACTIVE);
        programInstanceService.addProgramInstance(pi);
        programInstances.add(pi);
    } else if (programInstances.size() > 1) {
        update(sms, SmsMessageStatus.FAILED, false);
        sendFeedback("Multiple active program instances exists for program: " + smsCommand.getProgram().getUid(), sms.getOriginator(), ERROR);
        return;
    }
    ProgramInstance programInstance = null;
    programInstance = programInstances.get(0);
    ProgramStageInstance programStageInstance = new ProgramStageInstance();
    programStageInstance.setOrganisationUnit(orgUnit);
    programStageInstance.setProgramStage(smsCommand.getProgramStage());
    programStageInstance.setProgramInstance(programInstance);
    programStageInstance.setExecutionDate(sms.getSentDate());
    programStageInstance.setDueDate(sms.getSentDate());
    programStageInstance.setAttributeOptionCombo(dataElementCategoryService.getDefaultDataElementCategoryOptionCombo());
    programStageInstance.setCompletedBy("DHIS 2");
    programStageInstanceService.addProgramStageInstance(programStageInstance);
    for (SMSCode smsCode : smsCommand.getCodes()) {
        TrackedEntityDataValue dataValue = new TrackedEntityDataValue();
        dataValue.setAutoFields();
        dataValue.setDataElement(smsCode.getDataElement());
        dataValue.setProgramStageInstance(programStageInstance);
        dataValue.setValue(commandValuePairs.get(smsCode.getCode()));
        trackedEntityDataValueService.saveTrackedEntityDataValue(dataValue);
    }
    update(sms, SmsMessageStatus.PROCESSED, true);
    sendFeedback(StringUtils.defaultIfEmpty(smsCommand.getSuccessMessage(), EVENT_REGISTERED), sms.getOriginator(), INFO);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ArrayList(java.util.ArrayList) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) Date(java.util.Date) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 9 with SMSCode

use of org.hisp.dhis.sms.command.code.SMSCode in project dhis2-core by dhis2.

the class TrackedEntityRegistrationSMSListener method receive.

@Transactional
@Override
public void receive(IncomingSms sms) {
    String message = sms.getText();
    SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.TRACKED_ENTITY_REGISTRATION_PARSER);
    Map<String, String> parsedMessage = this.parse(message, smsCommand);
    Date date = SmsUtils.lookForDate(message);
    String senderPhoneNumber = StringUtils.replace(sms.getOriginator(), "+", "");
    Collection<OrganisationUnit> orgUnits = SmsUtils.getOrganisationUnitsByPhoneNumber(senderPhoneNumber, userService.getUsersByPhoneNumber(senderPhoneNumber));
    if (orgUnits == null || orgUnits.size() == 0) {
        if (StringUtils.isEmpty(smsCommand.getNoUserMessage())) {
            throw new SMSParserException(SMSCommand.NO_USER_MESSAGE);
        } else {
            throw new SMSParserException(smsCommand.getNoUserMessage());
        }
    }
    OrganisationUnit orgUnit = SmsUtils.selectOrganisationUnit(orgUnits, parsedMessage, smsCommand);
    TrackedEntityInstance trackedEntityInstance = new TrackedEntityInstance();
    trackedEntityInstance.setOrganisationUnit(orgUnit);
    trackedEntityInstance.setTrackedEntity(trackedEntityService.getTrackedEntityByName("Person"));
    Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
    for (SMSCode code : smsCommand.getCodes()) {
        if (parsedMessage.containsKey(code.getCode().toUpperCase())) {
            TrackedEntityAttributeValue trackedEntityAttributeValue = this.createTrackedEntityAttributeValue(parsedMessage, code, smsCommand, trackedEntityInstance);
            patientAttributeValues.add(trackedEntityAttributeValue);
        }
    }
    int trackedEntityInstanceId = 0;
    if (patientAttributeValues.size() > 0) {
        trackedEntityInstanceId = trackedEntityInstanceService.createTrackedEntityInstance(trackedEntityInstance, null, null, patientAttributeValues);
    }
    programInstanceService.enrollTrackedEntityInstance(trackedEntityInstanceService.getTrackedEntityInstance(trackedEntityInstanceId), smsCommand.getProgram(), new Date(), date, orgUnit);
    smsSender.sendMessage(null, "Entity Registered Successfully ", senderPhoneNumber);
    sms.setStatus(SmsMessageStatus.PROCESSED);
    sms.setParsed(true);
    incomingSmsService.update(sms);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with SMSCode

use of org.hisp.dhis.sms.command.code.SMSCode in project dhis2-core by dhis2.

the class DataValueSMSListener method sendSuccessFeedback.

protected void sendSuccessFeedback(String sender, SMSCommand command, Map<String, String> parsedMessage, Date date, OrganisationUnit orgunit) {
    String reportBack = "Thank you! Values entered: ";
    String notInReport = "Missing values for: ";
    Period period = null;
    Map<String, DataValue> codesWithDataValues = new TreeMap<>();
    List<String> codesWithoutDataValues = new ArrayList<>();
    for (SMSCode code : command.getCodes()) {
        DataElementCategoryOptionCombo optionCombo = dataElementCategoryService.getDataElementCategoryOptionCombo(code.getOptionId());
        period = getPeriod(command, date);
        DataValue dv = dataValueService.getDataValue(code.getDataElement(), period, orgunit, optionCombo);
        if (dv == null && !StringUtils.isEmpty(code.getCode())) {
            codesWithoutDataValues.add(code.getCode());
        } else if (dv != null) {
            codesWithDataValues.put(code.getCode(), dv);
        }
    }
    for (String key : codesWithDataValues.keySet()) {
        DataValue dv = codesWithDataValues.get(key);
        String value = dv.getValue();
        if (ValueType.BOOLEAN == dv.getDataElement().getValueType()) {
            if ("true".equals(value)) {
                value = "Yes";
            } else if ("false".equals(value)) {
                value = "No";
            }
        }
        reportBack += key + "=" + value + " ";
    }
    Collections.sort(codesWithoutDataValues);
    for (String key : codesWithoutDataValues) {
        notInReport += key + ",";
    }
    notInReport = notInReport.substring(0, notInReport.length() - 1);
    if (smsSender.isConfigured()) {
        if (command.getSuccessMessage() != null && !StringUtils.isEmpty(command.getSuccessMessage())) {
            smsSender.sendMessage(null, command.getSuccessMessage(), sender);
        } else {
            smsSender.sendMessage(null, reportBack, sender);
        }
    } else {
        Log.info("No sms configuration found.");
    }
}
Also used : DataValue(org.hisp.dhis.datavalue.DataValue) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) TreeMap(java.util.TreeMap) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Aggregations

SMSCode (org.hisp.dhis.sms.command.code.SMSCode)10 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)4 Period (org.hisp.dhis.period.Period)4 SMSCommand (org.hisp.dhis.sms.command.SMSCommand)4 Transactional (org.springframework.transaction.annotation.Transactional)4 Date (java.util.Date)3 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)3 DataValue (org.hisp.dhis.datavalue.DataValue)3 SMSParserException (org.hisp.dhis.sms.parse.SMSParserException)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 SMSSpecialCharacter (org.hisp.dhis.sms.command.SMSSpecialCharacter)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 TreeMap (java.util.TreeMap)1 Session (org.hibernate.Session)1 ProgramInstance (org.hisp.dhis.program.ProgramInstance)1 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)1 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)1