Search in sources :

Example 16 with ProductImage

use of com.salesmanager.core.model.catalog.product.image.ProductImage in project shopizer by shopizer-ecommerce.

the class ProductImageServiceImpl method getProductImage.

// TODO get default product image
@Override
public OutputContentFile getProductImage(ProductImage productImage, ProductImageSize size) throws ServiceException {
    ProductImage pi = new ProductImage();
    String imageName = productImage.getProductImage();
    if (size == ProductImageSize.LARGE) {
        imageName = "L-" + imageName;
    }
    if (size == ProductImageSize.SMALL) {
        imageName = "S-" + imageName;
    }
    pi.setProductImage(imageName);
    pi.setProduct(productImage.getProduct());
    return productFileManager.getProductImage(pi);
}
Also used : ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage)

Example 17 with ProductImage

use of com.salesmanager.core.model.catalog.product.image.ProductImage in project shopizer by shopizer-ecommerce.

the class ProductServiceImpl method saveOrUpdate.

private void saveOrUpdate(Product product) throws ServiceException {
    LOGGER.debug("Save or update product ");
    Validate.notNull(product, "product cannot be null");
    Validate.notNull(product.getAvailabilities(), "product must have at least one availability");
    Validate.notEmpty(product.getAvailabilities(), "product must have at least one availability");
    // take care of product images separately
    Set<ProductImage> originalProductImages = new HashSet<ProductImage>(product.getImages());
    if (product.getId() != null && product.getId() > 0) {
        super.update(product);
    } else {
        super.create(product);
    }
    /**
     * Image creation needs extra service to save the file in the CMS
     */
    List<Long> newImageIds = new ArrayList<Long>();
    Set<ProductImage> images = product.getImages();
    try {
        if (images != null && images.size() > 0) {
            for (ProductImage image : images) {
                if (image.getImage() != null && (image.getId() == null || image.getId() == 0L)) {
                    image.setProduct(product);
                    InputStream inputStream = image.getImage();
                    ImageContentFile cmsContentImage = new ImageContentFile();
                    cmsContentImage.setFileName(image.getProductImage());
                    cmsContentImage.setFile(inputStream);
                    cmsContentImage.setFileContentType(FileContentType.PRODUCT);
                    productImageService.addProductImage(product, image, cmsContentImage);
                    newImageIds.add(image.getId());
                } else {
                    if (image.getId() != null) {
                        productImageService.save(image);
                        newImageIds.add(image.getId());
                    }
                }
            }
        }
        // cleanup old and new images
        if (originalProductImages != null) {
            for (ProductImage image : originalProductImages) {
                if (image.getImage() != null && image.getId() == null) {
                    image.setProduct(product);
                    InputStream inputStream = image.getImage();
                    ImageContentFile cmsContentImage = new ImageContentFile();
                    cmsContentImage.setFileName(image.getProductImage());
                    cmsContentImage.setFile(inputStream);
                    cmsContentImage.setFileContentType(FileContentType.PRODUCT);
                    productImageService.addProductImage(product, image, cmsContentImage);
                    newImageIds.add(image.getId());
                } else {
                    if (!newImageIds.contains(image.getId())) {
                        productImageService.delete(image);
                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("Cannot save images " + e.getMessage());
    }
}
Also used : ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) InputStream(java.io.InputStream) ImageContentFile(com.salesmanager.core.model.content.ImageContentFile) ArrayList(java.util.ArrayList) ServiceException(com.salesmanager.core.business.exception.ServiceException) HashSet(java.util.HashSet)

Example 18 with ProductImage

use of com.salesmanager.core.model.catalog.product.image.ProductImage in project shopizer by shopizer-ecommerce.

the class ProductServiceImpl method delete.

@Override
public void delete(Product product) throws ServiceException {
    LOGGER.debug("Deleting product");
    Validate.notNull(product, "Product cannot be null");
    Validate.notNull(product.getMerchantStore(), "MerchantStore cannot be null in product");
    // Prevents detached entity
    product = this.getById(product.getId());
    // error
    product.setCategories(null);
    Set<ProductImage> images = product.getImages();
    for (ProductImage image : images) {
        productImageService.removeProductImage(image);
    }
    product.setImages(null);
    // delete reviews
    List<ProductReview> reviews = productReviewService.getByProductNoCustomers(product);
    for (ProductReview review : reviews) {
        productReviewService.delete(review);
    }
    // related - featured
    List<ProductRelationship> relationships = productRelationshipService.listByProduct(product);
    for (ProductRelationship relationship : relationships) {
        productRelationshipService.deleteRelationship(relationship);
    }
    super.delete(product);
    searchService.deleteIndex(product.getMerchantStore(), product);
}
Also used : ProductRelationship(com.salesmanager.core.model.catalog.product.relationship.ProductRelationship) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview)

Example 19 with ProductImage

use of com.salesmanager.core.model.catalog.product.image.ProductImage in project shopizer by shopizer-ecommerce.

the class ProductTest method testViewImage.

private void testViewImage(Product product) throws Exception {
    ProductImage productImage = product.getProductImage();
    // get physical small image
    OutputContentFile contentFile = productImageService.getProductImage(product.getMerchantStore().getCode(), product.getSku(), productImage.getProductImage(), ProductImageSize.SMALL);
    Assert.assertNotNull(contentFile);
    // get physical original image
    contentFile = productImageService.getProductImage(product.getMerchantStore().getCode(), product.getSku(), productImage.getProductImage(), ProductImageSize.LARGE);
    Assert.assertNotNull(contentFile);
}
Also used : ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile)

Example 20 with ProductImage

use of com.salesmanager.core.model.catalog.product.image.ProductImage in project shopizer by shopizer-ecommerce.

the class ProductTest method testInsertImage.

/**
 * Images
 * @param product
 * @throws Exception
 */
private void testInsertImage(Product product) throws Exception {
    ProductImage productImage = new ProductImage();
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    InputStream inputStream = classloader.getResourceAsStream("img/" + IMAGE_NAME);
    ImageContentFile cmsContentImage = new ImageContentFile();
    cmsContentImage.setFileName(IMAGE_NAME);
    cmsContentImage.setFile(inputStream);
    cmsContentImage.setFileContentType(FileContentType.PRODUCT);
    productImage.setProductImage(IMAGE_NAME);
    productImage.setProduct(product);
    // absolutely required otherwise the file is not created on disk
    productImage.setImage(inputStream);
    product.getImages().add(productImage);
    // saves the ProductImage entity and the file on disk
    productService.update(product);
}
Also used : ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) InputStream(java.io.InputStream) ImageContentFile(com.salesmanager.core.model.content.ImageContentFile)

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