Search in sources :

Example 31 with EntityManager

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

the class GroupEntityListener method createNewHierarchy.

protected void createNewHierarchy(Group entity, Group parent) {
    if (parent == null) {
        entity.setHierarchyList(new ArrayList<>());
        return;
    }
    if (!PersistenceHelper.isManaged(parent) && !PersistenceHelper.isDetached(parent))
        throw new IllegalStateException("Unable to create GroupHierarchy. Commit parent group first.");
    EntityManager em = persistence.getEntityManager();
    if (entity.getHierarchyList() == null) {
        entity.setHierarchyList(new ArrayList<>());
    } else {
        entity.getHierarchyList().clear();
    }
    if (PersistenceHelper.isDetached(parent))
        // refresh parent in case of detached
        parent = em.find(Group.class, parent.getId());
    int level = 0;
    for (GroupHierarchy hierarchy : parent.getHierarchyList()) {
        GroupHierarchy h = metadata.create(GroupHierarchy.class);
        h.setGroup(entity);
        h.setParent(hierarchy.getParent());
        h.setLevel(level++);
        em.persist(h);
        entity.getHierarchyList().add(h);
    }
    GroupHierarchy h = metadata.create(GroupHierarchy.class);
    h.setGroup(entity);
    h.setParent(parent);
    h.setLevel(level);
    em.persist(h);
    entity.getHierarchyList().add(h);
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) GroupHierarchy(com.haulmont.cuba.security.entity.GroupHierarchy)

Example 32 with EntityManager

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

the class UserSessionManager method getPermissionValue.

public Integer getPermissionValue(User user, PermissionType permissionType, String target) {
    Integer result;
    List<Role> roles = new ArrayList<>();
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        user = em.find(User.class, user.getId());
        for (UserRole userRole : user.getUserRoles()) {
            if (userRole.getRole() != null) {
                roles.add(userRole.getRole());
            }
        }
        UserSession session = new UserSession(uuidSource.createUuid(), user, roles, userSessionSource.getLocale(), false);
        compilePermissions(session, roles);
        result = session.getPermissionValue(permissionType, target);
        tx.commit();
    } finally {
        tx.end();
    }
    return result;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) UserSession(com.haulmont.cuba.security.global.UserSession) ArrayList(java.util.ArrayList)

Example 33 with EntityManager

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

the class UserSessionManager method compileConstraints.

protected void compileConstraints(UserSession session, Group group) {
    EntityManager em = persistence.getEntityManager();
    TypedQuery<Constraint> q = em.createQuery("select c from sec$GroupHierarchy h join h.parent.constraints c " + "where h.group.id = ?1", Constraint.class);
    q.setParameter(1, group);
    List<Constraint> constraints = q.getResultList();
    List<Constraint> list = new ArrayList<>(constraints);
    list.addAll(group.getConstraints());
    for (Constraint constraint : list) {
        if (Boolean.TRUE.equals(constraint.getIsActive())) {
            session.addConstraint(constraint);
        }
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) ArrayList(java.util.ArrayList)

Example 34 with EntityManager

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

the class CascadeDeleteTest method testRemoveCascade.

@Test
public void testRemoveCascade() throws Exception {
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        CascadeEntity loadedRoot = em.find(CascadeEntity.class, root.getId());
        em.find(CascadeEntity.class, first.getId());
        em.remove(loadedRoot);
        tx.commit();
    }
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        List r = em.createQuery("select e from test$CascadeEntity e").getResultList();
        assertEquals(0, r.size());
        tx.commit();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) List(java.util.List) CascadeEntity(com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity) Test(org.junit.Test)

Example 35 with EntityManager

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

the class SchedulingServiceBean method setActive.

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

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