Search in sources :

Example 11 with Transaction

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;
}
Also used : Binding(groovy.lang.Binding) Slf4JStopWatch(org.perf4j.slf4j.Slf4JStopWatch) AppFolder(com.haulmont.cuba.core.entity.AppFolder) SearchFolder(com.haulmont.cuba.security.entity.SearchFolder) Folder(com.haulmont.cuba.core.entity.Folder) IOException(java.io.IOException) Slf4JStopWatch(org.perf4j.slf4j.Slf4JStopWatch) StopWatch(org.perf4j.StopWatch) AppFolder(com.haulmont.cuba.core.entity.AppFolder) Transaction(com.haulmont.cuba.core.Transaction)

Example 12 with Transaction

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();
    }
}
Also used : Binding(groovy.lang.Binding) AppFolder(com.haulmont.cuba.core.entity.AppFolder) Slf4JStopWatch(org.perf4j.slf4j.Slf4JStopWatch) Transaction(com.haulmont.cuba.core.Transaction) Slf4JStopWatch(org.perf4j.slf4j.Slf4JStopWatch) StopWatch(org.perf4j.StopWatch)

Example 13 with Transaction

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;
}
Also used : LockDescriptor(com.haulmont.cuba.core.entity.LockDescriptor) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 14 with Transaction

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();
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) BigDecimal(java.math.BigDecimal)

Example 15 with Transaction

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();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) SequenceSupport(com.haulmont.cuba.core.sys.persistence.SequenceSupport) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) List(java.util.List)

Aggregations

Transaction (com.haulmont.cuba.core.Transaction)211 EntityManager (com.haulmont.cuba.core.EntityManager)138 Test (org.junit.Test)44 User (com.haulmont.cuba.security.entity.User)30 Query (com.haulmont.cuba.core.Query)26 View (com.haulmont.cuba.core.global.View)21 Before (org.junit.Before)15 Group (com.haulmont.cuba.security.entity.Group)13 TypedQuery (com.haulmont.cuba.core.TypedQuery)11 MetaClass (com.haulmont.chile.core.model.MetaClass)9 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 Entity (com.haulmont.cuba.core.entity.Entity)7 List (java.util.List)6 Role (com.haulmont.cuba.security.entity.Role)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)5 UUID (java.util.UUID)5 Nullable (javax.annotation.Nullable)5 AppFolder (com.haulmont.cuba.core.entity.AppFolder)4 IdentityEntity (com.haulmont.cuba.testmodel.primary_keys.IdentityEntity)4