Search in sources :

Example 41 with ErrorMessage

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));
        }
    }
}
Also used : DeliveredProductReservationKeyObject(com.qcadoo.mes.productFlowThruDivision.deliveries.helpers.DeliveredProductReservationKeyObject) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) DataDefinitionService(com.qcadoo.model.api.DataDefinitionService) ProductsToIssueFields(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.constans.ProductsToIssueFields) MaterialFlowConstants(com.qcadoo.mes.materialFlow.constants.MaterialFlowConstants) Autowired(org.springframework.beans.factory.annotation.Autowired) CreationWarehouseIssueState(com.qcadoo.mes.productFlowThruDivision.deliveries.helpers.CreationWarehouseIssueState) Multimap(com.google.common.collect.Multimap) DeliveredProductReservationKeyObject(com.qcadoo.mes.productFlowThruDivision.deliveries.helpers.DeliveredProductReservationKeyObject) StateMessageType(com.qcadoo.mes.states.messages.constants.StateMessageType) BigDecimal(java.math.BigDecimal) MaterialFlowResourcesConstants(com.qcadoo.mes.materialFlowResources.constants.MaterialFlowResourcesConstants) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) Map(java.util.Map) WarehouseIssueFields(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.constans.WarehouseIssueFields) WarehouseIssueGenerator(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.WarehouseIssueGenerator) StateChangeContext(com.qcadoo.mes.states.StateChangeContext) SearchRestrictions(com.qcadoo.model.api.search.SearchRestrictions) WarehouseIssueParameterService(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.WarehouseIssueParameterService) DeliveredProductReservationFields(com.qcadoo.mes.deliveries.constants.DeliveredProductReservationFields) DeliveriesConstants(com.qcadoo.mes.deliveries.constants.DeliveriesConstants) Collection(java.util.Collection) DeliveryFields(com.qcadoo.mes.deliveries.constants.DeliveryFields) TranslationService(com.qcadoo.localization.api.TranslationService) DataDefinition(com.qcadoo.model.api.DataDefinition) Objects(java.util.Objects) DeliveredProductFields(com.qcadoo.mes.deliveries.constants.DeliveredProductFields) DeliveredProductReservationObject(com.qcadoo.mes.productFlowThruDivision.deliveries.helpers.DeliveredProductReservationObject) List(java.util.List) Entity(com.qcadoo.model.api.Entity) WarehouseIssueState(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.states.constants.WarehouseIssueState) CollectionProducts(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.constans.CollectionProducts) Optional(java.util.Optional) StorageLocationFields(com.qcadoo.mes.materialFlowResources.constants.StorageLocationFields) SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) ProductFlowThruDivisionConstants(com.qcadoo.mes.productFlowThruDivision.constants.ProductFlowThruDivisionConstants) Entity(com.qcadoo.model.api.Entity) BigDecimal(java.math.BigDecimal) Objects(java.util.Objects) Collection(java.util.Collection) DeliveredProductReservationObject(com.qcadoo.mes.productFlowThruDivision.deliveries.helpers.DeliveredProductReservationObject) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) Map(java.util.Map)

Example 42 with ErrorMessage

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());
}
Also used : ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) Test(org.junit.Test)

Example 43 with ErrorMessage

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");
        }
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) Transactional(org.springframework.transaction.annotation.Transactional)

Example 44 with ErrorMessage

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;
}
Also used : LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) PathVariable(org.springframework.web.bind.annotation.PathVariable) DataDefinitionService(com.qcadoo.model.api.DataDefinitionService) DashboardKanbanDataProvider(com.qcadoo.mes.orders.controllers.dataProvider.DashboardKanbanDataProvider) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) OrderFields(com.qcadoo.mes.orders.constants.OrderFields) Controller(org.springframework.stereotype.Controller) OrdersConstants(com.qcadoo.mes.orders.constants.OrdersConstants) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) Lists(com.google.common.collect.Lists) OrderResponse(com.qcadoo.mes.orders.controllers.responses.OrderResponse) OrderHolder(com.qcadoo.mes.orders.controllers.dto.OrderHolder) MessagesUtil(com.qcadoo.mes.states.messages.util.MessagesUtil) StateChangeContext(com.qcadoo.mes.states.StateChangeContext) OrderState(com.qcadoo.mes.orders.states.constants.OrderState) MediaType(org.springframework.http.MediaType) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) TranslationService(com.qcadoo.localization.api.TranslationService) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Collectors(java.util.stream.Collectors) DataDefinition(com.qcadoo.model.api.DataDefinition) List(java.util.List) OrderStateChangeAspect(com.qcadoo.mes.orders.states.aop.OrderStateChangeAspect) StateChangeContextBuilder(com.qcadoo.mes.states.service.StateChangeContextBuilder) Entity(com.qcadoo.model.api.Entity) OperationalTaskHolder(com.qcadoo.mes.orders.controllers.dto.OperationalTaskHolder) Entity(com.qcadoo.model.api.Entity) OrderResponse(com.qcadoo.mes.orders.controllers.responses.OrderResponse) StateChangeContext(com.qcadoo.mes.states.StateChangeContext) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 45 with ErrorMessage

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());
    }
}
Also used : LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) java.util(java.util) RuntimeExceptionWithArguments(com.qcadoo.model.api.exception.RuntimeExceptionWithArguments) ProductsToIssueFields(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.constans.ProductsToIssueFields) IssueFields(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.constans.IssueFields) ComponentState(com.qcadoo.view.api.ComponentState) QcadooViewConstants(com.qcadoo.view.constants.QcadooViewConstants) OrderFields(com.qcadoo.mes.orders.constants.OrderFields) Autowired(org.springframework.beans.factory.annotation.Autowired) BooleanUtils(org.apache.commons.lang3.BooleanUtils) com.qcadoo.model.api(com.qcadoo.model.api) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) BigDecimal(java.math.BigDecimal) ViewDefinitionState(com.qcadoo.view.api.ViewDefinitionState) Lists(com.google.common.collect.Lists) WarehouseIssueService(com.qcadoo.mes.productFlowThruDivision.service.WarehouseIssueService) Service(org.springframework.stereotype.Service) WarehouseIssueFields(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.constans.WarehouseIssueFields) GlobalMessage(com.qcadoo.model.api.validators.GlobalMessage) EntityRuntimeException(com.qcadoo.model.api.exception.EntityRuntimeException) LocationFields(com.qcadoo.mes.materialFlow.constants.LocationFields) MaterialFlowResourcesService(com.qcadoo.mes.materialFlowResources.MaterialFlowResourcesService) RoundingMode(java.math.RoundingMode) ProductsToIssueDetailsHooks(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.hooks.ProductsToIssueDetailsHooks) WarehouseIssueParameterService(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.WarehouseIssueParameterService) IssueValidators(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.validators.IssueValidators) PositionFields(com.qcadoo.mes.materialFlowResources.constants.PositionFields) DocumentBuildException(com.qcadoo.mes.materialFlowResources.exceptions.DocumentBuildException) TranslationService(com.qcadoo.localization.api.TranslationService) com.qcadoo.view.api.components(com.qcadoo.view.api.components) Either(com.qcadoo.commons.functional.Either) ProductFields(com.qcadoo.mes.basic.constants.ProductFields) CalculationQuantityService(com.qcadoo.mes.basic.CalculationQuantityService) ProductFlowThruDivisionConstants(com.qcadoo.mes.productFlowThruDivision.constants.ProductFlowThruDivisionConstants) UpdateIssuesLocationsQuantityStatusHolder(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.UpdateIssuesLocationsQuantityStatusHolder) Transactional(org.springframework.transaction.annotation.Transactional) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage)

Aggregations

ErrorMessage (com.qcadoo.model.api.validators.ErrorMessage)50 Entity (com.qcadoo.model.api.Entity)30 BigDecimal (java.math.BigDecimal)11 Test (org.junit.Test)11 Transactional (org.springframework.transaction.annotation.Transactional)8 Map (java.util.Map)7 DataDefinition (com.qcadoo.model.api.DataDefinition)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 FormComponent (com.qcadoo.view.api.components.FormComponent)5 TranslationService (com.qcadoo.localization.api.TranslationService)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 LocaleContextHolder (org.springframework.context.i18n.LocaleContextHolder)4 Lists (com.google.common.collect.Lists)3 OrderFields (com.qcadoo.mes.orders.constants.OrderFields)3 ProductFlowThruDivisionConstants (com.qcadoo.mes.productFlowThruDivision.constants.ProductFlowThruDivisionConstants)3 WarehouseIssueParameterService (com.qcadoo.mes.productFlowThruDivision.warehouseIssue.WarehouseIssueParameterService)3 ProductsToIssueFields (com.qcadoo.mes.productFlowThruDivision.warehouseIssue.constans.ProductsToIssueFields)3 WarehouseIssueFields (com.qcadoo.mes.productFlowThruDivision.warehouseIssue.constans.WarehouseIssueFields)3 StateChangeContext (com.qcadoo.mes.states.StateChangeContext)3