use of org.hisp.dhis.sms.command.SMSCommand in project dhis2-core by dhis2.
the class DataValueSMSListener method receive.
@Transactional
@Override
public void receive(IncomingSms sms) {
String message = sms.getText();
SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.KEY_VALUE_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);
Period period = getPeriod(smsCommand, date);
if (dataSetService.isLocked(smsCommand.getDataset(), period, orgUnit, null, null)) {
throw new SMSParserException("Dataset is locked for the period " + period.getStartDate() + " - " + period.getEndDate());
}
boolean valueStored = false;
for (SMSCode code : smsCommand.getCodes()) {
if (parsedMessage.containsKey(code.getCode())) {
valueStored = storeDataValue(senderPhoneNumber, orgUnit, parsedMessage, code, smsCommand, date, smsCommand.getDataset());
}
}
if (parsedMessage.isEmpty()) {
if (StringUtils.isEmpty(smsCommand.getDefaultMessage())) {
throw new SMSParserException("No values reported for command '" + smsCommand.getName() + "'");
} else {
throw new SMSParserException(smsCommand.getDefaultMessage());
}
} else if (!valueStored) {
if (StringUtils.isEmpty(smsCommand.getWrongFormatMessage())) {
throw new SMSParserException(SMSCommand.WRONG_FORMAT_MESSAGE);
} else {
throw new SMSParserException(smsCommand.getWrongFormatMessage());
}
}
markCompleteDataSet(senderPhoneNumber, orgUnit, parsedMessage, smsCommand, date);
sendSuccessFeedback(senderPhoneNumber, smsCommand, parsedMessage, date, orgUnit);
sms.setStatus(SmsMessageStatus.PROCESSED);
sms.setParsed(true);
incomingSmsService.update(sms);
}
use of org.hisp.dhis.sms.command.SMSCommand in project dhis2-core by dhis2.
the class DhisMessageAlertListener method receive.
@Transactional
@Override
public void receive(IncomingSms sms) {
String message = sms.getText();
SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.ALERT_PARSER);
UserGroup userGroup = smsCommand.getUserGroup();
String senderPhoneNumber = StringUtils.replace(sms.getOriginator(), "+", "");
if (userGroup != null) {
Collection<User> users = userService.getUsersByPhoneNumber(senderPhoneNumber);
if (users != null && users.size() > 1) {
String messageMoreThanOneUser = smsCommand.getMoreThanOneOrgUnitMessage();
if (messageMoreThanOneUser.trim().equals("")) {
messageMoreThanOneUser = SMSCommand.MORE_THAN_ONE_ORGUNIT_MESSAGE;
}
for (Iterator<User> i = users.iterator(); i.hasNext(); ) {
User user = i.next();
messageMoreThanOneUser += " " + user.getName();
if (i.hasNext()) {
messageMoreThanOneUser += ",";
}
}
throw new SMSParserException(messageMoreThanOneUser);
} else if (users != null && users.size() == 1) {
User sender = users.iterator().next();
Set<User> receivers = new HashSet<>(userGroup.getMembers());
messageService.sendMessage(smsCommand.getName(), message, null, receivers, sender, MessageType.SYSTEM, false);
Set<User> feedbackList = new HashSet<>();
feedbackList.add(sender);
String confirmMessage = smsCommand.getReceivedMessage();
if (confirmMessage == null) {
confirmMessage = SMSCommand.ALERT_FEEDBACK;
}
if (smsSender.isConfigured()) {
smsSender.sendMessage(smsCommand.getName(), confirmMessage, null, null, feedbackList, false);
} else {
Log.info("No sms configuration found.");
}
sms.setStatus(SmsMessageStatus.PROCESSED);
sms.setParsed(true);
incomingSmsService.update(sms);
} else if (users == null || users.size() == 0) {
throw new SMSParserException("No user associated with this phone number. Please contact your supervisor.");
}
}
}
use of org.hisp.dhis.sms.command.SMSCommand in project dhis2-core by dhis2.
the class UnregisteredSMSListener method receive.
@Transactional
@Override
public void receive(IncomingSms sms) {
String message = sms.getText();
SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.UNREGISTERED_PARSER);
UserGroup userGroup = smsCommand.getUserGroup();
String senderPhoneNumber = StringUtils.replace(sms.getOriginator(), "+", "");
if (userGroup != null) {
Collection<User> users = userService.getUsersByPhoneNumber(senderPhoneNumber);
if (users != null && users.size() >= 1) {
String messageError = "This number is already registered for user: ";
for (Iterator<User> iterator = users.iterator(); iterator.hasNext(); ) {
User user = iterator.next();
messageError += user.getName();
if (iterator.hasNext()) {
messageError += ", ";
}
}
throw new SMSParserException(messageError);
} else {
Set<User> receivers = new HashSet<>(userGroup.getMembers());
UserCredentials anonymousUser = userService.getUserCredentialsByUsername("anonymous");
if (anonymousUser == null) {
User user = new User();
UserCredentials usercredential = new UserCredentials();
usercredential.setUsername(USER_NAME);
usercredential.setPassword(USER_NAME);
usercredential.setUserInfo(user);
user.setSurname(USER_NAME);
user.setFirstName(USER_NAME);
user.setUserCredentials(usercredential);
userService.addUserCredentials(usercredential);
userService.addUser(user);
anonymousUser = userService.getUserCredentialsByUsername("anonymous");
}
// forward to user group by SMS, E-mail, DHIS conversation
messageService.sendMessage(smsCommand.getName(), message, null, receivers, anonymousUser.getUserInfo(), MessageType.SYSTEM, false);
// confirm SMS was received and forwarded completely
Set<User> feedbackList = new HashSet<>();
User sender = new User();
sender.setPhoneNumber(senderPhoneNumber);
feedbackList.add(sender);
smsSender.sendMessage(smsCommand.getName(), smsCommand.getReceivedMessage(), null, null, feedbackList, true);
sms.setStatus(SmsMessageStatus.PROCESSED);
sms.setParsed(true);
incomingSmsService.update(sms);
}
}
}
use of org.hisp.dhis.sms.command.SMSCommand in project dhis2-core by dhis2.
the class HibernateSMSCommandStore method getSMSCommand.
@Override
public SMSCommand getSMSCommand(String name) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(SMSCommand.class);
criteria.add(Restrictions.eq("name", name));
if (criteria.list() != null && criteria.list().size() > 0) {
return (SMSCommand) criteria.list().get(0);
}
return null;
}
use of org.hisp.dhis.sms.command.SMSCommand in project dhis2-core by dhis2.
the class SingleEventListener method receive.
@Transactional
@Override
public void receive(IncomingSms sms) {
SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.EVENT_REGISTRATION_PARSER);
if (!hasCorrectFormat(sms.getText(), smsCommand)) {
sendFeedback(StringUtils.defaultIfEmpty(smsCommand.getWrongFormatMessage(), SMSCommand.WRONG_FORMAT_MESSAGE), sms.getOriginator(), ERROR);
return;
}
Map<String, String> commandValuePairs = parseMessageInput(sms, smsCommand);
if (!validate(commandValuePairs, smsCommand, sms)) {
return;
}
registerEvent(commandValuePairs, smsCommand, sms);
}
Aggregations