use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class ShoppingCategoryController method getCategories.
/**
* Returns all categories for a given MerchantStore
*/
@RequestMapping("/services/public/category/{store}/{language}")
@ResponseBody
public List<ReadableCategory> getCategories(@PathVariable final String language, @PathVariable final String store, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Language> langs = languageService.getLanguagesMap();
Language l = langs.get(language);
if (l == null) {
l = languageService.getByCode(Constants.DEFAULT_LANGUAGE);
}
MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
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;
}
List<Category> categories = categoryService.listByStore(merchantStore, l);
ReadableCategoryPopulator populator = new ReadableCategoryPopulator();
List<ReadableCategory> returnCategories = new ArrayList<ReadableCategory>();
for (Category category : categories) {
ReadableCategory categoryProxy = populator.populate(category, new ReadableCategory(), merchantStore, l);
returnCategories.add(categoryProxy);
}
return returnCategories;
}
use of com.salesmanager.core.model.catalog.category.Category 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