use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class FetchJoinTest method testNotLoadingCustomer.
@Test
public void testNotLoadingCustomer() throws Exception {
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.persistence().getEntityManager();
View partyView = new View(Party.class).addProperty("name");
View productView = new View(Product.class).addProperty("name");
View customerView = new View(Customer.class).addProperty("customerNumber").addProperty("party", partyView);
View salesPersonView = new View(SalesPerson.class).addProperty("salespersonNumber").addProperty("party", partyView);
View orderView = new View(Order.class).addProperty("orderNumber").addProperty("customer", customerView).addProperty("salesPerson", salesPersonView);
View orderLineView = new View(OrderLine.class).addProperty("order", orderView).addProperty("product", productView);
OrderLine reloadedOrderLine = em.find(OrderLine.class, orderLine.getId(), orderLineView);
assertNotNull(reloadedOrderLine);
assertNotNull(reloadedOrderLine.getOrder().getCustomer());
assertEquals(partyCustomer, reloadedOrderLine.getOrder().getCustomer().getParty());
assertNotNull(reloadedOrderLine.getOrder().getSalesPerson());
assertEquals(partyPerson, reloadedOrderLine.getOrder().getSalesPerson().getParty());
tx.commit();
}
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class ConstraintTest method setUp.
@Before
public void setUp() throws Exception {
passwordEncryption = AppBeans.get(PasswordEncryption.class);
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
Server server = new Server();
server.setName("someServer");
server.setRunning(false);
serverId = server.getId();
em.persist(server);
Group parentGroup = new Group();
parentGroupId = parentGroup.getId();
parentGroup.setName("testParentGroup");
em.persist(parentGroup);
tx.commitRetaining();
em = cont.persistence().getEntityManager();
Constraint parentConstraint = new Constraint();
parentConstraintId = parentConstraint.getId();
parentConstraint.setEntityName("sys$Server");
parentConstraint.setWhereClause("{E}.running = true");
parentConstraint.setGroup(parentGroup);
em.persist(parentConstraint);
Group group = new Group();
groupId = group.getId();
group.setName("testGroup");
group.setParent(parentGroup);
em.persist(group);
Constraint serverConstraint = new Constraint();
serverConstraintId = serverConstraint.getId();
serverConstraint.setEntityName("sys$Server");
serverConstraint.setWhereClause("{E}.name = 'localhost'");
serverConstraint.setGroup(group);
em.persist(serverConstraint);
Constraint userRoleConstraint = new Constraint();
userRoleConstraintId = userRoleConstraint.getId();
userRoleConstraint.setEntityName("sec$UserRole");
userRoleConstraint.setWhereClause("{E}.user.id = :session$userId");
userRoleConstraint.setGroup(group);
em.persist(userRoleConstraint);
User user = new User();
userId = user.getId();
user.setLogin(USER_LOGIN);
String pwd = passwordEncryption.getPasswordHash(userId, USER_PASSW);
user.setPassword(pwd);
user.setGroup(group);
em.persist(user);
Group otherGroup = new Group();
otherGroupId = otherGroup.getId();
otherGroup.setName("otherGroup");
otherGroup.setParent(parentGroup);
em.persist(otherGroup);
User user2 = new User();
user2.setGroup(otherGroup);
user2Id = user2.getId();
user2.setLogin("someOtherUser");
em.persist(user2);
UserRole userRole = new UserRole();
userRoleId = userRole.getId();
userRole.setUser(user2);
Role role = new Role();
role.setName("TestRole");
roleId = role.getId();
em.persist(role);
userRole.setRole(role);
em.persist(userRole);
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class DataManagerCommitConstraintTest method setUp.
@Before
public void setUp() {
passwordEncryption = AppBeans.get(PasswordEncryption.class);
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
parentGroup = new Group();
parentGroup.setName("parentGroup");
em.persist(parentGroup);
constraintGroup1 = new Group();
constraintGroup1.setName("constraintGroup1");
em.persist(constraintGroup1);
constraintGroup2 = new Group();
constraintGroup2.setName("constraintGroup2");
em.persist(constraintGroup2);
constraintUpdate = new Constraint();
constraintUpdate.setEntityName("sec$User");
constraintUpdate.setCheckType(ConstraintCheckType.MEMORY);
constraintUpdate.setOperationType(ConstraintOperationType.UPDATE);
constraintUpdate.setGroovyScript("import com.haulmont.cuba.core.Persistence\n" + "import com.haulmont.cuba.core.global.AppBeans\n" + "import com.haulmont.cuba.security.entity.User\n" + "import org.apache.commons.lang.BooleanUtils\n" + "\n" + "Persistence persistence = AppBeans.get(Persistence.class)\n" + "User user = {E}\n" + "boolean currentActive = BooleanUtils.isTrue(user.active)\n" + "boolean oldActive = BooleanUtils.isTrue(persistence.tools.getOldValue(user, \"active\"))\n" + "\n" + "if (!oldActive && currentActive) {\n" + " return true\n" + "}\n" + "\n" + "if (oldActive) {\n" + " return true\n" + "}\n" + "\n" + "return false");
constraintUpdate.setGroup(constraintGroup1);
em.persist(constraintUpdate);
constraintDelete = new Constraint();
constraintDelete.setEntityName("sec$User");
constraintDelete.setCheckType(ConstraintCheckType.MEMORY);
constraintDelete.setOperationType(ConstraintOperationType.DELETE);
constraintDelete.setGroovyScript(constraintUpdate.getGroovyScript());
constraintDelete.setGroup(constraintGroup1);
em.persist(constraintDelete);
constraintCreate = new Constraint();
constraintCreate.setEntityName("sec$UserRole");
constraintCreate.setCheckType(ConstraintCheckType.MEMORY);
constraintCreate.setOperationType(ConstraintOperationType.CREATE);
constraintCreate.setGroovyScript("import com.haulmont.cuba.core.Persistence\n" + "import com.haulmont.cuba.core.global.AppBeans\n" + "import com.haulmont.cuba.security.entity.User\n" + "import org.apache.commons.lang.BooleanUtils\n" + "import com.haulmont.cuba.core.global.PersistenceHelper\n" + "\n" + "Persistence persistence = AppBeans.get(Persistence.class)\n" + "User user = {E}.user\n" + "PersistenceHelper.checkLoaded(user, 'active')\n" + "return BooleanUtils.isTrue(user.active)");
constraintCreate.setGroup(constraintGroup2);
em.persist(constraintCreate);
constraintUserUpdate = new User();
constraintUserUpdate.setLogin("constraintuserupdate");
constraintUserUpdate.setPassword(passwordEncryption.getPasswordHash(constraintUserUpdate.getId(), PASSWORD));
constraintUserUpdate.setGroup(constraintGroup1);
em.persist(constraintUserUpdate);
constraintUserCreate = new User();
constraintUserCreate.setLogin("constraintusercreate");
constraintUserCreate.setPassword(passwordEncryption.getPasswordHash(constraintUserCreate.getId(), PASSWORD));
constraintUserCreate.setGroup(constraintGroup2);
em.persist(constraintUserCreate);
testUserUpdate1 = new User();
testUserUpdate1.setName("oldName");
testUserUpdate1.setLogin("testuserupdate1");
testUserUpdate1.setPassword(passwordEncryption.getPasswordHash(testUserUpdate1.getId(), PASSWORD));
testUserUpdate1.setActive(false);
testUserUpdate1.setGroup(parentGroup);
em.persist(testUserUpdate1);
testUserUpdate2 = new User();
testUserUpdate2.setName("oldName");
testUserUpdate2.setLogin("testuserupdate2");
testUserUpdate2.setPassword(passwordEncryption.getPasswordHash(testUserUpdate2.getId(), PASSWORD));
testUserUpdate2.setActive(true);
testUserUpdate2.setGroup(parentGroup);
em.persist(testUserUpdate2);
testUserUpdate3 = new User();
testUserUpdate3.setName("oldName");
testUserUpdate3.setLogin("testuserupdate3");
testUserUpdate3.setPassword(passwordEncryption.getPasswordHash(testUserUpdate3.getId(), PASSWORD));
testUserUpdate3.setActive(false);
testUserUpdate3.setGroup(parentGroup);
em.persist(testUserUpdate3);
testUserDelete1 = new User();
testUserDelete1.setName("oldName");
testUserDelete1.setLogin("testuserdelete1");
testUserDelete1.setPassword(passwordEncryption.getPasswordHash(testUserDelete1.getId(), PASSWORD));
testUserDelete1.setActive(false);
testUserDelete1.setGroup(parentGroup);
em.persist(testUserDelete1);
testUserDelete2 = new User();
testUserDelete2.setName("oldName");
testUserDelete2.setLogin("testuserdelete2");
testUserDelete2.setPassword(passwordEncryption.getPasswordHash(testUserDelete2.getId(), PASSWORD));
testUserDelete2.setActive(true);
testUserDelete2.setGroup(parentGroup);
em.persist(testUserDelete2);
role = new Role();
role.setName("role1");
em.persist(role);
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class DistinctConstraintTest method setUp.
@Before
public void setUp() throws Exception {
passwordEncryption = AppBeans.get(PasswordEncryption.class);
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
Group parentGroup = new Group();
parentGroupId = parentGroup.getId();
parentGroup.setName("testParentGroup");
em.persist(parentGroup);
tx.commitRetaining();
em = cont.persistence().getEntityManager();
Group group = new Group();
groupId = group.getId();
group.setName("testGroup");
group.setParent(parentGroup);
em.persist(group);
Constraint userConstraint = new Constraint();
userConstraintId = userConstraint.getId();
userConstraint.setEntityName("sec$User");
userConstraint.setJoinClause("join {E}.userRoles ur");
userConstraint.setWhereClause("{E}.id is not null");
userConstraint.setGroup(group);
em.persist(userConstraint);
User user = new User();
user1Id = user.getId();
user.setLogin(USER_LOGIN);
String pwd = passwordEncryption.getPasswordHash(user1Id, USER_PASSW);
user.setPassword(pwd);
user.setGroup(group);
em.persist(user);
User user2 = new User();
user2.setGroup(parentGroup);
user2Id = user2.getId();
user2.setLogin("someOtherUser");
em.persist(user2);
Role role1 = new Role();
role1.setName("TestRole1");
role1Id = role1.getId();
em.persist(role1);
Role role2 = new Role();
role2.setName("TestRole2");
role2Id = role2.getId();
em.persist(role2);
UserRole userRole1 = new UserRole();
userRole1Id = userRole1.getId();
userRole1.setUser(user2);
userRole1.setRole(role1);
em.persist(userRole1);
UserRole userRole2 = new UserRole();
userRole2Id = userRole2.getId();
userRole2.setUser(user2);
userRole2.setRole(role2);
em.persist(userRole2);
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class EntityLogTest method testMultipleFlush.
@Test
public void testMultipleFlush() throws Exception {
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
Group group = em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93"));
User user = new User();
userId = user.getId();
user.setGroup(group);
user.setLogin("test");
user.setName("test-name");
user.setEmail("name@test.com");
em.persist(user);
em.flush();
user.setEmail("changed-name@test.com");
tx.commit();
} finally {
tx.end();
}
List<EntityLogItem> items = getEntityLogItems();
assertNotNull(items);
assertEquals(1, items.size());
EntityLogItem item = items.get(0);
assertEquals(EntityLogItem.Type.CREATE, item.getType());
EntityLogAttr attr = Iterables.find(item.getAttributes(), new Predicate<EntityLogAttr>() {
@Override
public boolean apply(EntityLogAttr attr) {
return "email".equals(attr.getName());
}
});
assertEquals("changed-name@test.com", attr.getValue());
// //////
tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
User user = em.find(User.class, userId);
user.setEmail("changed-2@test.com");
em.flush();
user.setEmail("changed-3@test.com");
tx.commit();
} finally {
tx.end();
}
items = getEntityLogItems();
assertNotNull(items);
assertEquals(2, items.size());
// the last because of sorting in query
item = items.get(0);
assertEquals(EntityLogItem.Type.MODIFY, item.getType());
attr = Iterables.find(item.getAttributes(), new Predicate<EntityLogAttr>() {
@Override
public boolean apply(EntityLogAttr attr) {
return "email".equals(attr.getName());
}
});
assertEquals("changed-3@test.com", attr.getValue());
assertEquals("changed-name@test.com", attr.getOldValue());
}
Aggregations