Search in sources :

Example 1 with ConversionException

use of com.salesmanager.core.business.exception.ConversionException in project shopizer by shopizer-ecommerce.

the class PersistableProductDefinitionMapper method merge.

@Override
public Product merge(PersistableProductDefinition source, Product destination, MerchantStore store, Language language) {
    Validate.notNull(destination, "Product must not be null");
    try {
        destination.setSku(source.getIdentifier());
        destination.setAvailable(source.isVisible());
        destination.setDateAvailable(new Date());
        destination.setRefSku(source.getIdentifier());
        if (source.getId() != null && source.getId().longValue() == 0) {
            destination.setId(null);
        } else {
            destination.setId(source.getId());
        }
        // MANUFACTURER
        if (!StringUtils.isBlank(source.getManufacturer())) {
            Manufacturer manufacturer = manufacturerService.getByCode(store, source.getManufacturer());
            if (manufacturer == null) {
                throw new ConversionException("Manufacturer [" + source.getManufacturer() + "] does not exist");
            }
            destination.setManufacturer(manufacturer);
        }
        // PRODUCT TYPE
        if (!StringUtils.isBlank(source.getType())) {
            ProductType type = productTypeService.getByCode(source.getType(), store, language);
            if (type == null) {
                throw new ConversionException("Product type [" + source.getType() + "] does not exist");
            }
            destination.setType(type);
        }
        if (!StringUtils.isBlank(source.getDateAvailable())) {
            destination.setDateAvailable(DateUtil.getDate(source.getDateAvailable()));
        }
        destination.setMerchantStore(store);
        List<Language> languages = new ArrayList<Language>();
        Set<ProductDescription> descriptions = new HashSet<ProductDescription>();
        if (!CollectionUtils.isEmpty(source.getDescriptions())) {
            for (com.salesmanager.shop.model.catalog.product.ProductDescription description : source.getDescriptions()) {
                ProductDescription productDescription = new ProductDescription();
                Language lang = languageService.getByCode(description.getLanguage());
                if (lang == null) {
                    throw new ConversionException("Language code " + description.getLanguage() + " is invalid, use ISO code (en, fr ...)");
                }
                if (!CollectionUtils.isEmpty(destination.getDescriptions())) {
                    for (ProductDescription desc : destination.getDescriptions()) {
                        if (desc.getLanguage().getCode().equals(description.getLanguage())) {
                            productDescription = desc;
                            break;
                        }
                    }
                }
                productDescription.setProduct(destination);
                productDescription.setDescription(description.getDescription());
                productDescription.setProductHighlight(description.getHighlights());
                productDescription.setName(description.getName());
                productDescription.setSeUrl(description.getFriendlyUrl());
                productDescription.setMetatagKeywords(description.getKeyWords());
                productDescription.setMetatagDescription(description.getMetaDescription());
                productDescription.setTitle(description.getTitle());
                languages.add(lang);
                productDescription.setLanguage(lang);
                descriptions.add(productDescription);
            }
        }
        if (descriptions.size() > 0) {
            destination.setDescriptions(descriptions);
        }
        // if(source.getRating() != null) {
        // destination.setProductReviewAvg(new BigDecimal(source.getRating()));
        // }
        // destination.setProductReviewCount(source.getRatingCount());
        /**
         * Product definition
         */
        ProductAvailability productAvailability = null;
        ProductPrice defaultPrice = null;
        if (!CollectionUtils.isEmpty(destination.getAvailabilities())) {
            for (ProductAvailability avail : destination.getAvailabilities()) {
                Set<ProductPrice> prices = avail.getPrices();
                for (ProductPrice p : prices) {
                    if (p.isDefaultPrice()) {
                        if (productAvailability == null) {
                            productAvailability = avail;
                            defaultPrice = p;
                            productAvailability.setProductQuantity(source.getQuantity());
                            productAvailability.setProductStatus(source.isCanBePurchased());
                            p.setProductPriceAmount(source.getPrice());
                            break;
                        }
                    }
                }
            }
        }
        if (productAvailability == null) {
            // create with default values
            productAvailability = new ProductAvailability(destination, store);
            destination.getAvailabilities().add(productAvailability);
            productAvailability.setProductQuantity(source.getQuantity());
            productAvailability.setProductQuantityOrderMin(1);
            productAvailability.setProductQuantityOrderMax(1);
            productAvailability.setRegion(Constants.ALL_REGIONS);
            productAvailability.setAvailable(Boolean.valueOf(destination.isAvailable()));
            productAvailability.setProductStatus(source.isCanBePurchased());
        }
        if (defaultPrice == null) {
            defaultPrice = new ProductPrice();
            defaultPrice.setDefaultPrice(true);
            defaultPrice.setProductPriceAmount(source.getPrice());
            defaultPrice.setCode(ProductPriceEntity.DEFAULT_PRICE_CODE);
            defaultPrice.setProductAvailability(productAvailability);
            productAvailability.getPrices().add(defaultPrice);
            for (Language lang : languages) {
                ProductPriceDescription ppd = new ProductPriceDescription();
                ppd.setProductPrice(defaultPrice);
                ppd.setLanguage(lang);
                ppd.setName(ProductPriceDescription.DEFAULT_PRICE_DESCRIPTION);
                defaultPrice.getDescriptions().add(ppd);
            }
        }
        if (source.getProductSpecifications() != null) {
            destination.setProductHeight(source.getProductSpecifications().getHeight());
            destination.setProductLength(source.getProductSpecifications().getLength());
            destination.setProductWeight(source.getProductSpecifications().getWeight());
            destination.setProductWidth(source.getProductSpecifications().getWidth());
            if (source.getProductSpecifications().getManufacturer() != null) {
                Manufacturer manuf = null;
                if (!StringUtils.isBlank(source.getProductSpecifications().getManufacturer())) {
                    manuf = manufacturerService.getByCode(store, source.getProductSpecifications().getManufacturer());
                }
                if (manuf == null) {
                    throw new ConversionException("Invalid manufacturer id");
                }
                if (manuf != null) {
                    if (manuf.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                        throw new ConversionException("Invalid manufacturer id");
                    }
                    destination.setManufacturer(manuf);
                }
            }
        }
        destination.setSortOrder(source.getSortOrder());
        destination.setProductVirtual(source.isVirtual());
        destination.setProductShipeable(source.isShipeable());
        // attributes
        if (source.getProperties() != null) {
            for (com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute attr : source.getProperties()) {
                ProductAttribute attribute = persistableProductAttributeMapper.convert(attr, store, language);
                attribute.setProduct(destination);
                destination.getAttributes().add(attribute);
            }
        }
        // categories
        if (!CollectionUtils.isEmpty(source.getCategories())) {
            for (com.salesmanager.shop.model.catalog.category.Category categ : source.getCategories()) {
                Category c = null;
                if (!StringUtils.isBlank(categ.getCode())) {
                    c = categoryService.getByCode(store, categ.getCode());
                } else {
                    Validate.notNull(categ.getId(), "Category id nust not be null");
                    c = categoryService.getById(categ.getId(), store.getId());
                }
                if (c == null) {
                    throw new ConversionException("Category id " + categ.getId() + " does not exist");
                }
                if (c.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                    throw new ConversionException("Invalid category id");
                }
                destination.getCategories().add(c);
            }
        }
        return destination;
    } catch (Exception e) {
        throw new ConversionRuntimeException("Error converting product mapper", e);
    }
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) ArrayList(java.util.ArrayList) ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Language(com.salesmanager.core.model.reference.language.Language) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) Manufacturer(com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription) HashSet(java.util.HashSet) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductType(com.salesmanager.core.model.catalog.product.type.ProductType) Date(java.util.Date) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription)

Example 2 with ConversionException

use of com.salesmanager.core.business.exception.ConversionException in project shopizer by shopizer-ecommerce.

the class UserFacadeImpl method convertUserToReadableUser.

private ReadableUser convertUserToReadableUser(Language lang, User user) {
    ReadableUserPopulator populator = new ReadableUserPopulator();
    try {
        ReadableUser readableUser = new ReadableUser();
        readableUser = populator.populate(user, readableUser, user.getMerchantStore(), lang);
        List<Integer> groupIds = readableUser.getGroups().stream().map(ReadableGroup::getId).map(Long::intValue).collect(Collectors.toList());
        List<ReadablePermission> permissions = findPermissionsByGroups(groupIds);
        readableUser.setPermissions(permissions);
        return readableUser;
    } catch (ConversionException e) {
        throw new ConversionRuntimeException(e);
    }
}
Also used : ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) ReadablePermission(com.salesmanager.shop.model.security.ReadablePermission) ConversionException(com.salesmanager.core.business.exception.ConversionException) ReadableUser(com.salesmanager.shop.model.user.ReadableUser) ReadableUserPopulator(com.salesmanager.shop.populator.user.ReadableUserPopulator) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException)

Example 3 with ConversionException

use of com.salesmanager.core.business.exception.ConversionException in project shopizer by shopizer-ecommerce.

the class PersistableProductPopulator method populate.

@Override
public Product populate(PersistableProduct source, Product target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(target, "Product must not be null");
    try {
        target.setSku(source.getSku());
        target.setAvailable(source.isAvailable());
        target.setPreOrder(source.isPreOrder());
        target.setRefSku(source.getRefSku());
        if (source.getId() != null && source.getId().longValue() == 0) {
            target.setId(null);
        } else {
            target.setId(source.getId());
        }
        // PRODUCT TYPE
        if (!StringUtils.isBlank(source.getType())) {
            ProductType type = productTypeService.getByCode(source.getType(), store, language);
            if (type == null) {
                throw new ConversionException("Product type [" + source.getType() + "] does not exist");
            }
            target.setType(type);
        }
        if (source.getOwner() != null && source.getOwner().getId() != null) {
            com.salesmanager.core.model.customer.Customer owner = customerService.getById(source.getOwner().getId());
            target.setOwner(owner);
        }
        if (!StringUtils.isBlank(source.getDateAvailable())) {
            target.setDateAvailable(DateUtil.getDate(source.getDateAvailable()));
        }
        target.setMerchantStore(store);
        List<Language> languages = new ArrayList<Language>();
        Set<ProductDescription> descriptions = new HashSet<ProductDescription>();
        if (!CollectionUtils.isEmpty(source.getDescriptions())) {
            for (com.salesmanager.shop.model.catalog.product.ProductDescription description : source.getDescriptions()) {
                ProductDescription productDescription = new ProductDescription();
                Language lang = languageService.getByCode(description.getLanguage());
                if (lang == null) {
                    throw new ConversionException("Language code " + description.getLanguage() + " is invalid, use ISO code (en, fr ...)");
                }
                if (!CollectionUtils.isEmpty(target.getDescriptions())) {
                    for (ProductDescription desc : target.getDescriptions()) {
                        if (desc.getLanguage().getCode().equals(description.getLanguage())) {
                            productDescription = desc;
                            break;
                        }
                    }
                }
                productDescription.setProduct(target);
                productDescription.setDescription(description.getDescription());
                productDescription.setProductHighlight(description.getHighlights());
                productDescription.setName(description.getName());
                productDescription.setSeUrl(description.getFriendlyUrl());
                productDescription.setMetatagKeywords(description.getKeyWords());
                productDescription.setMetatagDescription(description.getMetaDescription());
                productDescription.setTitle(description.getTitle());
                languages.add(lang);
                productDescription.setLanguage(lang);
                descriptions.add(productDescription);
            }
        }
        if (descriptions.size() > 0) {
            target.setDescriptions(descriptions);
        }
        if (source.getProductSpecifications() != null) {
            target.setProductHeight(source.getProductSpecifications().getHeight());
            target.setProductLength(source.getProductSpecifications().getLength());
            target.setProductWeight(source.getProductSpecifications().getWeight());
            target.setProductWidth(source.getProductSpecifications().getWidth());
            if (source.getProductSpecifications().getManufacturer() != null) {
                Manufacturer manuf = null;
                if (!StringUtils.isBlank(source.getProductSpecifications().getManufacturer())) {
                    manuf = manufacturerService.getByCode(store, source.getProductSpecifications().getManufacturer());
                }
                if (manuf == null) {
                    throw new ConversionException("Invalid manufacturer id");
                }
                if (manuf != null) {
                    if (manuf.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                        throw new ConversionException("Invalid manufacturer id");
                    }
                    target.setManufacturer(manuf);
                }
            }
        }
        target.setSortOrder(source.getSortOrder());
        target.setProductVirtual(source.isProductVirtual());
        target.setProductShipeable(source.isProductShipeable());
        if (source.getRating() != null) {
            target.setProductReviewAvg(new BigDecimal(source.getRating()));
        }
        target.setProductReviewCount(source.getRatingCount());
        if (CollectionUtils.isNotEmpty(source.getProductPrices())) {
            // get product availability
            // create new ProductAvailability
            ProductAvailability productAvailability = new ProductAvailability(target, store);
            // todo now support for specific regions
            productAvailability.setRegion(Constants.ALL_REGIONS);
            productAvailability.setProductQuantity(source.getQuantity());
            productAvailability.setProductQuantityOrderMin(1);
            productAvailability.setProductQuantityOrderMax(1);
            productAvailability.setAvailable(Boolean.valueOf(target.isAvailable()));
            for (com.salesmanager.shop.model.catalog.product.PersistableProductPrice priceEntity : source.getProductPrices()) {
                ProductPrice price = new ProductPrice();
                price.setProductAvailability(productAvailability);
                price.setDefaultPrice(priceEntity.isDefaultPrice());
                price.setProductPriceAmount(priceEntity.getOriginalPrice());
                price.setCode(priceEntity.getCode());
                price.setProductPriceSpecialAmount(priceEntity.getDiscountedPrice());
                if (priceEntity.getDiscountStartDate() != null) {
                    Date startDate = DateUtil.getDate(priceEntity.getDiscountStartDate());
                    price.setProductPriceSpecialStartDate(startDate);
                }
                if (priceEntity.getDiscountEndDate() != null) {
                    Date endDate = DateUtil.getDate(priceEntity.getDiscountEndDate());
                    price.setProductPriceSpecialEndDate(endDate);
                }
                productAvailability.getPrices().add(price);
                target.getAvailabilities().add(productAvailability);
                for (Language lang : languages) {
                    ProductPriceDescription ppd = new ProductPriceDescription();
                    ppd.setProductPrice(price);
                    ppd.setLanguage(lang);
                    ppd.setName(ProductPriceDescription.DEFAULT_PRICE_DESCRIPTION);
                    // price appender
                    Optional<com.salesmanager.shop.model.catalog.product.ProductPriceDescription> description = priceEntity.getDescriptions().stream().filter(d -> d.getLanguage() != null && d.getLanguage().equals(lang.getCode())).findFirst();
                    if (description.isPresent()) {
                        ppd.setPriceAppender(description.get().getPriceAppender());
                    }
                    price.getDescriptions().add(ppd);
                }
            }
        } else {
            // create
            ProductAvailability productAvailability = null;
            ProductPrice defaultPrice = null;
            if (!CollectionUtils.isEmpty(target.getAvailabilities())) {
                for (ProductAvailability avail : target.getAvailabilities()) {
                    Set<ProductPrice> prices = avail.getPrices();
                    for (ProductPrice p : prices) {
                        if (p.isDefaultPrice()) {
                            if (productAvailability == null) {
                                productAvailability = avail;
                                defaultPrice = p;
                                break;
                            }
                            p.setDefaultPrice(false);
                        }
                    }
                }
            }
            if (productAvailability == null) {
                productAvailability = new ProductAvailability(target, store);
                target.getAvailabilities().add(productAvailability);
            }
            productAvailability.setProductQuantity(source.getQuantity());
            productAvailability.setProductQuantityOrderMin(1);
            productAvailability.setProductQuantityOrderMax(1);
            productAvailability.setRegion(Constants.ALL_REGIONS);
            productAvailability.setAvailable(Boolean.valueOf(target.isAvailable()));
            if (defaultPrice != null) {
                defaultPrice.setProductPriceAmount(source.getPrice());
            } else {
                defaultPrice = new ProductPrice();
                defaultPrice.setDefaultPrice(true);
                defaultPrice.setProductPriceAmount(source.getPrice());
                defaultPrice.setCode(ProductPriceEntity.DEFAULT_PRICE_CODE);
                defaultPrice.setProductAvailability(productAvailability);
                productAvailability.getPrices().add(defaultPrice);
                for (Language lang : languages) {
                    ProductPriceDescription ppd = new ProductPriceDescription();
                    ppd.setProductPrice(defaultPrice);
                    ppd.setLanguage(lang);
                    ppd.setName(ProductPriceDescription.DEFAULT_PRICE_DESCRIPTION);
                    defaultPrice.getDescriptions().add(ppd);
                }
            }
        }
        // image
        if (source.getImages() != null) {
            for (PersistableImage img : source.getImages()) {
                ProductImage productImage = new ProductImage();
                productImage.setImageType(img.getImageType());
                productImage.setDefaultImage(img.isDefaultImage());
                if (img.getImageType() == 1) {
                    productImage.setProductImageUrl(img.getImageUrl());
                    productImage.setImage(new ByteArrayInputStream(new byte[0]));
                } else {
                    ByteArrayInputStream in = new ByteArrayInputStream(img.getBytes());
                    productImage.setImage(in);
                }
                productImage.setProduct(target);
                productImage.setProductImage(img.getName());
                target.getImages().add(productImage);
            }
        }
        // attributes
        if (source.getAttributes() != null) {
            for (com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute attr : source.getAttributes()) {
                ProductAttribute attribute = persistableProductAttributeMapper.convert(attr, store, language);
                attribute.setProduct(target);
                target.getAttributes().add(attribute);
            }
        }
        // categories
        if (!CollectionUtils.isEmpty(source.getCategories())) {
            for (com.salesmanager.shop.model.catalog.category.Category categ : source.getCategories()) {
                Category c = null;
                if (!StringUtils.isBlank(categ.getCode())) {
                    c = categoryService.getByCode(store, categ.getCode());
                } else {
                    Validate.notNull(categ.getId(), "Category id nust not be null");
                    c = categoryService.getById(categ.getId(), store.getId());
                }
                if (c == null) {
                    throw new ConversionException("Category id " + categ.getId() + " does not exist");
                }
                if (c.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                    throw new ConversionException("Invalid category id");
                }
                target.getCategories().add(c);
            }
        }
        return target;
    } catch (Exception e) {
        throw new ConversionException(e);
    }
}
Also used : ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) Date(java.util.Date) DateUtil(com.salesmanager.shop.utils.DateUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ProductOptionValueService(com.salesmanager.core.business.services.catalog.product.attribute.ProductOptionValueService) TaxClassService(com.salesmanager.core.business.services.tax.TaxClassService) CollectionUtils(org.apache.commons.collections4.CollectionUtils) LanguageService(com.salesmanager.core.business.services.reference.language.LanguageService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Language(com.salesmanager.core.model.reference.language.Language) BigDecimal(java.math.BigDecimal) PersistableProduct(com.salesmanager.shop.model.catalog.product.PersistableProduct) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ByteArrayInputStream(java.io.ByteArrayInputStream) Manufacturer(com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer) CategoryService(com.salesmanager.core.business.services.catalog.category.CategoryService) ManufacturerService(com.salesmanager.core.business.services.catalog.product.manufacturer.ManufacturerService) PersistableProductAttributeMapper(com.salesmanager.shop.mapper.catalog.PersistableProductAttributeMapper) Constants(com.salesmanager.core.business.constants.Constants) CustomerService(com.salesmanager.core.business.services.customer.CustomerService) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Product(com.salesmanager.core.model.catalog.product.Product) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription) AbstractDataPopulator(com.salesmanager.core.business.utils.AbstractDataPopulator) Set(java.util.Set) Category(com.salesmanager.core.model.catalog.category.Category) List(java.util.List) Component(org.springframework.stereotype.Component) Validate(org.apache.commons.lang3.Validate) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) Optional(java.util.Optional) ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductType(com.salesmanager.core.model.catalog.product.type.ProductType) ProductOptionService(com.salesmanager.core.business.services.catalog.product.attribute.ProductOptionService) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) PersistableImage(com.salesmanager.shop.model.catalog.product.PersistableImage) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) ProductTypeService(com.salesmanager.core.business.services.catalog.product.type.ProductTypeService) ProductPriceEntity(com.salesmanager.shop.model.catalog.product.ProductPriceEntity) Category(com.salesmanager.core.model.catalog.category.Category) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ArrayList(java.util.ArrayList) ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Language(com.salesmanager.core.model.reference.language.Language) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) PersistableImage(com.salesmanager.shop.model.catalog.product.PersistableImage) Manufacturer(com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription) HashSet(java.util.HashSet) ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductType(com.salesmanager.core.model.catalog.product.type.ProductType) BigDecimal(java.math.BigDecimal) Date(java.util.Date) ConversionException(com.salesmanager.core.business.exception.ConversionException) ByteArrayInputStream(java.io.ByteArrayInputStream) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription)

Example 4 with ConversionException

use of com.salesmanager.core.business.exception.ConversionException 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 5 with ConversionException

use of com.salesmanager.core.business.exception.ConversionException in project shopizer by shopizer-ecommerce.

the class ReadableProductPricePopulator method populate.

@Override
public ReadableProductPrice populate(ProductPrice source, ReadableProductPrice target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(pricingService, "pricingService must be set");
    Validate.notNull(source.getProductAvailability(), "productPrice.availability cannot be null");
    Validate.notNull(source.getProductAvailability().getProduct(), "productPrice.availability.product cannot be null");
    try {
        if (language == null) {
            target = new ReadableProductPriceFull();
        }
        if (source.getId() != null && source.getId() > 0) {
            target.setId(source.getId());
        }
        FinalPrice finalPrice = pricingService.calculateProductPrice(source.getProductAvailability().getProduct());
        target.setOriginalPrice(pricingService.getDisplayAmount(source.getProductPriceAmount(), store));
        if (finalPrice.isDiscounted()) {
            target.setDiscounted(true);
            target.setFinalPrice(pricingService.getDisplayAmount(source.getProductPriceSpecialAmount(), store));
        } else {
            target.setFinalPrice(pricingService.getDisplayAmount(finalPrice.getOriginalPrice(), store));
        }
        if (source.getDescriptions() != null && source.getDescriptions().size() > 0) {
            List<com.salesmanager.shop.model.catalog.product.ProductPriceDescription> fulldescriptions = new ArrayList<com.salesmanager.shop.model.catalog.product.ProductPriceDescription>();
            Set<ProductPriceDescription> descriptions = source.getDescriptions();
            ProductPriceDescription description = null;
            for (ProductPriceDescription desc : descriptions) {
                if (language != null && desc.getLanguage().getCode().equals(language.getCode())) {
                    description = desc;
                    break;
                } else {
                    fulldescriptions.add(populateDescription(desc));
                }
            }
            if (description != null) {
                com.salesmanager.shop.model.catalog.product.ProductPriceDescription d = populateDescription(description);
                target.setDescription(d);
            }
            if (target instanceof ReadableProductPriceFull) {
                ((ReadableProductPriceFull) target).setDescriptions(fulldescriptions);
            }
        }
    } catch (Exception e) {
        throw new ConversionException("Exception while converting to ReadableProductPrice", e);
    }
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) ReadableProductPriceFull(com.salesmanager.shop.model.catalog.product.ReadableProductPriceFull) ArrayList(java.util.ArrayList) ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Aggregations

ConversionException (com.salesmanager.core.business.exception.ConversionException)57 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)17 Language (com.salesmanager.core.model.reference.language.Language)16 HashSet (java.util.HashSet)15 ArrayList (java.util.ArrayList)11 ServiceException (com.salesmanager.core.business.exception.ServiceException)10 Product (com.salesmanager.core.model.catalog.product.Product)10 Customer (com.salesmanager.core.model.customer.Customer)9 ReadableCustomer (com.salesmanager.shop.model.customer.ReadableCustomer)9 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)8 BigDecimal (java.math.BigDecimal)8 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)7 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)6 OrderProduct (com.salesmanager.core.model.order.orderproduct.OrderProduct)6 Country (com.salesmanager.core.model.reference.country.Country)6 Zone (com.salesmanager.core.model.reference.zone.Zone)6 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)6 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)6 Date (java.util.Date)6 Category (com.salesmanager.core.model.catalog.category.Category)5