Search in sources :

Example 6 with Transaction

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

the class Emailer method migrateEmailsToFileStorage.

@Override
public void migrateEmailsToFileStorage(List<SendingMessage> messages) {
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        for (SendingMessage msg : messages) {
            migrateMessage(em, msg);
        }
        tx.commit();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage)

Example 7 with Transaction

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

the class EntitySnapshotManager method migrateSnapshots.

@Override
public void migrateSnapshots(MetaClass metaClass, Object id, Map<Class, Class> classMapping) {
    metaClass = extendedEntities.getOriginalOrThisMetaClass(metaClass);
    // load snapshots
    List<EntitySnapshot> snapshotList = getSnapshots(metaClass, id);
    Class javaClass = metaClass.getJavaClass();
    MetaClass mappedMetaClass = null;
    if (classMapping.containsKey(javaClass)) {
        Class mappedClass = classMapping.get(javaClass);
        mappedMetaClass = extendedEntities.getOriginalOrThisMetaClass(metadata.getClass(mappedClass));
    }
    for (EntitySnapshot snapshot : snapshotList) {
        if (mappedMetaClass != null) {
            snapshot.setEntityMetaClass(mappedMetaClass.getName());
        }
        String snapshotXml = snapshot.getSnapshotXml();
        String viewXml = snapshot.getViewXml();
        snapshot.setSnapshotXml(processSnapshotXml(snapshotXml, classMapping));
        snapshot.setViewXml(processViewXml(viewXml, classMapping));
    }
    // Save snapshots to db
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        for (EntitySnapshot snapshot : snapshotList) {
            em.merge(snapshot);
        }
        tx.commit();
    } finally {
        tx.end();
    }
}
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 8 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 9 with Transaction

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

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