Search in sources :

Example 21 with SMSCommand

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

the class SmsInboundController method getUserByPhoneNumber.

// -------------------------------------------------------------------------
// SUPPORTIVE METHOD
// -------------------------------------------------------------------------
private User getUserByPhoneNumber(String phoneNumber, String text, User currentUser) throws WebMessageException {
    SMSCommand unregisteredParser = smsCommandService.getSMSCommand(SmsUtils.getCommandString(text), ParserType.UNREGISTERED_PARSER);
    List<User> users = userService.getUsersByPhoneNumber(phoneNumber);
    if (SmsUtils.isBase64(text)) {
        return handleCompressedCommands(currentUser, phoneNumber);
    }
    if (users == null || users.isEmpty()) {
        if (unregisteredParser != null) {
            return null;
        }
        // No user belong to this phone number
        throw new WebMessageException(conflict("User's phone number is not registered in the system"));
    }
    return users.iterator().next();
}
Also used : CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) SMSCommand(org.hisp.dhis.sms.command.SMSCommand)

Example 22 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(int id) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(SMSCommand.class);
    criteria.add(Restrictions.eq("id", id));
    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 23 with SMSCommand

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

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

the class ProgramStageDataEntrySMSListener method receive.

@Transactional
@Override
public void receive(IncomingSms sms) {
    String message = sms.getText();
    SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.PROGRAM_STAGE_DATAENTRY_PARSER);
    this.parse(message, smsCommand);
    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());
        }
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 25 with SMSCommand

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

the class CommandSMSListener method receive.

@Override
public void receive(IncomingSms sms) {
    SMSCommand smsCommand = getSMSCommand(sms);
    Map<String, String> parsedMessage = this.parseMessageInput(sms, smsCommand);
    if (!hasCorrectFormat(sms, smsCommand) || !validateInputValues(parsedMessage, smsCommand, sms)) {
        return;
    }
    postProcess(sms, smsCommand, parsedMessage);
}
Also used : SMSCommand(org.hisp.dhis.sms.command.SMSCommand)

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