use of com.haulmont.cuba.core.entity.annotation.OnDeleteInverse 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;
}
if (metadataTools.isOwningSide(property)) {
Object value = entity.getValue(property.getName());
if (value instanceof Entity) {
restoreEntity((Entity) value, storeName);
} else if (value instanceof Collection) {
for (Object detailEntity : (Collection) value) {
restoreEntity((Entity) detailEntity, storeName);
}
}
} else {
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;
if (property.getRange().getCardinality().isMany()) {
jpql = "select e from " + metaClassToRestore.getName() + " e join e." + property.getName() + " p" + " where p.id = ?1 and e.deleteTs >= ?2 and e.deleteTs <= ?3";
} else {
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);
}
}
}
}
}
}
use of com.haulmont.cuba.core.entity.annotation.OnDeleteInverse in project cuba by cuba-platform.
the class DeletePolicyProcessor method processOnDeleteInverse.
protected void processOnDeleteInverse(List<MetaProperty> properties) {
for (MetaProperty property : properties) {
MetaClass metaClass = property.getDomain();
List<MetaClass> persistentEntities = new ArrayList<>();
if (isPersistent(metaClass))
persistentEntities.add(metaClass);
for (MetaClass descendant : metaClass.getDescendants()) {
if (isPersistent(descendant))
persistentEntities.add(descendant);
}
for (MetaClass persistentEntity : persistentEntities) {
OnDeleteInverse annotation = property.getAnnotatedElement().getAnnotation(OnDeleteInverse.class);
DeletePolicy deletePolicy = annotation.value();
switch(deletePolicy) {
case DENY:
if (referenceExists(persistentEntity.getName(), property))
throw new DeletePolicyException(this.metaClass.getName(), persistentEntity.getName());
break;
case CASCADE:
cascade(persistentEntity.getName(), property);
break;
case UNLINK:
unlink(persistentEntity.getName(), property);
break;
}
}
}
}
Aggregations