use of com.qcadoo.model.api.validators.ErrorMessage in project mes by qcadoo.
the class DeliveryStatePFTDService method createWarehouseIssue.
private void createWarehouseIssue(final Long warehouse, final List<Entity> reservations, final CreationWarehouseIssueState creationWarehouseIssueState, final Entity delivery, final Entity placeOfIssue) {
Multimap<DeliveredProductReservationKeyObject, DeliveredProductReservationObject> flatReservations = flatReservations(reservations);
Map<DeliveredProductReservationKeyObject, Collection<DeliveredProductReservationObject>> simpleFlatReservations = flatReservations.asMap();
Entity warehouseIssue = createNewWarehouseIssue(placeOfIssue, delivery);
if (!warehouseIssue.isValid()) {
for (Map.Entry<String, ErrorMessage> entry : warehouseIssue.getErrors().entrySet()) {
creationWarehouseIssueState.getErrors().add(entry.getValue());
}
throw new IllegalStateException("Undone creation warehouse issue for delivery : " + delivery.getStringField(DeliveryFields.NUMBER));
}
Entity location = getLocationDD().get(warehouse);
List<Entity> createdProductsToIssue = Lists.newArrayList();
for (Map.Entry<DeliveredProductReservationKeyObject, Collection<DeliveredProductReservationObject>> entry : simpleFlatReservations.entrySet()) {
DeliveredProductReservationKeyObject key = entry.getKey();
Collection<DeliveredProductReservationObject> value = entry.getValue();
Entity product = key.getProduct();
Entity productToIssue = getProductsToIssueDD().create();
productToIssue.setField(ProductsToIssueFields.PRODUCT, product);
productToIssue.setField(ProductsToIssueFields.ADDITIONAL_CODE, key.getAdditionalCode());
productToIssue.setField(ProductsToIssueFields.CONVERSION, key.getConversion());
productToIssue.setField(ProductsToIssueFields.LOCATION, location);
if (productAlreadyCreated(createdProductsToIssue, product.getId(), Optional.ofNullable(key.getAdditionalCode()).orElse(productToIssue).getId(), location.getId(), key.getConversion())) {
warehouseIssue = createNewWarehouseIssue(placeOfIssue, delivery);
if (!warehouseIssue.isValid()) {
for (Map.Entry<String, ErrorMessage> e : warehouseIssue.getErrors().entrySet()) {
creationWarehouseIssueState.getErrors().add(e.getValue());
}
throw new IllegalStateException("Undone creation warehouse issue for delivery : " + delivery.getStringField(DeliveryFields.NUMBER));
}
createdProductsToIssue = Lists.newArrayList();
}
productToIssue.setField(ProductsToIssueFields.WAREHOUSE_ISSUE, warehouseIssue);
Optional<Entity> storageLocation = findStorageLocationForProduct(product, location);
if (storageLocation.isPresent()) {
productToIssue.setField(ProductsToIssueFields.STORAGE_LOCATION, storageLocation.get());
}
BigDecimal dQuantity = value.stream().filter(Objects::nonNull).map(val -> val.getReservationForDeliveredProduct().getDecimalField(DeliveredProductReservationFields.DELIVERED_QUANTITY)).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal aQuantity = value.stream().filter(Objects::nonNull).map(val -> val.getReservationForDeliveredProduct().getDecimalField(DeliveredProductReservationFields.ADDITIONAL_QUANTITY)).reduce(BigDecimal.ZERO, BigDecimal::add);
productToIssue.setField(ProductsToIssueFields.DEMAND_QUANTITY, dQuantity);
productToIssue.setField(ProductsToIssueFields.ADDITIONAL_DEMAND_QUANTITY, aQuantity);
productToIssue = productToIssue.getDataDefinition().save(productToIssue);
createdProductsToIssue.add(productToIssue);
if (!productToIssue.isValid()) {
for (Map.Entry<String, ErrorMessage> e : productToIssue.getErrors().entrySet()) {
creationWarehouseIssueState.getErrors().add(e.getValue());
}
throw new IllegalStateException("Undone creation warehouse issue for delivery : " + delivery.getStringField(DeliveryFields.NUMBER));
}
}
}
use of com.qcadoo.model.api.validators.ErrorMessage in project mes by qcadoo.
the class StateChangeContextTest method shouldAddMessagesOnlyOnce.
@Test
public final void shouldAddMessagesOnlyOnce() {
// given
given(owner.isValid()).willReturn(false);
given(ownerDD.save(owner)).willReturn(owner);
final Map<String, ErrorMessage> fieldErrorsMap = Maps.newHashMap();
final ErrorMessage fieldErrorMessage = buildErrorMessage(FIELD_1_MESSAGE_1);
fieldErrorsMap.put(FIELD_1_NAME, fieldErrorMessage);
given(owner.getErrors()).willReturn(fieldErrorsMap);
final List<ErrorMessage> globalErrors = Lists.newArrayList();
final ErrorMessage globalErrorMessage = buildErrorMessage(GLOBAL_MESSAGE_1);
globalErrors.add(globalErrorMessage);
given(owner.getGlobalErrors()).willReturn(globalErrors);
// when
stateChangeContext.setOwner(owner);
stateChangeContext.setOwner(owner);
// then
verify(ownerDD, never()).save(owner);
verify(stateChangeEntity, never()).setField(describer.getOwnerFieldName(), owner);
verify(stateChangeEntity, never()).setField(describer.getOwnerFieldName(), savedOwner);
verify(messageService).addValidationError(stateChangeContext, FIELD_1_NAME, FIELD_1_MESSAGE_1, EMPTY_STRING_ARRAY);
verify(messageService).addValidationError(stateChangeContext, null, GLOBAL_MESSAGE_1, EMPTY_STRING_ARRAY);
verify(stateChangeEntity).setField(describer.getStatusFieldName(), StateChangeStatus.FAILURE.getStringValue());
}
use of com.qcadoo.model.api.validators.ErrorMessage in project mes by qcadoo.
the class DeliveredProductAddMultiListeners method trySaveDeliveredProducts.
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void trySaveDeliveredProducts(final Entity deliveredProductMulti, final List<Entity> deliveredProductMultiPositions) {
Entity delivery = deliveredProductMulti.getBelongsToField(DeliveredProductMultiFields.DELIVERY);
for (Entity position : deliveredProductMultiPositions) {
Entity deliveredProduct = createDeliveredProduct(position, getDeliveredProductDD());
setStorageLocationFields(deliveredProduct, deliveredProductMulti);
deliveredProduct.setField(DeliveredProductFields.DELIVERY, delivery);
deliveredProduct = deliveredProduct.getDataDefinition().save(deliveredProduct);
if (!deliveredProduct.isValid()) {
for (Map.Entry<String, ErrorMessage> entry : deliveredProduct.getErrors().entrySet()) {
if (Objects.nonNull(position.getDataDefinition().getField(entry.getKey()))) {
position.addError(position.getDataDefinition().getField(entry.getKey()), entry.getValue().getMessage());
} else {
position.addGlobalError(entry.getValue().getMessage(), false);
}
}
deliveredProductMulti.addGlobalError(L_DELIVERIES_DELIVERED_PRODUCT_MULTI_ERROR_INVALID);
throw new IllegalStateException("Undone saved delivered product");
}
}
}
use of com.qcadoo.model.api.validators.ErrorMessage in project mes by qcadoo.
the class DashboardKanbanController method updateOrderState.
@ResponseBody
@RequestMapping(value = "/updateOrderState/{orderId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
public OrderResponse updateOrderState(@PathVariable final Long orderId) {
Entity order = getOrderDD().get(orderId);
String targetState = OrderState.IN_PROGRESS.getStringValue();
if (OrderState.IN_PROGRESS.getStringValue().equals(order.getStringField(OrderFields.STATE))) {
targetState = OrderState.COMPLETED.getStringValue();
}
StateChangeContext orderStateChangeContext = stateChangeContextBuilder.build(orderStateChangeAspect.getChangeEntityDescriber(), order, targetState);
orderStateChangeAspect.changeState(orderStateChangeContext);
OrderResponse orderResponse = new OrderResponse(dashboardKanbanDataProvider.getOrder(orderId));
List<ErrorMessage> errors = Lists.newArrayList();
if (!orderStateChangeContext.getAllMessages().isEmpty()) {
for (Entity entity : orderStateChangeContext.getAllMessages()) {
errors.add(new ErrorMessage(MessagesUtil.getKey(entity), MessagesUtil.getArgs(entity)));
}
}
if (!errors.isEmpty()) {
String errorMessages = errors.stream().map(errorMessage -> translationService.translate(errorMessage.getMessage(), LocaleContextHolder.getLocale(), errorMessage.getVars())).collect(Collectors.joining(", "));
orderResponse.setMessage(translationService.translate("orders.order.orderStates.error", LocaleContextHolder.getLocale(), errorMessages));
}
return orderResponse;
}
use of com.qcadoo.model.api.validators.ErrorMessage in project mes by qcadoo.
the class ProductsToIssueHelperDetailsListeners method copyPositionMessages.
private void copyPositionMessages(Entity invalidPosition, final ViewDefinitionState componentMessagesHolder) {
if (componentMessagesHolder == null) {
return;
}
Locale locale = LocaleContextHolder.getLocale();
String productNumber = Optional.of(invalidPosition).map(ip -> ip.getBelongsToField(PositionFields.PRODUCT)).map(p -> p.getStringField(ProductFields.NUMBER)).orElse("???");
for (ErrorMessage errorMessage : invalidPosition.getGlobalErrors()) {
String translatedMessage = translationService.translate(errorMessage.getMessage(), locale, errorMessage.getVars());
translatedMessage = translationService.translate("productFlowThruDivision.issue.documentBuild.position.error", locale, translatedMessage, productNumber);
componentMessagesHolder.addTranslatedMessage(translatedMessage, ComponentState.MessageType.FAILURE, errorMessage.getAutoClose(), errorMessage.isExtraLarge());
}
for (ErrorMessage errorMessage : invalidPosition.getErrors().values()) {
String translatedMessage = translationService.translate(errorMessage.getMessage(), locale, errorMessage.getVars());
translatedMessage = translationService.translate("productFlowThruDivision.issue.documentBuild.position.error", locale, translatedMessage, productNumber);
componentMessagesHolder.addTranslatedMessage(translatedMessage, ComponentState.MessageType.FAILURE, errorMessage.getAutoClose(), errorMessage.isExtraLarge());
}
}
Aggregations