Search in sources :

Example 46 with ConversionException

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

the class PersistableCategoryPopulator method populate.

@Override
public Category populate(PersistableCategory source, Category target, MerchantStore store, Language language) throws ConversionException {
    try {
        Validate.notNull(target, "Category target cannot be null");
        /*		Validate.notNull(categoryService, "Requires to set CategoryService");
		Validate.notNull(languageService, "Requires to set LanguageService");*/
        target.setMerchantStore(store);
        target.setCode(source.getCode());
        target.setSortOrder(source.getSortOrder());
        target.setVisible(source.isVisible());
        target.setFeatured(source.isFeatured());
        // children
        if (!CollectionUtils.isEmpty(source.getChildren())) {
        // no modifications to children category
        } else {
            target.getCategories().clear();
        }
        if (source.getParent() == null || (StringUtils.isBlank(source.getParent().getCode())) || source.getParent().getId() == null) {
            target.setParent(null);
            target.setDepth(0);
            target.setLineage(new StringBuilder().append("/").append(source.getId()).append("/").toString());
        } else {
            Category parent = null;
            if (!StringUtils.isBlank(source.getParent().getCode())) {
                parent = categoryService.getByCode(store.getCode(), source.getParent().getCode());
            } else if (source.getParent().getId() != null) {
                parent = categoryService.getById(source.getParent().getId(), store.getId());
            } else {
                throw new ConversionException("Category parent needs at least an id or a code for reference");
            }
            if (parent != null && parent.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                throw new ConversionException("Store id does not belong to specified parent id");
            }
            if (parent != null) {
                target.setParent(parent);
                String lineage = parent.getLineage();
                int depth = parent.getDepth();
                target.setDepth(depth + 1);
                target.setLineage(new StringBuilder().append(lineage).append(target.getId()).append("/").toString());
            }
        }
        if (!CollectionUtils.isEmpty(source.getChildren())) {
            for (PersistableCategory cat : source.getChildren()) {
                Category persistCategory = this.populate(cat, new Category(), store, language);
                target.getCategories().add(persistCategory);
            }
        }
        if (!CollectionUtils.isEmpty(source.getDescriptions())) {
            Set<com.salesmanager.core.model.catalog.category.CategoryDescription> descriptions = new HashSet<com.salesmanager.core.model.catalog.category.CategoryDescription>();
            if (CollectionUtils.isNotEmpty(target.getDescriptions())) {
                for (com.salesmanager.core.model.catalog.category.CategoryDescription description : target.getDescriptions()) {
                    for (CategoryDescription d : source.getDescriptions()) {
                        if (StringUtils.isBlank(d.getLanguage())) {
                            throw new ConversionException("Source category description has no language");
                        }
                        if (d.getLanguage().equals(description.getLanguage().getCode())) {
                            description.setCategory(target);
                            description = buildDescription(d, description);
                            descriptions.add(description);
                        }
                    }
                }
            } else {
                for (CategoryDescription d : source.getDescriptions()) {
                    com.salesmanager.core.model.catalog.category.CategoryDescription t = new com.salesmanager.core.model.catalog.category.CategoryDescription();
                    this.buildDescription(d, t);
                    t.setCategory(target);
                    descriptions.add(t);
                }
            }
            target.setDescriptions(descriptions);
        }
        return target;
    } catch (Exception e) {
        throw new ConversionException(e);
    }
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) Category(com.salesmanager.core.model.catalog.category.Category) PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) ConversionException(com.salesmanager.core.business.exception.ConversionException) CategoryDescription(com.salesmanager.shop.model.catalog.category.CategoryDescription) HashSet(java.util.HashSet)

Example 47 with ConversionException

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

the class PersistableProductOptionPopulator method populate.

@Override
public ProductOption populate(PersistableProductOption source, ProductOption target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(languageService, "Requires to set LanguageService");
    try {
        target.setMerchantStore(store);
        target.setProductOptionSortOrder(source.getOrder());
        target.setCode(source.getCode());
        if (!CollectionUtils.isEmpty(source.getDescriptions())) {
            Set<com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription> descriptions = new HashSet<com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription>();
            for (ProductOptionDescription desc : source.getDescriptions()) {
                com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription description = new com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription();
                Language lang = languageService.getByCode(desc.getLanguage());
                if (lang == null) {
                    throw new ConversionException("Language is null for code " + description.getLanguage() + " use language ISO code [en, fr ...]");
                }
                description.setLanguage(lang);
                description.setName(desc.getName());
                description.setTitle(desc.getTitle());
                description.setProductOption(target);
                descriptions.add(description);
            }
            target.setDescriptions(descriptions);
        }
    } catch (Exception e) {
        throw new ConversionException(e);
    }
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductOptionDescription(com.salesmanager.shop.model.catalog.product.attribute.ProductOptionDescription) Language(com.salesmanager.core.model.reference.language.Language) HashSet(java.util.HashSet)

Example 48 with ConversionException

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

the class PersistableOrderProductPopulator method populate.

/**
 * Converts a ShoppingCartItem carried in the ShoppingCart to an OrderProduct
 * that will be saved in the system
 */
@Override
public OrderProduct populate(PersistableOrderProduct 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.getProduct().getId());
        if (modelProduct == null) {
            throw new ConversionException("Cannot get product with id (productId) " + source.getProduct().getId());
        }
        if (modelProduct.getMerchantStore().getId().intValue() != store.getId().intValue()) {
            throw new ConversionException("Invalid product id " + source.getProduct().getId());
        }
        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.getPrice());
        target.setProductName(source.getProduct().getDescription().getName());
        target.setProductQuantity(source.getOrderedQuantity());
        target.setSku(source.getProduct().getSku());
        OrderProductPrice orderProductPrice = new OrderProductPrice();
        orderProductPrice.setDefaultPrice(true);
        orderProductPrice.setProductPrice(source.getPrice());
        orderProductPrice.setOrderProduct(target);
        Set<OrderProductPrice> prices = new HashSet<OrderProductPrice>();
        prices.add(orderProductPrice);
        /**
         * DO NOT SUPPORT MUTIPLE PRICES *
         */
        /*			//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
        List<ProductAttribute> attributeItems = source.getAttributes();
        if (!CollectionUtils.isEmpty(attributeItems)) {
            Set<OrderProductAttribute> attributes = new HashSet<OrderProductAttribute>();
            for (ProductAttribute attribute : attributeItems) {
                OrderProductAttribute orderProductAttribute = new OrderProductAttribute();
                orderProductAttribute.setOrderProduct(target);
                Long id = attribute.getId();
                com.salesmanager.core.model.catalog.product.attribute.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) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) DigitalProduct(com.salesmanager.core.model.catalog.product.file.DigitalProduct) OrderProductAttribute(com.salesmanager.core.model.order.orderproduct.OrderProductAttribute) ProductAttribute(com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute) 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) HashSet(java.util.HashSet)

Example 49 with ConversionException

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

the class ReadableOrderProductDownloadPopulator method populate.

@Override
public ReadableOrderProductDownload populate(OrderProductDownload source, ReadableOrderProductDownload target, MerchantStore store, Language language) throws ConversionException {
    try {
        target.setProductName(source.getOrderProduct().getProductName());
        target.setDownloadCount(source.getDownloadCount());
        target.setDownloadExpiryDays(source.getMaxdays());
        target.setId(source.getId());
        target.setFileName(source.getOrderProductFilename());
        target.setOrderId(source.getOrderProduct().getOrder().getId());
        return target;
    } catch (Exception e) {
        throw new ConversionException(e);
    }
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) ConversionException(com.salesmanager.core.business.exception.ConversionException)

Example 50 with ConversionException

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

the class ReadableOrderTotalPopulator method populate.

@Override
public ReadableOrderTotal populate(OrderTotal source, ReadableOrderTotal target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(pricingService, "PricingService must be set");
    Validate.notNull(messages, "LabelUtils must be set");
    Locale locale = LocaleUtils.getLocale(language);
    try {
        target.setCode(source.getOrderTotalCode());
        target.setId(source.getId());
        target.setModule(source.getModule());
        target.setOrder(source.getSortOrder());
        target.setTitle(messages.getMessage(source.getOrderTotalCode(), locale, source.getOrderTotalCode()));
        target.setText(source.getText());
        target.setValue(source.getValue());
        target.setTotal(pricingService.getDisplayAmount(source.getValue(), store));
        if (!StringUtils.isBlank(source.getOrderTotalCode())) {
            if (Constants.OT_DISCOUNT_TITLE.equals(source.getOrderTotalCode())) {
                target.setDiscounted(true);
            }
        }
    } catch (Exception e) {
        throw new ConversionException(e);
    }
    return target;
}
Also used : Locale(java.util.Locale) ConversionException(com.salesmanager.core.business.exception.ConversionException) ConversionException(com.salesmanager.core.business.exception.ConversionException)

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