Search in sources :

Example 76 with Transaction

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

the class GroupTest method createGroups.

private void createGroups() {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Group root = new Group();
        rootId = root.getId();
        root.setName("root");
        em.persist(root);
        tx.commitRetaining();
        em = cont.persistence().getEntityManager();
        root = em.find(Group.class, rootId);
        Group group1 = new Group();
        group1Id = group1.getId();
        group1.setName("group1");
        group1.setParent(root);
        em.persist(group1);
        tx.commitRetaining();
        em = cont.persistence().getEntityManager();
        group1 = em.find(Group.class, group1Id);
        Group group2 = new Group();
        group2Id = group2.getId();
        group2.setName("group2");
        group2.setParent(group1);
        em.persist(group2);
        tx.commitRetaining();
        em = cont.persistence().getEntityManager();
        root = em.find(Group.class, rootId);
        Group group3 = new Group();
        group3Id = group3.getId();
        group3.setName("group3");
        group3.setParent(root);
        em.persist(group3);
        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)

Example 77 with Transaction

use of com.haulmont.cuba.core.Transaction 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.jupiter.api.Test)

Example 78 with Transaction

use of com.haulmont.cuba.core.Transaction 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.jupiter.api.Test)

Example 79 with Transaction

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

the class LoadSubstitutionsTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Group group = em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93"));
        user = new User();
        user.setLogin("user-" + user.getId());
        user.setGroup(group);
        em.persist(user);
        substitutedUser = new User();
        substitutedUser.setLogin("substitutedUser");
        substitutedUser.setGroup(group);
        em.persist(substitutedUser);
        userSubstitution = new UserSubstitution();
        userSubstitution.setUser(user);
        userSubstitution.setSubstitutedUser(substitutedUser);
        user.setSubstitutions(new ArrayList<>(ImmutableList.of(userSubstitution)));
        em.persist(userSubstitution);
        tx.commit();
    } finally {
        tx.end();
    }
    cont.setupLogging("com.haulmont.cuba.core.sys.FetchGroupManager", Level.TRACE);
}
Also used : Group(com.haulmont.cuba.security.entity.Group) EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserSubstitution(com.haulmont.cuba.security.entity.UserSubstitution)

Example 80 with Transaction

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

the class LoadSubstitutionsTest method testQuerySubstitutions.

@Test
public void testQuerySubstitutions() throws Exception {
    ViewRepository viewRepository = AppBeans.get(ViewRepository.NAME);
    View userView = new View(new View.ViewParams().src(viewRepository.getView(User.class, View.LOCAL)));
    View substitutedUserView = new View(User.class);
    substitutedUserView.addProperty("login");
    View substitutionsView = new View(UserSubstitution.class);
    substitutionsView.addProperty("substitutedUser", substitutedUserView);
    substitutionsView.addProperty("startDate");
    userView.addProperty("substitutions", substitutionsView);
    User loadedUser;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        loadedUser = em.find(User.class, user.getId(), userView);
        tx.commit();
    }
    assertNotNull(loadedUser);
    assertNotNull(loadedUser.getSubstitutions());
    Assertions.assertEquals(1, loadedUser.getSubstitutions().size());
    UserSubstitution loadedSubstitution = loadedUser.getSubstitutions().iterator().next();
    assertEquals(user, loadedSubstitution.getUser());
    assertEquals(substitutedUser, loadedSubstitution.getSubstitutedUser());
}
Also used : ViewRepository(com.haulmont.cuba.core.global.ViewRepository) EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserSubstitution(com.haulmont.cuba.security.entity.UserSubstitution) View(com.haulmont.cuba.core.global.View)

Aggregations

Transaction (com.haulmont.cuba.core.Transaction)226 EntityManager (com.haulmont.cuba.core.EntityManager)142 Test (org.junit.jupiter.api.Test)59 Query (com.haulmont.cuba.core.Query)30 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)27 User (com.haulmont.cuba.security.entity.User)25 View (com.haulmont.cuba.core.global.View)22 BeforeEach (org.junit.jupiter.api.BeforeEach)18 TypedQuery (com.haulmont.cuba.core.TypedQuery)13 Group (com.haulmont.cuba.security.entity.Group)12 List (java.util.List)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 MetaClass (com.haulmont.chile.core.model.MetaClass)7 Entity (com.haulmont.cuba.core.entity.Entity)7 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)7 ChildCachedEntity (com.haulmont.cuba.testmodel.entity_cache.ChildCachedEntity)5 ParentCachedEntity (com.haulmont.cuba.testmodel.entity_cache.ParentCachedEntity)5 UUID (java.util.UUID)5 Nullable (javax.annotation.Nullable)5 EntityManagerFactory (javax.persistence.EntityManagerFactory)5