Search in sources :

Example 21 with ReadableCategory

use of com.salesmanager.shop.model.catalog.category.ReadableCategory in project shopizer by shopizer-ecommerce.

the class ShoppingCategoryRESTController method getCategory.

@RequestMapping(value = "/public/{store}/category/{id}", method = RequestMethod.GET)
@ResponseBody
public ReadableCategory getCategory(@PathVariable final String store, @PathVariable Long id, HttpServletRequest request, HttpServletResponse response) {
    try {
        /**
         * default routine *
         */
        MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
        if (merchantStore != null) {
            if (!merchantStore.getCode().equals(store)) {
                merchantStore = null;
            }
        }
        if (merchantStore == null) {
            merchantStore = merchantStoreService.getByCode(store);
        }
        if (merchantStore == null) {
            LOGGER.error("Merchant store is null for code " + store);
            response.sendError(503, "Merchant store is null for code " + store);
            return null;
        }
        Language language = languageUtils.getRequestLanguage(request, response);
        /**
         *			Language language = merchantStore.getDefaultLanguage();
         *
         *			Map<String,Language> langs = languageService.getLanguagesMap();
         *
         *			if(!StringUtils.isBlank(request.getParameter(Constants.LANG))) {
         *				String lang = request.getParameter(Constants.LANG);
         *				if(lang!=null) {
         *					language = langs.get(language);
         *				}
         *			}
         *
         *			if(language==null) {
         *				language = merchantStore.getDefaultLanguage();
         *			}
         */
        /**
         * end default routine *
         */
        ReadableCategory category = categoryFacade.getById(merchantStore, id, language);
        if (category == null) {
            response.sendError(503, "Invalid category id");
            return null;
        }
        return category;
    } catch (Exception e) {
        LOGGER.error("Error while saving category", e);
        try {
            response.sendError(503, "Error while saving category " + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : Language(com.salesmanager.core.model.reference.language.Language) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 22 with ReadableCategory

use of com.salesmanager.shop.model.catalog.category.ReadableCategory in project shopizer by shopizer-ecommerce.

the class BreadcrumbsUtils method buildCategoryBreadcrumb.

public Breadcrumb buildCategoryBreadcrumb(ReadableCategory categoryClicked, MerchantStore store, Language language, String contextPath) throws Exception {
    /**
     * Rebuild breadcrumb *
     */
    BreadcrumbItem home = new BreadcrumbItem();
    home.setItemType(BreadcrumbItemType.HOME);
    home.setLabel(messages.getMessage(Constants.HOME_MENU_KEY, LocaleUtils.getLocale(language)));
    home.setUrl(filePathUtils.buildStoreUri(store, contextPath) + Constants.SHOP_URI);
    Breadcrumb breadCrumb = new Breadcrumb();
    breadCrumb.setLanguage(language);
    List<BreadcrumbItem> items = new ArrayList<BreadcrumbItem>();
    items.add(home);
    List<String> categoryIds = parseCategoryLineage(categoryClicked.getLineage());
    List<Long> ids = new ArrayList<Long>();
    for (String c : categoryIds) {
        ids.add(Long.parseLong(c));
    }
    ids.add(categoryClicked.getId());
    List<Category> categories = categoryService.listByIds(store, ids, language);
    // category path - use lineage
    for (Category c : categories) {
        BreadcrumbItem categoryBreadcrump = new BreadcrumbItem();
        categoryBreadcrump.setItemType(BreadcrumbItemType.CATEGORY);
        categoryBreadcrump.setLabel(c.getDescription().getName());
        categoryBreadcrump.setUrl(filePathUtils.buildCategoryUrl(store, contextPath, c.getDescription().getSeUrl()));
        items.add(categoryBreadcrump);
    }
    breadCrumb.setUrlRefContent(buildBreadCrumb(ids));
    breadCrumb.setBreadCrumbs(items);
    breadCrumb.setItemType(BreadcrumbItemType.CATEGORY);
    return breadCrumb;
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) BreadcrumbItem(com.salesmanager.shop.model.shop.BreadcrumbItem) ArrayList(java.util.ArrayList) Breadcrumb(com.salesmanager.shop.model.shop.Breadcrumb)

Example 23 with ReadableCategory

use of com.salesmanager.shop.model.catalog.category.ReadableCategory in project shopizer by shopizer-ecommerce.

the class StoreFilter method getCategories.

/**
 * @param store
 * @param language
 * @return
 * @throws Exception
 */
// private Map<String, List<Category>> getCategories(MerchantStore store,
// Language language)
// throws Exception {
private Map<String, List<ReadableCategory>> getCategories(MerchantStore store, Language language) throws Exception {
    // Map<String, List<Category>> objects = new ConcurrentHashMap<String,
    // List<Category>>();
    Map<String, List<ReadableCategory>> objects = new ConcurrentHashMap<String, List<ReadableCategory>>();
    /**
     * returns categories with required depth, 0 = root category, 1 = root +
     * 1 layer child ...)
     */
    List<Category> categories = categoryService.getListByDepth(store, 0, language);
    ReadableCategoryPopulator readableCategoryPopulator = new ReadableCategoryPopulator();
    Map<String, ReadableCategory> subs = new ConcurrentHashMap<String, ReadableCategory>();
    if (categories != null && categories.size() > 0) {
        // create a Map<String,List<Content>
        for (Category category : categories) {
            if (category.isVisible()) {
                // if(category.getDepth().intValue()==0) {
                // ReadableCategory readableCategory = new
                // ReadableCategory();
                // readableCategoryPopulator.populate(category,
                // readableCategory, store, language);
                Set<CategoryDescription> descriptions = category.getDescriptions();
                for (CategoryDescription description : descriptions) {
                    Language lang = description.getLanguage();
                    ReadableCategory readableCategory = new ReadableCategory();
                    readableCategoryPopulator.populate(category, readableCategory, store, language);
                    String key = new StringBuilder().append(store.getId()).append("_").append(Constants.CATEGORIES_CACHE_KEY).append("-").append(lang.getCode()).toString();
                    if (category.getDepth().intValue() == 0) {
                        // List<Category> cacheCategories = null;
                        List<ReadableCategory> cacheCategories = null;
                        if (objects == null || objects.size() == 0) {
                            // objects = new HashMap<String,
                            // List<Category>>();
                            objects = new HashMap<String, List<ReadableCategory>>();
                        }
                        if (!objects.containsKey(key)) {
                            // cacheCategories = new ArrayList<Category>();
                            cacheCategories = new ArrayList<ReadableCategory>();
                            objects.put(key, cacheCategories);
                        } else {
                            cacheCategories = objects.get(key.toString());
                            if (cacheCategories == null) {
                                LOGGER.error("Cannot find categories key in cache " + key);
                                continue;
                            }
                        }
                        // cacheCategories.add(category);
                        cacheCategories.add(readableCategory);
                    } else {
                        subs.put(lang.getCode(), readableCategory);
                    }
                }
            }
        }
    }
    return objects;
}
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) ReadableCategoryList(com.salesmanager.shop.model.catalog.category.ReadableCategoryList) CategoryDescription(com.salesmanager.core.model.catalog.category.CategoryDescription) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ReadableCategoryPopulator(com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator)

Example 24 with ReadableCategory

use of com.salesmanager.shop.model.catalog.category.ReadableCategory 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 25 with ReadableCategory

use of com.salesmanager.shop.model.catalog.category.ReadableCategory 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)25 Category (com.salesmanager.core.model.catalog.category.Category)18 Language (com.salesmanager.core.model.reference.language.Language)13 ArrayList (java.util.ArrayList)13 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)12 ReadableCategoryPopulator (com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator)11 Collectors (java.util.stream.Collectors)8 List (java.util.List)7 CollectionUtils (org.apache.commons.collections4.CollectionUtils)7 Product (com.salesmanager.core.model.catalog.product.Product)6 Mapper (com.salesmanager.shop.mapper.Mapper)6 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)6 DateUtil (com.salesmanager.shop.utils.DateUtil)6 Map (java.util.Map)6 Optional (java.util.Optional)6 Validate (org.apache.commons.lang3.Validate)6 ConversionException (com.salesmanager.core.business.exception.ConversionException)5 ServiceException (com.salesmanager.core.business.exception.ServiceException)5 PricingService (com.salesmanager.core.business.services.catalog.product.PricingService)5 PersistableCategory (com.salesmanager.shop.model.catalog.category.PersistableCategory)5