Search in sources :

Example 6 with ReadableCategoryPopulator

use of com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator in project shopizer by shopizer-ecommerce.

the class ShoppingCategoryController method displayCategory.

@SuppressWarnings("unchecked")
private String displayCategory(final String friendlyUrl, final String ref, Model model, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {
    MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
    // set ref as request attribute
    String encoded = SanitizeUtils.getSafeRequestParamString(ref);
    if (!encoded.equals(ref)) {
        // possible xss
        throw new Exception("Wrong input parameter [" + ref + "]");
    }
    request.setAttribute("ref", encoded);
    // get category
    Category category = categoryService.getBySeUrl(store, friendlyUrl);
    Language language = (Language) request.getAttribute("LANGUAGE");
    if (category == null) {
        LOGGER.error("No category found for friendlyUrl " + friendlyUrl);
        // redirect on page not found
        return PageBuilderUtils.build404(store);
    }
    if (!category.isVisible()) {
        return PageBuilderUtils.buildHomePage(store);
    }
    ReadableCategoryPopulator populator = new ReadableCategoryPopulator();
    ReadableCategory categoryProxy = populator.populate(category, new ReadableCategory(), store, language);
    Breadcrumb breadCrumb = breadcrumbsUtils.buildCategoryBreadcrumb(categoryProxy, store, language, request.getContextPath());
    request.getSession().setAttribute(Constants.BREADCRUMB, breadCrumb);
    request.setAttribute(Constants.BREADCRUMB, breadCrumb);
    // meta information
    PageInformation pageInformation = new PageInformation();
    pageInformation.setPageDescription(categoryProxy.getDescription().getMetaDescription());
    pageInformation.setPageKeywords(categoryProxy.getDescription().getKeyWords());
    pageInformation.setPageTitle(categoryProxy.getDescription().getTitle());
    pageInformation.setPageUrl(categoryProxy.getDescription().getFriendlyUrl());
    // ** retrieves category id drill down**//
    String lineage = new StringBuilder().append(category.getLineage()).append(Constants.CATEGORY_LINEAGE_DELIMITER).toString();
    request.setAttribute(Constants.REQUEST_PAGE_INFORMATION, pageInformation);
    List<Category> categs = categoryService.getListByLineage(store, lineage);
    categs.add(category);
    StringBuilder subCategoriesCacheKey = new StringBuilder();
    subCategoriesCacheKey.append(store.getId()).append("_").append(category.getId()).append("_").append(Constants.SUBCATEGORIES_CACHE_KEY).append("-").append(language.getCode());
    StringBuilder subCategoriesMissed = new StringBuilder();
    subCategoriesMissed.append(subCategoriesCacheKey.toString()).append(Constants.MISSED_CACHE_KEY);
    List<BigDecimal> prices = new ArrayList<BigDecimal>();
    List<ReadableCategory> subCategories = null;
    Map<Long, Long> countProductsByCategories = null;
    if (store.isUseCache()) {
        // get from the cache
        subCategories = (List<ReadableCategory>) cache.getFromCache(subCategoriesCacheKey.toString());
        if (subCategories == null) {
            // get from missed cache
            // Boolean missedContent = (Boolean)cache.getFromCache(subCategoriesMissed.toString());
            // if(missedContent==null) {
            countProductsByCategories = getProductsByCategory(store, categs);
            subCategories = getSubCategories(store, category, countProductsByCategories, language, locale);
            if (subCategories != null) {
                cache.putInCache(subCategories, subCategoriesCacheKey.toString());
            } else {
            // cache.putInCache(new Boolean(true), subCategoriesCacheKey.toString());
            }
        // }
        }
    } else {
        countProductsByCategories = getProductsByCategory(store, categs);
        subCategories = getSubCategories(store, category, countProductsByCategories, language, locale);
    }
    // Parent category
    ReadableCategory parentProxy = null;
    if (category.getParent() != null) {
        Category parent = categoryService.getById(category.getParent().getId(), store.getId());
        parentProxy = populator.populate(parent, new ReadableCategory(), store, language);
    }
    // ** List of manufacturers **//
    List<ReadableManufacturer> manufacturerList = getManufacturersByProductAndCategory(store, category, categs, language);
    model.addAttribute("manufacturers", manufacturerList);
    model.addAttribute("parent", parentProxy);
    model.addAttribute("category", categoryProxy);
    model.addAttribute("subCategories", subCategories);
    if (parentProxy != null) {
        request.setAttribute(Constants.LINK_CODE, parentProxy.getDescription().getFriendlyUrl());
    }
    /**
     * template *
     */
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Category.category).append(".").append(store.getStoreTemplate());
    return template.toString();
}
Also used : ReadableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) Category(com.salesmanager.core.model.catalog.category.Category) ArrayList(java.util.ArrayList) Breadcrumb(com.salesmanager.shop.model.shop.Breadcrumb) BigDecimal(java.math.BigDecimal) Language(com.salesmanager.core.model.reference.language.Language) PageInformation(com.salesmanager.shop.model.shop.PageInformation) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ReadableCategoryPopulator(com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator)

Example 7 with ReadableCategoryPopulator

use of com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator 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)

Aggregations

ReadableCategory (com.salesmanager.shop.model.catalog.category.ReadableCategory)7 ReadableCategoryPopulator (com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator)7 Category (com.salesmanager.core.model.catalog.category.Category)6 Language (com.salesmanager.core.model.reference.language.Language)3 ArrayList (java.util.ArrayList)3 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)2 PersistableCategory (com.salesmanager.shop.model.catalog.category.PersistableCategory)2 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 CategoryDescription (com.salesmanager.core.model.catalog.category.CategoryDescription)1 ReadableCategoryList (com.salesmanager.shop.model.catalog.category.ReadableCategoryList)1 ReadableManufacturer (com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer)1 Breadcrumb (com.salesmanager.shop.model.shop.Breadcrumb)1 PageInformation (com.salesmanager.shop.model.shop.PageInformation)1 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)1 BigDecimal (java.math.BigDecimal)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1