Search in sources :

Example 11 with EntityManager

use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.

the class EntitySnapshotManager method getSnapshots.

@Override
public List<EntitySnapshot> getSnapshots(MetaClass metaClass, Object id) {
    metaClass = extendedEntities.getOriginalOrThisMetaClass(metaClass);
    Entity entity = dataManager.load(new LoadContext<>(metaClass).setId(id).setView(View.LOCAL));
    checkCompositePrimaryKey(entity);
    List<EntitySnapshot> resultList = null;
    View view = metadata.getViewRepository().getView(EntitySnapshot.class, "entitySnapshot.browse");
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        TypedQuery<EntitySnapshot> query = em.createQuery(format("select s from sys$EntitySnapshot s where s.entity.%s = :entityId and s.entityMetaClass = :metaClass " + "order by s.snapshotDate desc", referenceToEntitySupport.getReferenceIdPropertyName(metaClass)), EntitySnapshot.class);
        query.setParameter("entityId", referenceToEntitySupport.getReferenceId(entity));
        query.setParameter("metaClass", metaClass.getName());
        query.setView(view);
        resultList = query.getResultList();
        tx.commit();
    } finally {
        tx.end();
    }
    return resultList;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 12 with EntityManager

use of com.haulmont.cuba.core.EntityManager 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 fetch f.user u on u.id = ?1 " + "left join fetch f.presentation " + "where (u.id = ?1 or u is null) " + "order by f.sortOrder, f.name", SearchFolder.class);
        q.setParameter(1, userSessionSource.currentOrSubstitutedUserId());
        List<SearchFolder> list = q.getResultList();
        // fetch parents
        for (SearchFolder folder : list) {
            folder.getParent();
        }
        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 13 with EntityManager

use of com.haulmont.cuba.core.EntityManager 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 EntityManager

use of com.haulmont.cuba.core.EntityManager 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)

Example 15 with EntityManager

use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.

the class TestingServiceBean method clearScheduledTasks.

@Override
public void clearScheduledTasks() {
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        Query query = em.createNativeQuery("delete from SYS_SCHEDULED_EXECUTION");
        query.executeUpdate();
        query = em.createNativeQuery("delete from SYS_SCHEDULED_TASK");
        query.executeUpdate();
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query)

Aggregations

EntityManager (com.haulmont.cuba.core.EntityManager)167 Transaction (com.haulmont.cuba.core.Transaction)140 Query (com.haulmont.cuba.core.Query)27 User (com.haulmont.cuba.security.entity.User)27 Test (org.junit.Test)25 View (com.haulmont.cuba.core.global.View)22 MetaClass (com.haulmont.chile.core.model.MetaClass)14 Group (com.haulmont.cuba.security.entity.Group)12 Before (org.junit.Before)11 Entity (com.haulmont.cuba.core.entity.Entity)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 UUID (java.util.UUID)7 Nullable (javax.annotation.Nullable)7 TypedQuery (com.haulmont.cuba.core.TypedQuery)6 List (java.util.List)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)5 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 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)4