use of com.salesmanager.shop.model.catalog.product.ReadableProduct in project shopizer by shopizer-ecommerce.
the class ShopProductRESTController method updateProductQuantity.
/**
* Update the quantity of an item
* ?lang=en|fr otherwise default store language
*/
@RequestMapping(value = "/private/{store}/product/quantity/{sku}/{qty}", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public ReadableProduct updateProductQuantity(@PathVariable final String store, @PathVariable final String sku, @PathVariable final int qty, HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
if (merchantStore != null) {
if (!merchantStore.getCode().equals(store)) {
merchantStore = null;
}
}
String lang = request.getParameter("lang");
Language language = null;
if (merchantStore == null) {
merchantStore = merchantStoreService.getByCode(store);
}
if (merchantStore == null) {
LOGGER.error("Merchant store is null for code " + store);
response.sendError(503, "Merchant store is null for code " + store);
return null;
}
if (StringUtils.isBlank(lang)) {
language = merchantStore.getDefaultLanguage();
} else {
language = languageService.getByCode(lang);
}
if (language == null) {
language = merchantStore.getDefaultLanguage();
}
ReadableProduct product = productFacade.getProduct(merchantStore, sku, language);
if (product == null) {
LOGGER.error("Product is null for sku " + sku);
response.sendError(503, "Product is null for sku " + sku);
return null;
}
product = productFacade.updateProductQuantity(product, qty, language);
return product;
} catch (Exception e) {
LOGGER.error("Error while saving product", e);
try {
response.sendError(503, "Error while updating product " + e.getMessage());
} catch (Exception ignore) {
}
return null;
}
}
use of com.salesmanager.shop.model.catalog.product.ReadableProduct in project shopizer by shopizer-ecommerce.
the class ShopProductRESTController method getProduct.
@RequestMapping(value = "/public/{store}/product/{id}", method = RequestMethod.GET)
@ResponseBody
public ReadableProduct getProduct(@PathVariable String store, @PathVariable final Long id, @RequestParam String lang, HttpServletRequest request, HttpServletResponse response) throws Exception {
/**
* bcz of the filter *
*/
MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
if (merchantStore != null) {
if (!merchantStore.getCode().equals(store)) {
merchantStore = null;
}
}
if (store != null) {
merchantStore = merchantStoreService.getByCode(store);
}
if (merchantStore == null) {
LOGGER.error("Merchant store is null for code " + store);
response.sendError(503, "Merchant store is null for code " + store);
return null;
}
Language language = null;
if (!StringUtils.isBlank(lang)) {
language = languageService.getByCode(lang);
}
if (language == null) {
language = merchantStore.getDefaultLanguage();
}
ReadableProduct product = productFacade.getProduct(merchantStore, id, language);
if (product == null) {
response.sendError(404, "Product not fount for id " + id);
return null;
}
return product;
}
use of com.salesmanager.shop.model.catalog.product.ReadableProduct in project shopizer by shopizer-ecommerce.
the class ProductFacadeImpl method updateProductPrice.
@Override
public ReadableProduct updateProductPrice(ReadableProduct product, ProductPriceEntity price, Language language) throws Exception {
Product persistable = productService.getById(product.getId());
if (persistable == null) {
throw new Exception("product is null for id " + product.getId());
}
java.util.Set<ProductAvailability> availabilities = persistable.getAvailabilities();
for (ProductAvailability availability : availabilities) {
ProductPrice productPrice = availability.defaultPrice();
productPrice.setProductPriceAmount(price.getOriginalPrice());
if (price.isDiscounted()) {
productPrice.setProductPriceSpecialAmount(price.getDiscountedPrice());
if (!StringUtils.isBlank(price.getDiscountStartDate())) {
Date startDate = DateUtil.getDate(price.getDiscountStartDate());
productPrice.setProductPriceSpecialStartDate(startDate);
}
if (!StringUtils.isBlank(price.getDiscountEndDate())) {
Date endDate = DateUtil.getDate(price.getDiscountEndDate());
productPrice.setProductPriceSpecialEndDate(endDate);
}
}
}
productService.update(persistable);
ReadableProduct readableProduct = new ReadableProduct();
ReadableProductPopulator populator = new ReadableProductPopulator();
populator.setPricingService(pricingService);
populator.setimageUtils(imageUtils);
populator.populate(persistable, readableProduct, persistable.getMerchantStore(), language);
return readableProduct;
}
use of com.salesmanager.shop.model.catalog.product.ReadableProduct in project shopizer by shopizer-ecommerce.
the class ProductFacadeImpl method addProductToCategory.
@Override
public ReadableProduct addProductToCategory(Category category, Product product, Language language) throws Exception {
Validate.notNull(category, "Category cannot be null");
Validate.notNull(product, "Product cannot be null");
// not alloweed if category already attached
List<Category> assigned = product.getCategories().stream().filter(cat -> cat.getId().longValue() == category.getId().longValue()).collect(Collectors.toList());
if (assigned.size() > 0) {
throw new OperationNotAllowedException("Category with id [" + category.getId() + "] already attached to product [" + product.getId() + "]");
}
product.getCategories().add(category);
productService.update(product);
ReadableProduct readableProduct = new ReadableProduct();
ReadableProductPopulator populator = new ReadableProductPopulator();
populator.setPricingService(pricingService);
populator.setimageUtils(imageUtils);
populator.populate(product, readableProduct, product.getMerchantStore(), language);
return readableProduct;
}
use of com.salesmanager.shop.model.catalog.product.ReadableProduct in project shopizer by shopizer-ecommerce.
the class ProductFacadeImpl method getProduct.
@Override
public ReadableProduct getProduct(MerchantStore store, Long id, Language language) throws Exception {
Product product = productService.findOne(id, store);
if (product == null) {
throw new ResourceNotFoundException("Product [" + id + "] not found");
}
if (product.getMerchantStore().getId() != store.getId()) {
throw new ResourceNotFoundException("Product [" + id + "] not found for store [" + store.getId() + "]");
}
ReadableProduct readableProduct = new ReadableProduct();
ReadableProductPopulator populator = new ReadableProductPopulator();
populator.setPricingService(pricingService);
populator.setimageUtils(imageUtils);
readableProduct = populator.populate(product, readableProduct, store, language);
return readableProduct;
}
Aggregations