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();
}
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());
}
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();
}
}
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();
}
}
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();
}
}
Aggregations