use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class LockManager method getConfig.
private Map<String, LockDescriptor> getConfig() {
if (this.config == null) {
synchronized (this) {
if (this.config == null) {
Map<String, LockDescriptor> config = new ConcurrentHashMap<>();
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
TypedQuery<LockDescriptor> q = em.createQuery("select d from sys$LockDescriptor d", LockDescriptor.class);
List<LockDescriptor> list = q.getResultList();
for (LockDescriptor ld : list) {
config.put(ld.getName(), ld);
}
tx.commit();
} finally {
tx.end();
}
this.config = config;
}
}
}
return config;
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class FoldersServiceBean method loadAppFolders.
@Override
public List<AppFolder> loadAppFolders() {
log.debug("Loading AppFolders");
StopWatch stopWatch = new Slf4JStopWatch("AppFolders");
stopWatch.start();
List<AppFolder> resultList;
try (Transaction tx = persistence.createTransaction()) {
String metaClassName = metadata.getExtendedEntities().getEffectiveMetaClass(AppFolder.class).getName();
TypedQuery<AppFolder> q = persistence.getEntityManager().createQuery("select f from " + metaClassName + " f order by f.sortOrder, f.name", AppFolder.class);
resultList = q.getResultList();
// fetch parent folder
resultList.forEach(Folder::getParent);
tx.commit();
} finally {
stopWatch.stop();
}
if (CollectionUtils.isNotEmpty(resultList)) {
Binding binding = new Binding();
binding.setVariable("persistence", persistence);
binding.setVariable("metadata", metadata);
binding.setVariable("userSession", userSessionSource.getUserSession());
Iterator<AppFolder> iterator = resultList.iterator();
while (iterator.hasNext()) {
AppFolder folder = iterator.next();
try (Transaction tx = persistence.createTransaction()) {
boolean evaluatedVisibilityScript = true;
try {
if (!StringUtils.isBlank(folder.getVisibilityScript())) {
binding.setVariable("folder", folder);
Boolean visible = runScript(folder.getVisibilityScript(), binding);
if (BooleanUtils.isFalse(visible)) {
iterator.remove();
continue;
}
}
} catch (Exception e) {
log.warn("Unable to evaluate AppFolder visibility script for folder: id: {} name: {}", folder.getId(), folder.getName(), e);
// because EclipseLink Query marks transaction as rollback-only on JPQL syntax errors
evaluatedVisibilityScript = false;
}
boolean evaluatedQuantityScript = loadFolderQuantity(binding, folder);
if (evaluatedVisibilityScript && evaluatedQuantityScript) {
tx.commit();
}
}
}
}
return resultList;
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class FoldersServiceBean method loadSearchFolders.
@Override
public List<SearchFolder> loadSearchFolders() {
log.debug("Loading SearchFolders");
StopWatch stopWatch = new Slf4JStopWatch("SearchFolders");
stopWatch.start();
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
MetaClass effectiveMetaClass = metadata.getExtendedEntities().getEffectiveMetaClass(SearchFolder.class);
TypedQuery<SearchFolder> q = em.createQuery("select f from " + effectiveMetaClass.getName() + " f " + "left join f.user u on u.id = ?1 " + "where (u.id = ?1 or u is null) " + "order by f.sortOrder, f.name", SearchFolder.class);
q.setViewName("searchFolders");
q.setParameter(1, userSessionSource.currentOrSubstitutedUserId());
List<SearchFolder> list = q.getResultList();
tx.commit();
return list;
} finally {
tx.end();
stopWatch.stop();
}
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class FoldersServiceBean method reloadAppFolders.
@Override
public List<AppFolder> reloadAppFolders(List<AppFolder> folders) {
log.debug("Reloading AppFolders {}", folders);
StopWatch stopWatch = new Slf4JStopWatch("AppFolders");
stopWatch.start();
try {
if (!folders.isEmpty()) {
Binding binding = new Binding();
binding.setVariable("persistence", persistence);
binding.setVariable("metadata", metadata);
binding.setProperty("userSession", userSessionSource.getUserSession());
for (AppFolder folder : folders) {
Transaction tx = persistence.createTransaction();
try {
if (loadFolderQuantity(binding, folder)) {
tx.commit();
}
} finally {
tx.end();
}
}
}
return folders;
} finally {
stopWatch.stop();
}
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class DistinctConstraintTest method setUp.
@BeforeEach
public void setUp() throws Exception {
PasswordEncryption 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();
}
}
Aggregations