Search in sources :

Example 1 with SMSParserException

use of org.hisp.dhis.sms.parse.SMSParserException 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 SMSParserException

use of org.hisp.dhis.sms.parse.SMSParserException in project dhis2-core by dhis2.

the class DhisMessageAlertListener method receive.

@Transactional
@Override
public void receive(IncomingSms sms) {
    String message = sms.getText();
    SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.ALERT_PARSER);
    UserGroup userGroup = smsCommand.getUserGroup();
    String senderPhoneNumber = StringUtils.replace(sms.getOriginator(), "+", "");
    if (userGroup != null) {
        Collection<User> users = userService.getUsersByPhoneNumber(senderPhoneNumber);
        if (users != null && users.size() > 1) {
            String messageMoreThanOneUser = smsCommand.getMoreThanOneOrgUnitMessage();
            if (messageMoreThanOneUser.trim().equals("")) {
                messageMoreThanOneUser = SMSCommand.MORE_THAN_ONE_ORGUNIT_MESSAGE;
            }
            for (Iterator<User> i = users.iterator(); i.hasNext(); ) {
                User user = i.next();
                messageMoreThanOneUser += " " + user.getName();
                if (i.hasNext()) {
                    messageMoreThanOneUser += ",";
                }
            }
            throw new SMSParserException(messageMoreThanOneUser);
        } else if (users != null && users.size() == 1) {
            User sender = users.iterator().next();
            Set<User> receivers = new HashSet<>(userGroup.getMembers());
            messageService.sendMessage(smsCommand.getName(), message, null, receivers, sender, MessageType.SYSTEM, false);
            Set<User> feedbackList = new HashSet<>();
            feedbackList.add(sender);
            String confirmMessage = smsCommand.getReceivedMessage();
            if (confirmMessage == null) {
                confirmMessage = SMSCommand.ALERT_FEEDBACK;
            }
            if (smsSender.isConfigured()) {
                smsSender.sendMessage(smsCommand.getName(), confirmMessage, null, null, feedbackList, false);
            } else {
                Log.info("No sms configuration found.");
            }
            sms.setStatus(SmsMessageStatus.PROCESSED);
            sms.setParsed(true);
            incomingSmsService.update(sms);
        } else if (users == null || users.size() == 0) {
            throw new SMSParserException("No user associated with this phone number. Please contact your supervisor.");
        }
    }
}
Also used : User(org.hisp.dhis.user.User) Set(java.util.Set) HashSet(java.util.HashSet) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException) UserGroup(org.hisp.dhis.user.UserGroup) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with SMSParserException

use of org.hisp.dhis.sms.parse.SMSParserException 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 4 with SMSParserException

use of org.hisp.dhis.sms.parse.SMSParserException in project dhis2-core by dhis2.

the class UnregisteredSMSListener method receive.

@Transactional
@Override
public void receive(IncomingSms sms) {
    String message = sms.getText();
    SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.UNREGISTERED_PARSER);
    UserGroup userGroup = smsCommand.getUserGroup();
    String senderPhoneNumber = StringUtils.replace(sms.getOriginator(), "+", "");
    if (userGroup != null) {
        Collection<User> users = userService.getUsersByPhoneNumber(senderPhoneNumber);
        if (users != null && users.size() >= 1) {
            String messageError = "This number is already registered for user: ";
            for (Iterator<User> iterator = users.iterator(); iterator.hasNext(); ) {
                User user = iterator.next();
                messageError += user.getName();
                if (iterator.hasNext()) {
                    messageError += ", ";
                }
            }
            throw new SMSParserException(messageError);
        } else {
            Set<User> receivers = new HashSet<>(userGroup.getMembers());
            UserCredentials anonymousUser = userService.getUserCredentialsByUsername("anonymous");
            if (anonymousUser == null) {
                User user = new User();
                UserCredentials usercredential = new UserCredentials();
                usercredential.setUsername(USER_NAME);
                usercredential.setPassword(USER_NAME);
                usercredential.setUserInfo(user);
                user.setSurname(USER_NAME);
                user.setFirstName(USER_NAME);
                user.setUserCredentials(usercredential);
                userService.addUserCredentials(usercredential);
                userService.addUser(user);
                anonymousUser = userService.getUserCredentialsByUsername("anonymous");
            }
            // forward to user group by SMS, E-mail, DHIS conversation
            messageService.sendMessage(smsCommand.getName(), message, null, receivers, anonymousUser.getUserInfo(), MessageType.SYSTEM, false);
            // confirm SMS was received and forwarded completely
            Set<User> feedbackList = new HashSet<>();
            User sender = new User();
            sender.setPhoneNumber(senderPhoneNumber);
            feedbackList.add(sender);
            smsSender.sendMessage(smsCommand.getName(), smsCommand.getReceivedMessage(), null, null, feedbackList, true);
            sms.setStatus(SmsMessageStatus.PROCESSED);
            sms.setParsed(true);
            incomingSmsService.update(sms);
        }
    }
}
Also used : User(org.hisp.dhis.user.User) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException) UserCredentials(org.hisp.dhis.user.UserCredentials) UserGroup(org.hisp.dhis.user.UserGroup) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with SMSParserException

use of org.hisp.dhis.sms.parse.SMSParserException in project dhis2-core by dhis2.

the class SmsUtils method selectOrganisationUnit.

public static OrganisationUnit selectOrganisationUnit(Collection<OrganisationUnit> orgUnits, Map<String, String> parsedMessage, SMSCommand smsCommand) {
    OrganisationUnit orgUnit = null;
    for (OrganisationUnit o : orgUnits) {
        if (orgUnits.size() == 1) {
            orgUnit = o;
        }
        if (parsedMessage.containsKey("ORG") && o.getCode().equals(parsedMessage.get("ORG"))) {
            orgUnit = o;
            break;
        }
    }
    if (orgUnit == null && orgUnits.size() > 1) {
        String messageListingOrgUnits = smsCommand.getMoreThanOneOrgUnitMessage();
        for (Iterator<OrganisationUnit> i = orgUnits.iterator(); i.hasNext(); ) {
            OrganisationUnit o = i.next();
            messageListingOrgUnits += TextUtils.SPACE + o.getName() + ":" + o.getCode();
            if (i.hasNext()) {
                messageListingOrgUnits += ",";
            }
        }
        throw new SMSParserException(messageListingOrgUnits);
    }
    return orgUnit;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException)

Aggregations

SMSParserException (org.hisp.dhis.sms.parse.SMSParserException)8 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 SMSCommand (org.hisp.dhis.sms.command.SMSCommand)6 Transactional (org.springframework.transaction.annotation.Transactional)6 HashSet (java.util.HashSet)3 SMSCode (org.hisp.dhis.sms.command.code.SMSCode)3 User (org.hisp.dhis.user.User)3 Date (java.util.Date)2 Period (org.hisp.dhis.period.Period)2 UserGroup (org.hisp.dhis.user.UserGroup)2 Set (java.util.Set)1 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)1 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)1 UserCredentials (org.hisp.dhis.user.UserCredentials)1