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);
}
}
}
}
}
}
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;
}
}
}
Aggregations