use of com.salesmanager.core.model.catalog.product.image.ProductImage in project shopizer by shopizer-ecommerce.
the class ProductImageServiceImpl method addProductImages.
@Override
public void addProductImages(Product product, List<ProductImage> productImages) throws ServiceException {
try {
for (ProductImage productImage : productImages) {
Assert.notNull(productImage.getImage());
InputStream inputStream = productImage.getImage();
ImageContentFile cmsContentImage = new ImageContentFile();
cmsContentImage.setFileName(productImage.getProductImage());
cmsContentImage.setFile(inputStream);
cmsContentImage.setFileContentType(FileContentType.PRODUCT);
addProductImage(product, productImage, cmsContentImage);
}
} catch (Exception e) {
throw new ServiceException(e);
}
}
use of com.salesmanager.core.model.catalog.product.image.ProductImage in project shopizer by shopizer-ecommerce.
the class ProductImageServiceImpl method getProductImage.
@Override
public Optional<ProductImage> getProductImage(Long imageId, Long productId, MerchantStore store) {
Optional<ProductImage> image = Optional.empty();
ProductImage img = productImageRepository.finById(imageId, productId, store.getCode());
if (img != null) {
image = Optional.of(img);
}
return image;
}
use of com.salesmanager.core.model.catalog.product.image.ProductImage in project shopizer by shopizer-ecommerce.
the class ShoppingCartDataPopulator method populate.
@Override
public ShoppingCartData populate(final ShoppingCart shoppingCart, final ShoppingCartData cart, final MerchantStore store, final Language language) {
Validate.notNull(shoppingCart, "Requires ShoppingCart");
Validate.notNull(language, "Requires Language not null");
int cartQuantity = 0;
cart.setCode(shoppingCart.getShoppingCartCode());
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = shoppingCart.getLineItems();
List<ShoppingCartItem> shoppingCartItemsList = Collections.emptyList();
try {
if (items != null) {
shoppingCartItemsList = new ArrayList<ShoppingCartItem>();
for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem item : items) {
ShoppingCartItem shoppingCartItem = new ShoppingCartItem();
shoppingCartItem.setCode(cart.getCode());
shoppingCartItem.setProductCode(item.getProduct().getSku());
shoppingCartItem.setProductVirtual(item.isProductVirtual());
shoppingCartItem.setProductId(item.getProductId());
shoppingCartItem.setId(item.getId());
String itemName = item.getProduct().getProductDescription().getName();
if (!CollectionUtils.isEmpty(item.getProduct().getDescriptions())) {
for (ProductDescription productDescription : item.getProduct().getDescriptions()) {
if (language != null && language.getId().intValue() == productDescription.getLanguage().getId().intValue()) {
itemName = productDescription.getName();
break;
}
}
}
shoppingCartItem.setName(itemName);
shoppingCartItem.setPrice(pricingService.getDisplayAmount(item.getItemPrice(), store));
shoppingCartItem.setQuantity(item.getQuantity());
cartQuantity = cartQuantity + item.getQuantity();
shoppingCartItem.setProductPrice(item.getItemPrice());
shoppingCartItem.setSubTotal(pricingService.getDisplayAmount(item.getSubTotal(), store));
ProductImage image = item.getProduct().getProductImage();
if (image != null && imageUtils != null) {
String imagePath = imageUtils.buildProductImageUtils(store, item.getProduct().getSku(), image.getProductImage());
shoppingCartItem.setImage(imagePath);
}
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> attributes = item.getAttributes();
if (attributes != null) {
List<ShoppingCartAttribute> cartAttributes = new ArrayList<ShoppingCartAttribute>();
for (com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attribute : attributes) {
ShoppingCartAttribute cartAttribute = new ShoppingCartAttribute();
cartAttribute.setId(attribute.getId());
cartAttribute.setAttributeId(attribute.getProductAttributeId());
cartAttribute.setOptionId(attribute.getProductAttribute().getProductOption().getId());
cartAttribute.setOptionValueId(attribute.getProductAttribute().getProductOptionValue().getId());
List<ProductOptionDescription> optionDescriptions = attribute.getProductAttribute().getProductOption().getDescriptionsSettoList();
List<ProductOptionValueDescription> optionValueDescriptions = attribute.getProductAttribute().getProductOptionValue().getDescriptionsSettoList();
if (!CollectionUtils.isEmpty(optionDescriptions) && !CollectionUtils.isEmpty(optionValueDescriptions)) {
String optionName = optionDescriptions.get(0).getName();
String optionValue = optionValueDescriptions.get(0).getName();
for (ProductOptionDescription optionDescription : optionDescriptions) {
if (optionDescription.getLanguage() != null && optionDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
optionName = optionDescription.getName();
break;
}
}
for (ProductOptionValueDescription optionValueDescription : optionValueDescriptions) {
if (optionValueDescription.getLanguage() != null && optionValueDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
optionValue = optionValueDescription.getName();
break;
}
}
cartAttribute.setOptionName(optionName);
cartAttribute.setOptionValue(optionValue);
cartAttributes.add(cartAttribute);
}
}
shoppingCartItem.setShoppingCartAttributes(cartAttributes);
}
shoppingCartItemsList.add(shoppingCartItem);
}
}
if (CollectionUtils.isNotEmpty(shoppingCartItemsList)) {
cart.setShoppingCartItems(shoppingCartItemsList);
}
if (shoppingCart.getOrderId() != null) {
cart.setOrderId(shoppingCart.getOrderId());
}
OrderSummary summary = new OrderSummary();
List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> productsList = new ArrayList<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
productsList.addAll(shoppingCart.getLineItems());
summary.setProducts(productsList.stream().filter(p -> p.getProduct().isAvailable()).collect(Collectors.toList()));
OrderTotalSummary orderSummary = shoppingCartCalculationService.calculate(shoppingCart, store, language);
if (CollectionUtils.isNotEmpty(orderSummary.getTotals())) {
List<OrderTotal> totals = new ArrayList<OrderTotal>();
for (com.salesmanager.core.model.order.OrderTotal t : orderSummary.getTotals()) {
OrderTotal total = new OrderTotal();
total.setCode(t.getOrderTotalCode());
total.setText(t.getText());
total.setValue(t.getValue());
totals.add(total);
}
cart.setTotals(totals);
}
cart.setSubTotal(pricingService.getDisplayAmount(orderSummary.getSubTotal(), store));
cart.setTotal(pricingService.getDisplayAmount(orderSummary.getTotal(), store));
cart.setQuantity(cartQuantity);
cart.setId(shoppingCart.getId());
} catch (ServiceException ex) {
LOG.error("Error while converting cart Model to cart Data.." + ex);
throw new ConversionException("Unable to create cart data", ex);
}
return cart;
}
use of com.salesmanager.core.model.catalog.product.image.ProductImage in project shopizer by shopizer-ecommerce.
the class ProductImageApi method imageDetails.
/**
* Patch image (change position)
*
* @param id
* @param files
* @param position
* @param merchantStore
* @param language
* @throws IOException
*/
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = { "/private/products/{id}/image/{imageId}", "/auth/products/{id}/image/{id}" }, method = RequestMethod.PATCH)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public void imageDetails(@PathVariable Long id, @PathVariable Long imageId, @RequestParam(value = "order", required = false, defaultValue = "0") Integer position, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) throws IOException {
try {
Product p = productService.getById(id);
if (p == null) {
throw new ResourceNotFoundException("Product image [" + imageId + "] not found for product id [" + id + "] and merchant [" + merchantStore.getCode() + "]");
}
if (p.getMerchantStore().getId() != merchantStore.getId()) {
throw new ResourceNotFoundException("Product image [" + imageId + "] not found for product id [" + id + "] and merchant [" + merchantStore.getCode() + "]");
}
Optional<ProductImage> productImage = productImageService.getProductImage(imageId, id, merchantStore);
if (productImage.isPresent()) {
productImage.get().setSortOrder(position);
productImageService.updateProductImage(p, productImage.get());
} else {
throw new ResourceNotFoundException("Product image [" + imageId + "] not found for product id [" + id + "] and merchant [" + merchantStore.getCode() + "]");
}
} catch (Exception e) {
LOGGER.error("Error while deleting ProductImage", e);
throw new ServiceRuntimeException("ProductImage [" + imageId + "] cannot be edited");
}
}
use of com.salesmanager.core.model.catalog.product.image.ProductImage in project shopizer by shopizer-ecommerce.
the class ProductImageApi method uploadImage.
/**
* To be used with MultipartFile
*
* @param id
* @param uploadfiles
* @param request
* @param response
* @throws Exception
*/
@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(value = { "/private/products/{id}/images", "/auth/products/{id}/images" }, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE }, method = RequestMethod.POST)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public void uploadImage(@PathVariable Long id, @RequestParam(value = "file", required = true) MultipartFile[] files, @RequestParam(value = "order", required = false, defaultValue = "0") Integer position, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) throws IOException {
try {
// get the product
Product product = productService.getById(id);
if (product == null) {
throw new ResourceNotFoundException("Product not found");
}
// product belongs to merchant store
if (product.getMerchantStore().getId().intValue() != merchantStore.getId().intValue()) {
throw new UnauthorizedException("Resource not authorized for this merchant");
}
boolean hasDefaultImage = false;
Set<ProductImage> images = product.getImages();
if (!CollectionUtils.isEmpty(images)) {
for (ProductImage image : images) {
if (image.isDefaultImage()) {
hasDefaultImage = true;
break;
}
}
}
List<ProductImage> contentImagesList = new ArrayList<ProductImage>();
int sortOrder = position;
for (MultipartFile multipartFile : files) {
if (!multipartFile.isEmpty()) {
ProductImage productImage = new ProductImage();
productImage.setImage(multipartFile.getInputStream());
productImage.setProductImage(multipartFile.getOriginalFilename());
productImage.setProduct(product);
if (!hasDefaultImage) {
productImage.setDefaultImage(true);
hasDefaultImage = true;
}
productImage.setSortOrder(sortOrder);
position++;
contentImagesList.add(productImage);
}
}
if (CollectionUtils.isNotEmpty(contentImagesList)) {
productImageService.addProductImages(product, contentImagesList);
}
} catch (Exception e) {
LOGGER.error("Error while creating ProductImage", e);
throw new ServiceRuntimeException("Error while creating image");
}
}
Aggregations