Search in sources :

Example 11 with View

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

the class SoftDeleteTest method testManyToOne.

@Test
public void testManyToOne() throws SQLException {
    System.out.println("===================== BEGIN testManyToOne =====================");
    QueryRunner queryRunner = new QueryRunner(persistence.getDataSource());
    queryRunner.update("update SEC_GROUP set DELETE_TS = current_timestamp, DELETED_BY = 'admin' where ID = ?", new Object[] { groupId.toString() });
    // test without view
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        User user = em.find(User.class, userId);
        Group group = user.getGroup();
        tx.commit();
        assertNotNull(group);
        assertTrue(group.isDeleted());
    }
    View view;
    // test fetchMode = AUTO (JOIN is used)
    view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"));
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        User user = em.find(User.class, userId, view);
        Group group = user.getGroup();
        tx.commit();
        assertNotNull(group);
        assertTrue(group.isDeleted());
    }
    // test fetchMode = UNDEFINED
    view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"), FetchMode.UNDEFINED);
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        User user = em.find(User.class, userId, view);
        Group group = user.getGroup();
        tx.commit();
        assertNotNull(group);
        assertTrue(group.isDeleted());
    }
    // test fetchMode = BATCH
    view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"), FetchMode.BATCH);
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        User user = em.find(User.class, userId, view);
        Group group = user.getGroup();
        tx.commit();
        assertNotNull(group);
        assertTrue(group.isDeleted());
    }
    System.out.println("===================== END testManyToOne =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View) QueryRunner(com.haulmont.bali.db.QueryRunner)

Example 12 with View

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

the class QueryResultTest method test.

@Test
public void test() {
    Transaction tx;
    javax.persistence.EntityManager emDelegate;
    EntityManager em;
    Query query;
    UUID sessionId = UUID.randomUUID();
    int queryKey = 1;
    tx = cont.persistence().createTransaction();
    try {
        emDelegate = cont.persistence().getEntityManager().getDelegate();
        QueryResult queryResult = new QueryResult();
        queryResult.setSessionId(sessionId);
        queryResult.setQueryKey(queryKey);
        queryResult.setEntityId(UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"));
        emDelegate.persist(queryResult);
        tx.commitRetaining();
        em = cont.persistence().getEntityManager();
        query = em.createQuery("select u from sec$User u, sys$QueryResult qr " + "where qr.entityId = u.id and qr.sessionId = ?1 and qr.queryKey = ?2");
        query.setParameter(1, sessionId);
        query.setParameter(2, queryKey);
        query.setView(new View(User.class).addProperty("login").addProperty("name").addProperty("group", new View(Group.class).addProperty("name")));
        // OpenJPAQuery openJPAQuery = (OpenJPAQuery) query.getDelegate();
        // Map params = new HashMap();
        // params.put(1, sessionId);
        // params.put(2, queryKey);
        // String[] dataStoreActions = openJPAQuery.getDataStoreActions(params);
        // 
        // System.out.println(dataStoreActions);
        List<User> list = query.getResultList();
        assertEquals(1, list.size());
        User user = list.get(0);
        assertEquals("admin", user.getLogin());
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) QueryResult(com.haulmont.cuba.core.entity.QueryResult) User(com.haulmont.cuba.security.entity.User) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 13 with View

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

the class UnfetchedAttributeTest method testSet.

@Test
public void testSet() throws Exception {
    User user = null;
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Query q = em.createQuery("select u from sec$User u where u.id = ?1");
        q.setView(new View(User.class, false).addProperty("login").addProperty("userRoles", new View(UserRole.class).addProperty("role", new View(Role.class).addProperty("name"))));
        q.setParameter(1, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"));
        List<User> list = q.getResultList();
        if (!list.isEmpty()) {
            user = list.get(0);
            // set value to not present in view
            user.setGroup(group);
        }
        tx.commit();
    } finally {
        tx.end();
    }
    user = reserialize(user);
    assertNotNull(user);
    assertNotNull(user.getUserRoles());
    user.getUserRoles().size();
    assertNotNull(user.getGroup());
}
Also used : User(com.haulmont.cuba.security.entity.User) UserRole(com.haulmont.cuba.security.entity.UserRole) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 14 with View

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

the class EntityManagerTest method testFind.

// Commented in EL: EntityManager has no setView() method anymore
// public void testViewPropagation() throws Exception {
// 
// View view = new View(User.class, false)
// .addProperty("name")
// .addProperty("login")
// .addProperty("group", new View(Group.class)
// .addProperty("name"));
// 
// User user;
// 
// Transaction tx = cont.persistence().createTransaction();
// try {
// EntityManager em = cont.persistence().getEntityManager();
// em.setView(view);
// 
// TypedQuery<User> query = em.createQuery("select u from sec$User u where u.id = ?1", User.class);
// query.setParameter(1, userId);
// user = query.getSingleResult();
// 
// tx.commit();
// } finally {
// tx.end();
// }
// 
// assertNull(user.getCreatedBy());
// assertNotNull(user.getGroup());
// }
@Test
public void testFind() throws Exception {
    View view = new View(User.class, false).addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"));
    User user;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        user = em.find(User.class, userId, view);
        tx.commit();
    }
    user = reserialize(user);
    assertNotNull(user.getCreatedBy());
    assertNotNull(user.getGroup());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 15 with View

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

the class EntityManagerTest method testQueryView.

@Test
public void testQueryView() throws Exception {
    View view = new View(User.class, "testQueryView", false).addProperty("name").addProperty("login").addProperty("group", new View(Group.class, false).addProperty("name"));
    ((AbstractViewRepository) cont.metadata().getViewRepository()).storeView(cont.metadata().getSession().getClassNN(User.class), view);
    User user;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        TypedQuery<User> query = em.createQuery("select u from sec$User u where u.id = ?1", User.class);
        query.setParameter(1, userId);
        query.setViewName("testQueryView");
        user = query.getSingleResult();
        tx.commit();
    }
    user = reserialize(user);
    assertNotNull(user.getCreatedBy());
    assertNotNull(user.getGroup());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Aggregations

View (com.haulmont.cuba.core.global.View)55 Test (org.junit.Test)24 EntityManager (com.haulmont.cuba.core.EntityManager)22 Transaction (com.haulmont.cuba.core.Transaction)21 User (com.haulmont.cuba.security.entity.User)19 Group (com.haulmont.cuba.security.entity.Group)10 MetaClass (com.haulmont.chile.core.model.MetaClass)5 ViewProperty (com.haulmont.cuba.core.global.ViewProperty)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)3 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 Entity (com.haulmont.cuba.core.entity.Entity)2 ViewRepository (com.haulmont.cuba.core.global.ViewRepository)2 Role (com.haulmont.cuba.security.entity.Role)2 SoftDeleteOneToOneB (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneB)2 QueryRunner (com.haulmont.bali.db.QueryRunner)1 MetadataObject (com.haulmont.chile.core.model.MetadataObject)1 DynamicAttributesMetaProperty (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty)1 QueryResult (com.haulmont.cuba.core.entity.QueryResult)1 Server (com.haulmont.cuba.core.entity.Server)1