Search in sources :

Example 26 with Category

use of com.salesmanager.core.model.catalog.category.Category 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 27 with Category

use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.

the class CategoryFacadeImpl method move.

@Override
public void move(Long child, Long parent, MerchantStore store) {
    Validate.notNull(child, "Child category must not be null");
    Validate.notNull(parent, "Parent category must not be null");
    Validate.notNull(store, "Merhant must not be null");
    try {
        Category c = categoryService.getById(child, store.getId());
        if (c == null) {
            throw new ResourceNotFoundException("Category with id [" + child + "] for store [" + store.getCode() + "]");
        }
        if (parent.longValue() == -1) {
            categoryService.addChild(null, c);
            return;
        }
        Category p = categoryService.getById(parent, store.getId());
        if (p == null) {
            throw new ResourceNotFoundException("Category with id [" + parent + "] for store [" + store.getCode() + "]");
        }
        if (c.getParent() != null && c.getParent().getId() == parent) {
            return;
        }
        if (c.getMerchantStore().getId().intValue() != store.getId().intValue()) {
            throw new OperationNotAllowedException("Invalid identifiers for Merchant [" + c.getMerchantStore().getCode() + "]");
        }
        if (p.getMerchantStore().getId().intValue() != store.getId().intValue()) {
            throw new OperationNotAllowedException("Invalid identifiers for Merchant [" + c.getMerchantStore().getCode() + "]");
        }
        p.getAuditSection().setModifiedBy("Api");
        categoryService.addChild(p, c);
    } catch (ResourceNotFoundException re) {
        throw re;
    } catch (OperationNotAllowedException oe) {
        throw oe;
    } catch (Exception e) {
        throw new ServiceRuntimeException(e);
    }
}
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) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) 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 28 with Category

use of com.salesmanager.core.model.catalog.category.Category 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 29 with Category

use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.

the class CategoryFacadeImpl method saveCategory.

@Override
public PersistableCategory saveCategory(MerchantStore store, PersistableCategory category) {
    try {
        Long categoryId = category.getId();
        Category target = Optional.ofNullable(categoryId).filter(merchant -> store != null).filter(id -> id > 0).map(categoryService::getById).orElse(new Category());
        Category dbCategory = populateCategory(store, category, target);
        saveCategory(store, dbCategory, null);
        // set category id
        category.setId(dbCategory.getId());
        return category;
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("Error while updating category", 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) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 30 with Category

use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.

the class CategoryFacadeImpl method setVisible.

@Override
public void setVisible(PersistableCategory category, MerchantStore store) {
    Validate.notNull(category, "Category must not be null");
    Validate.notNull(store, "Store must not be null");
    try {
        Category c = this.getById(store, category.getId());
        c.setVisible(category.isVisible());
        categoryService.saveOrUpdate(c);
    } catch (Exception e) {
        throw new ServiceRuntimeException("Error while getting category [" + category.getId() + "]", e);
    }
}
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) 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)

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