Search in sources :

Example 6 with ProductImage

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);
    }
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) InputStream(java.io.InputStream) ImageContentFile(com.salesmanager.core.model.content.ImageContentFile) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 7 with ProductImage

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;
}
Also used : ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage)

Example 8 with ProductImage

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;
}
Also used : ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) OrderSummary(com.salesmanager.core.model.order.OrderSummary) OrderTotalSummary(com.salesmanager.core.model.order.OrderTotalSummary) ArrayList(java.util.ArrayList) ProductOptionDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription) ShoppingCartAttribute(com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute) ConversionException(org.apache.commons.beanutils.ConversionException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) OrderTotal(com.salesmanager.shop.model.order.total.OrderTotal) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)

Example 9 with ProductImage

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");
    }
}
Also used : ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) Product(com.salesmanager.core.model.catalog.product.Product) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) IOException(java.io.IOException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with ProductImage

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");
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) IOException(java.io.IOException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ProductImage (com.salesmanager.core.model.catalog.product.image.ProductImage)20 ArrayList (java.util.ArrayList)12 Product (com.salesmanager.core.model.catalog.product.Product)10 ServiceException (com.salesmanager.core.business.exception.ServiceException)8 ProductDescription (com.salesmanager.core.model.catalog.product.description.ProductDescription)7 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)6 Language (com.salesmanager.core.model.reference.language.Language)6 ReadableImage (com.salesmanager.shop.model.catalog.product.ReadableImage)6 List (java.util.List)6 Set (java.util.Set)6 CollectionUtils (org.apache.commons.collections4.CollectionUtils)6 Category (com.salesmanager.core.model.catalog.category.Category)5 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)5 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)5 ProductSpecification (com.salesmanager.shop.model.catalog.product.ProductSpecification)5 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)5 Collectors (java.util.stream.Collectors)5 PricingService (com.salesmanager.core.business.services.catalog.product.PricingService)4 ReadableCategory (com.salesmanager.shop.model.catalog.category.ReadableCategory)4 ReadableManufacturer (com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer)4