Search in sources :

Example 1 with Transaction

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

the class EntitySnapshotManager method createSnapshot.

@Override
public EntitySnapshot createSnapshot(Entity entity, View view, Date snapshotDate, User author) {
    Preconditions.checkNotNullArgument(entity);
    Preconditions.checkNotNullArgument(view);
    Preconditions.checkNotNullArgument(snapshotDate);
    checkCompositePrimaryKey(entity);
    Class viewEntityClass = view.getEntityClass();
    Class entityClass = entity.getClass();
    if (!viewEntityClass.isAssignableFrom(entityClass)) {
        throw new IllegalStateException("View could not be used with this propertyValue");
    }
    MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(metadata.getClass(entity.getClass()));
    EntitySnapshot snapshot = metadata.create(EntitySnapshot.class);
    snapshot.setObjectEntityId(referenceToEntitySupport.getReferenceId(entity));
    snapshot.setEntityMetaClass(metaClass.getName());
    snapshot.setViewXml(viewSerializationAPI.toJson(view, ViewSerializationOption.COMPACT_FORMAT));
    snapshot.setSnapshotXml(entitySerializationAPI.toJson(entity));
    snapshot.setSnapshotDate(snapshotDate);
    snapshot.setAuthor(author);
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        em.persist(snapshot);
        tx.commit();
    } finally {
        tx.end();
    }
    return snapshot;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) Transaction(com.haulmont.cuba.core.Transaction) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 2 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 3 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)

Example 4 with Transaction

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

the class UniqueNumbers method checkSequenceExists.

protected void checkSequenceExists(String domain) {
    String seqName = getSequenceName(domain);
    if (containsSequence(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(domain));
    try {
        lock.readLock().unlock();
        lock.writeLock().lock();
        EntityManager em = persistence.getEntityManager(getDataStore(domain));
        Query query = em.createNativeQuery(getSequenceSupport(domain).sequenceExistsSql(seqName));
        List list = query.getResultList();
        if (list.isEmpty()) {
            query = em.createNativeQuery(getSequenceSupport(domain).createSequenceSql(seqName, 1, 1));
            query.executeUpdate();
        }
        tx.commit();
        existingSequences.add(seqName);
    } finally {
        lock.readLock().lock();
        lock.writeLock().unlock();
        tx.end();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) List(java.util.List)

Example 5 with Transaction

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

the class StandardCacheLoader method updateData.

@Override
public void updateData(CacheSet cacheSet, Map<String, Object> params) throws CacheException {
    if (configuration.getConfig(GlobalConfig.class).getPerformanceTestMode())
        return;
    Collection<Object> items = cacheSet.getItems();
    List updateItems = (List) params.get("items");
    if ((updateItems != null) && (updateItems.size() > 0)) {
        MetaClass metaClass = metadata.getSession().getClass(metaClassName);
        View view = metadata.getViewRepository().getView(metaClass, viewName);
        Transaction tx = persistence.createTransaction();
        try {
            EntityManager em = persistence.getEntityManager();
            for (Object item : updateItems) {
                Entity entity = (Entity) item;
                entity = em.find(entity.getClass(), entity.getId(), view);
                items.remove(item);
                if (entity != null)
                    items.add(entity);
            }
            tx.commit();
        } catch (Exception e) {
            throw new CacheException(e);
        } finally {
            tx.end();
        }
    } else {
        log.debug("Nothing to update");
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) Transaction(com.haulmont.cuba.core.Transaction) List(java.util.List)

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