Search in sources :

Example 51 with Category

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;
}
Also used : ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) Category(com.salesmanager.core.model.catalog.category.Category) Language(com.salesmanager.core.model.reference.language.Language) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ArrayList(java.util.ArrayList) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ReadableCategoryPopulator(com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 52 with Category

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;
}
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) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) ProductList(com.salesmanager.shop.model.catalog.ProductList) Language(com.salesmanager.core.model.reference.language.Language) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Category (com.salesmanager.core.model.catalog.category.Category)52 Language (com.salesmanager.core.model.reference.language.Language)30 ReadableCategory (com.salesmanager.shop.model.catalog.category.ReadableCategory)28 ArrayList (java.util.ArrayList)28 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)26 Product (com.salesmanager.core.model.catalog.product.Product)20 ServiceException (com.salesmanager.core.business.exception.ServiceException)15 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)14 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)14 ConversionException (com.salesmanager.core.business.exception.ConversionException)13 ProductDescription (com.salesmanager.core.model.catalog.product.description.ProductDescription)13 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)13 List (java.util.List)13 Collectors (java.util.stream.Collectors)13 ReadableCategoryPopulator (com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator)12 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)12 Validate (org.apache.commons.lang3.Validate)12 Manufacturer (com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer)11 ProductPrice (com.salesmanager.core.model.catalog.product.price.ProductPrice)11 PersistableCategory (com.salesmanager.shop.model.catalog.category.PersistableCategory)11