Search in sources :

Example 1 with OnDelete

use of com.haulmont.cuba.core.entity.annotation.OnDelete 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 2 with OnDelete

use of com.haulmont.cuba.core.entity.annotation.OnDelete in project cuba by cuba-platform.

the class DeletePolicyProcessor method processOnDelete.

protected void processOnDelete(List<MetaProperty> properties) {
    for (MetaProperty property : properties) {
        MetaClass metaClass = property.getRange().asClass();
        OnDelete annotation = property.getAnnotatedElement().getAnnotation(OnDelete.class);
        DeletePolicy deletePolicy = annotation.value();
        switch(deletePolicy) {
            case DENY:
                if (property.getRange().getCardinality().isMany()) {
                    if (!isCollectionEmpty(property))
                        throw new DeletePolicyException(this.metaClass.getName(), metaClass.getName());
                } else {
                    Object value = getReference(entity, property);
                    if (value != null)
                        throw new DeletePolicyException(this.metaClass.getName(), metaClass.getName());
                }
                break;
            case CASCADE:
                if (property.getRange().getCardinality().isMany()) {
                    Collection<Entity> value = getCollection(property);
                    if (value != null && !value.isEmpty()) {
                        for (Entity e : value) {
                            entityManager.remove(e);
                        }
                    }
                } else {
                    Entity value = getReference(entity, property);
                    if (value != null && checkIfEntityBelongsToMaster(property, value)) {
                        if (!(value instanceof SoftDelete)) {
                            if (PersistenceHelper.isLoaded(entity, property.getName())) {
                                entity.setValue(property.getName(), null);
                                entityManager.remove(value);
                            } else {
                                hardDeleteNotLoadedReference(entity, property, value);
                            }
                        } else {
                            entityManager.remove(value);
                        }
                    }
                }
                break;
            case UNLINK:
                if (property.getRange().getCardinality().isMany()) {
                    if (metadata.getTools().isOwningSide(property)) {
                        Collection<Entity> value = entity.getValue(property.getName());
                        if (value != null) {
                            value.clear();
                        }
                    } else if (property.getInverse() != null) {
                        Collection<Entity> value = getCollection(property);
                        if (value != null) {
                            value.forEach(e -> setReferenceNull(e, property.getInverse()));
                        }
                    } else {
                        throw new UnsupportedOperationException("Unable to unlink nested collection items");
                    }
                } else {
                    if (metadata.getTools().isOwningSide(property)) {
                        setReferenceNull(entity, property);
                    } else {
                        Entity value = getReference(entity, property);
                        if (value != null && property.getInverse() != null) {
                            setReferenceNull(value, property.getInverse());
                        }
                    }
                }
                break;
        }
    }
}
Also used : SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) Query(com.haulmont.cuba.core.Query) java.util(java.util) Logger(org.slf4j.Logger) Range(com.haulmont.chile.core.model.Range) EntityManager(com.haulmont.cuba.core.EntityManager) Persistence(com.haulmont.cuba.core.Persistence) OnDelete(com.haulmont.cuba.core.entity.annotation.OnDelete) MetaProperty(com.haulmont.chile.core.model.MetaProperty) LoggerFactory(org.slf4j.LoggerFactory) OnDeleteInverse(com.haulmont.cuba.core.entity.annotation.OnDeleteInverse) MetaClass(com.haulmont.chile.core.model.MetaClass) PersistenceImpl(com.haulmont.cuba.core.sys.PersistenceImpl) Scope(org.springframework.context.annotation.Scope) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) Component(org.springframework.stereotype.Component) SQLException(java.sql.SQLException) QueryRunner(com.haulmont.bali.db.QueryRunner) Entity(com.haulmont.cuba.core.entity.Entity) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) MetaProperty(com.haulmont.chile.core.model.MetaProperty) OnDelete(com.haulmont.cuba.core.entity.annotation.OnDelete)

Aggregations

MetaClass (com.haulmont.chile.core.model.MetaClass)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 EntityManager (com.haulmont.cuba.core.EntityManager)2 Query (com.haulmont.cuba.core.Query)2 Entity (com.haulmont.cuba.core.entity.Entity)2 SoftDelete (com.haulmont.cuba.core.entity.SoftDelete)2 OnDelete (com.haulmont.cuba.core.entity.annotation.OnDelete)2 OnDeleteInverse (com.haulmont.cuba.core.entity.annotation.OnDeleteInverse)2 QueryRunner (com.haulmont.bali.db.QueryRunner)1 Range (com.haulmont.chile.core.model.Range)1 Persistence (com.haulmont.cuba.core.Persistence)1 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)1 DeletePolicy (com.haulmont.cuba.core.global.DeletePolicy)1 PersistenceImpl (com.haulmont.cuba.core.sys.PersistenceImpl)1 SQLException (java.sql.SQLException)1 java.util (java.util)1 Inject (javax.inject.Inject)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Scope (org.springframework.context.annotation.Scope)1