Search in sources :

Example 1 with EntityDiff

use of com.haulmont.cuba.core.entity.diff.EntityDiff in project cuba by cuba-platform.

the class EntityDiffViewer method compareSnapshots.

@SuppressWarnings("unused")
public void compareSnapshots() {
    entityDiffDs.setItem(null);
    EntitySnapshot firstSnap = null;
    EntitySnapshot secondSnap = null;
    Set selected = snapshotsTable.getSelected();
    Object[] selectedItems = selected.toArray();
    if ((selected.size() == 2)) {
        firstSnap = (EntitySnapshot) selectedItems[0];
        secondSnap = (EntitySnapshot) selectedItems[1];
    } else if (selected.size() == 1) {
        secondSnap = (EntitySnapshot) selectedItems[0];
        firstSnap = snapshotsDs.getLatestSnapshot();
        if (firstSnap == secondSnap)
            firstSnap = null;
    }
    if ((secondSnap != null) || (firstSnap != null)) {
        EntityDiff diff = diffDs.loadDiff(firstSnap, secondSnap);
        entityDiffDs.setItem(diff);
    }
    diffTable.refresh();
    diffTable.expandAll();
}
Also used : EntitySnapshot(com.haulmont.cuba.core.entity.EntitySnapshot) Set(java.util.Set) EntityDiff(com.haulmont.cuba.core.entity.diff.EntityDiff)

Example 2 with EntityDiff

use of com.haulmont.cuba.core.entity.diff.EntityDiff in project cuba by cuba-platform.

the class PersistenceHelperTest method testEntityStates.

@Test
public void testEntityStates() {
    try {
        PersistenceHelper.isNew(null);
        fail("isNew() should not accept null");
    } catch (Exception e) {
    // 
    }
    try {
        PersistenceHelper.isManaged(null);
        fail("isManaged() should not accept null");
    } catch (Exception e) {
    // 
    }
    try {
        PersistenceHelper.isDetached(null);
        fail("isDetached() should not accept null");
    } catch (Exception e) {
    // 
    }
    assertTrue(PersistenceHelper.isNew(new EntityDiff(null)));
    UUID id;
    Server server;
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        assertNotNull(em);
        server = new Server();
        assertTrue(PersistenceHelper.isNew(server));
        assertFalse(PersistenceHelper.isManaged(server));
        assertFalse(PersistenceHelper.isDetached(server));
        id = server.getId();
        server.setName("localhost");
        server.setRunning(true);
        em.persist(server);
        assertTrue(PersistenceHelper.isNew(server));
        assertTrue(PersistenceHelper.isManaged(server));
        assertFalse(PersistenceHelper.isDetached(server));
        tx.commit();
    } finally {
        tx.end();
    }
    assertFalse(PersistenceHelper.isNew(server));
    assertFalse(PersistenceHelper.isManaged(server));
    assertTrue(PersistenceHelper.isDetached(server));
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        server = em.find(Server.class, id);
        assertNotNull(server);
        assertEquals(id, server.getId());
        assertFalse(PersistenceHelper.isNew(server));
        assertTrue(PersistenceHelper.isManaged(server));
        assertFalse(PersistenceHelper.isDetached(server));
        server.setRunning(false);
        tx.commit();
    } finally {
        tx.end();
    }
    assertFalse(PersistenceHelper.isNew(server));
    assertFalse(PersistenceHelper.isManaged(server));
    assertTrue(PersistenceHelper.isDetached(server));
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        server = em.merge(server);
        assertFalse(PersistenceHelper.isNew(server));
        assertTrue(PersistenceHelper.isManaged(server));
        assertFalse(PersistenceHelper.isDetached(server));
        tx.commit();
    } finally {
        tx.end();
    }
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        server = em.find(Server.class, id);
        assertNotNull(server);
        assertEquals(id, server.getId());
        em.remove(server);
        assertFalse(PersistenceHelper.isNew(server));
        // is it correct?
        assertTrue(PersistenceHelper.isManaged(server));
        assertFalse(PersistenceHelper.isDetached(server));
        tx.commit();
    } finally {
        tx.end();
    }
    assertFalse(PersistenceHelper.isNew(server));
    assertFalse(PersistenceHelper.isManaged(server));
    // is it correct?
    assertTrue(PersistenceHelper.isDetached(server));
}
Also used : Server(com.haulmont.cuba.core.entity.Server) UUID(java.util.UUID) EntityDiff(com.haulmont.cuba.core.entity.diff.EntityDiff)

Aggregations

EntityDiff (com.haulmont.cuba.core.entity.diff.EntityDiff)2 EntitySnapshot (com.haulmont.cuba.core.entity.EntitySnapshot)1 Server (com.haulmont.cuba.core.entity.Server)1 Set (java.util.Set)1 UUID (java.util.UUID)1