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