Search in sources :

Example 1 with QueryFilter

use of com.salesmanager.shop.store.model.filter.QueryFilter 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 2 with QueryFilter

use of com.salesmanager.shop.store.model.filter.QueryFilter in project shopizer by shopizer-ecommerce.

the class ShopProductRESTController method getProducts.

private ReadableProductList getProducts(final int start, final int max, final String store, final String language, final String category, final List<QueryFilter> filters, HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {
        /**
         * How to Spring MVC Rest web service - ajax / jquery
         * http://codetutr.com/2013/04/09/spring-mvc-easy-rest-based-json-services-with-responsebody/
         */
        MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
        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;
        }
        Language lang = langs.get(language);
        if (lang == null) {
            lang = langs.get(Constants.DEFAULT_LANGUAGE);
        }
        ProductCriteria productCriteria = new ProductCriteria();
        productCriteria.setMaxCount(max);
        productCriteria.setStartIndex(start);
        // get the category by code
        if (!StringUtils.isBlank(category)) {
            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()).append(cat.getId()).append("/").toString();
            List<Category> categories = categoryService.getListByLineage(store, lineage);
            List<Long> ids = new ArrayList<Long>();
            if (categories != null && categories.size() > 0) {
                for (Category c : categories) {
                    ids.add(c.getId());
                }
            }
            ids.add(cat.getId());
            productCriteria.setCategoryIds(ids);
        }
        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);
        ReadableProductList productList = new ReadableProductList();
        for (Product product : products.getProducts()) {
            // create new proxy product
            ReadableProduct readProduct = populator.populate(product, new ReadableProduct(), merchantStore, lang);
            productList.getProducts().add(readProduct);
        }
        productList.setTotalPages(Math.toIntExact(products.getTotalCount()));
        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 : ProductCriteria(com.salesmanager.core.model.catalog.product.ProductCriteria) Category(com.salesmanager.core.model.catalog.category.Category) ReadableProductList(com.salesmanager.shop.model.catalog.product.ReadableProductList) 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) QueryFilter(com.salesmanager.shop.store.model.filter.QueryFilter) Language(com.salesmanager.core.model.reference.language.Language) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore)

Example 3 with QueryFilter

use of com.salesmanager.shop.store.model.filter.QueryFilter in project shopizer by shopizer-ecommerce.

the class ShopProductRESTController method getProductsFilteredByType.

/*	*/
/**
 * Will get products for a given category
 * supports language by setting land as a query parameter
 * supports paging by adding start and max as query parameters
 * @param store
 * @param language
 * @param category
 * @param model
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
/*
	@RequestMapping("/public/products/page/{start}/{max}/{store}/{language}/{category}.html")
	@ResponseBody
	public ReadableProductList getProducts(@PathVariable String store, @PathVariable final String category, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		
		*/
/**
 * default routine *
 */
/*
		
		MerchantStore merchantStore = (MerchantStore)request.getAttribute(Constants.MERCHANT_STORE);
		if(merchantStore!=null) {
			if(!merchantStore.getCode().equals(store)) {
				merchantStore = null;
			}
		}
		
		if(merchantStore== null) {
			merchantStore = merchantStoreService.getByCode(store);
		}
		
		if(merchantStore==null) {
			LOGGER.error("Merchant store is null for code " + store);
			response.sendError(503, "Merchant store is null for code " + store);
			return null;
		}
		
		Language language = merchantStore.getDefaultLanguage();
		
		String lang = language.getCode();
		
		if(!StringUtils.isBlank(request.getParameter(Constants.LANG))) {
			
			lang = request.getParameter(Constants.LANG);
			
		}
		
		
		*/
/**
 * end default routine *
 */
/*
		
		
		//start
		int iStart = 0;
		if(!StringUtils.isBlank(request.getParameter(Constants.START))) {
			
			String start = request.getParameter(Constants.START);
			
			try {
				iStart = Integer.parseInt(start);
			} catch(Exception e) {
				LOGGER.error("Cannot parse start parameter " + start);
			}

		}
		
		//max
		int iMax = 0;
		if(!StringUtils.isBlank(request.getParameter(Constants.MAX))) {
			
			String max = request.getParameter(Constants.MAX);
			
			try {
				iMax = Integer.parseInt(max);
			} catch(Exception e) {
				LOGGER.error("Cannot parse max parameter " + max);
			}

		}

		
		return this.getProducts(iStart, iMax, store, lang, category, null, request, response);
	}*/
/**
 * An entry point for filtering by another entity such as Manufacturer
 * filter=BRAND&filter-value=123
 * @param start
 * @param max
 * @param store
 * @param language
 * @param category
 * @param filterType
 * @param filterValue
 * @param model
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping("/products/public/page/{start}/{max}/{store}/{language}/{category}.html/filter={filterType}/filter-value={filterValue}")
@ResponseBody
public ReadableProductList getProductsFilteredByType(@PathVariable int start, @PathVariable int max, @PathVariable String store, @PathVariable final String language, @PathVariable final String category, @PathVariable final String filterType, @PathVariable final String filterValue, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    List<QueryFilter> queryFilters = null;
    try {
        if (filterType.equals(QueryFilterType.BRAND.name())) {
            // the only one implemented so far
            QueryFilter filter = new QueryFilter();
            filter.setFilterType(QueryFilterType.BRAND);
            filter.setFilterId(Long.parseLong(filterValue));
            if (queryFilters == null) {
                queryFilters = new ArrayList<QueryFilter>();
            }
            queryFilters.add(filter);
        }
    } catch (Exception e) {
        LOGGER.error("Invalid filter or filter-value " + filterType + " - " + filterValue, e);
    }
    return this.getProducts(start, max, store, language, category, queryFilters, request, response);
}
Also used : QueryFilter(com.salesmanager.shop.store.model.filter.QueryFilter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with QueryFilter

use of com.salesmanager.shop.store.model.filter.QueryFilter in project shopizer by shopizer-ecommerce.

the class ShoppingCategoryController method getProductsFilteredByType.

/**
 * An entry point for filtering by another entity such as Manufacturer
 * filter=BRAND&filter-value=123
 * @param start
 * @param max
 * @param store
 * @param language
 * @param category
 * @param filterType
 * @param filterValue
 * @param model
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping("/services/public/products/page/{start}/{max}/{store}/{language}/{category}/filter={filterType}/filter-value={filterValue}")
@ResponseBody
public ProductList getProductsFilteredByType(@PathVariable int start, @PathVariable int max, @PathVariable String store, @PathVariable final String language, @PathVariable final String category, @PathVariable final String filterType, @PathVariable final String filterValue, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    List<QueryFilter> queryFilters = null;
    try {
        if (filterType.equals(QueryFilterType.BRAND.name())) {
            // the only one implemented so far
            QueryFilter filter = new QueryFilter();
            filter.setFilterType(QueryFilterType.BRAND);
            filter.setFilterId(Long.parseLong(filterValue));
            if (queryFilters == null) {
                queryFilters = new ArrayList<QueryFilter>();
            }
            queryFilters.add(filter);
        }
    } catch (Exception e) {
        LOGGER.error("Invalid filter or filter-value " + filterType + " - " + filterValue, e);
    }
    return this.getProducts(start, max, store, language, category, queryFilters, model, request, response);
}
Also used : QueryFilter(com.salesmanager.shop.store.model.filter.QueryFilter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

QueryFilter (com.salesmanager.shop.store.model.filter.QueryFilter)4 Category (com.salesmanager.core.model.catalog.category.Category)2 Product (com.salesmanager.core.model.catalog.product.Product)2 ProductCriteria (com.salesmanager.core.model.catalog.product.ProductCriteria)2 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)2 Language (com.salesmanager.core.model.reference.language.Language)2 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)2 ReadableProductPopulator (com.salesmanager.shop.populator.catalog.ReadableProductPopulator)2 ArrayList (java.util.ArrayList)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ProductList (com.salesmanager.shop.model.catalog.ProductList)1 ReadableCategory (com.salesmanager.shop.model.catalog.category.ReadableCategory)1 ReadableProductList (com.salesmanager.shop.model.catalog.product.ReadableProductList)1 BigDecimal (java.math.BigDecimal)1