Search in sources :

Example 11 with Product

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

the class ShoppingCategoryController method getProducts.

private ProductList getProducts(final int start, final int max, final String store, final String language, final String category, final List<QueryFilter> filters, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {
        MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
        List<BigDecimal> prices = new ArrayList<BigDecimal>();
        String ref = "";
        if (request.getParameter("ref") != null) {
            ref = request.getParameter("ref");
        }
        request.setAttribute("ref", ref);
        Map<String, Language> langs = languageService.getLanguagesMap();
        if (merchantStore != null) {
            if (!merchantStore.getCode().equals(store)) {
                // reset for the current request
                merchantStore = null;
            }
        }
        if (merchantStore == null) {
            merchantStore = merchantStoreService.getByCode(store);
        }
        if (merchantStore == null) {
            LOGGER.error("Merchant store is null for code " + store);
            // TODO localized message
            response.sendError(503, "Merchant store is null for code " + store);
            return null;
        }
        // get the category by code
        Category cat = categoryService.getBySeUrl(merchantStore, category);
        if (cat == null) {
            LOGGER.error("Category " + category + " is null");
            // TODO localized message
            response.sendError(503, "Category is null");
            return null;
        }
        String lineage = new StringBuilder().append(cat.getLineage()).toString();
        List<Category> categories = categoryService.getListByLineage(store, lineage);
        List<Long> ids = new ArrayList<Long>();
        if (categories != null && categories.size() > 0) {
            for (Category c : categories) {
                if (c.isVisible()) {
                    ids.add(c.getId());
                }
            }
        }
        ids.add(cat.getId());
        Language lang = langs.get(language);
        if (lang == null) {
            lang = langs.get(Constants.DEFAULT_LANGUAGE);
        }
        ProductCriteria productCriteria = new ProductCriteria();
        productCriteria.setMaxCount(max);
        productCriteria.setStartIndex(start);
        productCriteria.setCategoryIds(ids);
        productCriteria.setAvailable(true);
        if (filters != null) {
            for (QueryFilter filter : filters) {
                if (filter.getFilterType().name().equals(QueryFilterType.BRAND.name())) {
                    // the only filter implemented
                    productCriteria.setManufacturerId(filter.getFilterId());
                }
            }
        }
        com.salesmanager.core.model.catalog.product.ProductList products = productService.listByStore(merchantStore, lang, productCriteria);
        ReadableProductPopulator populator = new ReadableProductPopulator();
        populator.setPricingService(pricingService);
        populator.setimageUtils(imageUtils);
        ProductList productList = new ProductList();
        for (Product product : products.getProducts()) {
            // create new proxy product
            ReadableProduct p = populator.populate(product, new ReadableProduct(), merchantStore, lang);
            productList.getProducts().add(p);
            prices.add(p.getPrice());
        }
        /**
         * order products based on the specified order *
         */
        Collections.sort(productList.getProducts(), new Comparator<ReadableProduct>() {

            @Override
            public int compare(ReadableProduct o1, ReadableProduct o2) {
                int order1 = o1.getSortOrder();
                int order2 = o2.getSortOrder();
                return order1 - order2;
            }
        });
        productList.setProductCount(Math.toIntExact(products.getTotalCount()));
        if (CollectionUtils.isNotEmpty(prices)) {
            BigDecimal minPrice = (BigDecimal) Collections.min(prices);
            BigDecimal maxPrice = (BigDecimal) Collections.max(prices);
            if (minPrice != null && maxPrice != null) {
                productList.setMinPrice(minPrice);
                productList.setMaxPrice(maxPrice);
            }
        }
        return productList;
    } catch (Exception e) {
        LOGGER.error("Error while getting products", e);
        response.sendError(503, "An error occured while retrieving products " + e.getMessage());
    }
    return null;
}
Also used : ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) Category(com.salesmanager.core.model.catalog.category.Category) ArrayList(java.util.ArrayList) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) Language(com.salesmanager.core.model.reference.language.Language) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ProductCriteria(com.salesmanager.core.model.catalog.product.ProductCriteria) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) BigDecimal(java.math.BigDecimal) ProductList(com.salesmanager.shop.model.catalog.ProductList) QueryFilter(com.salesmanager.shop.store.model.filter.QueryFilter)

Example 12 with Product

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

the class ShoppingCartController method shoppingCart.

private String shoppingCart(final Model model, final HttpServletRequest request, final HttpServletResponse response, final Locale locale) throws Exception {
    LOG.debug("Starting to calculate shopping cart...");
    Language language = (Language) request.getAttribute(Constants.LANGUAGE);
    // meta information
    PageInformation pageInformation = new PageInformation();
    pageInformation.setPageTitle(messages.getMessage("label.cart.placeorder", locale));
    request.setAttribute(Constants.REQUEST_PAGE_INFORMATION, pageInformation);
    MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
    Customer customer = getSessionAttribute(Constants.CUSTOMER, request);
    /**
     * there must be a cart in the session *
     */
    String cartCode = (String) request.getSession().getAttribute(Constants.SHOPPING_CART);
    if (StringUtils.isBlank(cartCode)) {
        // display empty cart
        StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.ShoppingCart.shoppingCart).append(".").append(store.getStoreTemplate());
        return template.toString();
    }
    ShoppingCartData shoppingCart = shoppingCartFacade.getShoppingCartData(customer, store, cartCode, language);
    if (shoppingCart == null) {
        // display empty cart
        StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.ShoppingCart.shoppingCart).append(".").append(store.getStoreTemplate());
        return template.toString();
    }
    Language lang = languageUtils.getRequestLanguage(request, response);
    // Filter unavailables
    List<ShoppingCartItem> unavailables = new ArrayList<ShoppingCartItem>();
    List<ShoppingCartItem> availables = new ArrayList<ShoppingCartItem>();
    // Take out items no more available
    List<ShoppingCartItem> items = shoppingCart.getShoppingCartItems();
    for (ShoppingCartItem item : items) {
        String code = item.getProductCode();
        Product p = productService.getByCode(code, lang);
        if (!p.isAvailable()) {
            unavailables.add(item);
        } else {
            availables.add(item);
        }
    }
    shoppingCart.setShoppingCartItems(availables);
    shoppingCart.setUnavailables(unavailables);
    model.addAttribute("cart", shoppingCart);
    /**
     * template *
     */
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.ShoppingCart.shoppingCart).append(".").append(store.getStoreTemplate());
    return template.toString();
}
Also used : Language(com.salesmanager.core.model.reference.language.Language) PageInformation(com.salesmanager.shop.model.shop.PageInformation) Customer(com.salesmanager.core.model.customer.Customer) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ShoppingCartData(com.salesmanager.shop.model.shoppingcart.ShoppingCartData)

Example 13 with Product

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

the class ShoppingCartController method displayShoppingCart.

@RequestMapping(value = { "/shoppingCartByCode" }, method = { RequestMethod.GET })
public String displayShoppingCart(@ModelAttribute String shoppingCartCode, final Model model, HttpServletRequest request, HttpServletResponse response, final Locale locale) throws Exception {
    MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
    Customer customer = getSessionAttribute(Constants.CUSTOMER, request);
    Language language = (Language) request.getAttribute(Constants.LANGUAGE);
    if (StringUtils.isBlank(shoppingCartCode)) {
        return "redirect:/shop";
    }
    ShoppingCartData cart = shoppingCartFacade.getShoppingCartData(customer, merchantStore, shoppingCartCode, language);
    if (cart == null) {
        return "redirect:/shop";
    }
    Language lang = languageUtils.getRequestLanguage(request, response);
    // Filter unavailables
    List<ShoppingCartItem> unavailables = new ArrayList<ShoppingCartItem>();
    List<ShoppingCartItem> availables = new ArrayList<ShoppingCartItem>();
    // Take out items no more available
    List<ShoppingCartItem> items = cart.getShoppingCartItems();
    for (ShoppingCartItem item : items) {
        String code = item.getProductCode();
        Product p = productService.getByCode(code, lang);
        if (!p.isAvailable()) {
            unavailables.add(item);
        } else {
            availables.add(item);
        }
    }
    cart.setShoppingCartItems(availables);
    cart.setUnavailables(unavailables);
    // meta information
    PageInformation pageInformation = new PageInformation();
    pageInformation.setPageTitle(messages.getMessage("label.cart.placeorder", locale));
    request.setAttribute(Constants.REQUEST_PAGE_INFORMATION, pageInformation);
    request.getSession().setAttribute(Constants.SHOPPING_CART, cart.getCode());
    model.addAttribute("cart", cart);
    /**
     * template *
     */
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.ShoppingCart.shoppingCart).append(".").append(merchantStore.getStoreTemplate());
    return template.toString();
}
Also used : Language(com.salesmanager.core.model.reference.language.Language) Customer(com.salesmanager.core.model.customer.Customer) PageInformation(com.salesmanager.shop.model.shop.PageInformation) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ShoppingCartData(com.salesmanager.shop.model.shoppingcart.ShoppingCartData) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with Product

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

the class ProductFacadeImpl method updateProduct.

public void updateProduct(MerchantStore store, PersistableProduct product, Language language) {
    Validate.notNull(product, "Product must not be null");
    Validate.notNull(product.getId(), "Product id must not be null");
    // get original product
    Product productModel = productService.getById(product.getId());
// merge original product with persistable product
/*
		 * String manufacturer = Manufacturer.DEFAULT_MANUFACTURER; if
		 * (product.getProductSpecifications() != null) { manufacturer =
		 * product.getProductSpecifications().getManufacturer(); } else {
		 * ProductSpecification specifications = new ProductSpecification();
		 * specifications.setManufacturer(manufacturer); }
		 *
		 * Product target = null; if (product.getId() != null &&
		 * product.getId().longValue() > 0) { target =
		 * productService.getById(product.getId()); } else { target = new
		 * Product(); }
		 *
		 *
		 * try { persistableProductPopulator.populate(product, target, store,
		 * language); productService.create(target);
		 * product.setId(target.getId()); return product; } catch (Exception e)
		 * { throw new ServiceRuntimeException(e); }
		 */
}
Also used : PersistableProduct(com.salesmanager.shop.model.catalog.product.PersistableProduct) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) LightPersistableProduct(com.salesmanager.shop.model.catalog.product.LightPersistableProduct)

Example 15 with Product

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

the class ShopProductController method relatedItems.

private List<ReadableProduct> relatedItems(MerchantStore store, Product product, Language language) throws Exception {
    ReadableProductPopulator populator = new ReadableProductPopulator();
    populator.setPricingService(pricingService);
    populator.setimageUtils(imageUtils);
    List<ProductRelationship> relatedItems = productRelationshipService.getByType(store, product, ProductRelationshipType.RELATED_ITEM);
    if (relatedItems != null && relatedItems.size() > 0) {
        List<ReadableProduct> items = new ArrayList<ReadableProduct>();
        for (ProductRelationship relationship : relatedItems) {
            Product relatedProduct = relationship.getRelatedProduct();
            ReadableProduct proxyProduct = populator.populate(relatedProduct, new ReadableProduct(), store, language);
            items.add(proxyProduct);
        }
        return items;
    }
    return null;
}
Also used : ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) ProductRelationship(com.salesmanager.core.model.catalog.product.relationship.ProductRelationship) ArrayList(java.util.ArrayList) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct)

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