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