Search in sources :

Example 16 with SMSCommand

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

the class DataValueListenerTest method setUpInstances.

private void setUpInstances() {
    organisationUnitA = createOrganisationUnit('O');
    organisationUnitB = createOrganisationUnit('P');
    dataSet = createDataSet('D');
    dataSetB = createDataSet('B');
    dataSet.addOrganisationUnit(organisationUnitA);
    dataSet.addOrganisationUnit(organisationUnitB);
    period = createPeriod(new Date(), new Date());
    user = createUser('U');
    user.setPhoneNumber(ORIGINATOR);
    user.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    userB = createUser('B');
    userB.setPhoneNumber(ORIGINATOR);
    userB.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    userC = createUser('C');
    userC.setPhoneNumber(ORIGINATOR);
    userC.setOrganisationUnits(Sets.newHashSet(organisationUnitA, organisationUnitB));
    userWithNoOu = createUser('V');
    userWithNoOu.setPhoneNumber(null);
    userWithNoOu.setOrganisationUnits(null);
    userwithMultipleOu = createUser('W');
    userwithMultipleOu.setPhoneNumber(ORIGINATOR);
    userwithMultipleOu.setOrganisationUnits(Sets.newHashSet(organisationUnitA, organisationUnitB));
    dataElement = createDataElement('D');
    dataElement.setValueType(ValueType.TEXT);
    defaultCategoryOptionCombo = createCategoryOptionCombo('D');
    categoryOptionCombo = createCategoryOptionCombo('C');
    dataElementB = createDataElement('B');
    dataElementB.setValueType(ValueType.TEXT);
    fetchedDataValue = createDataValue(dataElement, period, organisationUnitA, FETCHED_DATA_VALUE, categoryOptionCombo);
    fetchedCompleteDataSetRegistration = new CompleteDataSetRegistration(dataSet, period, organisationUnitA, categoryOptionCombo, new Date(), STORED_BY, new Date(), LAST_UPDATED_BY, true);
    smsCode = new SMSCode();
    smsCode.setCode("de");
    smsCode.setDataElement(dataElement);
    smsCode.setOptionId(defaultCategoryOptionCombo);
    smsCodeForcompulsory = new SMSCode();
    smsCodeForcompulsory.setCode("deb");
    smsCodeForcompulsory.setDataElement(dataElementB);
    smsCodeForcompulsory.setOptionId(categoryOptionCombo);
    smsCodeForcompulsory.setCompulsory(true);
    smsSpecialCharacter = new SMSSpecialCharacter();
    smsSpecialCharacter.setName("special");
    smsSpecialCharacter.setValue("spc1");
    keyValueCommand = new SMSCommand();
    keyValueCommand.setName(DATA_ENTRY_COMMAND);
    keyValueCommand.setParserType(ParserType.KEY_VALUE_PARSER);
    keyValueCommand.setDataset(dataSet);
    keyValueCommand.setCodes(Sets.newHashSet(smsCode));
    keyValueCommand.setSuccessMessage(SUCCESS_MESSAGE);
    response = new OutboundMessageResponse();
    response.setResponseObject(GatewayResponse.SUCCESS);
    response.setOk(true);
    incomingSms = new IncomingSms();
    incomingSms.setText(SMS_TEXT);
    incomingSms.setOriginator(ORIGINATOR);
    incomingSms.setCreatedBy(user);
    incomingSmsForCompulsoryCode = new IncomingSms();
    incomingSmsForCompulsoryCode.setText(SMS_TEXT_FOR_COMPULSORY);
    incomingSmsForCompulsoryCode.setOriginator(ORIGINATOR);
    incomingSmsForCompulsoryCode.setCreatedBy(user);
    incomingSmsForCustomSeparator = new IncomingSms();
    incomingSmsForCustomSeparator.setText(SMS_TEXT_FOR_CUSTOM_SEPARATOR);
    incomingSmsForCustomSeparator.setOriginator(ORIGINATOR);
    incomingSmsForCustomSeparator.setCreatedBy(user);
}
Also used : IncomingSms(org.hisp.dhis.sms.incoming.IncomingSms) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) OutboundMessageResponse(org.hisp.dhis.outboundmessage.OutboundMessageResponse) SMSSpecialCharacter(org.hisp.dhis.sms.command.SMSSpecialCharacter) Date(java.util.Date)

Example 17 with SMSCommand

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

the class TrackedEntityRegistrationListenerTest method setUpInstances.

private void setUpInstances() {
    trackedEntityType = createTrackedEntityType('T');
    organisationUnit = createOrganisationUnit('O');
    program = createProgram('P');
    user = createUser('U');
    user.setPhoneNumber(ORIGINATOR);
    user.setOrganisationUnits(Sets.newHashSet(organisationUnit));
    programTrackedEntityAttribute = createProgramTrackedEntityAttribute(program, trackedEntityAttribute);
    trackedEntityAttribute = createTrackedEntityAttribute('A', ValueType.TEXT);
    program.getProgramAttributes().add(programTrackedEntityAttribute);
    program.getOrganisationUnits().add(organisationUnit);
    program.setTrackedEntityType(trackedEntityType);
    trackedEntityInstance = createTrackedEntityInstance(organisationUnit);
    trackedEntityInstance.getTrackedEntityAttributeValues().add(trackedEntityAttributeValue);
    trackedEntityInstance.setOrganisationUnit(organisationUnit);
    trackedEntityAttributeValue = createTrackedEntityAttributeValue('A', trackedEntityInstance, trackedEntityAttribute);
    trackedEntityAttributeValue.setValue(ATTRIBUTE_VALUE);
    smsCode = new SMSCode();
    smsCode.setCode("attr");
    smsCode.setTrackedEntityAttribute(trackedEntityAttribute);
    teiRegistrationCommand = new SMSCommand();
    teiRegistrationCommand.setName(TEI_REGISTRATION_COMMAND);
    teiRegistrationCommand.setParserType(ParserType.TRACKED_ENTITY_REGISTRATION_PARSER);
    teiRegistrationCommand.setProgram(program);
    teiRegistrationCommand.setCodes(Sets.newHashSet(smsCode));
    incomingSms = new IncomingSms();
    incomingSms.setText(SMS_TEXT);
    incomingSms.setOriginator(ORIGINATOR);
    incomingSms.setCreatedBy(user);
}
Also used : IncomingSms(org.hisp.dhis.sms.incoming.IncomingSms) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) SMSCode(org.hisp.dhis.sms.command.code.SMSCode)

Example 18 with SMSCommand

use of org.hisp.dhis.sms.command.SMSCommand 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 = getOrganisationUnits(sms);
    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())) {
            storeDataValue(sms, orgUnit, parsedMessage, code, smsCommand, period);
            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 19 with SMSCommand

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

the class TrackedEntityRegistrationSMSListener method postProcess.

// -------------------------------------------------------------------------
// IncomingSmsListener implementation
// -------------------------------------------------------------------------
@Override
protected void postProcess(IncomingSms sms, SMSCommand smsCommand, Map<String, String> parsedMessage) {
    String message = sms.getText();
    Date date = SmsUtils.lookForDate(message);
    String senderPhoneNumber = StringUtils.replace(sms.getOriginator(), "+", "");
    Collection<OrganisationUnit> orgUnits = getOrganisationUnits(sms);
    Program program = smsCommand.getProgram();
    OrganisationUnit orgUnit = SmsUtils.selectOrganisationUnit(orgUnits, parsedMessage, smsCommand);
    if (!programService.hasOrgUnit(program, orgUnit)) {
        sendFeedback(SMSCommand.NO_OU_FOR_PROGRAM, senderPhoneNumber, WARNING);
        throw new SMSParserException(SMSCommand.NO_OU_FOR_PROGRAM);
    }
    TrackedEntityInstance trackedEntityInstance = new TrackedEntityInstance();
    trackedEntityInstance.setOrganisationUnit(orgUnit);
    trackedEntityInstance.setTrackedEntityType(trackedEntityTypeService.getTrackedEntityByName(smsCommand.getProgram().getTrackedEntityType().getName()));
    Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
    smsCommand.getCodes().stream().filter(code -> parsedMessage.containsKey(code.getCode())).forEach(code -> {
        TrackedEntityAttributeValue trackedEntityAttributeValue = this.createTrackedEntityAttributeValue(parsedMessage, code, trackedEntityInstance);
        patientAttributeValues.add(trackedEntityAttributeValue);
    });
    long trackedEntityInstanceId = 0;
    if (patientAttributeValues.size() > 0) {
        trackedEntityInstanceId = trackedEntityInstanceService.createTrackedEntityInstance(trackedEntityInstance, patientAttributeValues);
    } else {
        sendFeedback("No TrackedEntityAttribute found", senderPhoneNumber, WARNING);
    }
    TrackedEntityInstance tei = trackedEntityInstanceService.getTrackedEntityInstance(trackedEntityInstanceId);
    programInstanceService.enrollTrackedEntityInstance(tei, smsCommand.getProgram(), new Date(), date, orgUnit);
    sendFeedback(StringUtils.defaultIfBlank(smsCommand.getSuccessMessage(), SUCCESS_MESSAGE + tei.getUid()), senderPhoneNumber, INFO);
    update(sms, SmsMessageStatus.PROCESSED, true);
}
Also used : MessageSender(org.hisp.dhis.message.MessageSender) CategoryService(org.hisp.dhis.category.CategoryService) TrackedEntityTypeService(org.hisp.dhis.trackedentity.TrackedEntityTypeService) Date(java.util.Date) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) StringUtils(org.apache.commons.lang3.StringUtils) Program(org.hisp.dhis.program.Program) HashSet(java.util.HashSet) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) SmsMessageStatus(org.hisp.dhis.sms.incoming.SmsMessageStatus) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException) Map(java.util.Map) Qualifier(org.springframework.beans.factory.annotation.Qualifier) IncomingSmsService(org.hisp.dhis.sms.incoming.IncomingSmsService) ParserType(org.hisp.dhis.sms.parse.ParserType) SmsUtils(org.hisp.dhis.system.util.SmsUtils) UserService(org.hisp.dhis.user.UserService) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Set(java.util.Set) IncomingSms(org.hisp.dhis.sms.incoming.IncomingSms) TrackedEntityInstanceService(org.hisp.dhis.trackedentity.TrackedEntityInstanceService) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Component(org.springframework.stereotype.Component) SMSCommandService(org.hisp.dhis.sms.command.SMSCommandService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) ProgramService(org.hisp.dhis.program.ProgramService) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Transactional(org.springframework.transaction.annotation.Transactional) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) HashSet(java.util.HashSet)

Example 20 with SMSCommand

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

the class SMSCommandObjectBundleServiceTest method textCreateSMSCommandWithCorrectCOCId.

@Test
public void textCreateSMSCommandWithCorrectCOCId() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/import/metadata_with_sms_commands.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    validate.forEachErrorReport(System.out::println);
    assertFalse(validate.hasErrorReports());
    objectBundleService.commit(bundle);
    SMSCommand smsCommand = manager.get(SMSCommand.class, "em8Bg4LCr1a");
    Set<SMSCode> smsCodes = smsCommand.getCodes();
    SMSCode smsCode1 = smsCodes.stream().filter(c -> c.getCode().equals("a")).findFirst().get();
    SMSCode smsCode2 = smsCodes.stream().filter(c -> c.getCode().equals("b")).findFirst().get();
    assertSame(smsCode1.getOptionId(), categoryService.getDefaultCategoryOptionCombo());
    assertSame(smsCode2.getOptionId(), categoryService.getCategoryOptionCombo(smsCode2.getOptionId().getId()));
}
Also used : ObjectBundleParams(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleParams) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) List(java.util.List) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

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