Skip to content

Commit

Permalink
[BalajiN] #61 sending seperate request for every 10 recipients
Browse files Browse the repository at this point in the history
  • Loading branch information
balajin committed Jun 30, 2011
1 parent 5f6f6f4 commit bd5e17c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;

import static org.apache.commons.lang.StringUtils.join;

@Controller
public class SMSController {

Expand All @@ -40,9 +44,23 @@ public void setLocationSerializer(JSONLocationSerializer locationSerializer) {

@RequestMapping(value = VIEW, method = RequestMethod.POST)
public ModelAndView send(@ModelAttribute WebBulkMessage bulkMessage) {
MessageStatus status = messageService.sendMessage(bulkMessage.content(new SpaceEncoder()), bulkMessage.recipients());
String recipients = bulkMessage.getRecipients();
String[] individualRecipients = recipients.split(",");
MessageStatus statusOfAllBatches = MessageStatus.REJECTED;
int i = 0;

for (i = 0; i < (individualRecipients.length) / 10; i++) {
String[] tenRecipients = Arrays.copyOfRange(individualRecipients, i * 10 , (i * 10) + 10);
statusOfAllBatches = sendMessageToAllRecipients(bulkMessage, statusOfAllBatches, tenRecipients);
}

int leftOutRecipientsStartingIndex = i * 10;
String[] remainingRecipients = Arrays.copyOfRange(individualRecipients, leftOutRecipientsStartingIndex, individualRecipients.length);
statusOfAllBatches = sendMessageToAllRecipients(bulkMessage, statusOfAllBatches, remainingRecipients);

ModelAndView modelAndView = new ModelAndView(VIEW);
WebResponse response = createResponse(status,bulkMessage);
bulkMessage.setRecipients(recipients);
WebResponse response = createResponse(statusOfAllBatches, bulkMessage);
modelAndView.addObject("response", response);
bulkMessage.setRecipients("");
modelAndView.addObject("bulkMessage", bulkMessage);
Expand All @@ -51,7 +69,13 @@ public ModelAndView send(@ModelAttribute WebBulkMessage bulkMessage) {
return modelAndView;
}

private WebResponse createResponse(MessageStatus status,WebBulkMessage message) {
private MessageStatus sendMessageToAllRecipients(WebBulkMessage bulkMessage, MessageStatus statusOfAllBatches, String[] tenRecipients) {
String tenRecipientsTogether = join(tenRecipients, ",");
bulkMessage.setRecipients(tenRecipientsTogether);
return messageService.sendMessage(bulkMessage.content(new SpaceEncoder()), bulkMessage.recipients());
}

private WebResponse createResponse(MessageStatus status, WebBulkMessage message) {

if (MessageStatus.REJECTED.equals(status)) {
return new WebResponse("Message Delivery Failed");
Expand Down
4 changes: 2 additions & 2 deletions motech-server-omod/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ ${moduleName}.Pregnancy.active=Only one active registered pregnancy at a time.
${moduleName}.staffType.required=Staff Type is required.
${moduleName}.birthdate.future=Birth Date can't be in future.
${moduleName}.facility.does.not.exist="Facility does not exist"
${moduleName}.phoneNumber.commaSeparated="Recipents should be a comma seperated list of phone numbers"
${moduleName}.message.required="Message is required"
${moduleName}.phoneNumber.commaSeparated=Recipients should be a comma seperated list of phone numbers
${moduleName}.message.required=Message is required

${moduleName}.ws.notfound={0}=not found
${moduleName}.ws.inuse={0}=in use
Expand Down
3 changes: 2 additions & 1 deletion motech-server-omod/src/main/webapp/resources/sms.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,12 @@ function Country(country) {
};

var validate = function(e){
hideErrors();
if(!$j("#recipients").val()){
$j("#error-number-required").show();
e.preventDefault();
}
if(!$j("#content").val()){
if(!$j("textarea[name='content']").val()){
$j("#error-message-required").show();
e.preventDefault();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,50 @@ public void shouldSendSMS() {
assertTrue(response.getSuccess());
assertEquals("Message delivered successfully to 0123456788,0123456789", response.getText());
}

@Test
public void shouldSendSeparateRequestsForEveryTenSMS() {
expect(contextService.getMotechService()).andReturn(motechService);
expect(motechService.getAllFacilities()).andReturn(Collections.<Facility>emptyList());
expect(motechService.getAllLanguages()).andReturn(Collections.<MessageLanguage>emptyList());

WebBulkMessage message = new WebBulkMessage();
message.setRecipients("0123456788,0123456789,0123456788,0123456789,0123456788,0123456789,0123456788,0123456789,0123456788,0123456789,0123456789");
message.setContent("Hello World");
expect(messageService.sendMessage("Hello%2BWorld", "0123456788,0123456789,0123456788,0123456789,0123456788,0123456789,0123456788,0123456789,0123456788,0123456789")).andReturn(MessageStatus.DELIVERED);
expect(messageService.sendMessage("Hello%2BWorld", "0123456789")).andReturn(MessageStatus.DELIVERED);

replay(messageService, contextService, motechService);

ModelAndView modelAndView = controller.send(message);

verify(messageService, contextService, motechService);

assertEquals("/module/motechmodule/sms", modelAndView.getViewName());
WebResponse response = (WebResponse) modelAndView.getModelMap().get("response");
assertTrue(response.getSuccess());
}

@Test
public void ifOneBatchFailsResultIsFailed() {
expect(contextService.getMotechService()).andReturn(motechService);
expect(motechService.getAllFacilities()).andReturn(Collections.<Facility>emptyList());
expect(motechService.getAllLanguages()).andReturn(Collections.<MessageLanguage>emptyList());

WebBulkMessage message = new WebBulkMessage();
message.setRecipients("0123456788,0123456789,0123456788,0123456789,0123456788,0123456789,0123456788,0123456789,0123456788,0123456789,0123456789");
message.setContent("Hello World");
expect(messageService.sendMessage("Hello%2BWorld", "0123456788,0123456789,0123456788,0123456789,0123456788,0123456789,0123456788,0123456789,0123456788,0123456789")).andReturn(MessageStatus.DELIVERED);
expect(messageService.sendMessage("Hello%2BWorld", "0123456789")).andReturn(MessageStatus.REJECTED);

replay(messageService, contextService, motechService);

ModelAndView modelAndView = controller.send(message);

verify(messageService, contextService, motechService);

assertEquals("/module/motechmodule/sms", modelAndView.getViewName());
WebResponse response = (WebResponse) modelAndView.getModelMap().get("response");
assertNull(response.getSuccess());
}
}

0 comments on commit bd5e17c

Please sign in to comment.