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;
}
Aggregations