Search in sources :

Example 1 with EntityListenerManager

use of com.haulmont.cuba.core.sys.listener.EntityListenerManager in project cuba by cuba-platform.

the class EntityListenerTest method setUp.

@Before
public void setUp() throws Exception {
    persistence = cont.persistence();
    metadata = cont.metadata();
    QueryRunner runner = new QueryRunner(cont.persistence().getDataSource());
    runner.update("delete from SYS_SERVER");
    listenerBean = AppBeans.get("cuba_TestListenerBean");
    listenerBean.events.clear();
    detachAttachListener = AppBeans.get("cuba_TestDetachAttachListener");
    detachAttachListener.events.clear();
    TestListener.events.clear();
    TestUserEntityListener.events.clear();
    entityListenerManager = AppBeans.get(EntityListenerManager.class);
    entityListenerManager.addListener(User.class, TestUserEntityListener.class);
    entityListenerManager.addListener(Server.class, TestListener.class);
    entityListenerManager.addListener(Server.class, "cuba_TestListenerBean");
    entityListenerManager.addListener(Server.class, "cuba_TestDetachAttachListener");
    entityListenerManager.addListener(Server.class, "cuba_TestListenerUsingEntityManager");
}
Also used : EntityListenerManager(com.haulmont.cuba.core.sys.listener.EntityListenerManager) QueryRunner(com.haulmont.bali.db.QueryRunner) Before(org.junit.Before)

Example 2 with EntityListenerManager

use of com.haulmont.cuba.core.sys.listener.EntityListenerManager in project cuba by cuba-platform.

the class CascadeDeleteTest method testEntityListenerOnCascadeDelete.

@Test
public void testEntityListenerOnCascadeDelete() throws Exception {
    EntityListenerManager entityListenerManager = AppBeans.get(EntityListenerManager.class);
    entityListenerManager.addListener(CascadeEntity.class, DeleteCascadeEntityListener.class);
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        CascadeEntity loadedSecond = em.find(CascadeEntity.class, second.getId());
        em.find(CascadeEntity.class, third.getId());
        em.remove(loadedSecond);
        tx.commit();
    }
    entityListenerManager.removeListener(CascadeEntity.class, DeleteCascadeEntityListener.class);
    assertEquals(2, DeleteCascadeEntityListener.deletedEvents.size());
    assertTrue(DeleteCascadeEntityListener.deletedEvents.contains(second.getId().toString()));
    assertTrue(DeleteCascadeEntityListener.deletedEvents.contains(third.getId().toString()));
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) EntityListenerManager(com.haulmont.cuba.core.sys.listener.EntityListenerManager) CascadeEntity(com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity) Test(org.junit.Test)

Example 3 with EntityListenerManager

use of com.haulmont.cuba.core.sys.listener.EntityListenerManager in project cuba by cuba-platform.

the class CascadeDeleteTest method testEntityListenerOnUpdate.

@Test
public void testEntityListenerOnUpdate() throws Exception {
    EntityListenerManager entityListenerManager = AppBeans.get(EntityListenerManager.class);
    entityListenerManager.addListener(CascadeEntity.class, UpdateCascadeEntityListener.class);
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        CascadeEntity loadedThird = em.find(CascadeEntity.class, third.getId());
        CascadeEntity loadedSecond = em.find(CascadeEntity.class, second.getId());
        loadedThird.setName("third#1");
        tx.commit();
    }
    entityListenerManager.removeListener(CascadeEntity.class, UpdateCascadeEntityListener.class);
    assertEquals(2, UpdateCascadeEntityListener.updatedEvents.size());
    assertTrue(UpdateCascadeEntityListener.updatedEvents.contains(second.getId().toString()));
    assertTrue(UpdateCascadeEntityListener.updatedEvents.contains(third.getId().toString()));
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) EntityListenerManager(com.haulmont.cuba.core.sys.listener.EntityListenerManager) CascadeEntity(com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity) Test(org.junit.Test)

Aggregations

EntityListenerManager (com.haulmont.cuba.core.sys.listener.EntityListenerManager)3 EntityManager (com.haulmont.cuba.core.EntityManager)2 Transaction (com.haulmont.cuba.core.Transaction)2 CascadeEntity (com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity)2 Test (org.junit.Test)2 QueryRunner (com.haulmont.bali.db.QueryRunner)1 Before (org.junit.Before)1