use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.
the class Emailer method persistMessageIfPossible.
/*
* Try to persist message and catch all errors to allow actual delivery
* in case of database or file storage failure.
*/
@Nullable
protected SendingMessage persistMessageIfPossible(SendingMessage sendingMessage) {
// to avoid additional overhead to load body and attachments back from FS
try {
SendingMessage clonedMessage = createClone(sendingMessage);
persistMessages(Collections.singletonList(clonedMessage), SendingStatus.SENDING);
return clonedMessage;
} catch (Exception e) {
log.error("Failed to persist message " + sendingMessage.getCaption(), e);
return null;
}
}
use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.
the class Emailer method convertToSendingMessage.
protected SendingMessage convertToSendingMessage(String address, String from, String caption, String body, @Nullable List<EmailHeader> headers, @Nullable EmailAttachment[] attachments, @Nullable Integer attemptsCount, @Nullable Date deadline) {
SendingMessage sendingMessage = metadata.create(SendingMessage.class);
sendingMessage.setAddress(address);
sendingMessage.setFrom(from);
sendingMessage.setContentText(body);
sendingMessage.setCaption(caption);
sendingMessage.setAttemptsCount(attemptsCount);
sendingMessage.setDeadline(deadline);
sendingMessage.setAttemptsMade(0);
if (attachments != null && attachments.length > 0) {
StringBuilder attachmentsName = new StringBuilder();
List<SendingAttachment> sendingAttachments = new ArrayList<>(attachments.length);
for (EmailAttachment ea : attachments) {
attachmentsName.append(ea.getName()).append(";");
SendingAttachment sendingAttachment = toSendingAttachment(ea);
sendingAttachment.setMessage(sendingMessage);
sendingAttachments.add(sendingAttachment);
}
sendingMessage.setAttachments(sendingAttachments);
sendingMessage.setAttachmentsName(attachmentsName.toString());
} else {
sendingMessage.setAttachments(Collections.<SendingAttachment>emptyList());
}
if (headers != null && !headers.isEmpty()) {
StringBuilder headersLine = new StringBuilder();
for (EmailHeader header : headers) {
headersLine.append(header.toString()).append(SendingMessage.HEADERS_SEPARATOR);
}
sendingMessage.setHeaders(headersLine.toString());
} else {
sendingMessage.setHeaders(null);
}
replaceRecipientIfNecessary(sendingMessage);
return sendingMessage;
}
use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.
the class Emailer method markAsNonSent.
protected void markAsNonSent(SendingMessage sendingMessage) {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
SendingMessage msg = em.merge(sendingMessage);
msg.setStatus(SendingStatus.NOTSENT);
msg.setAttemptsMade(msg.getAttemptsMade() + 1);
tx.commit();
} catch (Exception e) {
log.error("Error marking message to '{}' as not sent", sendingMessage.getAddress(), e);
}
}
use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.
the class Emailer method loadEmailsToSend.
protected List<SendingMessage> loadEmailsToSend() {
Date sendTimeoutTime = DateUtils.addSeconds(timeSource.currentTimestamp(), -config.getSendingTimeoutSec());
List<SendingMessage> emailsToSend = new ArrayList<>();
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
TypedQuery<SendingMessage> query = em.createQuery("select sm from sys$SendingMessage sm" + " where sm.status = :statusQueue or (sm.status = :statusSending and sm.updateTs < :time)" + " order by sm.createTs", SendingMessage.class);
query.setParameter("statusQueue", SendingStatus.QUEUE.getId());
query.setParameter("time", sendTimeoutTime);
query.setParameter("statusSending", SendingStatus.SENDING.getId());
View view = metadata.getViewRepository().getView(SendingMessage.class, "sendingMessage.loadFromQueue");
// because SendingAttachment.content has FetchType.LAZY
view.setLoadPartialEntities(true);
query.setView(view);
query.setMaxResults(config.getMessageQueueCapacity());
List<SendingMessage> resList = query.getResultList();
for (SendingMessage msg : resList) {
if (shouldMarkNotSent(msg)) {
msg.setStatus(SendingStatus.NOTSENT);
} else {
msg.setStatus(SendingStatus.SENDING);
emailsToSend.add(msg);
}
}
tx.commit();
}
for (SendingMessage message : emailsToSend) {
loadBodyAndAttachments(message);
}
return emailsToSend;
}
use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.
the class Emailer method createClone.
protected SendingMessage createClone(SendingMessage srcMessage) {
SendingMessage clonedMessage = metadata.getTools().copy(srcMessage);
List<SendingAttachment> clonedList = new ArrayList<>();
for (SendingAttachment srcAttach : srcMessage.getAttachments()) {
SendingAttachment clonedAttach = (SendingAttachment) metadata.getTools().copy(srcAttach);
clonedAttach.setMessage(null);
clonedAttach.setMessage(clonedMessage);
clonedList.add(clonedAttach);
}
clonedMessage.setAttachments(clonedList);
return clonedMessage;
}
Aggregations