Search in sources :

Example 81 with EntityManager

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

the class EntityLogTest method setUp.

@Before
public void setUp() throws Exception {
    cleanup();
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Query q;
        q = em.createNativeQuery("delete from SEC_ENTITY_LOG");
        q.executeUpdate();
        LoggedEntity le = new LoggedEntity();
        le.setName("sec$User");
        le.setAuto(true);
        em.persist(le);
        LoggedAttribute la = new LoggedAttribute();
        la.setEntity(le);
        la.setName("email");
        em.persist(la);
        le = new LoggedEntity();
        le.setName("sec$Role");
        le.setAuto(true);
        em.persist(le);
        la = new LoggedAttribute();
        la.setEntity(le);
        la.setName("type");
        em.persist(la);
        tx.commit();
    } finally {
        tx.end();
    }
    entityLog = AppBeans.get(EntityLogAPI.NAME);
    entityLog.invalidateCache();
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) TypedQuery(com.haulmont.cuba.core.TypedQuery) Before(org.junit.Before)

Example 82 with EntityManager

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

the class EntityLogTest method test.

@Test
public void test() 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 user = new User();
        userId = user.getId();
        user.setGroup(group);
        user.setLogin("test");
        user.setName("test-name");
        em.persist(user);
        tx.commit();
    } finally {
        tx.end();
    }
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = em.find(User.class, userId);
        user.setEmail("test-email");
        tx.commit();
    } finally {
        tx.end();
    }
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = em.find(User.class, userId);
        user.setName("test-name-1");
        tx.commit();
    } finally {
        tx.end();
    }
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = em.find(User.class, userId);
        user.setEmail("test-email-1");
        tx.commit();
    } finally {
        tx.end();
    }
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = em.find(User.class, userId);
        em.remove(user);
        tx.commit();
    } finally {
        tx.end();
    }
    List<EntityLogItem> items;
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        TypedQuery<EntityLogItem> query = em.createQuery("select i from sec$EntityLog i where i.entity = ?1 and i.entityRef.entityId = ?2", EntityLogItem.class);
        query.setParameter(1, "sec$User");
        query.setParameter(2, userId);
        items = query.getResultList();
        tx.commit();
    } finally {
        tx.end();
    }
    assertNotNull(items);
    assertEquals(4, items.size());
    assertNotNull(items.get(0).getAttributes());
    assertEquals(1, items.get(0).getAttributes().size());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Test(org.junit.Test)

Example 83 with EntityManager

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

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

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

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