Search in sources :

Example 31 with ErrorMessage

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

the class OrderReportService method printForOrder.

public Entity printForOrder(final ComponentState state, final String plugin, final String entityName, final Map<String, Object> entityFieldsMap, final OrderValidator orderValidator) {
    if (!(state instanceof GridComponent)) {
        throw new IllegalStateException("method avalible only for grid");
    }
    GridComponent gridState = (GridComponent) state;
    Set<Entity> ordersEntities = new HashSet<Entity>();
    if (gridState.getSelectedEntitiesIds().isEmpty()) {
        state.addMessage("qcadooView.message.noRecordSelected", MessageType.FAILURE);
        return null;
    }
    List<ErrorMessage> errors = Lists.newLinkedList();
    for (Long orderId : gridState.getSelectedEntitiesIds()) {
        Entity order = dataDefinitionService.get(OrdersConstants.PLUGIN_IDENTIFIER, OrdersConstants.MODEL_ORDER).get(orderId);
        if (order == null) {
            errors.add(new ErrorMessage("qcadooView.message.entityNotFound"));
            continue;
        }
        ErrorMessage errorMessage = orderValidator.validateOrder(order);
        if (errorMessage == null) {
            ordersEntities.add(order);
        } else {
            errors.add(errorMessage);
        }
    }
    if (errors.isEmpty()) {
        return createNewOrderPrint(ordersEntities, plugin, entityName, entityFieldsMap, state.getLocale());
    } else {
        StringBuilder errorMessages = new StringBuilder();
        for (ErrorMessage error : errors) {
            errorMessages.append(" - ");
            String translatedError = translationService.translate(error.getMessage(), state.getLocale(), error.getVars());
            errorMessages.append(translatedError);
            errorMessages.append("\n");
        }
        state.addTranslatedMessage(errorMessages.toString(), MessageType.FAILURE, false);
    }
    return null;
}
Also used : Entity(com.qcadoo.model.api.Entity) GridComponent(com.qcadoo.view.api.components.GridComponent) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) HashSet(java.util.HashSet)

Example 32 with ErrorMessage

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

the class BasicException method createMessageForValidationErrors.

private static String createMessageForValidationErrors(final String message, final Entity entity) {
    StringBuilder sb = new StringBuilder();
    sb.append(message).append("\n");
    for (ErrorMessage error : entity.getGlobalErrors()) {
        sb.append("- ").append(error.getMessage()).append("\n");
    }
    for (Map.Entry<String, ErrorMessage> error : entity.getErrors().entrySet()) {
        sb.append("- ").append(error.getKey()).append(" - ").append(error.getValue().getMessage()).append("\n");
    }
    return sb.toString();
}
Also used : ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) Map(java.util.Map)

Example 33 with ErrorMessage

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

the class BasicApiController method saveProduct.

@ResponseBody
@RequestMapping(value = "/product", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ProductResponse saveProduct(@RequestBody ProductRequest product) {
    Entity productEntity = dataDefinitionService.get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_PRODUCT).create();
    productEntity.setField(ProductFields.NUMBER, product.getNumber());
    productEntity.setField(ProductFields.NAME, product.getName());
    productEntity.setField(ProductFields.UNIT, product.getUnit());
    productEntity.setField(ProductFields.ENTITY_TYPE, ProductFamilyElementType.PARTICULAR_PRODUCT.getStringValue());
    productEntity.setField(ProductFields.GLOBAL_TYPE_OF_MATERIAL, product.getGlobalTypeOfMaterial());
    productEntity = productEntity.getDataDefinition().save(productEntity);
    if (productEntity.isValid()) {
        ProductResponse productResponse = new ProductResponse(ProductResponse.StatusCode.OK);
        productResponse.setId(productEntity.getId());
        productResponse.setNumber(product.getNumber());
        productResponse.setName(product.getName());
        productResponse.setUnit(product.getUnit());
        return productResponse;
    } else {
        // 
        ErrorMessage numberError = productEntity.getError(ProductFields.NUMBER);
        if (Objects.nonNull(numberError) && numberError.getMessage().equals("qcadooView.validate.field.error.duplicated")) {
            ProductResponse response = new ProductResponse(ProductResponse.StatusCode.ERROR);
            response.setMessage(translationService.translate("basic.dashboard.orderDefinitionWizard.error.validationError.productDuplicated", LocaleContextHolder.getLocale()));
            return response;
        }
    }
    ProductResponse response = new ProductResponse(ProductResponse.StatusCode.ERROR);
    response.setMessage(translationService.translate("basic.dashboard.orderDefinitionWizard.error.validationError.productErrors", LocaleContextHolder.getLocale()));
    return response;
}
Also used : Entity(com.qcadoo.model.api.Entity) ProductResponse(com.qcadoo.mes.basic.controllers.dataProvider.responses.ProductResponse) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 34 with ErrorMessage

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

the class SubassemblyToWorkstationHelperHooks method onSave.

public void onSave(DataDefinition subassemblyToWorkstationHelperDD, Entity subassemblyToWorkstationHelper) {
    DataDefinition subassemblyDD = dataDefinitionService.get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_SUBASSEMBLY);
    Entity subassembly = subassemblyToWorkstationHelper.getBelongsToField(SubassemblyToWorkstationHelperFields.SUBASSEMBLY);
    if (subassembly != null) {
        subassembly.setField(SubassemblyFields.WORKSTATION, subassemblyToWorkstationHelper.getField(SubassemblyToWorkstationHelperFields.WORKSTATION));
        subassembly.setField(SubassemblyFields.TYPE, subassemblyToWorkstationHelper.getField(SubassemblyToWorkstationHelperFields.TYPE));
        subassembly = subassemblyDD.save(subassembly);
        if (!subassembly.isValid()) {
            for (Map.Entry<String, ErrorMessage> entry : subassembly.getErrors().entrySet()) {
                subassemblyToWorkstationHelper.addGlobalError(entry.getValue().getMessage(), entry.getValue().getAutoClose(), entry.getValue().getVars());
                subassemblyToWorkstationHelper.addError(subassemblyToWorkstationHelperDD.getField(SubassemblyToWorkstationHelperFields.TYPE), entry.getValue().getMessage());
            }
            for (ErrorMessage msg : subassembly.getGlobalErrors()) {
                subassemblyToWorkstationHelper.addGlobalError(msg.getMessage(), msg.getAutoClose(), msg.getVars());
                subassemblyToWorkstationHelper.addError(subassemblyToWorkstationHelperDD.getField(SubassemblyToWorkstationHelperFields.TYPE), msg.getMessage());
            }
            subassemblyToWorkstationHelper.setNotValid();
        }
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) DataDefinition(com.qcadoo.model.api.DataDefinition) Map(java.util.Map)

Example 35 with ErrorMessage

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

the class OrderStatesListenerServicePFTD method tryAcceptInboundDocumentsFor.

@Transactional()
private Either<String, Void> tryAcceptInboundDocumentsFor(final Entity order) {
    updateDocumentQuantities(order);
    DataDefinition documentDD = dataDefinitionService.get(MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER, MaterialFlowResourcesConstants.MODEL_DOCUMENT);
    SearchResult searchResult = documentDD.find().add(SearchRestrictions.belongsTo(DocumentFieldsPFTD.ORDER, order)).add(SearchRestrictions.eq(DocumentFields.TYPE, DocumentType.INTERNAL_INBOUND.getStringValue())).list();
    String priceBasedOn = parameterService.getParameter().getStringField(ParameterFieldsPC.PRICE_BASED_ON);
    boolean isNominalProductCost = priceBasedOn != null && priceBasedOn.equals(PriceBasedOn.NOMINAL_PRODUCT_COST.getStringValue());
    Optional<BigDecimal> price = Optional.empty();
    if (!isNominalProductCost) {
        price = Optional.ofNullable(order.getDecimalField("productPricePerUnit"));
    }
    for (Entity document : searchResult.getEntities()) {
        if (!isNominalProductCost) {
            fillInDocumentPositionsPrice(document, order.getBelongsToField(OrderFields.PRODUCT), price);
        }
        if (DocumentState.of(document) == DocumentState.ACCEPTED) {
            LOG.warn("Document {} already accepted.", document.getId());
            continue;
        }
        Entity acceptedDocument = acceptInboundDocument(document);
        if (!acceptedDocument.isValid()) {
            documentStateChangeService.buildFailureStateChange(acceptedDocument.getId());
            for (ErrorMessage error : acceptedDocument.getGlobalErrors()) {
                order.addGlobalError(error.getMessage(), error.getVars());
            }
            order.addGlobalError(L_ACCEPT_INBOUND_DOCUMENT_ERROR);
            return Either.left(L_ACCEPT_INBOUND_DOCUMENT_ERROR);
        }
    }
    Either<String, Void> documentsForNotUsedMaterials = createDocumentsForNotUsedMaterials(order);
    if (documentsForNotUsedMaterials.isLeft()) {
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    }
    return documentsForNotUsedMaterials;
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchResult(com.qcadoo.model.api.search.SearchResult) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) DataDefinition(com.qcadoo.model.api.DataDefinition) BigDecimal(java.math.BigDecimal) Transactional(org.springframework.transaction.annotation.Transactional)

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