use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class Emailer method persistMessages.
protected void persistMessages(List<SendingMessage> sendingMessageList, SendingStatus status) {
MessagePersistingContext context = new MessagePersistingContext();
try {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
for (SendingMessage message : sendingMessageList) {
message.setStatus(status);
try {
persistSendingMessage(em, message, context);
} catch (FileStorageException e) {
throw new RuntimeException("Failed to store message " + message.getCaption(), e);
}
}
tx.commit();
}
context.finished();
} finally {
removeOrphanFiles(context);
}
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class Emailer method loadContentText.
@Override
public String loadContentText(SendingMessage sendingMessage) {
SendingMessage msg;
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
msg = em.reload(sendingMessage, "sendingMessage.loadContentText");
tx.commit();
}
Objects.requireNonNull(msg, "Sending message not found: " + sendingMessage.getId());
if (msg.getContentTextFile() != null) {
byte[] bodyContent;
try {
bodyContent = fileStorage.loadFile(msg.getContentTextFile());
} catch (FileStorageException e) {
throw new RuntimeFileStorageException("Unable to load file from file storage", e);
}
// noinspection UnnecessaryLocalVariable
String res = bodyTextFromByteArray(bodyContent);
return res;
} else {
return msg.getContentText();
}
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class Emailer method markAsSent.
protected void markAsSent(SendingMessage sendingMessage) {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
SendingMessage msg = em.merge(sendingMessage);
msg.setStatus(SendingStatus.SENT);
msg.setAttemptsMade(msg.getAttemptsMade() + 1);
msg.setDateSent(timeSource.currentTimestamp());
if (config.isFileStorageUsed()) {
msg.setContentText(null);
}
tx.commit();
} catch (Exception e) {
log.error("Error marking message to '{}' as sent", sendingMessage.getAddress(), e);
}
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class Emailer method migrateEmailsToFileStorage.
@Override
public void migrateEmailsToFileStorage(List<SendingMessage> messages) {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
for (SendingMessage msg : messages) {
migrateMessage(em, msg);
}
tx.commit();
}
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class Emailer method returnToQueue.
protected void returnToQueue(SendingMessage sendingMessage) {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
SendingMessage msg = em.merge(sendingMessage);
msg.setAttemptsMade(msg.getAttemptsMade() + 1);
msg.setStatus(SendingStatus.QUEUE);
if (config.isFileStorageUsed()) {
msg.setContentText(null);
}
tx.commit();
} catch (Exception e) {
log.error("Error returning message to '{}' to the queue", sendingMessage.getAddress(), e);
}
}
Aggregations