Search in sources :

Example 1 with EntityManager

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

the class AttributeAccessServiceBean method computeSecurityState.

@Override
public SecurityState computeSecurityState(Entity entity) {
    Preconditions.checkNotNullArgument(entity, "entity is null");
    SecurityState state;
    String storeName = metadataTools.getStoreName(metadata.getClassNN(entity.getClass()));
    Transaction tx = persistence.createTransaction(storeName);
    try {
        EntityManager em = persistence.getEntityManager(storeName);
        Entity managedEntity = em.merge(entity);
        support.setupAttributeAccess(managedEntity);
        state = BaseEntityInternalAccess.getSecurityState(managedEntity);
    // do not commit the transaction
    } finally {
        tx.end();
    }
    return state;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SecurityState(com.haulmont.cuba.core.entity.SecurityState)

Example 2 with EntityManager

use of com.haulmont.cuba.core.EntityManager 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);
        tx.commit();
    } catch (Exception e) {
        log.error("Error returning message to '{}' to the queue", sendingMessage.getAddress(), e);
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SMTPAddressFailedException(com.sun.mail.smtp.SMTPAddressFailedException) AddressException(javax.mail.internet.AddressException) MailSendException(org.springframework.mail.MailSendException)

Example 3 with EntityManager

use of com.haulmont.cuba.core.EntityManager 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());
        tx.commit();
    } catch (Exception e) {
        log.error("Error marking message to '{}' as sent", sendingMessage.getAddress(), e);
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SMTPAddressFailedException(com.sun.mail.smtp.SMTPAddressFailedException) AddressException(javax.mail.internet.AddressException) MailSendException(org.springframework.mail.MailSendException)

Example 4 with EntityManager

use of com.haulmont.cuba.core.EntityManager 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 RuntimeException(e);
        }
        // noinspection UnnecessaryLocalVariable
        String res = bodyTextFromByteArray(bodyContent);
        return res;
    } else {
        return msg.getContentText();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage)

Example 5 with EntityManager

use of com.haulmont.cuba.core.EntityManager 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);
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage)

Aggregations

EntityManager (com.haulmont.cuba.core.EntityManager)167 Transaction (com.haulmont.cuba.core.Transaction)140 Query (com.haulmont.cuba.core.Query)27 User (com.haulmont.cuba.security.entity.User)27 Test (org.junit.Test)25 View (com.haulmont.cuba.core.global.View)22 MetaClass (com.haulmont.chile.core.model.MetaClass)14 Group (com.haulmont.cuba.security.entity.Group)12 Before (org.junit.Before)11 Entity (com.haulmont.cuba.core.entity.Entity)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 UUID (java.util.UUID)7 Nullable (javax.annotation.Nullable)7 TypedQuery (com.haulmont.cuba.core.TypedQuery)6 List (java.util.List)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)5 Role (com.haulmont.cuba.security.entity.Role)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)5 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)4