Search in sources :

Example 1 with ProductCriteria

use of com.salesmanager.core.model.catalog.product.ProductCriteria 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 ProductCriteria

use of com.salesmanager.core.model.catalog.product.ProductCriteria 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 ProductCriteria

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

the class SearchFacadeImpl method convertToSearchProductList.

@Override
public SearchProductList convertToSearchProductList(SearchResponse searchResponse, MerchantStore merchantStore, int start, int count, Language language) {
    SearchProductList returnList = new SearchProductList();
    List<SearchEntry> entries = searchResponse.getEntries();
    if (CollectionUtils.isNotEmpty(entries)) {
        List<Long> ids = entries.stream().map(SearchEntry::getIndexProduct).map(IndexProduct::getId).map(Long::parseLong).collect(Collectors.toList());
        ProductCriteria searchCriteria = new ProductCriteria();
        searchCriteria.setMaxCount(count);
        searchCriteria.setStartIndex(start);
        searchCriteria.setProductIds(ids);
        searchCriteria.setAvailable(true);
        ProductList productList = productService.listByStore(merchantStore, language, searchCriteria);
        List<ReadableProduct> readableProducts = productList.getProducts().stream().map(product -> convertProductToReadableProduct(product, merchantStore, language)).collect(Collectors.toList());
        returnList.getProducts().addAll(readableProducts);
        returnList.setProductCount(productList.getProducts().size());
    }
    // Facets
    Map<String, List<SearchFacet>> facets = Optional.ofNullable(searchResponse.getFacets()).orElse(Collections.emptyMap());
    List<ReadableCategory> categoryProxies = getCategoryFacets(merchantStore, language, facets);
    returnList.setCategoryFacets(categoryProxies);
    List<SearchFacet> manufacturersFacets = facets.entrySet().stream().filter(e -> MANUFACTURER_FACET_NAME.equals(e.getKey())).findFirst().map(Entry::getValue).orElse(Collections.emptyList());
    if (CollectionUtils.isNotEmpty(manufacturersFacets)) {
    // TODO add manufacturer facets
    }
    return returnList;
}
Also used : ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) Async(org.springframework.scheduling.annotation.Async) LoggerFactory(org.slf4j.LoggerFactory) AutoCompleteRequest(com.salesmanager.shop.store.model.search.AutoCompleteRequest) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ServiceException(com.salesmanager.core.business.exception.ServiceException) Inject(javax.inject.Inject) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) Service(org.springframework.stereotype.Service) Map(java.util.Map) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) ReadableCategoryPopulator(com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator) CategoryService(com.salesmanager.core.business.services.catalog.category.CategoryService) SearchKeywords(com.salesmanager.core.model.search.SearchKeywords) SearchProductRequest(com.salesmanager.shop.model.catalog.SearchProductRequest) SearchFacet(com.salesmanager.core.model.search.SearchFacet) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ProductCriteria(com.salesmanager.core.model.catalog.product.ProductCriteria) Logger(org.slf4j.Logger) SearchService(com.salesmanager.core.business.services.search.SearchService) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) ProductList(com.salesmanager.core.model.catalog.product.ProductList) Collectors(java.util.stream.Collectors) SearchEntry(com.salesmanager.core.model.search.SearchEntry) Category(com.salesmanager.core.model.catalog.category.Category) List(java.util.List) ValueList(com.salesmanager.shop.model.entity.ValueList) CoreConfiguration(com.salesmanager.core.business.utils.CoreConfiguration) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) SearchProductList(com.salesmanager.shop.model.catalog.SearchProductList) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) Entry(java.util.Map.Entry) SearchResponse(com.salesmanager.core.model.search.SearchResponse) Optional(java.util.Optional) ConversionException(com.salesmanager.core.business.exception.ConversionException) Collections(java.util.Collections) IndexProduct(com.salesmanager.core.model.search.IndexProduct) ProductCriteria(com.salesmanager.core.model.catalog.product.ProductCriteria) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) IndexProduct(com.salesmanager.core.model.search.IndexProduct) ProductList(com.salesmanager.core.model.catalog.product.ProductList) SearchProductList(com.salesmanager.shop.model.catalog.SearchProductList) SearchFacet(com.salesmanager.core.model.search.SearchFacet) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ProductList(com.salesmanager.core.model.catalog.product.ProductList) List(java.util.List) ValueList(com.salesmanager.shop.model.entity.ValueList) SearchProductList(com.salesmanager.shop.model.catalog.SearchProductList) SearchEntry(com.salesmanager.core.model.search.SearchEntry) SearchProductList(com.salesmanager.shop.model.catalog.SearchProductList)

Example 4 with ProductCriteria

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

the class ProductApi method list.

/**
 * Filtering product lists based on product attributes ?category=1
 * &manufacturer=2 &type=... &lang=en|fr NOT REQUIRED, will use request language
 * &start=0 NOT REQUIRED, can be used for pagination &count=10 NOT REQUIRED, can
 * be used to limit item count
 *
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/products", method = RequestMethod.GET)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableProductList list(@RequestParam(value = "lang", required = false) String lang, @RequestParam(value = "category", required = false) Long category, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "sku", required = false) String sku, @RequestParam(value = "manufacturer", required = false) Long manufacturer, @RequestParam(value = "optionValues", required = false) List<Long> optionValueIds, @RequestParam(value = "status", required = false) String status, @RequestParam(value = "owner", required = false) Long owner, // current
@RequestParam(value = "page", required = false, defaultValue = "0") Integer page, @RequestParam(value = "origin", required = false, defaultValue = ProductCriteria.ORIGIN_SHOP) String origin, // count
@RequestParam(value = "count", required = false, defaultValue = "100") Integer count, // page
@ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response) throws Exception {
    ProductCriteria criteria = new ProductCriteria();
    criteria.setOrigin(origin);
    // do not use legacy pagination anymore
    if (lang != null) {
        criteria.setLanguage(lang);
    } else {
        criteria.setLanguage(language.getCode());
    }
    if (!StringUtils.isBlank(status)) {
        criteria.setStatus(status);
    }
    if (category != null) {
        List<Long> categoryIds = new ArrayList<Long>();
        categoryIds.add(category);
        criteria.setCategoryIds(categoryIds);
    }
    if (manufacturer != null) {
        criteria.setManufacturerId(manufacturer);
    }
    if (CollectionUtils.isNotEmpty(optionValueIds)) {
        criteria.setOptionValueIds(optionValueIds);
    }
    if (owner != null) {
        criteria.setOwnerId(owner);
    }
    if (page != null) {
        criteria.setStartPage(page);
    }
    if (count != null) {
        criteria.setMaxCount(count);
    }
    if (!StringUtils.isBlank(name)) {
        criteria.setProductName(name);
    }
    if (!StringUtils.isBlank(sku)) {
        criteria.setCode(sku);
    }
    try {
        return productFacade.getProductListsByCriterias(merchantStore, language, criteria);
    } catch (Exception e) {
        LOGGER.error("Error while filtering products product", e);
        try {
            response.sendError(503, "Error while filtering products " + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : ProductCriteria(com.salesmanager.core.model.catalog.product.ProductCriteria) ArrayList(java.util.ArrayList) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) IOException(java.io.IOException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with ProductCriteria

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

the class ProductItemsFacadeImpl method listItemsByIds.

@Override
public ReadableProductList listItemsByIds(MerchantStore store, Language language, List<Long> ids, int startCount, int maxCount) throws Exception {
    if (CollectionUtils.isEmpty(ids)) {
        return new ReadableProductList();
    }
    ProductCriteria productCriteria = new ProductCriteria();
    productCriteria.setMaxCount(maxCount);
    productCriteria.setStartIndex(startCount);
    productCriteria.setProductIds(ids);
    com.salesmanager.core.model.catalog.product.ProductList products = productService.listByStore(store, language, 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(), store, language);
        productList.getProducts().add(readProduct);
    }
    productList.setNumber(Math.toIntExact(products.getTotalCount()));
    productList.setRecordsTotal(new Long(products.getTotalCount()));
    return productList;
}
Also used : ProductCriteria(com.salesmanager.core.model.catalog.product.ProductCriteria) ReadableProductList(com.salesmanager.shop.model.catalog.product.ReadableProductList) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) 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

ProductCriteria (com.salesmanager.core.model.catalog.product.ProductCriteria)7 Product (com.salesmanager.core.model.catalog.product.Product)6 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)5 ReadableProductPopulator (com.salesmanager.shop.populator.catalog.ReadableProductPopulator)5 Category (com.salesmanager.core.model.catalog.category.Category)4 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)4 Language (com.salesmanager.core.model.reference.language.Language)4 ReadableProductList (com.salesmanager.shop.model.catalog.product.ReadableProductList)3 ReadableCategory (com.salesmanager.shop.model.catalog.category.ReadableCategory)2 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)2 QueryFilter (com.salesmanager.shop.store.model.filter.QueryFilter)2 ArrayList (java.util.ArrayList)2 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 ServiceException (com.salesmanager.core.business.exception.ServiceException)1 CategoryService (com.salesmanager.core.business.services.catalog.category.CategoryService)1 PricingService (com.salesmanager.core.business.services.catalog.product.PricingService)1 ProductService (com.salesmanager.core.business.services.catalog.product.ProductService)1 SearchService (com.salesmanager.core.business.services.search.SearchService)1 CoreConfiguration (com.salesmanager.core.business.utils.CoreConfiguration)1 CategoryDescription (com.salesmanager.core.model.catalog.category.CategoryDescription)1