Search in sources :

Example 1 with SendingAttachment

use of com.haulmont.cuba.core.entity.SendingAttachment in project cuba by cuba-platform.

the class Emailer method loadBodyAndAttachments.

protected void loadBodyAndAttachments(SendingMessage message) {
    try {
        if (message.getContentTextFile() != null) {
            byte[] bodyContent = fileStorage.loadFile(message.getContentTextFile());
            String body = bodyTextFromByteArray(bodyContent);
            message.setContentText(body);
        }
        for (SendingAttachment attachment : message.getAttachments()) {
            if (attachment.getContentFile() != null) {
                byte[] content = fileStorage.loadFile(attachment.getContentFile());
                attachment.setContent(content);
            }
        }
    } catch (FileStorageException e) {
        log.error("Failed to load body or attachments for " + message);
    }
}
Also used : SendingAttachment(com.haulmont.cuba.core.entity.SendingAttachment)

Example 2 with SendingAttachment

use of com.haulmont.cuba.core.entity.SendingAttachment in project cuba by cuba-platform.

the class Emailer method migrateAttachmentsBatch.

protected int migrateAttachmentsBatch() {
    List<SendingAttachment> resultList;
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        String qstr = "select a from sys$SendingAttachment a where a.content is not null";
        TypedQuery<SendingAttachment> query = em.createQuery(qstr, SendingAttachment.class);
        query.setMaxResults(50);
        query.setViewName(View.MINIMAL);
        resultList = query.getResultList();
        tx.commit();
    } finally {
        tx.end();
    }
    if (!resultList.isEmpty()) {
        emailer.migrateAttachmentsToFileStorage(resultList);
    }
    return resultList.size();
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) SendingAttachment(com.haulmont.cuba.core.entity.SendingAttachment) Transaction(com.haulmont.cuba.core.Transaction)

Example 3 with SendingAttachment

use of com.haulmont.cuba.core.entity.SendingAttachment in project cuba by cuba-platform.

the class EmailerTest method testMigration.

@Test
public void testMigration() throws Exception {
    emailerConfig.setFileStorageUsed(false);
    byte[] expectedBytes = new byte[] { 1, 2, 3, 4, 6 };
    EmailAttachment fileAttachment = new EmailAttachment(expectedBytes, "invoice.pdf");
    String body = "Hi! This is test email. Bye.";
    EmailInfo emailInfo = new EmailInfo("test@example.com", "Test", body);
    emailInfo.setAttachments(new EmailAttachment[] { fileAttachment });
    List<SendingMessage> messages = emailer.sendEmailAsync(emailInfo);
    SendingMessage msg;
    SendingAttachment attachment;
    // check DB storage
    msg = reload(messages.get(0), "sendingMessage.loadFromQueue");
    attachment = msg.getAttachments().get(0);
    assertNotNull(msg.getContentText());
    assertNull(msg.getContentTextFile());
    assertNotNull(attachment.getContent());
    assertNull(attachment.getContentFile());
    emailer.migrateEmailsToFileStorage(Lists.newArrayList(msg));
    emailer.migrateAttachmentsToFileStorage(Lists.newArrayList(attachment));
    // check file storage
    msg = reload(msg, "sendingMessage.loadFromQueue");
    attachment = msg.getAttachments().get(0);
    assertNull(msg.getContentText());
    assertNotNull(msg.getContentTextFile());
    assertEquals(body, emailer.loadContentText(msg));
    assertNull(attachment.getContent());
    assertNotNull(attachment.getContentFile());
    FileStorageAPI fileStorage = AppBeans.get(FileStorageAPI.NAME);
    byte[] actualBytes = fileStorage.loadFile(attachment.getContentFile());
    assertByteArrayEquals(expectedBytes, actualBytes);
}
Also used : SendingAttachment(com.haulmont.cuba.core.entity.SendingAttachment) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage) Test(org.junit.Test)

Example 4 with SendingAttachment

use of com.haulmont.cuba.core.entity.SendingAttachment in project cuba by cuba-platform.

the class EmailSender method createMimeMessage.

protected MimeMessage createMimeMessage(SendingMessage sendingMessage) throws MessagingException {
    MimeMessage msg = mailSender.createMimeMessage();
    assignRecipient(sendingMessage, msg);
    msg.setSubject(sendingMessage.getCaption(), StandardCharsets.UTF_8.name());
    msg.setSentDate(timeSource.currentTimestamp());
    assignFromAddress(sendingMessage, msg);
    addHeaders(sendingMessage, msg);
    MimeMultipart content = new MimeMultipart("mixed");
    MimeMultipart textPart = new MimeMultipart("related");
    setMimeMessageContent(sendingMessage, content, textPart);
    for (SendingAttachment attachment : sendingMessage.getAttachments()) {
        MimeBodyPart attachmentPart = createAttachmentPart(attachment);
        if (attachment.getContentId() == null) {
            content.addBodyPart(attachmentPart);
        } else
            textPart.addBodyPart(attachmentPart);
    }
    msg.setContent(content);
    msg.saveChanges();
    return msg;
}
Also used : SendingAttachment(com.haulmont.cuba.core.entity.SendingAttachment)

Example 5 with SendingAttachment

use of com.haulmont.cuba.core.entity.SendingAttachment in project cuba by cuba-platform.

the class Emailer method persistSendingMessage.

protected void persistSendingMessage(EntityManager em, SendingMessage message, MessagePersistingContext context) throws FileStorageException {
    boolean useFileStorage = config.isFileStorageUsed();
    if (useFileStorage) {
        byte[] bodyBytes = bodyTextToBytes(message);
        FileDescriptor contentTextFile = createBodyFileDescriptor(message, bodyBytes);
        fileStorage.saveFile(contentTextFile, bodyBytes);
        context.files.add(contentTextFile);
        em.persist(contentTextFile);
        message.setContentTextFile(contentTextFile);
        message.setContentText(null);
    }
    em.persist(message);
    for (SendingAttachment attachment : message.getAttachments()) {
        if (useFileStorage) {
            FileDescriptor contentFile = createAttachmentFileDescriptor(attachment);
            fileStorage.saveFile(contentFile, attachment.getContent());
            context.files.add(contentFile);
            em.persist(contentFile);
            attachment.setContentFile(contentFile);
            attachment.setContent(null);
        }
        em.persist(attachment);
    }
}
Also used : SendingAttachment(com.haulmont.cuba.core.entity.SendingAttachment) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Aggregations

SendingAttachment (com.haulmont.cuba.core.entity.SendingAttachment)9 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)3 EntityManager (com.haulmont.cuba.core.EntityManager)2 Transaction (com.haulmont.cuba.core.Transaction)2 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)1 Test (org.junit.Test)1