Search in sources :

Example 36 with Product

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

the class PersistableProductReviewPopulator method populate.

@Override
public ProductReview populate(PersistableProductReview source, ProductReview target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(customerService, "customerService cannot be null");
    Validate.notNull(productService, "productService cannot be null");
    Validate.notNull(languageService, "languageService cannot be null");
    Validate.notNull(source.getRating(), "Rating cannot bot be null");
    try {
        if (target == null) {
            target = new ProductReview();
        }
        Customer customer = customerService.getById(source.getCustomerId());
        // check if customer belongs to store
        if (customer == null || customer.getMerchantStore().getId().intValue() != store.getId().intValue()) {
            throw new ConversionException("Invalid customer id for the given store");
        }
        if (source.getDate() == null) {
            String date = DateUtil.formatDate(new Date());
            source.setDate(date);
        }
        target.setReviewDate(DateUtil.getDate(source.getDate()));
        target.setCustomer(customer);
        target.setReviewRating(source.getRating());
        Product product = productService.getById(source.getProductId());
        // check if product belongs to store
        if (product == null || product.getMerchantStore().getId().intValue() != store.getId().intValue()) {
            throw new ConversionException("Invalid product id for the given store");
        }
        target.setProduct(product);
        Language lang = languageService.getByCode(language.getCode());
        if (lang == null) {
            throw new ConversionException("Invalid language code, use iso codes (en, fr ...)");
        }
        ProductReviewDescription description = new ProductReviewDescription();
        description.setDescription(source.getDescription());
        description.setLanguage(lang);
        description.setName("-");
        description.setProductReview(target);
        Set<ProductReviewDescription> descriptions = new HashSet<ProductReviewDescription>();
        descriptions.add(description);
        target.setDescriptions(descriptions);
        return target;
    } catch (Exception e) {
        throw new ConversionException("Cannot populate ProductReview", e);
    }
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) Language(com.salesmanager.core.model.reference.language.Language) Customer(com.salesmanager.core.model.customer.Customer) Product(com.salesmanager.core.model.catalog.product.Product) ProductReviewDescription(com.salesmanager.core.model.catalog.product.review.ProductReviewDescription) ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview) PersistableProductReview(com.salesmanager.shop.model.catalog.product.PersistableProductReview) Date(java.util.Date) ConversionException(com.salesmanager.core.business.exception.ConversionException) HashSet(java.util.HashSet)

Example 37 with Product

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

the class PersistableProductInstanceMapper method merge.

@Override
public ProductInstance merge(PersistableProductInstance source, ProductInstance destination, MerchantStore store, Language language) {
    // 
    Long productVariant = source.getVariant();
    Long productVariantValue = source.getVariantValue();
    Optional<ProductVariation> variant = productVariationService.getById(store, productVariant);
    Optional<ProductVariation> variantValue = productVariationService.getById(store, productVariantValue);
    if (variant.isEmpty()) {
        throw new ResourceNotFoundException("ProductVariant [" + productVariant + "] + not found for store [" + store.getCode() + "]");
    }
    destination.setVariant(variant.get());
    if (variantValue.isEmpty()) {
        throw new ResourceNotFoundException("ProductVariant [" + productVariantValue + "] + not found for store [" + store.getCode() + "]");
    }
    destination.setVariantValue(variantValue.get());
    destination.setCode(variant.get().getCode() + ":" + variantValue.get().getCode());
    destination.setAvailable(source.isAvailable());
    destination.setDefaultSelection(source.isDefaultSelection());
    destination.setSku(source.getSku());
    if (StringUtils.isBlank(source.getDateAvailable())) {
        source.setDateAvailable(DateUtil.formatDate(new Date()));
    }
    if (source.getDateAvailable() != null) {
        try {
            destination.setDateAvailable(DateUtil.getDate(source.getDateAvailable()));
        } catch (Exception e) {
            throw new ServiceRuntimeException("Cant format date [" + source.getDateAvailable() + "]");
        }
    }
    destination.setSortOrder(source.getSortOrder());
    Product product = productService.getById(source.getProductId());
    if (product == null) {
        throw new ResourceNotFoundException("Product [" + source.getId() + "] + not found for store [" + store.getCode() + "]");
    }
    if (product.getMerchantStore().getId() != store.getId()) {
        throw new ResourceNotFoundException("Product [" + source.getId() + "] + not found for store [" + store.getCode() + "]");
    }
    destination.setProduct(product);
    return destination;
}
Also used : ProductVariation(com.salesmanager.core.model.catalog.product.variation.ProductVariation) Product(com.salesmanager.core.model.catalog.product.Product) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Date(java.util.Date) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 38 with Product

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

the class ReadableProductDefinitionMapper method merge.

@Override
public ReadableProductDefinition merge(Product source, ReadableProductDefinition destination, MerchantStore store, Language language) {
    Validate.notNull(source, "Product cannot be null");
    Validate.notNull(destination, "Product destination cannot be null");
    ReadableProductDefinition returnDestination = destination;
    if (language == null) {
        returnDestination = new ReadableProductDefinitionFull();
    }
    List<com.salesmanager.shop.model.catalog.product.ProductDescription> fulldescriptions = new ArrayList<com.salesmanager.shop.model.catalog.product.ProductDescription>();
    returnDestination.setIdentifier(source.getSku());
    returnDestination.setId(source.getId());
    returnDestination.setVisible(source.isAvailable());
    returnDestination.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
    ProductDescription description = null;
    if (source.getDescriptions() != null && source.getDescriptions().size() > 0) {
        for (ProductDescription desc : source.getDescriptions()) {
            if (language != null && desc.getLanguage() != null && desc.getLanguage().getId().intValue() == language.getId().intValue()) {
                description = desc;
                break;
            } else {
                fulldescriptions.add(populateDescription(desc));
            }
        }
    }
    if (description != null) {
        com.salesmanager.shop.model.catalog.product.ProductDescription tragetDescription = populateDescription(description);
        returnDestination.setDescription(tragetDescription);
    }
    if (source.getManufacturer() != null) {
        ReadableManufacturer manufacturer = readableManufacturerMapper.convert(source.getManufacturer(), store, language);
        returnDestination.setManufacturer(manufacturer);
    }
    if (!CollectionUtils.isEmpty(source.getCategories())) {
        List<ReadableCategory> categoryList = new ArrayList<ReadableCategory>();
        for (Category category : source.getCategories()) {
            ReadableCategory readableCategory = readableCategoryMapper.convert(category, store, language);
            categoryList.add(readableCategory);
        }
        returnDestination.setCategories(categoryList);
    }
    ProductSpecification specifications = new ProductSpecification();
    specifications.setHeight(source.getProductHeight());
    specifications.setLength(source.getProductLength());
    specifications.setWeight(source.getProductWeight());
    specifications.setWidth(source.getProductWidth());
    if (!StringUtils.isBlank(store.getSeizeunitcode())) {
        specifications.setDimensionUnitOfMeasure(DimensionUnitOfMeasure.valueOf(store.getSeizeunitcode().toLowerCase()));
    }
    if (!StringUtils.isBlank(store.getWeightunitcode())) {
        specifications.setWeightUnitOfMeasure(WeightUnitOfMeasure.valueOf(store.getWeightunitcode().toLowerCase()));
    }
    returnDestination.setProductSpecifications(specifications);
    if (source.getType() != null) {
        ReadableProductType readableType = readableProductTypeMapper.convert(source.getType(), store, language);
        returnDestination.setType(readableType);
    }
    returnDestination.setSortOrder(source.getSortOrder());
    // images
    Set<ProductImage> images = source.getImages();
    if (CollectionUtils.isNotEmpty(images)) {
        List<ReadableImage> imageList = images.stream().map(i -> this.convertImage(source, i, store)).collect(Collectors.toList());
        returnDestination.setImages(imageList);
    }
    // quantity
    ProductAvailability availability = null;
    for (ProductAvailability a : source.getAvailabilities()) {
        availability = a;
        returnDestination.setCanBePurchased(availability.getProductStatus());
        returnDestination.setQuantity(availability.getProductQuantity() == null ? 1 : availability.getProductQuantity());
    }
    FinalPrice price = null;
    try {
        price = pricingService.calculateProductPrice(source);
    } catch (ServiceException e) {
        throw new ConversionRuntimeException("Unable to get product price", e);
    }
    if (price != null) {
        returnDestination.setPrice(price.getStringPrice());
    }
    if (returnDestination instanceof ReadableProductDefinitionFull) {
        ((ReadableProductDefinitionFull) returnDestination).setDescriptions(fulldescriptions);
    }
    return returnDestination;
}
Also used : DateUtil(com.salesmanager.shop.utils.DateUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) ReadableCategoryMapper(com.salesmanager.shop.mapper.catalog.ReadableCategoryMapper) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) ServiceException(com.salesmanager.core.business.exception.ServiceException) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ReadableProductDefinition(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinition) DimensionUnitOfMeasure(com.salesmanager.shop.model.references.DimensionUnitOfMeasure) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) ReadableProductDefinitionFull(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinitionFull) Mapper(com.salesmanager.shop.mapper.Mapper) Product(com.salesmanager.core.model.catalog.product.Product) ReadableManufacturerMapper(com.salesmanager.shop.mapper.catalog.ReadableManufacturerMapper) ReadableProductType(com.salesmanager.shop.model.catalog.product.type.ReadableProductType) Set(java.util.Set) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) Collectors(java.util.stream.Collectors) Category(com.salesmanager.core.model.catalog.category.Category) ReadableProductTypeMapper(com.salesmanager.shop.mapper.catalog.ReadableProductTypeMapper) ReadableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer) List(java.util.List) Component(org.springframework.stereotype.Component) Validate(org.apache.commons.lang3.Validate) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) WeightUnitOfMeasure(com.salesmanager.shop.model.references.WeightUnitOfMeasure) Category(com.salesmanager.core.model.catalog.category.Category) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ArrayList(java.util.ArrayList) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ReadableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer) ReadableProductDefinition(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinition) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ReadableProductDefinitionFull(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinitionFull) ServiceException(com.salesmanager.core.business.exception.ServiceException) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) ReadableProductType(com.salesmanager.shop.model.catalog.product.type.ReadableProductType)

Example 39 with Product

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

the class OrderProductPopulator method populate.

/**
 * Converts a ShoppingCartItem carried in the ShoppingCart to an OrderProduct
 * that will be saved in the system
 */
@Override
public OrderProduct populate(ShoppingCartItem source, OrderProduct target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(productService, "productService must be set");
    Validate.notNull(digitalProductService, "digitalProductService must be set");
    Validate.notNull(productAttributeService, "productAttributeService must be set");
    try {
        Product modelProduct = productService.getById(source.getProductId());
        if (modelProduct == null) {
            throw new ConversionException("Cannot get product with id (productId) " + source.getProductId());
        }
        if (modelProduct.getMerchantStore().getId().intValue() != store.getId().intValue()) {
            throw new ConversionException("Invalid product id " + source.getProductId());
        }
        DigitalProduct digitalProduct = digitalProductService.getByProduct(store, modelProduct);
        if (digitalProduct != null) {
            OrderProductDownload orderProductDownload = new OrderProductDownload();
            orderProductDownload.setOrderProductFilename(digitalProduct.getProductFileName());
            orderProductDownload.setOrderProduct(target);
            orderProductDownload.setDownloadCount(0);
            orderProductDownload.setMaxdays(ApplicationConstants.MAX_DOWNLOAD_DAYS);
            target.getDownloads().add(orderProductDownload);
        }
        target.setOneTimeCharge(source.getItemPrice());
        target.setProductName(source.getProduct().getDescriptions().iterator().next().getName());
        target.setProductQuantity(source.getQuantity());
        target.setSku(source.getProduct().getSku());
        FinalPrice finalPrice = source.getFinalPrice();
        if (finalPrice == null) {
            throw new ConversionException("Object final price not populated in shoppingCartItem (source)");
        }
        // Default price
        OrderProductPrice orderProductPrice = orderProductPrice(finalPrice);
        orderProductPrice.setOrderProduct(target);
        Set<OrderProductPrice> prices = new HashSet<OrderProductPrice>();
        prices.add(orderProductPrice);
        // Other prices
        List<FinalPrice> otherPrices = finalPrice.getAdditionalPrices();
        if (otherPrices != null) {
            for (FinalPrice otherPrice : otherPrices) {
                OrderProductPrice other = orderProductPrice(otherPrice);
                other.setOrderProduct(target);
                prices.add(other);
            }
        }
        target.setPrices(prices);
        // OrderProductAttribute
        Set<ShoppingCartAttributeItem> attributeItems = source.getAttributes();
        if (!CollectionUtils.isEmpty(attributeItems)) {
            Set<OrderProductAttribute> attributes = new HashSet<OrderProductAttribute>();
            for (ShoppingCartAttributeItem attribute : attributeItems) {
                OrderProductAttribute orderProductAttribute = new OrderProductAttribute();
                orderProductAttribute.setOrderProduct(target);
                Long id = attribute.getProductAttributeId();
                ProductAttribute attr = productAttributeService.getById(id);
                if (attr == null) {
                    throw new ConversionException("Attribute id " + id + " does not exists");
                }
                if (attr.getProduct().getMerchantStore().getId().intValue() != store.getId().intValue()) {
                    throw new ConversionException("Attribute id " + id + " invalid for this store");
                }
                orderProductAttribute.setProductAttributeIsFree(attr.getProductAttributeIsFree());
                orderProductAttribute.setProductAttributeName(attr.getProductOption().getDescriptionsSettoList().get(0).getName());
                orderProductAttribute.setProductAttributeValueName(attr.getProductOptionValue().getDescriptionsSettoList().get(0).getName());
                orderProductAttribute.setProductAttributePrice(attr.getProductAttributePrice());
                orderProductAttribute.setProductAttributeWeight(attr.getProductAttributeWeight());
                orderProductAttribute.setProductOptionId(attr.getProductOption().getId());
                orderProductAttribute.setProductOptionValueId(attr.getProductOptionValue().getId());
                attributes.add(orderProductAttribute);
            }
            target.setOrderAttributes(attributes);
        }
    } catch (Exception e) {
        throw new ConversionException(e);
    }
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) Product(com.salesmanager.core.model.catalog.product.Product) OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) DigitalProduct(com.salesmanager.core.model.catalog.product.file.DigitalProduct) ShoppingCartAttributeItem(com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) OrderProductAttribute(com.salesmanager.core.model.order.orderproduct.OrderProductAttribute) ConversionException(com.salesmanager.core.business.exception.ConversionException) OrderProductPrice(com.salesmanager.core.model.order.orderproduct.OrderProductPrice) OrderProductAttribute(com.salesmanager.core.model.order.orderproduct.OrderProductAttribute) OrderProductDownload(com.salesmanager.core.model.order.orderproduct.OrderProductDownload) DigitalProduct(com.salesmanager.core.model.catalog.product.file.DigitalProduct) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) HashSet(java.util.HashSet)

Example 40 with Product

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

the class ReadableProductInstanceMapper method merge.

@Override
public ReadableProductInstance merge(ProductInstance source, ReadableProductInstance destination, MerchantStore store, Language language) {
    Validate.notNull(source, "Product instance cannot be null");
    Validate.notNull(source.getProduct(), "Product cannot be null");
    if (destination == null) {
        destination = new ReadableProductInstance();
    }
    destination.setSortOrder(source.getSortOrder() != null ? source.getSortOrder().intValue() : 0);
    destination.setAvailable(source.isAvailable());
    destination.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
    destination.setId(source.getId());
    destination.setDefaultSelection(source.isDefaultSelection());
    destination.setProductId(source.getProduct().getId());
    destination.setSku(source.getSku());
    destination.setSortOrder(source.getSortOrder());
    destination.setCode(source.getCode());
    // get product
    Product baseProduct = source.getProduct();
    if (baseProduct == null) {
        throw new ResourceNotFoundException("Product instances do not include the parent product [" + destination.getSku() + "]");
    }
    destination.setProductShipeable(baseProduct.isProductShipeable());
    // destination.setStore(null);
    destination.setStore(store.getCode());
    destination.setVariant(readableProductVariationMapper.convert(source.getVariant(), store, language));
    destination.setVariantValue(readableProductVariationMapper.convert(source.getVariantValue(), store, language));
    if (source.getProductInstanceGroup() != null) {
        Set<String> nameSet = new HashSet<>();
        List<ReadableImage> instanceImages = source.getProductInstanceGroup().getImages().stream().map(i -> this.image(i, store, language)).filter(e -> nameSet.add(e.getImageUrl())).collect(Collectors.toList());
        destination.setImages(instanceImages);
    }
    if (!CollectionUtils.isEmpty(source.getAvailabilities())) {
        List<ReadableInventory> inventories = source.getAvailabilities().stream().map(i -> readableInventoryMapper.convert(i, store, language)).collect(Collectors.toList());
        destination.setInventory(inventories);
    }
    return destination;
}
Also used : ReadableProductInstance(com.salesmanager.shop.model.catalog.product.product.instance.ReadableProductInstance) DateUtil(com.salesmanager.shop.utils.DateUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) CollectionUtils(org.apache.commons.collections.CollectionUtils) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ReadableProductVariationMapper(com.salesmanager.shop.mapper.catalog.ReadableProductVariationMapper) FileContentType(com.salesmanager.core.model.content.FileContentType) Mapper(com.salesmanager.shop.mapper.Mapper) Validate(org.jsoup.helper.Validate) Product(com.salesmanager.core.model.catalog.product.Product) Set(java.util.Set) ReadableInventoryMapper(com.salesmanager.shop.mapper.inventory.ReadableInventoryMapper) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) ProductInstance(com.salesmanager.core.model.catalog.product.instance.ProductInstance) Collectors(java.util.stream.Collectors) ReadableInventory(com.salesmanager.shop.model.catalog.product.inventory.ReadableInventory) List(java.util.List) Component(org.springframework.stereotype.Component) ProductInstanceImage(com.salesmanager.core.model.catalog.product.instance.ProductInstanceImage) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ReadableInventory(com.salesmanager.shop.model.catalog.product.inventory.ReadableInventory) Product(com.salesmanager.core.model.catalog.product.Product) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ReadableProductInstance(com.salesmanager.shop.model.catalog.product.product.instance.ReadableProductInstance) HashSet(java.util.HashSet)

Aggregations

Product (com.salesmanager.core.model.catalog.product.Product)120 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)53 ArrayList (java.util.ArrayList)45 Language (com.salesmanager.core.model.reference.language.Language)42 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)41 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)36 ServiceException (com.salesmanager.core.business.exception.ServiceException)35 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)35 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)33 ReadableProductPopulator (com.salesmanager.shop.populator.catalog.ReadableProductPopulator)33 PersistableProduct (com.salesmanager.shop.model.catalog.product.PersistableProduct)29 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)28 List (java.util.List)25 Date (java.util.Date)23 LightPersistableProduct (com.salesmanager.shop.model.catalog.product.LightPersistableProduct)22 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)21 Category (com.salesmanager.core.model.catalog.category.Category)20 Collectors (java.util.stream.Collectors)20 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)19 ConversionException (com.salesmanager.core.business.exception.ConversionException)17