use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class GroupEntityListener method createNewHierarchy.
protected void createNewHierarchy(Group entity, Group parent) {
if (parent == null) {
entity.setHierarchyList(new ArrayList<>());
return;
}
if (!PersistenceHelper.isManaged(parent) && !PersistenceHelper.isDetached(parent))
throw new IllegalStateException("Unable to create GroupHierarchy. Commit parent group first.");
EntityManager em = persistence.getEntityManager();
if (entity.getHierarchyList() == null) {
entity.setHierarchyList(new ArrayList<>());
} else {
entity.getHierarchyList().clear();
}
if (PersistenceHelper.isDetached(parent))
// refresh parent in case of detached
parent = em.find(Group.class, parent.getId());
int level = 0;
for (GroupHierarchy hierarchy : parent.getHierarchyList()) {
GroupHierarchy h = metadata.create(GroupHierarchy.class);
h.setGroup(entity);
h.setParent(hierarchy.getParent());
h.setLevel(level++);
em.persist(h);
entity.getHierarchyList().add(h);
}
GroupHierarchy h = metadata.create(GroupHierarchy.class);
h.setGroup(entity);
h.setParent(parent);
h.setLevel(level);
em.persist(h);
entity.getHierarchyList().add(h);
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class UserSessionManager method getPermissionValue.
public Integer getPermissionValue(User user, PermissionType permissionType, String target) {
Integer result;
List<Role> roles = new ArrayList<>();
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
user = em.find(User.class, user.getId());
for (UserRole userRole : user.getUserRoles()) {
if (userRole.getRole() != null) {
roles.add(userRole.getRole());
}
}
UserSession session = new UserSession(uuidSource.createUuid(), user, roles, userSessionSource.getLocale(), false);
compilePermissions(session, roles);
result = session.getPermissionValue(permissionType, target);
tx.commit();
} finally {
tx.end();
}
return result;
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class UserSessionManager method compileConstraints.
protected void compileConstraints(UserSession session, Group group) {
EntityManager em = persistence.getEntityManager();
TypedQuery<Constraint> q = em.createQuery("select c from sec$GroupHierarchy h join h.parent.constraints c " + "where h.group.id = ?1", Constraint.class);
q.setParameter(1, group);
List<Constraint> constraints = q.getResultList();
List<Constraint> list = new ArrayList<>(constraints);
list.addAll(group.getConstraints());
for (Constraint constraint : list) {
if (Boolean.TRUE.equals(constraint.getIsActive())) {
session.addConstraint(constraint);
}
}
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class CascadeDeleteTest method testRemoveCascade.
@Test
public void testRemoveCascade() throws Exception {
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.persistence().getEntityManager();
CascadeEntity loadedRoot = em.find(CascadeEntity.class, root.getId());
em.find(CascadeEntity.class, first.getId());
em.remove(loadedRoot);
tx.commit();
}
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.persistence().getEntityManager();
List r = em.createQuery("select e from test$CascadeEntity e").getResultList();
assertEquals(0, r.size());
tx.commit();
}
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class SchedulingServiceBean method setActive.
@Override
public void setActive(Set<ScheduledTask> tasks, boolean active) {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
for (ScheduledTask task : tasks) {
ScheduledTask t = em.find(ScheduledTask.class, task.getId());
t.setActive(active);
}
tx.commit();
}
}
Aggregations