Search in sources :

Example 1 with GroupHierarchy

use of com.haulmont.cuba.security.entity.GroupHierarchy 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 2 with GroupHierarchy

use of com.haulmont.cuba.security.entity.GroupHierarchy in project cuba by cuba-platform.

the class GroupTest method testNew.

@Test
public void testNew() {
    createGroups();
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Group group2 = em.find(Group.class, group2Id);
        for (GroupHierarchy hierarchy : group2.getHierarchyList()) {
            assertEquals(group2, hierarchy.getGroup());
            if (hierarchy.getLevel() == 0)
                assertEquals(rootId, hierarchy.getParent().getId());
            else if (hierarchy.getLevel() == 1)
                assertEquals(group1Id, hierarchy.getParent().getId());
        }
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) GroupHierarchy(com.haulmont.cuba.security.entity.GroupHierarchy) Test(org.junit.Test)

Example 3 with GroupHierarchy

use of com.haulmont.cuba.security.entity.GroupHierarchy in project cuba by cuba-platform.

the class GroupTest method testUpdate.

@Test
public void testUpdate() {
    createGroups();
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Group group1 = em.find(Group.class, group1Id);
        Group group3 = em.find(Group.class, group3Id);
        group1.setParent(group3);
        tx.commitRetaining();
        em = cont.persistence().getEntityManager();
        group1 = em.find(Group.class, group1Id);
        for (GroupHierarchy hierarchy : group1.getHierarchyList()) {
            assertEquals(group1, hierarchy.getGroup());
            if (hierarchy.getLevel() == 0)
                assertEquals(rootId, hierarchy.getParent().getId());
            else if (hierarchy.getLevel() == 1)
                assertEquals(group3Id, hierarchy.getParent().getId());
        }
        Group group2 = em.find(Group.class, group2Id);
        for (GroupHierarchy hierarchy : group2.getHierarchyList()) {
            assertEquals(group2, hierarchy.getGroup());
            if (hierarchy.getLevel() == 0)
                assertEquals(rootId, hierarchy.getParent().getId());
            else if (hierarchy.getLevel() == 1)
                assertEquals(group3Id, hierarchy.getParent().getId());
            else if (hierarchy.getLevel() == 2)
                assertEquals(group1Id, hierarchy.getParent().getId());
        }
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) GroupHierarchy(com.haulmont.cuba.security.entity.GroupHierarchy) Test(org.junit.Test)

Example 4 with GroupHierarchy

use of com.haulmont.cuba.security.entity.GroupHierarchy in project cuba by cuba-platform.

the class GroupEntityListener method onBeforeUpdate.

@Override
public void onBeforeUpdate(Group entity, EntityManager entityManager) {
    if (!persistence.getTools().getDirtyFields(entity).contains("parent"))
        return;
    EntityManager em = persistence.getEntityManager();
    for (GroupHierarchy oldHierarchy : entity.getHierarchyList()) {
        em.remove(oldHierarchy);
    }
    createNewHierarchy(entity, entity.getParent());
    TypedQuery<GroupHierarchy> q = em.createQuery("select h from sec$GroupHierarchy h join fetch h.group " + "where h.parent.id = ?1", GroupHierarchy.class);
    q.setParameter(1, entity);
    List<GroupHierarchy> list = q.getResultList();
    for (GroupHierarchy hierarchy : list) {
        Group dependentGroup = hierarchy.getGroup();
        for (GroupHierarchy depHierarchy : dependentGroup.getHierarchyList()) {
            em.remove(depHierarchy);
        }
        em.remove(hierarchy);
        createNewHierarchy(dependentGroup, dependentGroup.getParent());
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) EntityManager(com.haulmont.cuba.core.EntityManager) GroupHierarchy(com.haulmont.cuba.security.entity.GroupHierarchy)

Aggregations

EntityManager (com.haulmont.cuba.core.EntityManager)4 GroupHierarchy (com.haulmont.cuba.security.entity.GroupHierarchy)4 Group (com.haulmont.cuba.security.entity.Group)3 Transaction (com.haulmont.cuba.core.Transaction)2 Test (org.junit.Test)2