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);
}
}
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;
}
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);
}
}
}
}
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);
}
}
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;
}
Aggregations