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