Search in sources :

Example 6 with EntityManager

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

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

the class EntityRestoreServiceBean method restoreDetails.

protected void restoreDetails(Entity entity, Date deleteTs, String storeName) {
    EntityManager em = persistence.getEntityManager(storeName);
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    List<MetaProperty> properties = new ArrayList<>();
    fillProperties(metaClass, properties, OnDelete.class.getName());
    for (MetaProperty property : properties) {
        OnDelete annotation = property.getAnnotatedElement().getAnnotation(OnDelete.class);
        DeletePolicy deletePolicy = annotation.value();
        if (deletePolicy == DeletePolicy.CASCADE) {
            MetaClass detailMetaClass = property.getRange().asClass();
            if (!storeName.equals(metadata.getTools().getStoreName(detailMetaClass))) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it is from different data store");
                continue;
            }
            if (!SoftDelete.class.isAssignableFrom(detailMetaClass.getJavaClass())) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it is hard deleted");
                continue;
            }
            MetaProperty inverseProp = property.getInverse();
            if (inverseProp == null) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it has no inverse property for " + metaClass);
                continue;
            }
            String jpql = "select e from " + detailMetaClass + " e where e." + inverseProp.getName() + ".id = ?1 " + "and e.deleteTs >= ?2 and e.deleteTs <= ?3";
            Query query = em.createQuery(jpql);
            query.setParameter(1, entity.getId());
            query.setParameter(2, DateUtils.addMilliseconds(deleteTs, -100));
            query.setParameter(3, DateUtils.addMilliseconds(deleteTs, 1000));
            // noinspection unchecked
            List<Entity> list = query.getResultList();
            for (Entity detailEntity : list) {
                if (entity instanceof SoftDelete) {
                    restoreEntity(detailEntity, storeName);
                }
            }
        }
    }
    properties = new ArrayList<>();
    fillProperties(metaClass, properties, OnDeleteInverse.class.getName());
    for (MetaProperty property : properties) {
        OnDeleteInverse annotation = property.getAnnotatedElement().getAnnotation(OnDeleteInverse.class);
        DeletePolicy deletePolicy = annotation.value();
        if (deletePolicy == DeletePolicy.CASCADE) {
            MetaClass detailMetaClass = property.getDomain();
            if (!storeName.equals(metadata.getTools().getStoreName(detailMetaClass))) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it is from different data store");
                continue;
            }
            if (!SoftDelete.class.isAssignableFrom(detailMetaClass.getJavaClass())) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it is hard deleted");
                continue;
            }
            List<MetaClass> metClassesToRestore = new ArrayList<>();
            metClassesToRestore.add(detailMetaClass);
            metClassesToRestore.addAll(detailMetaClass.getDescendants());
            for (MetaClass metaClassToRestore : metClassesToRestore) {
                if (!metadata.getTools().isPersistent(metaClassToRestore))
                    continue;
                String jpql = "select e from " + metaClassToRestore.getName() + " e where e." + property.getName() + ".id = ?1 and e.deleteTs >= ?2 and e.deleteTs <= ?3";
                Query query = em.createQuery(jpql);
                query.setParameter(1, entity.getId());
                query.setParameter(2, DateUtils.addMilliseconds(deleteTs, -100));
                query.setParameter(3, DateUtils.addMilliseconds(deleteTs, 1000));
                // noinspection unchecked
                List<Entity> list = query.getResultList();
                for (Entity detailEntity : list) {
                    if (entity instanceof SoftDelete) {
                        restoreEntity(detailEntity, storeName);
                    }
                }
            }
        }
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) Query(com.haulmont.cuba.core.Query) DeletePolicy(com.haulmont.cuba.core.global.DeletePolicy) EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) OnDeleteInverse(com.haulmont.cuba.core.entity.annotation.OnDeleteInverse) MetaProperty(com.haulmont.chile.core.model.MetaProperty) OnDelete(com.haulmont.cuba.core.entity.annotation.OnDelete)

Example 8 with EntityManager

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

the class EntityRestoreServiceBean method restoreEntity.

protected void restoreEntity(Entity entity, String storeName) {
    EntityManager em = persistence.getEntityManager(storeName);
    Entity reloadedEntity = em.find(entity.getClass(), entity.getId());
    if (reloadedEntity != null && ((SoftDelete) reloadedEntity).isDeleted()) {
        log.info("Restoring deleted entity " + entity);
        Date deleteTs = ((SoftDelete) reloadedEntity).getDeleteTs();
        ((SoftDelete) reloadedEntity).setDeleteTs(null);
        em.merge(reloadedEntity);
        restoreDetails(reloadedEntity, deleteTs, storeName);
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) SoftDelete(com.haulmont.cuba.core.entity.SoftDelete)

Example 9 with EntityManager

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

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

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