Search in sources :

Example 6 with SMSParserException

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

the class SmsUtils method getUser.

public static User getUser(String sender, SMSCommand smsCommand, List<User> userList) {
    OrganisationUnit orgunit = null;
    User user = null;
    for (User u : userList) {
        OrganisationUnit ou = u.getOrganisationUnit();
        if (ou != null) {
            if (orgunit == null) {
                orgunit = ou;
            } else if (orgunit.getId() == ou.getId()) {
            } else {
                if (StringUtils.isEmpty(smsCommand.getMoreThanOneOrgUnitMessage())) {
                    throw new SMSParserException(SMSCommand.MORE_THAN_ONE_ORGUNIT_MESSAGE);
                } else {
                    throw new SMSParserException(smsCommand.getMoreThanOneOrgUnitMessage());
                }
            }
        }
        user = u;
    }
    if (user == null) {
        throw new SMSParserException("User is not associated with any orgunit. Please contact your supervisor.");
    }
    return user;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException)

Example 7 with SMSParserException

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

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

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