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 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 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 NumberIdWorker method getResult.
protected long getResult(String entityName, String sqlScript, long startValue, long increment) {
Transaction tx = persistence.getTransaction(getDataStore(entityName));
try {
checkSequenceExists(entityName, startValue, increment);
Object value = executeScript(entityName, sqlScript);
tx.commit();
if (value instanceof Long)
return (Long) value;
else if (value instanceof BigDecimal)
return ((BigDecimal) value).longValue();
else if (value instanceof String)
return Long.parseLong((String) value);
else if (value == null)
throw new IllegalStateException("No value returned");
else
throw new IllegalStateException("Unsupported value type: " + value.getClass());
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class NumberIdWorker method checkSequenceExists.
protected void checkSequenceExists(String entityName, long startValue, long increment) {
String seqName = getSequenceName(entityName);
if (existingSequences.contains(seqName))
return;
// Create sequence in separate transaction because it's name is cached and we want to be sure it is created
// regardless of possible errors in the invoking code
Transaction tx = persistence.createTransaction(getDataStore(entityName));
try {
EntityManager em = persistence.getEntityManager(getDataStore(entityName));
SequenceSupport sequenceSupport = getSequenceSupport(entityName);
Query query = em.createNativeQuery(sequenceSupport.sequenceExistsSql(seqName));
List list = query.getResultList();
if (list.isEmpty()) {
query = em.createNativeQuery(sequenceSupport.createSequenceSql(seqName, startValue, increment));
query.executeUpdate();
}
existingSequences.add(seqName);
tx.commit();
} finally {
tx.end();
}
}
Aggregations