Search in sources :

Example 16 with ProductOptionValueDescription

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

the class ShoppingCartTest method createShoppingCart.

@Test
public void createShoppingCart() throws Exception {
    MerchantStore store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);
    Language en = languageService.getByCode("en");
    /**
     * CATALOG CREATION *
     */
    ProductType generalType = productTypeService.getProductType(ProductType.GENERAL_TYPE);
    /**
     * Create the category
     */
    Category shirts = new Category();
    shirts.setMerchantStore(store);
    shirts.setCode("shirts");
    CategoryDescription shirtsEnglishDescription = new CategoryDescription();
    shirtsEnglishDescription.setName("Shirts");
    shirtsEnglishDescription.setCategory(shirts);
    shirtsEnglishDescription.setLanguage(en);
    Set<CategoryDescription> descriptions = new HashSet<CategoryDescription>();
    descriptions.add(shirtsEnglishDescription);
    shirts.setDescriptions(descriptions);
    categoryService.create(shirts);
    /**
     * Create a manufacturer
     */
    Manufacturer addidas = new Manufacturer();
    addidas.setMerchantStore(store);
    addidas.setCode("addidas");
    ManufacturerDescription addidasDesc = new ManufacturerDescription();
    addidasDesc.setLanguage(en);
    addidasDesc.setManufacturer(addidas);
    addidasDesc.setName("Addidas");
    addidas.getDescriptions().add(addidasDesc);
    manufacturerService.create(addidas);
    /**
     * Create an option
     */
    ProductOption option = new ProductOption();
    option.setMerchantStore(store);
    option.setCode("color");
    option.setProductOptionType(ProductOptionType.Radio.name());
    ProductOptionDescription optionDescription = new ProductOptionDescription();
    optionDescription.setLanguage(en);
    optionDescription.setName("Color");
    optionDescription.setDescription("Item color");
    optionDescription.setProductOption(option);
    option.getDescriptions().add(optionDescription);
    productOptionService.saveOrUpdate(option);
    /**
     * first option value *
     */
    ProductOptionValue white = new ProductOptionValue();
    white.setMerchantStore(store);
    white.setCode("white");
    ProductOptionValueDescription whiteDescription = new ProductOptionValueDescription();
    whiteDescription.setLanguage(en);
    whiteDescription.setName("White");
    whiteDescription.setDescription("White color");
    whiteDescription.setProductOptionValue(white);
    white.getDescriptions().add(whiteDescription);
    productOptionValueService.saveOrUpdate(white);
    ProductOptionValue black = new ProductOptionValue();
    black.setMerchantStore(store);
    black.setCode("black");
    /**
     * second option value *
     */
    ProductOptionValueDescription blackDesc = new ProductOptionValueDescription();
    blackDesc.setLanguage(en);
    blackDesc.setName("Black");
    blackDesc.setDescription("Black color");
    blackDesc.setProductOptionValue(black);
    black.getDescriptions().add(blackDesc);
    productOptionValueService.saveOrUpdate(black);
    /**
     * Create a complex product
     */
    Product product = new Product();
    product.setProductHeight(new BigDecimal(4));
    product.setProductLength(new BigDecimal(3));
    product.setProductWidth(new BigDecimal(1));
    product.setSku("XABC12");
    product.setManufacturer(addidas);
    product.setType(generalType);
    product.setMerchantStore(store);
    // Product description
    ProductDescription description = new ProductDescription();
    description.setName("Short sleeves shirt");
    description.setLanguage(en);
    description.setProduct(product);
    product.getDescriptions().add(description);
    product.getCategories().add(shirts);
    // availability
    ProductAvailability availability = new ProductAvailability();
    availability.setProductDateAvailable(new Date());
    availability.setProductQuantity(100);
    availability.setRegion("*");
    // associate with product
    availability.setProduct(product);
    // price
    ProductPrice dprice = new ProductPrice();
    dprice.setDefaultPrice(true);
    dprice.setProductPriceAmount(new BigDecimal(29.99));
    dprice.setProductAvailability(availability);
    ProductPriceDescription dpd = new ProductPriceDescription();
    dpd.setName("Base price");
    dpd.setProductPrice(dprice);
    dpd.setLanguage(en);
    dprice.getDescriptions().add(dpd);
    availability.getPrices().add(dprice);
    product.getAvailabilities().add(availability);
    // attributes
    // white
    ProductAttribute whiteAttribute = new ProductAttribute();
    whiteAttribute.setProduct(product);
    whiteAttribute.setProductOption(option);
    whiteAttribute.setAttributeDefault(true);
    // no price variation
    whiteAttribute.setProductAttributePrice(new BigDecimal(0));
    // no weight variation
    whiteAttribute.setProductAttributeWeight(new BigDecimal(0));
    whiteAttribute.setProductOption(option);
    whiteAttribute.setProductOptionValue(white);
    product.getAttributes().add(whiteAttribute);
    // black
    ProductAttribute blackAttribute = new ProductAttribute();
    blackAttribute.setProduct(product);
    blackAttribute.setProductOption(option);
    // 5 + dollars
    blackAttribute.setProductAttributePrice(new BigDecimal(5));
    // no weight variation
    blackAttribute.setProductAttributeWeight(new BigDecimal(0));
    blackAttribute.setProductOption(option);
    blackAttribute.setProductOptionValue(black);
    product.getAttributes().add(blackAttribute);
    productService.create(product);
    /**
     * Create Shopping cart *
     */
    ShoppingCart shoppingCart = new ShoppingCart();
    shoppingCart.setMerchantStore(store);
    UUID cartCode = UUID.randomUUID();
    shoppingCart.setShoppingCartCode(cartCode.toString());
    ShoppingCartItem item = new ShoppingCartItem(shoppingCart, product);
    item.setShoppingCart(shoppingCart);
    FinalPrice price = pricingService.calculateProductPrice(product);
    item.setItemPrice(price.getFinalPrice());
    item.setQuantity(1);
    /**
     * user selects black *
     */
    ShoppingCartAttributeItem attributeItem = new ShoppingCartAttributeItem(item, blackAttribute);
    item.getAttributes().add(attributeItem);
    shoppingCart.getLineItems().add(item);
    // create cart
    shoppingCartService.create(shoppingCart);
    /**
     * Retrieve cart *
     */
    ShoppingCart retrievedCart = shoppingCartService.getByCode(cartCode.toString(), store);
    Assert.assertNotNull(retrievedCart);
    /**
     * Delete cart *
     */
    shoppingCartService.delete(retrievedCart);
    /**
     * Check if cart has been deleted *
     */
    retrievedCart = shoppingCartService.getByCode(cartCode.toString(), store);
    Assert.assertNull(retrievedCart);
    // Clean up for other tests
    categoryService.delete(shirts);
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) Product(com.salesmanager.core.model.catalog.product.Product) ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ShoppingCartAttributeItem(com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem) ProductOptionDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription) ProductOptionValue(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue) 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) UUID(java.util.UUID) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription) HashSet(java.util.HashSet) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ProductOption(com.salesmanager.core.model.catalog.product.attribute.ProductOption) ProductType(com.salesmanager.core.model.catalog.product.type.ProductType) BigDecimal(java.math.BigDecimal) Date(java.util.Date) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ManufacturerDescription(com.salesmanager.core.model.catalog.product.manufacturer.ManufacturerDescription) CategoryDescription(com.salesmanager.core.model.catalog.category.CategoryDescription) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription) Test(org.junit.Test)

Example 17 with ProductOptionValueDescription

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

the class CatalogServiceHelper method setToLanguage.

/**
 * Filters descriptions and set the appropriate language
 * @param p
 * @param language
 */
public static void setToLanguage(Product p, int language) {
    Set<ProductAttribute> attributes = p.getAttributes();
    if (attributes != null) {
        for (ProductAttribute attribute : attributes) {
            ProductOption po = attribute.getProductOption();
            Set<ProductOptionDescription> spod = po.getDescriptions();
            if (spod != null) {
                Set<ProductOptionDescription> podDescriptions = new HashSet<ProductOptionDescription>();
                for (ProductOptionDescription pod : spod) {
                    // System.out.println("    ProductOptionDescription : " + pod.getProductOptionName());
                    if (pod.getLanguage().getId() == language) {
                        podDescriptions.add(pod);
                    }
                }
                po.setDescriptions(podDescriptions);
            }
            ProductOptionValue pov = attribute.getProductOptionValue();
            Set<ProductOptionValueDescription> spovd = pov.getDescriptions();
            if (spovd != null) {
                Set<ProductOptionValueDescription> povdDescriptions = new HashSet<>();
                for (ProductOptionValueDescription povd : spovd) {
                    if (povd.getLanguage().getId() == language) {
                        povdDescriptions.add(povd);
                    }
                }
                pov.setDescriptions(povdDescriptions);
            }
        }
    }
}
Also used : ProductOption(com.salesmanager.core.model.catalog.product.attribute.ProductOption) ProductOptionDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription) ProductOptionValue(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) HashSet(java.util.HashSet) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)

Example 18 with ProductOptionValueDescription

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

the class ShopProductController method display.

@SuppressWarnings("unchecked")
public String display(final String reference, final String friendlyUrl, Model model, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {
    MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
    Language language = (Language) request.getAttribute("LANGUAGE");
    Product product = productService.getBySeUrl(store, friendlyUrl, locale);
    if (product == null) {
        return PageBuilderUtils.build404(store);
    }
    ReadableProductPopulator populator = new ReadableProductPopulator();
    populator.setPricingService(pricingService);
    populator.setimageUtils(imageUtils);
    ReadableProduct productProxy = populator.populate(product, new ReadableProduct(), store, language);
    // meta information
    PageInformation pageInformation = new PageInformation();
    pageInformation.setPageDescription(productProxy.getDescription().getMetaDescription());
    pageInformation.setPageKeywords(productProxy.getDescription().getKeyWords());
    pageInformation.setPageTitle(productProxy.getDescription().getTitle());
    pageInformation.setPageUrl(productProxy.getDescription().getFriendlyUrl());
    if (productProxy.getImage() != null) {
        MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
        StringBuilder imageUrl = new StringBuilder(filePathUtils.buildStoreForwardedUri(merchantStore, request));
        imageUrl.append(productProxy.getImage().getImageUrl());
        pageInformation.setPageImageUrl(imageUrl.toString());
    }
    request.setAttribute(Constants.REQUEST_PAGE_INFORMATION, pageInformation);
    Breadcrumb breadCrumb = breadcrumbsUtils.buildProductBreadcrumb(reference, productProxy, store, language, request.getContextPath());
    request.getSession().setAttribute(Constants.BREADCRUMB, breadCrumb);
    request.setAttribute(Constants.BREADCRUMB, breadCrumb);
    StringBuilder relatedItemsCacheKey = new StringBuilder();
    relatedItemsCacheKey.append(store.getId()).append("_").append(Constants.RELATEDITEMS_CACHE_KEY).append("-").append(language.getCode());
    StringBuilder relatedItemsMissed = new StringBuilder();
    relatedItemsMissed.append(relatedItemsCacheKey.toString()).append(Constants.MISSED_CACHE_KEY);
    Map<Long, List<ReadableProduct>> relatedItemsMap = null;
    List<ReadableProduct> relatedItems = null;
    if (store.isUseCache()) {
        // get from the cache
        relatedItemsMap = (Map<Long, List<ReadableProduct>>) cache.getFromCache(relatedItemsCacheKey.toString());
        if (relatedItemsMap == null) {
            // get from missed cache
            // Boolean missedContent = (Boolean)cache.getFromCache(relatedItemsMissed.toString());
            // if(missedContent==null) {
            relatedItems = relatedItems(store, product, language);
            if (relatedItems != null) {
                relatedItemsMap = new HashMap<Long, List<ReadableProduct>>();
                relatedItemsMap.put(product.getId(), relatedItems);
                cache.putInCache(relatedItemsMap, relatedItemsCacheKey.toString());
            } else {
            // cache.putInCache(new Boolean(true), relatedItemsMissed.toString());
            }
        // }
        } else {
            relatedItems = relatedItemsMap.get(product.getId());
        }
    } else {
        relatedItems = relatedItems(store, product, language);
    }
    model.addAttribute("relatedProducts", relatedItems);
    Set<ProductAttribute> attributes = product.getAttributes();
    // split read only and options
    Map<Long, Attribute> readOnlyAttributes = null;
    Map<Long, Attribute> selectableOptions = null;
    if (!CollectionUtils.isEmpty(attributes)) {
        for (ProductAttribute attribute : attributes) {
            Attribute attr = null;
            AttributeValue attrValue = new AttributeValue();
            ProductOptionValue optionValue = attribute.getProductOptionValue();
            if (attribute.getAttributeDisplayOnly() == true) {
                // read only attribute
                if (readOnlyAttributes == null) {
                    readOnlyAttributes = new TreeMap<Long, Attribute>();
                }
                attr = readOnlyAttributes.get(attribute.getProductOption().getId());
                if (attr == null) {
                    attr = createAttribute(attribute, language);
                }
                if (attr != null) {
                    readOnlyAttributes.put(attribute.getProductOption().getId(), attr);
                    attr.setReadOnlyValue(attrValue);
                }
            } else {
                // selectable option
                if (selectableOptions == null) {
                    selectableOptions = new TreeMap<Long, Attribute>();
                }
                attr = selectableOptions.get(attribute.getProductOption().getId());
                if (attr == null) {
                    attr = createAttribute(attribute, language);
                }
                if (attr != null) {
                    selectableOptions.put(attribute.getProductOption().getId(), attr);
                }
            }
            attrValue.setDefaultAttribute(attribute.getAttributeDefault());
            // id of the attribute
            attrValue.setId(attribute.getId());
            attrValue.setLanguage(language.getCode());
            if (attribute.getProductAttributePrice() != null && attribute.getProductAttributePrice().doubleValue() > 0) {
                String formatedPrice = pricingService.getDisplayAmount(attribute.getProductAttributePrice(), store);
                attrValue.setPrice(formatedPrice);
            }
            if (!StringUtils.isBlank(attribute.getProductOptionValue().getProductOptionValueImage())) {
                attrValue.setImage(imageUtils.buildProductPropertyImageUtils(store, attribute.getProductOptionValue().getProductOptionValueImage()));
            }
            attrValue.setSortOrder(0);
            if (attribute.getProductOptionSortOrder() != null) {
                attrValue.setSortOrder(attribute.getProductOptionSortOrder().intValue());
            }
            List<ProductOptionValueDescription> descriptions = optionValue.getDescriptionsSettoList();
            ProductOptionValueDescription description = null;
            if (descriptions != null && descriptions.size() > 0) {
                description = descriptions.get(0);
                if (descriptions.size() > 1) {
                    for (ProductOptionValueDescription optionValueDescription : descriptions) {
                        if (optionValueDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
                            description = optionValueDescription;
                            break;
                        }
                    }
                }
            }
            attrValue.setName(description.getName());
            attrValue.setDescription(description.getDescription());
            List<AttributeValue> attrs = attr.getValues();
            if (attrs == null) {
                attrs = new ArrayList<AttributeValue>();
                attr.setValues(attrs);
            }
            attrs.add(attrValue);
        }
    }
    List<ProductReview> reviews = productReviewService.getByProduct(product, language);
    if (!CollectionUtils.isEmpty(reviews)) {
        List<ReadableProductReview> revs = new ArrayList<ReadableProductReview>();
        ReadableProductReviewPopulator reviewPopulator = new ReadableProductReviewPopulator();
        for (ProductReview review : reviews) {
            ReadableProductReview rev = new ReadableProductReview();
            reviewPopulator.populate(review, rev, store, language);
            revs.add(rev);
        }
        model.addAttribute("reviews", revs);
    }
    List<Attribute> attributesList = null;
    if (readOnlyAttributes != null) {
        attributesList = new ArrayList<Attribute>(readOnlyAttributes.values());
    }
    List<Attribute> optionsList = null;
    if (selectableOptions != null) {
        optionsList = new ArrayList<Attribute>(selectableOptions.values());
        // order attributes by sort order
        for (Attribute attr : optionsList) {
            Collections.sort(attr.getValues(), new Comparator<AttributeValue>() {

                public int compare(AttributeValue o1, AttributeValue o2) {
                    if (o1.getSortOrder() == o2.getSortOrder())
                        return 0;
                    return o1.getSortOrder() < o2.getSortOrder() ? -1 : 1;
                }
            });
        }
    }
    model.addAttribute("attributes", attributesList);
    model.addAttribute("options", optionsList);
    model.addAttribute("product", productProxy);
    /**
     * template *
     */
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Product.product).append(".").append(store.getStoreTemplate());
    return template.toString();
}
Also used : AttributeValue(com.salesmanager.shop.store.model.catalog.AttributeValue) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Attribute(com.salesmanager.shop.store.model.catalog.Attribute) ReadableProductReview(com.salesmanager.shop.model.catalog.product.ReadableProductReview) ArrayList(java.util.ArrayList) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview) ReadableProductReview(com.salesmanager.shop.model.catalog.product.ReadableProductReview) ReadableProductReviewPopulator(com.salesmanager.shop.populator.catalog.ReadableProductReviewPopulator) ProductOptionValue(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue) Language(com.salesmanager.core.model.reference.language.Language) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) PageInformation(com.salesmanager.shop.model.shop.PageInformation) List(java.util.List) ArrayList(java.util.ArrayList) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) Breadcrumb(com.salesmanager.shop.model.shop.Breadcrumb) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)

Aggregations

ProductOptionValueDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)18 ProductOptionDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription)12 ProductOptionValue (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue)10 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)9 Language (com.salesmanager.core.model.reference.language.Language)9 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)8 ProductOption (com.salesmanager.core.model.catalog.product.attribute.ProductOption)7 ArrayList (java.util.ArrayList)7 Category (com.salesmanager.core.model.catalog.category.Category)5 Product (com.salesmanager.core.model.catalog.product.Product)5 ProductDescription (com.salesmanager.core.model.catalog.product.description.ProductDescription)5 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)4 ProductPrice (com.salesmanager.core.model.catalog.product.price.ProductPrice)4 ProductPriceDescription (com.salesmanager.core.model.catalog.product.price.ProductPriceDescription)4 ReadableProductProperty (com.salesmanager.shop.model.catalog.product.attribute.ReadableProductProperty)4 ReadableProductPropertyValue (com.salesmanager.shop.model.catalog.product.attribute.ReadableProductPropertyValue)4 BigDecimal (java.math.BigDecimal)4 ProductImage (com.salesmanager.core.model.catalog.product.image.ProductImage)3 ManufacturerDescription (com.salesmanager.core.model.catalog.product.manufacturer.ManufacturerDescription)3 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)3