Search in sources :

Example 1 with ProductResponse

use of com.qcadoo.mes.basic.controllers.dataProvider.responses.ProductResponse 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)

Aggregations

ProductResponse (com.qcadoo.mes.basic.controllers.dataProvider.responses.ProductResponse)1 Entity (com.qcadoo.model.api.Entity)1 ErrorMessage (com.qcadoo.model.api.validators.ErrorMessage)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1