Search in sources :

Example 46 with ErrorMessage

use of com.qcadoo.model.api.validators.ErrorMessage in project mes by qcadoo.

the class WarehouseIssueDetailsListeners method fillProductsToIssue.

public void fillProductsToIssue(final ViewDefinitionState view, final ComponentState state, final String[] args) {
    FormComponent issueForm = (FormComponent) view.getComponentByReference(QcadooViewConstants.L_FORM);
    FieldComponent collectionProductsField = (FieldComponent) view.getComponentByReference(WarehouseIssueFields.COLLECTION_PRODUCTS);
    LookupComponent operation = (LookupComponent) view.getComponentByReference(WarehouseIssueFields.TECHNOLOGY_OPERATION_COMPONENT);
    LookupComponent division = (LookupComponent) view.getComponentByReference(WarehouseIssueFields.DIVISION);
    Long warehouseIssueId = issueForm.getEntityId();
    Entity toc = operation.getEntity();
    Entity divisionEntity = division.getEntity();
    List<Entity> createdProducts = warehouseIssueService.fillProductsToIssue(warehouseIssueId, CollectionProducts.fromStringValue(collectionProductsField.getFieldValue().toString()), toc, divisionEntity);
    if (createdProducts != null) {
        List<Entity> invalidProducts = createdProducts.stream().filter(productToIssue -> !productToIssue.isValid()).collect(Collectors.toList());
        if (invalidProducts.isEmpty()) {
            view.addMessage("productFlowThruDivision.issue.downloadedProducts", ComponentState.MessageType.SUCCESS);
        } else {
            Multimap<String, String> errors = ArrayListMultimap.create();
            for (Entity productToIssue : invalidProducts) {
                String number = productToIssue.getBelongsToField(ProductsToIssueFields.PRODUCT).getStringField(ProductFields.NUMBER);
                Map<String, ErrorMessage> errorMessages = productToIssue.getErrors();
                errorMessages.entrySet().stream().forEach(entry -> errors.put(entry.getValue().getMessage(), number));
                productToIssue.getGlobalErrors().stream().forEach(error -> errors.put(error.getMessage(), number));
            }
            view.addMessage("productFlowThruDivision.issue.downloadedProductsError", ComponentState.MessageType.INFO, false);
            for (String message : errors.keySet()) {
                String translatedMessage = translationService.translate(message, LocaleContextHolder.getLocale());
                String products = errors.get(message).stream().collect(Collectors.joining(", "));
                if ((translatedMessage + products).length() < 255) {
                    view.addMessage("productFlowThruDivision.issue.downloadedProductsErrorMessages", ComponentState.MessageType.FAILURE, false, translatedMessage, products);
                }
            }
        }
    } else {
        view.addMessage("productFlowThruDivision.issue.noProductsToDownload", ComponentState.MessageType.INFO);
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) java.util(java.util) DataDefinitionService(com.qcadoo.model.api.DataDefinitionService) 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) Autowired(org.springframework.beans.factory.annotation.Autowired) OrderFields(com.qcadoo.mes.orders.constants.OrderFields) ProductionLineFields(com.qcadoo.mes.productionLines.constants.ProductionLineFields) Multimap(com.google.common.collect.Multimap) OrdersConstants(com.qcadoo.mes.orders.constants.OrdersConstants) DateUtils(com.qcadoo.localization.api.utils.DateUtils) BigDecimal(java.math.BigDecimal) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) ViewDefinitionState(com.qcadoo.view.api.ViewDefinitionState) WarehouseIssueService(com.qcadoo.mes.productFlowThruDivision.service.WarehouseIssueService) Service(org.springframework.stereotype.Service) FormComponent(com.qcadoo.view.api.components.FormComponent) WarehouseIssueFields(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.constans.WarehouseIssueFields) WarehouseIssueHooks(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.hooks.WarehouseIssueHooks) WarehouseIssueParameterService(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.WarehouseIssueParameterService) TranslationService(com.qcadoo.localization.api.TranslationService) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) DataDefinition(com.qcadoo.model.api.DataDefinition) GridComponent(com.qcadoo.view.api.components.GridComponent) Entity(com.qcadoo.model.api.Entity) CollectionProducts(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.constans.CollectionProducts) FieldComponent(com.qcadoo.view.api.components.FieldComponent) WarehouseIssueDetailHooks(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.hooks.WarehouseIssueDetailHooks) NumberService(com.qcadoo.model.api.NumberService) LookupComponent(com.qcadoo.view.api.components.LookupComponent) ProductFields(com.qcadoo.mes.basic.constants.ProductFields) ProductFlowThruDivisionConstants(com.qcadoo.mes.productFlowThruDivision.constants.ProductFlowThruDivisionConstants) IssueDetailsHooks(com.qcadoo.mes.productFlowThruDivision.warehouseIssue.hooks.IssueDetailsHooks) FormComponent(com.qcadoo.view.api.components.FormComponent) Entity(com.qcadoo.model.api.Entity) LookupComponent(com.qcadoo.view.api.components.LookupComponent) FieldComponent(com.qcadoo.view.api.components.FieldComponent) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage)

Example 47 with ErrorMessage

use of com.qcadoo.model.api.validators.ErrorMessage in project mes by qcadoo.

the class TechnologyValidationService method createMessageForValidationErrors.

private String createMessageForValidationErrors(final String message, final Entity entity) {
    List<ErrorMessage> errors = Lists.newArrayList();
    if (!entity.getErrors().isEmpty()) {
        errors.addAll(entity.getErrors().values());
    }
    if (!entity.getGlobalErrors().isEmpty()) {
        errors.addAll(entity.getGlobalErrors());
    }
    StringBuilder errorMessages = new StringBuilder();
    errorMessages.append(message).append("\n");
    for (ErrorMessage error : errors) {
        if (!error.getMessage().equals("qcadooView.validate.global.error.custom")) {
            String translatedErrorMessage = translationService.translate(error.getMessage(), Locale.getDefault(), error.getVars());
            errorMessages.append("- ").append(translatedErrorMessage);
            errorMessages.append(",\n ");
        }
    }
    String msg = errorMessages.toString();
    int length = msg.length();
    String lastSign = msg.substring(length - 3);
    if (",\n ".equals(lastSign)) {
        msg = msg.substring(0, length - 3);
    }
    return msg;
}
Also used : ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage)

Example 48 with ErrorMessage

use of com.qcadoo.model.api.validators.ErrorMessage in project qcadoo by qcadoo.

the class DataAccessServiceImpl method logEntityErrors.

private void logEntityErrors(final Entity entity, final String msg) {
    if (!LOG.isInfoEnabled()) {
        return;
    }
    StringBuilder sb = new StringBuilder();
    if (StringUtils.isNotEmpty(msg)) {
        sb.append(msg);
        sb.append('\n');
    }
    for (ErrorMessage error : entity.getGlobalErrors()) {
        sb.append(" --- " + error.getMessage());
        sb.append('\n');
    }
    for (Map.Entry<String, ErrorMessage> error : entity.getErrors().entrySet()) {
        sb.append(" --- " + error.getKey() + ": " + error.getValue().getMessage());
        sb.append('\n');
    }
    LOG.info(sb.toString());
}
Also used : ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) Map(java.util.Map)

Example 49 with ErrorMessage

use of com.qcadoo.model.api.validators.ErrorMessage in project qcadoo by qcadoo.

the class CustomValidatorTest method shouldNotAddDefaultMessageIfValidationSuccessAndThereIsAlreadyMessageAssignedToField.

@Test
public final void shouldNotAddDefaultMessageIfValidationSuccessAndThereIsAlreadyMessageAssignedToField() {
    // given
    Map<String, ErrorMessage> errors = Maps.newHashMap();
    errors.put("another" + FIELD_NAME, new ErrorMessage(CUSTOM_MSG));
    stubFieldHookDef(true, errors);
    // when
    boolean isValid = customValidator.call(entity, oldValue, newValue);
    // then
    assertTrue(isValid);
    verify(fieldHookDef, times(1)).call(entity, oldValue, newValue);
    verify(entity, never()).addError(Mockito.eq(fieldDefinition), Mockito.eq(CUSTOM_MSG), Mockito.<String>anyVararg());
    verify(entity, never()).addError(Mockito.eq(fieldDefinition), Mockito.eq(DEFAULT_MSG), Mockito.<String>anyVararg());
}
Also used : ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) Test(org.junit.Test)

Example 50 with ErrorMessage

use of com.qcadoo.model.api.validators.ErrorMessage in project qcadoo by qcadoo.

the class CustomValidatorTest method shouldNotAddDefaultMessageIfValidationSuccessAndThereIsAlreadyMessageAssignedToOtherField.

@Test
public final void shouldNotAddDefaultMessageIfValidationSuccessAndThereIsAlreadyMessageAssignedToOtherField() {
    // given
    Map<String, ErrorMessage> errors = Maps.newHashMap();
    errors.put(FIELD_NAME, new ErrorMessage(CUSTOM_MSG));
    stubFieldHookDef(true, errors);
    // when
    boolean isValid = customValidator.call(entity, oldValue, newValue);
    // then
    assertTrue(isValid);
    verify(fieldHookDef, times(1)).call(entity, oldValue, newValue);
    ArgumentCaptor<String> argCaptor = ArgumentCaptor.forClass(String.class);
    verify(entity, times(1)).addError(Mockito.eq(fieldDefinition), argCaptor.capture(), Mockito.<String>anyVararg());
    assertEquals(CUSTOM_MSG, argCaptor.getValue());
}
Also used : ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) Test(org.junit.Test)

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