use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class ReadableProductDefinitionMapper method merge.
@Override
public ReadableProductDefinition merge(Product source, ReadableProductDefinition destination, MerchantStore store, Language language) {
Validate.notNull(source, "Product cannot be null");
Validate.notNull(destination, "Product destination cannot be null");
ReadableProductDefinition returnDestination = destination;
if (language == null) {
returnDestination = new ReadableProductDefinitionFull();
}
List<com.salesmanager.shop.model.catalog.product.ProductDescription> fulldescriptions = new ArrayList<com.salesmanager.shop.model.catalog.product.ProductDescription>();
returnDestination.setIdentifier(source.getSku());
returnDestination.setId(source.getId());
returnDestination.setVisible(source.isAvailable());
returnDestination.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
ProductDescription description = null;
if (source.getDescriptions() != null && source.getDescriptions().size() > 0) {
for (ProductDescription desc : source.getDescriptions()) {
if (language != null && desc.getLanguage() != null && desc.getLanguage().getId().intValue() == language.getId().intValue()) {
description = desc;
break;
} else {
fulldescriptions.add(populateDescription(desc));
}
}
}
if (description != null) {
com.salesmanager.shop.model.catalog.product.ProductDescription tragetDescription = populateDescription(description);
returnDestination.setDescription(tragetDescription);
}
if (source.getManufacturer() != null) {
ReadableManufacturer manufacturer = readableManufacturerMapper.convert(source.getManufacturer(), store, language);
returnDestination.setManufacturer(manufacturer);
}
if (!CollectionUtils.isEmpty(source.getCategories())) {
List<ReadableCategory> categoryList = new ArrayList<ReadableCategory>();
for (Category category : source.getCategories()) {
ReadableCategory readableCategory = readableCategoryMapper.convert(category, store, language);
categoryList.add(readableCategory);
}
returnDestination.setCategories(categoryList);
}
ProductSpecification specifications = new ProductSpecification();
specifications.setHeight(source.getProductHeight());
specifications.setLength(source.getProductLength());
specifications.setWeight(source.getProductWeight());
specifications.setWidth(source.getProductWidth());
if (!StringUtils.isBlank(store.getSeizeunitcode())) {
specifications.setDimensionUnitOfMeasure(DimensionUnitOfMeasure.valueOf(store.getSeizeunitcode().toLowerCase()));
}
if (!StringUtils.isBlank(store.getWeightunitcode())) {
specifications.setWeightUnitOfMeasure(WeightUnitOfMeasure.valueOf(store.getWeightunitcode().toLowerCase()));
}
returnDestination.setProductSpecifications(specifications);
if (source.getType() != null) {
ReadableProductType readableType = readableProductTypeMapper.convert(source.getType(), store, language);
returnDestination.setType(readableType);
}
returnDestination.setSortOrder(source.getSortOrder());
// images
Set<ProductImage> images = source.getImages();
if (CollectionUtils.isNotEmpty(images)) {
List<ReadableImage> imageList = images.stream().map(i -> this.convertImage(source, i, store)).collect(Collectors.toList());
returnDestination.setImages(imageList);
}
// quantity
ProductAvailability availability = null;
for (ProductAvailability a : source.getAvailabilities()) {
availability = a;
returnDestination.setCanBePurchased(availability.getProductStatus());
returnDestination.setQuantity(availability.getProductQuantity() == null ? 1 : availability.getProductQuantity());
}
FinalPrice price = null;
try {
price = pricingService.calculateProductPrice(source);
} catch (ServiceException e) {
throw new ConversionRuntimeException("Unable to get product price", e);
}
if (price != null) {
returnDestination.setPrice(price.getStringPrice());
}
if (returnDestination instanceof ReadableProductDefinitionFull) {
((ReadableProductDefinitionFull) returnDestination).setDescriptions(fulldescriptions);
}
return returnDestination;
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class ProductFacadeImpl method addProductToCategory.
@Override
public ReadableProduct addProductToCategory(Category category, Product product, Language language) throws Exception {
Validate.notNull(category, "Category cannot be null");
Validate.notNull(product, "Product cannot be null");
// not alloweed if category already attached
List<Category> assigned = product.getCategories().stream().filter(cat -> cat.getId().longValue() == category.getId().longValue()).collect(Collectors.toList());
if (assigned.size() > 0) {
throw new OperationNotAllowedException("Category with id [" + category.getId() + "] already attached to product [" + product.getId() + "]");
}
product.getCategories().add(category);
productService.update(product);
ReadableProduct readableProduct = new ReadableProduct();
ReadableProductPopulator populator = new ReadableProductPopulator();
populator.setPricingService(pricingService);
populator.setimageUtils(imageUtils);
populator.populate(product, readableProduct, product.getMerchantStore(), language);
return readableProduct;
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class ShoppingCategoryController method getProducts.
private ProductList getProducts(final int start, final int max, final String store, final String language, final String category, final List<QueryFilter> filters, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
List<BigDecimal> prices = new ArrayList<BigDecimal>();
String ref = "";
if (request.getParameter("ref") != null) {
ref = request.getParameter("ref");
}
request.setAttribute("ref", ref);
Map<String, Language> langs = languageService.getLanguagesMap();
if (merchantStore != null) {
if (!merchantStore.getCode().equals(store)) {
// reset for the current request
merchantStore = null;
}
}
if (merchantStore == null) {
merchantStore = merchantStoreService.getByCode(store);
}
if (merchantStore == null) {
LOGGER.error("Merchant store is null for code " + store);
// TODO localized message
response.sendError(503, "Merchant store is null for code " + store);
return null;
}
// get the category by code
Category cat = categoryService.getBySeUrl(merchantStore, category);
if (cat == null) {
LOGGER.error("Category " + category + " is null");
// TODO localized message
response.sendError(503, "Category is null");
return null;
}
String lineage = new StringBuilder().append(cat.getLineage()).toString();
List<Category> categories = categoryService.getListByLineage(store, lineage);
List<Long> ids = new ArrayList<Long>();
if (categories != null && categories.size() > 0) {
for (Category c : categories) {
if (c.isVisible()) {
ids.add(c.getId());
}
}
}
ids.add(cat.getId());
Language lang = langs.get(language);
if (lang == null) {
lang = langs.get(Constants.DEFAULT_LANGUAGE);
}
ProductCriteria productCriteria = new ProductCriteria();
productCriteria.setMaxCount(max);
productCriteria.setStartIndex(start);
productCriteria.setCategoryIds(ids);
productCriteria.setAvailable(true);
if (filters != null) {
for (QueryFilter filter : filters) {
if (filter.getFilterType().name().equals(QueryFilterType.BRAND.name())) {
// the only filter implemented
productCriteria.setManufacturerId(filter.getFilterId());
}
}
}
com.salesmanager.core.model.catalog.product.ProductList products = productService.listByStore(merchantStore, lang, productCriteria);
ReadableProductPopulator populator = new ReadableProductPopulator();
populator.setPricingService(pricingService);
populator.setimageUtils(imageUtils);
ProductList productList = new ProductList();
for (Product product : products.getProducts()) {
// create new proxy product
ReadableProduct p = populator.populate(product, new ReadableProduct(), merchantStore, lang);
productList.getProducts().add(p);
prices.add(p.getPrice());
}
/**
* order products based on the specified order *
*/
Collections.sort(productList.getProducts(), new Comparator<ReadableProduct>() {
@Override
public int compare(ReadableProduct o1, ReadableProduct o2) {
int order1 = o1.getSortOrder();
int order2 = o2.getSortOrder();
return order1 - order2;
}
});
productList.setProductCount(Math.toIntExact(products.getTotalCount()));
if (CollectionUtils.isNotEmpty(prices)) {
BigDecimal minPrice = (BigDecimal) Collections.min(prices);
BigDecimal maxPrice = (BigDecimal) Collections.max(prices);
if (minPrice != null && maxPrice != null) {
productList.setMinPrice(minPrice);
productList.setMaxPrice(maxPrice);
}
}
return productList;
} catch (Exception e) {
LOGGER.error("Error while getting products", e);
response.sendError(503, "An error occured while retrieving products " + e.getMessage());
}
return null;
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class ShoppingCategoryController method getSubCategories.
private List<ReadableCategory> getSubCategories(MerchantStore store, Category category, Map<Long, Long> productCount, Language language, Locale locale) throws Exception {
// sub categories
List<Category> subCategories = categoryService.listByParent(category, language);
ReadableCategoryPopulator populator = new ReadableCategoryPopulator();
List<ReadableCategory> subCategoryProxies = new ArrayList<ReadableCategory>();
for (Category sub : subCategories) {
ReadableCategory cProxy = populator.populate(sub, new ReadableCategory(), store, language);
if (productCount != null) {
Long total = productCount.get(cProxy.getId());
if (total != null) {
cProxy.setProductCount(total.intValue());
}
}
subCategoryProxies.add(cProxy);
}
return subCategoryProxies;
}
use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.
the class StoreFilter method setTopCategories.
@SuppressWarnings("unchecked")
private void setTopCategories(MerchantStore store, Language language, HttpServletRequest request) throws Exception {
StringBuilder categoriesKey = new StringBuilder();
categoriesKey.append(store.getId()).append("_").append(Constants.CATEGORIES_CACHE_KEY).append("-").append(language.getCode());
StringBuilder categoriesKeyMissed = new StringBuilder();
categoriesKeyMissed.append(categoriesKey.toString()).append(Constants.MISSED_CACHE_KEY);
// language code - List of category
Map<String, List<ReadableCategory>> objects = null;
List<ReadableCategory> loadedCategories = null;
if (store.isUseCache()) {
objects = (Map<String, List<ReadableCategory>>) webApplicationCache.getFromCache(categoriesKey.toString());
if (objects == null) {
// load categories
ReadableCategoryList categoryList = categoryFacade.getCategoryHierarchy(store, null, 0, language, null, 0, // null
200);
loadedCategories = categoryList.getCategories();
// filter out invisible category
loadedCategories.stream().filter(cat -> cat.isVisible() == true).collect(Collectors.toList());
objects = new ConcurrentHashMap<String, List<ReadableCategory>>();
objects.put(language.getCode(), loadedCategories);
webApplicationCache.putInCache(categoriesKey.toString(), objects);
} else {
loadedCategories = objects.get(language.getCode());
}
} else {
ReadableCategoryList categoryList = categoryFacade.getCategoryHierarchy(store, null, 0, language, null, 0, // null // filter
200);
loadedCategories = categoryList.getCategories();
}
if (loadedCategories != null) {
request.setAttribute(Constants.REQUEST_TOP_CATEGORIES, loadedCategories);
}
}
Aggregations