Search in sources :

Example 1 with CollectionFieldType

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);
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) CollectionFieldType(com.qcadoo.model.api.types.CollectionFieldType) EntityOpResult(com.qcadoo.model.api.EntityOpResult) Collection(java.util.Collection) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException)

Aggregations

Entity (com.qcadoo.model.api.Entity)1 EntityOpResult (com.qcadoo.model.api.EntityOpResult)1 FieldDefinition (com.qcadoo.model.api.FieldDefinition)1 CollectionFieldType (com.qcadoo.model.api.types.CollectionFieldType)1 InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)1 Collection (java.util.Collection)1 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1