Search in sources :

Example 71 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 72 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 73 with Transaction

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();
    }
}
Also used : Slf4JStopWatch(org.perf4j.slf4j.Slf4JStopWatch) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) MetaClass(com.haulmont.chile.core.model.MetaClass) SearchFolder(com.haulmont.cuba.security.entity.SearchFolder) Slf4JStopWatch(org.perf4j.slf4j.Slf4JStopWatch) StopWatch(org.perf4j.StopWatch)

Example 74 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 75 with Transaction

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();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

Transaction (com.haulmont.cuba.core.Transaction)226 EntityManager (com.haulmont.cuba.core.EntityManager)142 Test (org.junit.jupiter.api.Test)59 Query (com.haulmont.cuba.core.Query)30 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)27 User (com.haulmont.cuba.security.entity.User)25 View (com.haulmont.cuba.core.global.View)22 BeforeEach (org.junit.jupiter.api.BeforeEach)18 TypedQuery (com.haulmont.cuba.core.TypedQuery)13 Group (com.haulmont.cuba.security.entity.Group)12 List (java.util.List)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 MetaClass (com.haulmont.chile.core.model.MetaClass)7 Entity (com.haulmont.cuba.core.entity.Entity)7 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)7 ChildCachedEntity (com.haulmont.cuba.testmodel.entity_cache.ChildCachedEntity)5 ParentCachedEntity (com.haulmont.cuba.testmodel.entity_cache.ParentCachedEntity)5 UUID (java.util.UUID)5 Nullable (javax.annotation.Nullable)5 EntityManagerFactory (javax.persistence.EntityManagerFactory)5