use of com.qcadoo.model.api.types.CollectionFieldType in project qcadoo by qcadoo.
the class DataAccessServiceImpl method deleteEntity.
private EntityOpResult deleteEntity(final InternalDataDefinition dataDefinition, final Long entityId, final boolean testOnly, final Set<EntitySignature> traversedEntities) {
Object databaseEntity = getDatabaseEntity(dataDefinition, entityId);
if (databaseEntity == null) {
logEntityDebug(dataDefinition, entityId, "has been deleted earlier, for example onDelete hook");
return new EntityOpResult(true, new EntityMessagesHolderImpl());
}
Entity entity = get(dataDefinition, entityId);
if (!dataDefinition.callDeleteHook(entity)) {
logDeletionErrors(entity);
entity.addGlobalError("qcadooView.message.deleteFailedMessage");
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return EntityOpResult.failure(entity);
}
priorityService.deprioritizeEntity(dataDefinition, databaseEntity);
Map<String, FieldDefinition> fields = dataDefinition.getFields();
for (FieldDefinition fieldDefinition : fields.values()) {
if (fieldDefinition.getType() instanceof CollectionFieldType) {
CollectionFieldType collectionFieldType = (CollectionFieldType) fieldDefinition.getType();
@SuppressWarnings("unchecked") Collection<Entity> children = (Collection<Entity>) entity.getField(fieldDefinition.getName());
EntityOpResult cascadeDeletionRes = performCascadeStrategy(entity, collectionFieldType, children, traversedEntities);
if (!cascadeDeletionRes.isSuccessfull()) {
return cascadeDeletionRes;
}
}
}
if (testOnly) {
logEntityInfo(dataDefinition, entityId, "may be cascade deleted");
} else {
try {
databaseEntity = getDatabaseEntity(dataDefinition, entityId);
if (databaseEntity != null) {
hibernateService.getCurrentSession().delete(databaseEntity);
hibernateService.getCurrentSession().flush();
}
} catch (ConstraintViolationException e) {
throw new IllegalStateException(getConstraintViolationMessage(entity), e);
}
logEntityDebug(dataDefinition, entityId, "has been deleted");
}
return new EntityOpResult(true, entity);
}
Aggregations