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);
}
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);
}
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);
}
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);
}
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()));
}
Aggregations