Search in sources :

Example 1 with ReadableCategoryList

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

the class StoreFilter method setTopCategories.

@SuppressWarnings("unchecked")
private void setTopCategories(MerchantStore store, Language language, HttpServletRequest request) throws Exception {
    StringBuilder categoriesKey = new StringBuilder();
    categoriesKey.append(store.getId()).append("_").append(Constants.CATEGORIES_CACHE_KEY).append("-").append(language.getCode());
    StringBuilder categoriesKeyMissed = new StringBuilder();
    categoriesKeyMissed.append(categoriesKey.toString()).append(Constants.MISSED_CACHE_KEY);
    // language code - List of category
    Map<String, List<ReadableCategory>> objects = null;
    List<ReadableCategory> loadedCategories = null;
    if (store.isUseCache()) {
        objects = (Map<String, List<ReadableCategory>>) webApplicationCache.getFromCache(categoriesKey.toString());
        if (objects == null) {
            // load categories
            ReadableCategoryList categoryList = categoryFacade.getCategoryHierarchy(store, null, 0, language, null, 0, // null
            200);
            loadedCategories = categoryList.getCategories();
            // filter out invisible category
            loadedCategories.stream().filter(cat -> cat.isVisible() == true).collect(Collectors.toList());
            objects = new ConcurrentHashMap<String, List<ReadableCategory>>();
            objects.put(language.getCode(), loadedCategories);
            webApplicationCache.putInCache(categoriesKey.toString(), objects);
        } else {
            loadedCategories = objects.get(language.getCode());
        }
    } else {
        ReadableCategoryList categoryList = categoryFacade.getCategoryHierarchy(store, null, 0, language, null, 0, // null // filter
        200);
        loadedCategories = categoryList.getCategories();
    }
    if (loadedCategories != null) {
        request.setAttribute(Constants.REQUEST_TOP_CATEGORIES, loadedCategories);
    }
}
Also used : ContentDescription(com.salesmanager.core.model.content.ContentDescription) LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) LoggerFactory(org.slf4j.LoggerFactory) AnonymousCustomer(com.salesmanager.shop.model.customer.AnonymousCustomer) StringUtils(org.apache.commons.lang3.StringUtils) LanguageService(com.salesmanager.core.business.services.reference.language.LanguageService) HandlerInterceptorAdapter(org.springframework.web.servlet.handler.HandlerInterceptorAdapter) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) Content(com.salesmanager.core.model.content.Content) UserContext(com.salesmanager.core.model.common.UserContext) ContentType(com.salesmanager.core.model.content.ContentType) ReadableCategoryPopulator(com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator) CategoryService(com.salesmanager.core.business.services.catalog.category.CategoryService) MerchantStoreService(com.salesmanager.core.business.services.merchant.MerchantStoreService) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) CustomerService(com.salesmanager.core.business.services.customer.CustomerService) Breadcrumb(com.salesmanager.shop.model.shop.Breadcrumb) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collectors(java.util.stream.Collectors) CategoryDescription(com.salesmanager.core.model.catalog.category.CategoryDescription) ModelAndView(org.springframework.web.servlet.ModelAndView) BreadcrumbItem(com.salesmanager.shop.model.shop.BreadcrumbItem) CacheUtils(com.salesmanager.core.business.utils.CacheUtils) MerchantConfiguration(com.salesmanager.core.model.system.MerchantConfiguration) LanguageUtils(com.salesmanager.shop.utils.LanguageUtils) GeoLocationUtils(com.salesmanager.shop.utils.GeoLocationUtils) CoreConfiguration(com.salesmanager.core.business.utils.CoreConfiguration) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) Address(com.salesmanager.shop.model.customer.address.Address) Authentication(org.springframework.security.core.Authentication) WebApplicationCacheUtils(com.salesmanager.shop.utils.WebApplicationCacheUtils) ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) java.util(java.util) CategoryFacade(com.salesmanager.shop.store.controller.category.facade.CategoryFacade) Constants(com.salesmanager.shop.constants.Constants) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Language(com.salesmanager.core.model.reference.language.Language) Inject(javax.inject.Inject) BreadcrumbItemType(com.salesmanager.shop.model.shop.BreadcrumbItemType) HttpServletRequest(javax.servlet.http.HttpServletRequest) LabelUtils(com.salesmanager.shop.utils.LabelUtils) PageInformation(com.salesmanager.shop.model.shop.PageInformation) Nullable(javax.annotation.Nullable) Product(com.salesmanager.core.model.catalog.product.Product) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Customer(com.salesmanager.core.model.customer.Customer) MerchantConfig(com.salesmanager.core.model.system.MerchantConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) MerchantConfigurationService(com.salesmanager.core.business.services.system.MerchantConfigurationService) ReadableCategoryList(com.salesmanager.shop.model.catalog.category.ReadableCategoryList) ContentService(com.salesmanager.core.business.services.content.ContentService) Category(com.salesmanager.core.model.catalog.category.Category) MerchantConfigurationType(com.salesmanager.core.model.system.MerchantConfigurationType) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ReadableCategoryList(com.salesmanager.shop.model.catalog.category.ReadableCategoryList) ReadableCategoryList(com.salesmanager.shop.model.catalog.category.ReadableCategoryList)

Example 2 with ReadableCategoryList

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

the class CategoryFacadeImpl method getCategoryHierarchy.

@Override
public ReadableCategoryList getCategoryHierarchy(MerchantStore store, ListCriteria criteria, int depth, Language language, List<String> filter, int page, int count) {
    Validate.notNull(store, "MerchantStore can not be null");
    // get parent store
    try {
        MerchantStore parent = merchantStoreService.getParent(store.getCode());
        List<Category> categories = null;
        ReadableCategoryList returnList = new ReadableCategoryList();
        if (!CollectionUtils.isEmpty(filter) && filter.contains(FEATURED_CATEGORY)) {
            categories = categoryService.getListByDepthFilterByFeatured(parent, depth, language);
            returnList.setRecordsTotal(categories.size());
            returnList.setNumber(categories.size());
            returnList.setTotalPages(1);
        } else {
            org.springframework.data.domain.Page<Category> pageable = categoryService.getListByDepth(parent, language, criteria != null ? criteria.getName() : null, depth, page, count);
            categories = pageable.getContent();
            returnList.setRecordsTotal(pageable.getTotalElements());
            returnList.setTotalPages(pageable.getTotalPages());
            returnList.setNumber(categories.size());
        }
        List<ReadableCategory> readableCategories = null;
        if (filter != null && filter.contains(VISIBLE_CATEGORY)) {
            readableCategories = categories.stream().filter(Category::isVisible).map(cat -> categoryReadableCategoryConverter.convert(cat, store, language)).collect(Collectors.toList());
        } else {
            readableCategories = categories.stream().map(cat -> categoryReadableCategoryConverter.convert(cat, store, language)).collect(Collectors.toList());
        }
        Map<Long, ReadableCategory> readableCategoryMap = readableCategories.stream().collect(Collectors.toMap(ReadableCategory::getId, Function.identity()));
        readableCategories.stream().filter(cat -> Objects.nonNull(cat.getParent())).filter(cat -> readableCategoryMap.containsKey(cat.getParent().getId())).forEach(readableCategory -> {
            ReadableCategory parentCategory = readableCategoryMap.get(readableCategory.getParent().getId());
            if (parentCategory != null) {
                parentCategory.getChildren().add(readableCategory);
            }
        });
        List<ReadableCategory> filteredList = readableCategoryMap.values().stream().collect(Collectors.toList());
        // execute only if not admin filtered
        if (filter == null || (filter != null && !filter.contains(ADMIN_CATEGORY))) {
            filteredList = readableCategoryMap.values().stream().filter(cat -> cat.getDepth() == 0).sorted(Comparator.comparing(ReadableCategory::getSortOrder)).collect(Collectors.toList());
            returnList.setNumber(filteredList.size());
        }
        returnList.setCategories(filteredList);
        return returnList;
    } catch (ServiceException e) {
        throw new ServiceRuntimeException(e);
    }
}
Also used : CategoryFacade(com.salesmanager.shop.store.controller.category.facade.CategoryFacade) PersistableCategoryPopulator(com.salesmanager.shop.populator.catalog.PersistableCategoryPopulator) HashMap(java.util.HashMap) ReadableProductVariantValue(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductVariantValue) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ServiceException(com.salesmanager.core.business.exception.ServiceException) Inject(javax.inject.Inject) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription) Language(com.salesmanager.core.model.reference.language.Language) ListCriteria(com.salesmanager.shop.model.entity.ListCriteria) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Service(org.springframework.stereotype.Service) Map(java.util.Map) ProductOption(com.salesmanager.core.model.catalog.product.attribute.ProductOption) ReadableProductVariant(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductVariant) ReadableCategoryPopulator(com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator) CategoryService(com.salesmanager.core.business.services.catalog.category.CategoryService) MerchantStoreService(com.salesmanager.core.business.services.merchant.MerchantStoreService) ProductAttributeService(com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Mapper(com.salesmanager.shop.mapper.Mapper) Iterator(java.util.Iterator) ProductOptionDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription) Collectors(java.util.stream.Collectors) ReadableCategoryList(com.salesmanager.shop.model.catalog.category.ReadableCategoryList) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) Objects(java.util.Objects) Category(com.salesmanager.core.model.catalog.category.Category) List(java.util.List) Validate(org.apache.commons.lang3.Validate) CollectionUtils(org.springframework.util.CollectionUtils) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) Entry(java.util.Map.Entry) Optional(java.util.Optional) ConversionException(com.salesmanager.core.business.exception.ConversionException) Comparator(java.util.Comparator) PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) Category(com.salesmanager.core.model.catalog.category.Category) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) ReadableCategoryList(com.salesmanager.shop.model.catalog.category.ReadableCategoryList) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore)

Example 3 with ReadableCategoryList

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

the class CategoryFacadeImpl method listByProduct.

@Override
public ReadableCategoryList listByProduct(MerchantStore store, Long product, Language language) {
    Validate.notNull(product, "Product id must not be null");
    Validate.notNull(store, "Store must not be null");
    List<ReadableCategory> readableCategories = new ArrayList<ReadableCategory>();
    List<Category> categories = categoryService.getByProductId(product, store);
    readableCategories = categories.stream().map(cat -> categoryReadableCategoryConverter.convert(cat, store, language)).collect(Collectors.toList());
    ReadableCategoryList readableList = new ReadableCategoryList();
    readableList.setCategories(readableCategories);
    readableList.setTotalPages(1);
    readableList.setNumber(readableCategories.size());
    readableList.setRecordsTotal(readableCategories.size());
    return readableList;
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ArrayList(java.util.ArrayList) ReadableCategoryList(com.salesmanager.shop.model.catalog.category.ReadableCategoryList)

Aggregations

Category (com.salesmanager.core.model.catalog.category.Category)3 ReadableCategory (com.salesmanager.shop.model.catalog.category.ReadableCategory)3 ReadableCategoryList (com.salesmanager.shop.model.catalog.category.ReadableCategoryList)3 CategoryService (com.salesmanager.core.business.services.catalog.category.CategoryService)2 MerchantStoreService (com.salesmanager.core.business.services.merchant.MerchantStoreService)2 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)2 Language (com.salesmanager.core.model.reference.language.Language)2 PersistableCategory (com.salesmanager.shop.model.catalog.category.PersistableCategory)2 ReadableCategoryPopulator (com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator)2 CategoryFacade (com.salesmanager.shop.store.controller.category.facade.CategoryFacade)2 ArrayList (java.util.ArrayList)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 ServiceException (com.salesmanager.core.business.exception.ServiceException)1 ProductService (com.salesmanager.core.business.services.catalog.product.ProductService)1 ProductAttributeService (com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService)1 ContentService (com.salesmanager.core.business.services.content.ContentService)1 CustomerService (com.salesmanager.core.business.services.customer.CustomerService)1 LanguageService (com.salesmanager.core.business.services.reference.language.LanguageService)1 MerchantConfigurationService (com.salesmanager.core.business.services.system.MerchantConfigurationService)1