use of com.salesmanager.shop.model.catalog.ProductList 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;
}
use of com.salesmanager.shop.model.catalog.ProductList in project shopizer by shopizer-ecommerce.
the class ShoppingCategoryController method getProducts.
/**
* Returns an array of products belonging to a given category
* in a given language for a given store
* url example : http://<host>/sm-shop/shop/services/public/products/DEFAULT/BOOKS
* @param store
* @param language
* @param category
* @param model
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping("/services/public/products/{store}/{language}/{category}")
@ResponseBody
public ProductList getProducts(@PathVariable final String store, @PathVariable final String language, @PathVariable final String category, Model model, 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;
}
// get the category by code
Category cat = categoryService.getBySeUrl(merchantStore, category);
if (cat == null) {
LOGGER.error("Category with friendly url " + category + " is null");
// TODO localized message
response.sendError(503, "Category is 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());
Language lang = langs.get(language);
if (lang == null) {
lang = langs.get(Constants.DEFAULT_LANGUAGE);
}
List<com.salesmanager.core.model.catalog.product.Product> products = productService.getProducts(ids, lang);
ProductList productList = new ProductList();
ReadableProductPopulator populator = new ReadableProductPopulator();
populator.setPricingService(pricingService);
populator.setimageUtils(imageUtils);
for (Product product : products) {
// create new proxy product
ReadableProduct p = populator.populate(product, new ReadableProduct(), merchantStore, lang);
productList.getProducts().add(p);
}
productList.setProductCount(productList.getProducts().size());
return productList;
} catch (Exception e) {
LOGGER.error("Error while getting category", e);
response.sendError(503, "Error while getting category");
}
return null;
}
Aggregations