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