Search in sources :

Example 11 with ReadableCategory

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

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

Example 13 with ReadableCategory

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

the class CategoryFacadeImpl method addChildToParent.

private void addChildToParent(ReadableCategory readableCategory, List<ReadableCategory> childrenCats) {
    Map<Long, ReadableCategory> categoryMap = childrenCats.stream().collect(Collectors.toMap(ReadableCategory::getId, Function.identity()));
    categoryMap.put(readableCategory.getId(), readableCategory);
    // traverse map and add child to parent
    for (ReadableCategory readable : childrenCats) {
        if (readable.getParent() != null) {
            ReadableCategory rc = categoryMap.get(readable.getParent().getId());
            if (rc != null) {
                rc.getChildren().add(readable);
            }
        }
    }
}
Also used : ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory)

Example 14 with ReadableCategory

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

the class CategoryFacadeImpl method getById.

@Override
public ReadableCategory getById(MerchantStore store, Long id, Language language) {
    try {
        Category categoryModel = null;
        if (language != null) {
            categoryModel = getCategoryById(id, language);
        } else {
            // all langs
            categoryModel = getById(store, id);
        }
        if (categoryModel == null)
            throw new ResourceNotFoundException("Categori id [" + id + "] not found");
        StringBuilder lineage = new StringBuilder().append(categoryModel.getLineage());
        ReadableCategory readableCategory = categoryReadableCategoryConverter.convert(categoryModel, store, language);
        // get children
        List<Category> children = getListByLineage(store, lineage.toString());
        List<ReadableCategory> childrenCats = children.stream().map(cat -> categoryReadableCategoryConverter.convert(cat, store, language)).collect(Collectors.toList());
        addChildToParent(readableCategory, childrenCats);
        return readableCategory;
    } catch (Exception 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) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 15 with ReadableCategory

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

the class CategoryFacadeImpl method getByCode.

@Override
public ReadableCategory getByCode(MerchantStore store, String code, Language language) throws Exception {
    Validate.notNull(code, "category code must not be null");
    ReadableCategoryPopulator categoryPopulator = new ReadableCategoryPopulator();
    ReadableCategory readableCategory = new ReadableCategory();
    Category category = categoryService.getByCode(store, code);
    categoryPopulator.populate(category, readableCategory, store, language);
    return readableCategory;
}
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) ReadableCategoryPopulator(com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator)

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