Search in sources :

Example 51 with Group

use of com.haulmont.cuba.security.entity.Group in project cuba by cuba-platform.

the class QueryResultTest method createEntities.

private void createEntities() {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user;
        Group group = em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93"));
        int k = 0;
        for (String domain : Arrays.asList("@aaa.com", "@bbb.com")) {
            for (String name : Arrays.asList("A-", "B-")) {
                for (String firstName : Arrays.asList("C-", "D-")) {
                    for (int i = 0; i < 5; i++) {
                        user = new User();
                        user.setGroup(group);
                        userIds.add(user.getId());
                        user.setLogin("user" + StringUtils.leftPad(String.valueOf(k++), 2, '0'));
                        user.setName(name + "User" + i);
                        user.setFirstName(firstName + "User" + i);
                        user.setEmail(user.getLogin() + domain);
                        em.persist(user);
                    }
                }
            }
        }
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User)

Example 52 with Group

use of com.haulmont.cuba.security.entity.Group in project cuba by cuba-platform.

the class TypedNativeQueryTest method testTypedNativeQueryByChangingAttribute.

/*
     * Test that entity which is loaded by native typed query, is MANAGED,
     * by changing loaded entity attribute.
     */
@Test
public void testTypedNativeQueryByChangingAttribute() {
    Group group = new Group();
    groupId = group.getId();
    group.setName("Old Name");
    Transaction tx = cont.persistence().createTransaction();
    try {
        cont.persistence().getEntityManager().persist(group);
        tx.commit();
    } finally {
        tx.end();
    }
    // load with native query, change attribute
    String nativeQuery = "select ID, VERSION, CREATE_TS, CREATED_BY, UPDATE_TS, UPDATED_BY, NAME from SEC_GROUP where ID = ?";
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        TypedQuery<Group> q = em.createNativeQuery(nativeQuery, Group.class);
        q.setParameter(1, group.getId().toString());
        Group g = q.getResultList().get(0);
        g.setName("New Name");
        tx.commit();
    } finally {
        tx.end();
    }
    // load again, check
    Group g2;
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        g2 = em.find(Group.class, group.getId());
        assertNotNull(g2);
        assertEquals("New Name", g2.getName());
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) Test(org.junit.Test)

Example 53 with Group

use of com.haulmont.cuba.security.entity.Group in project cuba by cuba-platform.

the class TypedNativeQueryTest method testTypedNativeQueryByPersistingAnotherEntity.

/*
     * Test that entity which is loaded by native typed query,
     * is MANAGED, by persisting another entity linked to it.
     */
@Test
public void testTypedNativeQueryByPersistingAnotherEntity() {
    Group group = new Group();
    groupId = group.getId();
    group.setName("Old Name");
    Transaction tx = cont.persistence().createTransaction();
    try {
        cont.persistence().getEntityManager().persist(group);
        tx.commit();
    } finally {
        tx.end();
    }
    String nativeQuery = "select ID, CREATE_TS, CREATED_BY, UPDATE_TS, UPDATED_BY, VERSION, NAME from SEC_GROUP where ID = ?";
    tx = cont.persistence().createTransaction();
    Group g;
    try {
        EntityManager em = cont.persistence().getEntityManager();
        TypedQuery<Group> q = em.createNativeQuery(nativeQuery, Group.class);
        q.setParameter(1, group.getId().toString());
        g = q.getResultList().get(0);
        tx.commit();
    } finally {
        tx.end();
    }
    User user = new User();
    userId = user.getId();
    user.setLogin("typednativesqlquery");
    user.setGroup(g);
    user.setName("Test");
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        em.persist(user);
        tx.commit();
    } finally {
        tx.end();
    }
// gets persisted without error
}
Also used : Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) Test(org.junit.Test)

Example 54 with Group

use of com.haulmont.cuba.security.entity.Group in project cuba by cuba-platform.

the class UnfetchedAttributeTest method tearDown.

@After
public void tearDown() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        // load default 'Company' group
        Query q = em.createQuery("select g from sec$Group g where g.name = ?1");
        q.setView(new View(Group.class, false).addProperty("name"));
        q.setParameter(1, "Company");
        // noinspection unchecked
        List<Group> groups = q.getResultList();
        if (!groups.isEmpty()) {
            Group defaultGroup = groups.get(0);
            // load 'admin' user
            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 user = list.get(0);
                // set the default value
                user.setGroup(defaultGroup);
            }
        }
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) UserRole(com.haulmont.cuba.security.entity.UserRole) View(com.haulmont.cuba.core.global.View) After(org.junit.After)

Example 55 with Group

use of com.haulmont.cuba.security.entity.Group in project cuba by cuba-platform.

the class UserRoleTest method test.

@Test
public void test() {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Role role = new Role();
        role.setName("testRole1");
        em.persist(role);
        Group group = new Group();
        group.setName("testGroup1");
        em.persist(group);
        User user = new User();
        UUID userId = user.getId();
        user.setLogin("testUser1");
        user.setName("Test User 1");
        user.setGroup(group);
        em.persist(user);
        UserRole userRole = new UserRole();
        userRole.setUser(user);
        userRole.setRole(role);
        em.persist(userRole);
        tx.commitRetaining();
        em = cont.persistence().getEntityManager();
        user = em.find(User.class, userId);
        List<UserRole> userRoles = user.getUserRoles();
        assertEquals(1, userRoles.size());
        for (UserRole ur : userRoles) {
            Role r = ur.getRole();
            assertEquals(role.getName(), r.getName());
        }
    } finally {
        tx.end();
    }
}
Also used : Role(com.haulmont.cuba.security.entity.Role) UserRole(com.haulmont.cuba.security.entity.UserRole) Group(com.haulmont.cuba.security.entity.Group) EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserRole(com.haulmont.cuba.security.entity.UserRole) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

Group (com.haulmont.cuba.security.entity.Group)79 User (com.haulmont.cuba.security.entity.User)65 Test (org.junit.Test)60 UUID (java.util.UUID)30 Datasource (com.haulmont.cuba.gui.data.Datasource)29 Ignore (org.junit.Ignore)29 Component (com.haulmont.cuba.gui.components.Component)24 ArrayList (java.util.ArrayList)24 Assert.assertEquals (org.junit.Assert.assertEquals)24 Assert.assertTrue (org.junit.Assert.assertTrue)24 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)23 List (java.util.List)20 View (com.haulmont.cuba.core.global.View)15 Assert.assertNotNull (org.junit.Assert.assertNotNull)12 DsBuilder (com.haulmont.cuba.gui.data.DsBuilder)11 DatasourceImpl (com.haulmont.cuba.gui.data.impl.DatasourceImpl)10 Before (org.junit.Before)10 LookupPickerField (com.haulmont.cuba.gui.components.LookupPickerField)9 EntityManager (com.haulmont.cuba.core.EntityManager)8 Transaction (com.haulmont.cuba.core.Transaction)7