Search in sources :

Example 11 with EntityOpResult

use of com.qcadoo.model.api.EntityOpResult in project mes by qcadoo.

the class DetailedProductionCountingAndProgressListHooks method onRemoveSelectedProductionCountingQuantities.

public void onRemoveSelectedProductionCountingQuantities(final ViewDefinitionState view, final ComponentState state, final String[] args) {
    GridComponent grid = ((GridComponent) view.getComponentByReference(QcadooViewConstants.L_GRID));
    List<Entity> selectedEntities = grid.getSelectedEntities();
    List<Long> ids = new ArrayList<>();
    boolean deleteSuccessful = true;
    List<ErrorMessage> errors = Lists.newArrayList();
    for (Entity productionCountingQuantity : selectedEntities) {
        String typeOfMaterial = productionCountingQuantity.getStringField(ProductionCountingQuantityFields.TYPE_OF_MATERIAL);
        if (GlobalTypeOfMaterial.FINAL_PRODUCT.getStringValue().equals(typeOfMaterial)) {
            state.addMessage("basicProductionCounting.productionCountingQuantity.error.cantDeleteFinal", ComponentState.MessageType.INFO);
        } else {
            ids.add(productionCountingQuantity.getId());
            if (deleteSuccessful) {
                EntityOpResult result = productionCountingQuantity.getDataDefinition().delete(productionCountingQuantity.getId());
                if (!result.isSuccessfull()) {
                    deleteSuccessful = false;
                    errors.addAll(result.getMessagesHolder().getGlobalErrors());
                }
            }
        }
    }
    if (ids.size() == 1 && deleteSuccessful) {
        state.addMessage("qcadooView.message.deleteMessage", ComponentState.MessageType.SUCCESS);
    } else if (ids.size() > 1 && deleteSuccessful) {
        state.addMessage("qcadooView.message.deleteMessages", ComponentState.MessageType.SUCCESS, String.valueOf(ids.size()));
    } else if (!deleteSuccessful) {
        errors.stream().forEach(error -> state.addMessage(error));
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) GridComponent(com.qcadoo.view.api.components.GridComponent) ArrayList(java.util.ArrayList) EntityOpResult(com.qcadoo.model.api.EntityOpResult) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage)

Example 12 with EntityOpResult

use of com.qcadoo.model.api.EntityOpResult 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)

Example 13 with EntityOpResult

use of com.qcadoo.model.api.EntityOpResult in project qcadoo by qcadoo.

the class DataAccessServiceImpl method deleteOrphans.

private EntityOpResult deleteOrphans(final InternalDataDefinition dataDefinition, final Iterable<Entity> orphans) {
    checkNotNull(dataDefinition, L_DATA_DEFINITION_MUST_BE_GIVEN);
    checkState(dataDefinition.isDeletable(), "Entity must be deletable");
    checkState(dataDefinition.isEnabled(), L_DATA_DEFINITION_BELONGS_TO_DISABLED_PLUGIN);
    for (Entity orphan : orphans) {
        EntityOpResult result = deleteEntity(dataDefinition, orphan.getId());
        if (!result.isSuccessfull()) {
            return result;
        }
    }
    return EntityOpResult.successfull();
}
Also used : Entity(com.qcadoo.model.api.Entity) EntityOpResult(com.qcadoo.model.api.EntityOpResult)

Example 14 with EntityOpResult

use of com.qcadoo.model.api.EntityOpResult in project qcadoo by qcadoo.

the class DataAccessServiceImpl method performSave.

@SuppressWarnings("unchecked")
private Entity performSave(final InternalDataDefinition dataDefinition, final Entity genericEntity, final Set<Entity> alreadySavedEntities, final Set<Entity> newlySavedEntities, boolean fast) {
    checkNotNull(dataDefinition, L_DATA_DEFINITION_MUST_BE_GIVEN);
    checkState(dataDefinition.isEnabled(), L_DATA_DEFINITION_BELONGS_TO_DISABLED_PLUGIN);
    checkNotNull(genericEntity, "Entity must be given");
    if (alreadySavedEntities.contains(genericEntity)) {
        return genericEntity;
    }
    Entity genericEntityToSave = genericEntity.copy();
    Object existingDatabaseEntity = getExistingDatabaseEntity(dataDefinition, genericEntity);
    Entity existingGenericEntity = null;
    if (existingDatabaseEntity != null) {
        existingGenericEntity = entityService.convertToGenericEntity(dataDefinition, existingDatabaseEntity);
    }
    if (!fast) {
        validationService.validateGenericEntity(dataDefinition, genericEntity, existingGenericEntity);
    }
    if (!genericEntity.isValid()) {
        copyValidationErrors(dataDefinition, genericEntityToSave, genericEntity);
        if (existingGenericEntity != null) {
            copyMissingFields(genericEntityToSave, existingGenericEntity);
        }
        logValidationErrors(genericEntityToSave);
        return genericEntityToSave;
    }
    Object databaseEntity = entityService.convertToDatabaseEntity(dataDefinition, genericEntity, existingDatabaseEntity);
    if (genericEntity.getId() == null) {
        priorityService.prioritizeEntity(dataDefinition, databaseEntity);
    }
    saveDatabaseEntity(dataDefinition, databaseEntity);
    if (dataDefinition.isVersionable()) {
        hibernateService.getCurrentSession().flush();
    }
    Entity savedEntity = entityService.convertToGenericEntity(dataDefinition, databaseEntity);
    copyGlobalMessages(dataDefinition, savedEntity, genericEntity);
    for (Entry<String, FieldDefinition> fieldEntry : dataDefinition.getFields().entrySet()) {
        if (fieldEntry.getValue().getType() instanceof HasManyType) {
            List<Entity> entities = (List<Entity>) genericEntity.getField(fieldEntry.getKey());
            HasManyType hasManyType = (HasManyType) fieldEntry.getValue().getType();
            if (entities == null || entities instanceof EntityListImpl) {
                savedEntity.setField(fieldEntry.getKey(), entities);
                continue;
            }
            List<Entity> savedEntities = saveHasManyEntities(alreadySavedEntities, newlySavedEntities, hasManyType.getJoinFieldName(), savedEntity, entities, (InternalDataDefinition) hasManyType.getDataDefinition());
            EntityList dbEntities = savedEntity.getHasManyField(fieldEntry.getKey());
            EntityOpResult results = removeOrphans(hasManyType, findOrphans(savedEntities, dbEntities));
            if (!results.isSuccessfull()) {
                // #TODO MAKU
                copyValidationErrors(dataDefinition, savedEntity, results.getMessagesHolder());
                savedEntity.setField(fieldEntry.getKey(), existingGenericEntity.getField(fieldEntry.getKey()));
                return savedEntity;
            }
            savedEntity.setField(fieldEntry.getKey(), savedEntities);
        } else if (fieldEntry.getValue().getType() instanceof TreeType) {
            List<Entity> entities = (List<Entity>) genericEntity.getField(fieldEntry.getKey());
            if (entities == null || entities instanceof EntityTreeImpl) {
                savedEntity.setField(fieldEntry.getKey(), entities);
                continue;
            }
            TreeType treeType = (TreeType) fieldEntry.getValue().getType();
            List<Entity> savedEntities = saveTreeEntities(alreadySavedEntities, newlySavedEntities, treeType.getJoinFieldName(), savedEntity, entities, (InternalDataDefinition) treeType.getDataDefinition(), null);
            savedEntity.setField(fieldEntry.getKey(), savedEntities);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(savedEntity + " has been saved");
    }
    alreadySavedEntities.add(savedEntity);
    if (genericEntity.getId() == null && savedEntity.getId() != null) {
        newlySavedEntities.add(savedEntity);
    }
    return savedEntity;
}
Also used : Entity(com.qcadoo.model.api.Entity) TreeType(com.qcadoo.model.api.types.TreeType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) EntityList(com.qcadoo.model.api.EntityList) EntityOpResult(com.qcadoo.model.api.EntityOpResult) HasManyType(com.qcadoo.model.api.types.HasManyType) EntityList(com.qcadoo.model.api.EntityList) List(java.util.List) ArrayList(java.util.ArrayList) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 15 with EntityOpResult

use of com.qcadoo.model.api.EntityOpResult in project qcadoo by qcadoo.

the class CyclesPreventionTest method shouldNotFallIntoInfiniteCycleDuringCascadeManyToManyDeletion3entities.

@Test
public final void shouldNotFallIntoInfiniteCycleDuringCascadeManyToManyDeletion3entities() {
    // given
    Entity modelA = buildEntity(aDataDefinition);
    Entity modelB = buildEntity(bDataDefinition);
    Entity modelC = buildEntity(cDataDefinition);
    modelA.setField(FIELD_MTM_B, Lists.newArrayList(modelB));
    modelB.setField(FIELD_MTM_A, Lists.newArrayList(modelA));
    modelB.setField(FIELD_MTM_C, Lists.newArrayList(modelC));
    modelC.setField(FIELD_MTM_B, Lists.newArrayList(modelB));
    modelC.setField(FIELD_MTM_A, Lists.newArrayList(modelA));
    modelA.setField(FIELD_MTM_C, Lists.newArrayList(modelC));
    // BEWARE! Remember about filling both sides before save!
    modelA = fromDb(save(modelA));
    modelB = fromDb(save(modelB));
    modelC = fromDb(save(modelC));
    // when
    EntityOpResult result = aDataDefinition.delete(modelA.getId());
    // then
    Assert.assertTrue(result.isSuccessfull());
    Assert.assertNull(fromDb(modelA));
    Assert.assertNull(fromDb(modelB));
    Assert.assertNull(fromDb(modelC));
}
Also used : Entity(com.qcadoo.model.api.Entity) EntityOpResult(com.qcadoo.model.api.EntityOpResult) Test(org.junit.Test)

Aggregations

EntityOpResult (com.qcadoo.model.api.EntityOpResult)21 Entity (com.qcadoo.model.api.Entity)19 Test (org.junit.Test)15 ProxyEntity (com.qcadoo.model.internal.ProxyEntity)4 FieldDefinition (com.qcadoo.model.api.FieldDefinition)2 ErrorMessage (com.qcadoo.model.api.validators.ErrorMessage)2 InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)2 InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)2 ArrayList (java.util.ArrayList)2 EntityList (com.qcadoo.model.api.EntityList)1 EntityMessagesHolder (com.qcadoo.model.api.EntityMessagesHolder)1 Monitorable (com.qcadoo.model.api.aop.Monitorable)1 CollectionFieldType (com.qcadoo.model.api.types.CollectionFieldType)1 HasManyType (com.qcadoo.model.api.types.HasManyType)1 TreeType (com.qcadoo.model.api.types.TreeType)1 EntitySignature (com.qcadoo.model.internal.utils.EntitySignature)1 GridComponent (com.qcadoo.view.api.components.GridComponent)1 AbstractStateTest (com.qcadoo.view.internal.states.AbstractStateTest)1 Collection (java.util.Collection)1 List (java.util.List)1