Search in sources :

Example 1 with SMSCode

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

the class DataValueSMSListener method receive.

@Transactional
@Override
public void receive(IncomingSms sms) {
    String message = sms.getText();
    SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.KEY_VALUE_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);
    Period period = getPeriod(smsCommand, date);
    if (dataSetService.isLocked(smsCommand.getDataset(), period, orgUnit, null, null)) {
        throw new SMSParserException("Dataset is locked for the period " + period.getStartDate() + " - " + period.getEndDate());
    }
    boolean valueStored = false;
    for (SMSCode code : smsCommand.getCodes()) {
        if (parsedMessage.containsKey(code.getCode())) {
            valueStored = storeDataValue(senderPhoneNumber, orgUnit, parsedMessage, code, smsCommand, date, smsCommand.getDataset());
        }
    }
    if (parsedMessage.isEmpty()) {
        if (StringUtils.isEmpty(smsCommand.getDefaultMessage())) {
            throw new SMSParserException("No values reported for command '" + smsCommand.getName() + "'");
        } else {
            throw new SMSParserException(smsCommand.getDefaultMessage());
        }
    } else if (!valueStored) {
        if (StringUtils.isEmpty(smsCommand.getWrongFormatMessage())) {
            throw new SMSParserException(SMSCommand.WRONG_FORMAT_MESSAGE);
        } else {
            throw new SMSParserException(smsCommand.getWrongFormatMessage());
        }
    }
    markCompleteDataSet(senderPhoneNumber, orgUnit, parsedMessage, smsCommand, date);
    sendSuccessFeedback(senderPhoneNumber, smsCommand, parsedMessage, date, orgUnit);
    sms.setStatus(SmsMessageStatus.PROCESSED);
    sms.setParsed(true);
    incomingSmsService.update(sms);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException) Period(org.hisp.dhis.period.Period) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with SMSCode

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

the class J2MEDataValueSMSListener method receive.

@Transactional
@Override
public void receive(IncomingSms sms) {
    String message = sms.getText();
    SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.J2ME_PARSER);
    String[] token = message.split("!");
    Map<String, String> parsedMessage = this.parse(token[1], smsCommand);
    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);
    Period period = this.getPeriod(token[0].trim(), smsCommand.getDataset().getPeriodType());
    boolean valueStored = false;
    for (SMSCode code : smsCommand.getCodes()) {
        if (parsedMessage.containsKey(code.getCode().toUpperCase())) {
            storeDataValue(senderPhoneNumber, orgUnit, parsedMessage, code, smsCommand, period, smsCommand.getDataset());
            valueStored = true;
        }
    }
    if (parsedMessage.isEmpty() || !valueStored) {
        if (StringUtils.isEmpty(smsCommand.getDefaultMessage())) {
            throw new SMSParserException("No values reported for command '" + smsCommand.getName() + "'");
        } else {
            throw new SMSParserException(smsCommand.getDefaultMessage());
        }
    }
    this.registerCompleteDataSet(smsCommand.getDataset(), period, orgUnit, "mobile");
    this.sendSuccessFeedback(senderPhoneNumber, smsCommand, parsedMessage, period, orgUnit);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException) Period(org.hisp.dhis.period.Period) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with SMSCode

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

the class J2MEDataValueSMSListener method sendSuccessFeedback.

private void sendSuccessFeedback(String sender, SMSCommand command, Map<String, String> parsedMessage, Period period, OrganisationUnit orgunit) {
    String reportBack = "Thank you! Values entered: ";
    String notInReport = "Missing values for: ";
    boolean missingElements = false;
    for (SMSCode code : command.getCodes()) {
        DataElementCategoryOptionCombo optionCombo = dataElementCategoryService.getDataElementCategoryOptionCombo(code.getOptionId());
        DataValue dv = dataValueService.getDataValue(code.getDataElement(), period, orgunit, optionCombo);
        if (dv == null && !StringUtils.isEmpty(code.getCode())) {
            notInReport += code.getCode() + ",";
            missingElements = true;
        } else if (dv != null) {
            String value = dv.getValue();
            if (ValueType.BOOLEAN == dv.getDataElement().getValueType()) {
                if ("true".equals(value)) {
                    value = "Yes";
                } else if ("false".equals(value)) {
                    value = "No";
                }
            }
            reportBack += code.getCode() + "=" + value + " ";
        }
    }
    notInReport = notInReport.substring(0, notInReport.length() - 1);
    if (missingElements) {
        reportBack += notInReport;
    }
    if (command.getSuccessMessage() != null && !StringUtils.isEmpty(command.getSuccessMessage())) {
        reportBack = command.getSuccessMessage();
    }
    smsSender.sendMessage(null, reportBack, sender);
}
Also used : DataValue(org.hisp.dhis.datavalue.DataValue) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 4 with SMSCode

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

the class HibernateSMSCommandStore method delete.

@Override
@Transactional
public void delete(SMSCommand cmd) {
    Session session = sessionFactory.getCurrentSession();
    for (SMSCode x : cmd.getCodes()) {
        session.delete(x);
    }
    for (SMSSpecialCharacter x : cmd.getSpecialCharacters()) {
        session.delete(x);
    }
    session.delete(cmd);
}
Also used : SMSCode(org.hisp.dhis.sms.command.code.SMSCode) SMSSpecialCharacter(org.hisp.dhis.sms.command.SMSSpecialCharacter) Session(org.hibernate.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with SMSCode

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

the class EditSMSCommandForm method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    Set<SMSCode> codeSet = new HashSet<>();
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readValue(codeDataelementOption, JsonNode.class);
    JsonNode codes = root.get("codes");
    if (codes != null && codes.size() > 0) {
        codes.iterator().forEachRemaining(code -> {
            if (code.get("code") != null && code.get("dataElementId") != null && code.get("optionId") != null && code.get("formula") != null) {
                SMSCode c = new SMSCode();
                c.setCode(code.get("code").asText());
                c.setDataElement(dataElementService.getDataElement(code.get("dataElementId").asInt()));
                c.setOptionId(code.get("optionId").asInt());
                if (!StringUtils.isEmpty(code.get("formula").asText())) {
                    c.setFormula(code.get("formula").asText());
                } else {
                    c.setFormula(null);
                }
                codeSet.add(c);
            }
        });
    }
    Set<SMSSpecialCharacter> specialCharacterSet = new HashSet<>();
    root = mapper.readValue(specialCharactersInfo, JsonNode.class);
    JsonNode specialChars = root.get("specialCharacters");
    if (specialChars != null && StringUtils.isNoneEmpty(specialChars.toString())) {
        specialCharacterSet = mapper.readValue(specialChars.toString(), new TypeReference<HashSet<SMSSpecialCharacter>>() {
        });
        smsCommandService.saveSpecialCharacterSet(specialCharacterSet);
    }
    SMSCommand command = getSMSCommand();
    if (selectedDataSetID > -1 && command != null) {
        if (command.getParserType() == ParserType.TRACKED_ENTITY_REGISTRATION_PARSER) {
            root = mapper.readValue(trackedEntityAttributeCodes, JsonNode.class);
            JsonNode regCodes = root.get("trackedEntityAttributeCodes");
            if (regCodes != null && regCodes.size() > 0) {
                regCodes.iterator().forEachRemaining(regCode -> {
                    if (regCode.get("code") != null && regCode.get("trackedEntityAttributeId") != null) {
                        SMSCode c = new SMSCode();
                        c.setCode(regCode.get("code").asText());
                        c.setTrackedEntityAttribute(trackedEntityAttributeService.getTrackedEntityAttribute(regCode.get("trackedEntityAttributeId").asInt()));
                        codeSet.add(c);
                    }
                });
            }
        }
        if (command.getParserType() == ParserType.EVENT_REGISTRATION_PARSER) {
            root = mapper.readValue(programStageDataElementCodes, JsonNode.class);
            JsonNode regCodes = root.get("programStageDataElementCodes");
            if (regCodes != null && regCodes.size() > 0) {
                regCodes.iterator().forEachRemaining(regCode -> {
                    if (regCode.get("code") != null && regCode.get("programStageDataElementId") != null) {
                        SMSCode c = new SMSCode();
                        c.setCode(regCode.get("code").asText());
                        c.setDataElement(dataElementService.getDataElement(regCode.get("programStageDataElementId").asInt()));
                        c.setCompulsory(regCode.get("compulsory").asBoolean());
                        codeSet.add(c);
                    }
                });
            }
        }
    }
    if (codeSet.size() > 0) {
        smsCommandService.save(codeSet);
    }
    if (selectedDataSetID > -1 && command != null) {
        command.setCurrentPeriodUsedForReporting(currentPeriodUsedForReporting);
        command.setName(name);
        command.setSeparator(separator);
        if (completenessMethod != null) {
            command.setCompletenessMethod(completenessMethod);
        }
        // remove codes
        Set<SMSCode> toRemoveCodes = command.getCodes();
        smsCommandService.deleteCodeSet(toRemoveCodes);
        // remove special characters
        Set<SMSSpecialCharacter> toRemoveCharacters = command.getSpecialCharacters();
        smsCommandService.deleteSpecialCharacterSet(toRemoveCharacters);
        command.setCodes(codeSet);
        // message
        command.setDefaultMessage(defaultMessage);
        command.setReceivedMessage(receivedMessage);
        command.setMoreThanOneOrgUnitMessage(moreThanOneOrgUnitMessage);
        command.setNoUserMessage(noUserMessage);
        command.setWrongFormatMessage(wrongFormatMessage);
        command.setSuccessMessage(successMessage);
        if (userGroupID != null && userGroupID > -1) {
            command.setUserGroup(userGroupService.getUserGroup(userGroupID));
        }
        command.setSpecialCharacters(specialCharacterSet);
        smsCommandService.save(command);
    }
    return SUCCESS;
}
Also used : SMSCommand(org.hisp.dhis.sms.command.SMSCommand) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeReference(com.fasterxml.jackson.core.type.TypeReference) SMSSpecialCharacter(org.hisp.dhis.sms.command.SMSSpecialCharacter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

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