use of com.haulmont.cuba.core.global.DeletePolicy 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);
}
}
}
}
}
}
Aggregations