public void sendMailMessages(NSArray mailMessages) {
if (mailMessages.count() > 0) {
log.info("Sending " + mailMessages.count() + " mail message(s).");
for (Enumeration messageEnumerator = mailMessages.objectEnumerator();
messageEnumerator.hasMoreElements();) {
ERCMailMessage mailMessage = (ERCMailMessage)messageEnumerator.nextElement();
if( !mailMessage.isReadyToSendState() ) { //due to the operation of the batch iterator, we may pull records that have already been sent
continue;
}
if (log.isDebugEnabled())
log.debug("Sending mail message: " + mailMessage);
try {
ERMailDelivery delivery = createMailDeliveryForMailMessage(mailMessage);
if (delivery != null) {
mailMessage.setState(ERCMailState.PROCESSING_STATE);
mailMessage.editingContext().saveChanges(); // This will throw if optimistic locking occurs
delivery.sendMail(true);
mailMessage.setState(ERCMailState.SENT_STATE);
mailMessage.setDateSent(new NSTimestamp());
if (shouldDeleteSentMail()) {
if (mailMessage.shouldArchiveSentMailAsBoolean()) {
mailMessage.archive();
}
// FIXME: Nasty stack overflow bug
if (!mailMessage.hasAttachments()) {
mailMessage.editingContext().deleteObject(mailMessage);
}
}
} else {
log.warn("Unable to create mail delivery for mail message: " + mailMessage);
}
} catch (EOGeneralAdaptorException ge) {
if ( _warnOnGeneralAdaptorExceptionLockingMessage )
log.warn("Caught general adaptor exception, reverting context. Might be running multiple mailers", ge);
mailMessage.editingContext().revert();
} catch (Throwable e) {
if (e instanceof NSForwardException)
e = ((NSForwardException)e).originalException();
log.warn("Caught exception when sending mail: " + ERXUtilities.stackTrace(e));
log.warn("Message trying to send: " + mailMessage + " pk: " + mailMessage.primaryKey());
// ENHANCEME: Need to implement a waiting state to retry sending mails.
mailMessage.setState(ERCMailState.EXCEPTION_STATE);
mailMessage.setExceptionReason(e.getMessage());
// Report the mailing error
ERCoreBusinessLogic.sharedInstance().reportException(e, new NSDictionary(mailMessage.snapshot(),
"Mail Message Snapshot"));
} finally {
// The editingcontext will not have any changes if an optimistic error occurred
if (mailMessage.editingContext().hasChanges()) {
try {
mailMessage.editingContext().saveChanges();
} catch (RuntimeException runtime) {
log.error("RuntimeException during save changes!", runtime);
throw runtime;
}
}