Search in sources :

Example 36 with EntityManager

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

the class SchedulingServiceBean method setActive.

@Override
public void setActive(ScheduledTask task, boolean active) {
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        ScheduledTask t = em.find(ScheduledTask.class, task.getId());
        t.setActive(active);
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) ScheduledTask(com.haulmont.cuba.core.entity.ScheduledTask)

Example 37 with EntityManager

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

the class SchedulingServiceBean method getAvailableUsers.

@Override
public List<String> getAvailableUsers() {
    List<String> result = new ArrayList<>();
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        Query query = em.createQuery("select u from sec$User u");
        List<User> userList = query.getResultList();
        for (User user : userList) {
            result.add(user.getLogin());
        }
        tx.commit();
    } finally {
        tx.end();
    }
    return result;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) ArrayList(java.util.ArrayList)

Example 38 with EntityManager

use of com.haulmont.cuba.core.EntityManager 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 39 with EntityManager

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

the class PersistenceSecurityImpl method restoreFilteredData.

@Override
@SuppressWarnings("unchecked")
public void restoreFilteredData(Entity entity) {
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    String storeName = metadataTools.getStoreName(metaClass);
    EntityManager entityManager = persistence.getEntityManager(storeName);
    Multimap<String, Object> filtered = BaseEntityInternalAccess.getFilteredData(entity);
    if (filtered == null) {
        return;
    }
    for (Map.Entry<String, Collection<Object>> entry : filtered.asMap().entrySet()) {
        MetaProperty property = metaClass.getPropertyNN(entry.getKey());
        Collection filteredIds = entry.getValue();
        if (property.getRange().isClass() && CollectionUtils.isNotEmpty(filteredIds)) {
            Class entityClass = property.getRange().asClass().getJavaClass();
            Class propertyClass = property.getJavaType();
            if (Collection.class.isAssignableFrom(propertyClass)) {
                Collection currentCollection = entity.getValue(property.getName());
                if (currentCollection == null) {
                    throw new RowLevelSecurityException(format("Could not restore an object to currentValue because it is null [%s]. Entity [%s].", property.getName(), metaClass.getName()), metaClass.getName());
                }
                for (Object entityId : filteredIds) {
                    Entity reference = entityManager.getReference((Class<Entity>) entityClass, entityId);
                    // we ignore situations when the currentValue is immutable
                    currentCollection.add(reference);
                }
            } else if (Entity.class.isAssignableFrom(propertyClass)) {
                Object entityId = filteredIds.iterator().next();
                Entity reference = entityManager.getReference((Class<Entity>) entityClass, entityId);
                // we ignore the situation when the field is read-only
                entity.setValue(property.getName(), reference);
            }
        }
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 40 with EntityManager

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

the class RelationsTest method createRole.

public UUID createRole() {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = em.find(User.class, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"));
        Role role = new Role();
        role.setName("RelationTest");
        em.persist(role);
        UserRole userRole = new UserRole();
        userRole.setUser(user);
        userRole.setRole(role);
        em.persist(userRole);
        tx.commit();
        return role.getId();
    } finally {
        tx.end();
    }
}
Also used : Role(com.haulmont.cuba.security.entity.Role) UserRole(com.haulmont.cuba.security.entity.UserRole) EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserRole(com.haulmont.cuba.security.entity.UserRole)

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