Search in sources :

Example 1 with SMSCommand

use of org.hisp.dhis.sms.command.SMSCommand 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 SMSCommand

use of org.hisp.dhis.sms.command.SMSCommand 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 SMSCommand

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

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

the class HibernateSMSCommandStore method getSMSCommand.

@Override
public SMSCommand getSMSCommand(String name) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(SMSCommand.class);
    criteria.add(Restrictions.eq("name", name));
    if (criteria.list() != null && criteria.list().size() > 0) {
        return (SMSCommand) criteria.list().get(0);
    }
    return null;
}
Also used : SMSCommand(org.hisp.dhis.sms.command.SMSCommand) Criteria(org.hibernate.Criteria)

Example 5 with SMSCommand

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

the class SingleEventListener method receive.

@Transactional
@Override
public void receive(IncomingSms sms) {
    SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.EVENT_REGISTRATION_PARSER);
    if (!hasCorrectFormat(sms.getText(), smsCommand)) {
        sendFeedback(StringUtils.defaultIfEmpty(smsCommand.getWrongFormatMessage(), SMSCommand.WRONG_FORMAT_MESSAGE), sms.getOriginator(), ERROR);
        return;
    }
    Map<String, String> commandValuePairs = parseMessageInput(sms, smsCommand);
    if (!validate(commandValuePairs, smsCommand, sms)) {
        return;
    }
    registerEvent(commandValuePairs, smsCommand, sms);
}
Also used : SMSCommand(org.hisp.dhis.sms.command.SMSCommand) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

SMSCommand (org.hisp.dhis.sms.command.SMSCommand)41 Test (org.junit.jupiter.api.Test)17 DhisSpringTest (org.hisp.dhis.DhisSpringTest)14 SMSCode (org.hisp.dhis.sms.command.code.SMSCode)14 User (org.hisp.dhis.user.User)9 Transactional (org.springframework.transaction.annotation.Transactional)8 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 SMSParserException (org.hisp.dhis.sms.parse.SMSParserException)7 SMSSpecialCharacter (org.hisp.dhis.sms.command.SMSSpecialCharacter)6 UserGroup (org.hisp.dhis.user.UserGroup)6 List (java.util.List)5 Set (java.util.Set)5 DataSet (org.hisp.dhis.dataset.DataSet)5 Program (org.hisp.dhis.program.Program)5 ParserType (org.hisp.dhis.sms.parse.ParserType)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 ProgramService (org.hisp.dhis.program.ProgramService)4 SMSCommandService (org.hisp.dhis.sms.command.SMSCommandService)4 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)4