return handleSending(mapping, form, req, resp, calendar.getTime());
}
/* handles all form evaluation and preparing of sending for direct and scheduled sending */
private ActionForward handleSending(ActionMapping mapping, ActionForm form,HttpServletRequest req, HttpServletResponse resp, Date sendAt) {
SmsSendForm smsSendForm = (SmsSendForm)form;
if (isTokenValid(req)) {
resetToken(req);
saveToken(req);
} else {
String invalidToken = getResources(req).getMessage(locale,"smssvc.invalidTokenOnSend");
req.setAttribute("generalError", invalidToken);
// reset also confirmation pending because enter will not be called
smsSendForm.setSendConfirmationPending(false);
return mapping.findForward("invalidToken");
}
Group group = getCurrentGroup(req);
Sender sender = validSenderFromForm(req, smsSendForm,group);
if (!validText(smsSendForm)){
String noMessageOrLinkString = getResources(req).getMessage(locale,"smssvc.noMessageOrLink");
req.setAttribute("generalError",noMessageOrLinkString);
return mapping.findForward("notSent");
}
SmsTextValidator.checkForValidLatin1AndSmsAllowingPlaceholders(smsSendForm.getMessage(), locale);
SmsSourceAddress fromAddress = new SmsSourceAddress(sender, webUser);
Long userSetId = smsSendForm.getUserSetId();
Long userId = smsSendForm.getUserId();
Set<User> users=null;
Integer destinationSelection = smsSendForm.getDestinationSelection();
switch (destinationSelection==null?0:destinationSelection){
case 1: // user set
UserSetManager userSetManager = new UserSetManager(locale,session);
if (userSetId.longValue() == -1){
String noUserSetSelectedString = getResources(req).getMessage(locale,"smssvc.MessageNotSentBecauseNoUserSetSelected");
req.setAttribute("generalError",noUserSetSelectedString);
return mapping.findForward("notSent");
}
UserSet userSet = userSetManager.getUserSet(userSetId);
if (userSet == null){
throw new ConfigurationException("illegal user set id: " + userSetId);
}
users = userSet.getUsers();
break;
case 2: // user
if (userId.longValue() == -1){
String noUserSelectedString = getResources(req).getMessage(locale,"smssvc.MessageNotSentBecauseNoUserSelected");
req.setAttribute("generalError",noUserSelectedString);
return mapping.findForward("notSent");
}
UserManager userManager = new UserManager(locale,session);
User user = userManager.getUserDetails(userId);
if (user == null){
throw new ConfigurationException("illegal user id: " + user);
}
Set<User> oneManSet = new HashSet<User>();
oneManSet.add(user);
users = oneManSet;
break;
case 3: // MSISDN
String countryCode = smsSendForm.getCountryCode();
String mobileNumber = smsSendForm.getMobileNumber();
if (countryCode == null || mobileNumber == null || countryCode.length() <= 0 || mobileNumber.length() <= 0){
String missingCountryCodeOrPhoneNumber = getResources(req).getMessage(locale,"smssvc.missingCountryCodeOrPhoneNumber");
req.setAttribute("generalError",missingCountryCodeOrPhoneNumber);
return mapping.findForward("notSent");
}
try {
long countryCodeNumber = Long.parseLong(countryCode);
long mobileNumberNumber = Long.parseLong(mobileNumber);
long msisdn = Long.parseLong(countryCodeNumber + "" + mobileNumberNumber); // ignores leading zeros
if (!smsSendForm.isSendConfirmationPending() && smsSendForm.getMessage() != null && smsSendForm.getMessage().length() > 160) {
String confirmMsg = getResources(req).getMessage(locale, "smssvc.ConfirmConcatenatedSMS.");
confirmMsg += " ";
confirmMsg += getResources(req).getMessage(locale, "smssvc.ConfirmSending.");
smsSendForm.setSendConfirmationPending(true);
req.setAttribute("generalError", confirmMsg);
return mapping.findForward("confirm");
} else {
SmsDestinationAddress receiver = new SmsDestinationAddress(msisdn);
SortedSet<SmsDestinationAddress> receivers = new TreeSet<SmsDestinationAddress>();
receivers.add(receiver);
MessagingManager msgMgr = new MessagingManager(locale, session);
msgMgr.sendSms(fromAddress, receivers, getCurrentGroup(req), smsSendForm.getMessage(), sendAt);
Date date = new Date();
String formattedDate = DateFormat.getTimeInstance(DateFormat.MEDIUM,locale).format(date);
String generalInformation = getResources(req).getMessage(locale, "smssvc.asyncSendStarted", new Object[] {new Integer(receivers.size()), formattedDate});
req.setAttribute("generalInformation", generalInformation);
return mapping.findForward("sent");
}
} catch (NumberFormatException e){
throw new BugException("validation of countryCode and mobileNumber failed");
}
default:
String noDestinationSelectedString = getResources(req).getMessage(locale,"smssvc.noDestinationSelected");
req.setAttribute("generalError",noDestinationSelectedString);
return mapping.findForward("notSent");
}
if (!smsSendForm.isSendConfirmationPending()) {
String confirmMsg = "";
if (users != null && users.size() >= CONFIRMATION_MINIMUM) {
confirmMsg += getResources(req).getMessage(locale, "smssvc.ConfirmSendToManyReceivers.", users.size());
}
if (smsSendForm.getMessage() != null && smsSendForm.getMessage().length() > 160) {
if (confirmMsg.length() > 0) {
confirmMsg += " ";
}
confirmMsg += getResources(req).getMessage(locale, "smssvc.ConfirmConcatenatedSMS.");
}
if (confirmMsg.length() > 0) {
confirmMsg += " ";
confirmMsg += getResources(req).getMessage(locale, "smssvc.ConfirmSending.");
smsSendForm.setSendConfirmationPending(true);
req.setAttribute("generalError", confirmMsg);
return mapping.findForward("confirm");
}
}
smsSendForm.setSendConfirmationPending(false);
return sendToUsersWithMsisdn(fromAddress, users, mapping,form,req,resp, sendAt);
}