use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class CategoryFacadeImpl method categoryProductVariants.
@Override
public List<ReadableProductVariant> categoryProductVariants(Long categoryId, MerchantStore store, Language language) {
Category category = categoryService.getById(categoryId, store.getId());
List<ReadableProductVariant> variants = new ArrayList<ReadableProductVariant>();
if (category == null) {
throw new ResourceNotFoundException("Category [" + categoryId + "] not found");
}
try {
List<ProductAttribute> attributes = productAttributeService.getProductAttributesByCategoryLineage(store, category.getLineage(), language);
/**
* Option NAME OptionValueName OptionValueName
*/
Map<String, List<com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValue>> rawFacet = new HashMap<String, List<com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValue>>();
Map<String, ProductOption> references = new HashMap<String, ProductOption>();
for (ProductAttribute attr : attributes) {
references.put(attr.getProductOption().getCode(), attr.getProductOption());
List<com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValue> values = rawFacet.get(attr.getProductOption().getCode());
if (values == null) {
values = new ArrayList<com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValue>();
rawFacet.put(attr.getProductOption().getCode(), values);
}
if (attr.getProductOptionValue() != null) {
Optional<ProductOptionValueDescription> desc = attr.getProductOptionValue().getDescriptions().stream().filter(o -> o.getLanguage().getId() == language.getId()).findFirst();
com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValue val = new com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValue();
val.setCode(attr.getProductOption().getCode());
String order = attr.getAttributeSortOrder();
val.setSortOrder(order == null ? attr.getId().intValue() : Integer.parseInt(attr.getAttributeSortOrder()));
if (desc.isPresent()) {
val.setName(desc.get().getName());
} else {
val.setName(attr.getProductOption().getCode());
}
values.add(val);
}
}
// for each reference set Option
Iterator<Entry<String, ProductOption>> it = references.entrySet().iterator();
while (it.hasNext()) {
@SuppressWarnings("rawtypes") Map.Entry pair = (Map.Entry) it.next();
ProductOption option = (ProductOption) pair.getValue();
List<com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValue> values = rawFacet.get(option.getCode());
ReadableProductVariant productVariant = new ReadableProductVariant();
Optional<ProductOptionDescription> optionDescription = option.getDescriptions().stream().filter(o -> o.getLanguage().getId() == language.getId()).findFirst();
if (optionDescription.isPresent()) {
productVariant.setName(optionDescription.get().getName());
productVariant.setId(optionDescription.get().getId());
productVariant.setCode(optionDescription.get().getProductOption().getCode());
List<ReadableProductVariantValue> optionValues = new ArrayList<ReadableProductVariantValue>();
for (com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValue value : values) {
ReadableProductVariantValue v = new ReadableProductVariantValue();
v.setCode(value.getCode());
v.setName(value.getName());
v.setDescription(value.getName());
v.setOption(option.getId());
v.setValue(value.getId());
v.setOrder(option.getProductOptionSortOrder());
optionValues.add(v);
}
Comparator<ReadableProductVariantValue> orderComparator = Comparator.comparingInt(ReadableProductVariantValue::getOrder);
// Arrays.sort(employees, employeeSalaryComparator);
List<ReadableProductVariantValue> readableValues = optionValues.stream().sorted(orderComparator).collect(Collectors.toList());
// sort by name
// remove duplicates
readableValues = optionValues.stream().distinct().collect(Collectors.toList());
readableValues.sort(Comparator.comparing(ReadableProductVariantValue::getName));
productVariant.setOptions(readableValues);
variants.add(productVariant);
}
}
return variants;
} catch (Exception e) {
throw new ServiceRuntimeException("An error occured while retrieving ProductAttributes", e);
}
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class CategoryFacadeImpl method saveCategory.
private void saveCategory(MerchantStore store, Category category, Category parent) throws ServiceException {
/**
* set lineage *
*/
if (parent != null) {
category.setParent(category);
String lineage = parent.getLineage();
int depth = parent.getDepth();
category.setDepth(depth + 1);
// service
category.setLineage(new StringBuilder().append(lineage).toString());
// will
// adjust
// lineage
}
category.setMerchantStore(store);
// remove children
List<Category> children = category.getCategories();
List<Category> saveAfter = children.stream().filter(c -> c.getId() == null || c.getId().longValue() == 0).collect(Collectors.toList());
List<Category> saveNow = children.stream().filter(c -> c.getId() != null && c.getId().longValue() > 0).collect(Collectors.toList());
category.setCategories(saveNow);
/**
* set parent *
*/
if (parent != null) {
category.setParent(parent);
}
categoryService.saveOrUpdate(category);
if (!CollectionUtils.isEmpty(saveAfter)) {
parent = category;
for (Category c : saveAfter) {
if (c.getId() == null || c.getId().longValue() == 0) {
for (Category sub : children) {
saveCategory(store, sub, parent);
}
}
}
}
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class ManufacturerFacadeImpl method getByProductInCategory.
@Override
public List<ReadableManufacturer> getByProductInCategory(MerchantStore store, Language language, Long categoryId) {
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(language, "Language cannot be null");
Validate.notNull(categoryId, "Category id cannot be null");
Category category = categoryService.getById(categoryId, store.getId());
if (category == null) {
throw new ResourceNotFoundException("Category with id [" + categoryId + "] not found");
}
if (category.getMerchantStore().getId().longValue() != store.getId().longValue()) {
throw new UnauthorizedException("Merchant [" + store.getCode() + "] not authorized");
}
try {
List<Manufacturer> manufacturers = manufacturerService.listByProductsInCategory(store, category, language);
List<ReadableManufacturer> manufacturersList = manufacturers.stream().sorted(new Comparator<Manufacturer>() {
@Override
public int compare(final Manufacturer object1, final Manufacturer object2) {
return object1.getCode().compareTo(object2.getCode());
}
}).map(manuf -> readableManufacturerConverter.convert(manuf, store, language)).collect(Collectors.toList());
return manufacturersList;
} catch (ServiceException e) {
throw new ServiceRuntimeException(e);
}
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class CategoryServiceImpl method getById.
@Override
public Category getById(Long id, int merchantId) {
Category category = categoryRepository.findByIdAndStore(id, merchantId);
if (category == null) {
return null;
}
List<CategoryDescription> descriptions = categoryDescriptionRepository.listByCategoryId(id);
Set<CategoryDescription> desc = new HashSet<CategoryDescription>(descriptions);
category.setDescriptions(desc);
return category;
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class CategoryServiceImpl method addChild.
@Override
public void addChild(Category parent, Category child) throws ServiceException {
if (child == null || child.getMerchantStore() == null) {
throw new ServiceException("Child category and merchant store should not be null");
}
try {
if (parent == null) {
// assign to root
child.setParent(null);
child.setDepth(0);
// child.setLineage(new
// StringBuilder().append("/").append(child.getId()).append("/").toString());
child.setLineage(new StringBuilder().append("/").append(child.getId()).append("/").toString());
} else {
// parent
Category p = getById(parent.getId(), parent.getMerchantStore().getId());
String lineage = p.getLineage();
int depth = p.getDepth();
child.setParent(p);
child.setDepth(depth + 1);
child.setLineage(new StringBuilder().append(lineage).append(Constants.SLASH).append(child.getId()).append(Constants.SLASH).toString());
}
update(child);
StringBuilder childLineage = new StringBuilder();
childLineage.append(child.getLineage()).append(child.getId()).append("/");
List<Category> subCategories = getListByLineage(child.getMerchantStore(), childLineage.toString());
// ajust all sub categories lineages
if (subCategories != null && subCategories.size() > 0) {
for (Category subCategory : subCategories) {
if (!child.getId().equals(subCategory.getId())) {
addChild(child, subCategory);
}
}
}
} catch (Exception e) {
throw new ServiceException(e);
}
}
Aggregations