use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method createCartItems.
// used for api
private List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> createCartItems(ShoppingCart cartModel, List<PersistableShoppingCartItem> shoppingCartItems, MerchantStore store) throws Exception {
List<Long> productIds = shoppingCartItems.stream().map(s -> Long.valueOf(s.getProduct())).collect(Collectors.toList());
List<Product> products = productService.getProductsByIds(productIds);
if (products == null || products.size() != shoppingCartItems.size()) {
LOG.warn("----------------------- Items with in id-list " + productIds + " does not exist");
throw new ResourceNotFoundException("Item with id " + productIds + " does not exist");
}
List<Product> wrongStoreProducts = products.stream().filter(p -> p.getMerchantStore().getId() != store.getId()).collect(Collectors.toList());
if (wrongStoreProducts.size() > 0) {
throw new ResourceNotFoundException("One or more of the items with id's " + wrongStoreProducts.stream().map(s -> Long.valueOf(s.getId())).collect(Collectors.toList()) + " does not belong to merchant " + store.getId());
}
List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = new ArrayList<>();
for (Product p : products) {
com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = shoppingCartService.populateShoppingCartItem(p);
Optional<PersistableShoppingCartItem> oShoppingCartItem = shoppingCartItems.stream().filter(i -> i.getProduct() == p.getId()).findFirst();
if (!oShoppingCartItem.isPresent()) {
// Should never happen if not something is updated in realtime or user has item in local storage and add it long time after to cart!
LOG.warn("Missing shoppingCartItem for product " + p.getSku() + " ( " + p.getId() + " )");
continue;
}
PersistableShoppingCartItem shoppingCartItem = oShoppingCartItem.get();
item.setQuantity(shoppingCartItem.getQuantity());
item.setShoppingCart(cartModel);
/**
* Check if product is available
* Check if product quantity is 0
* Check if date available <= now
*/
if (!p.isAvailable()) {
throw new Exception("Item with id " + p.getId() + " is not available");
}
Set<ProductAvailability> availabilities = p.getAvailabilities();
if (availabilities == null) {
throw new Exception("Item with id " + p.getId() + " is not properly configured");
}
for (ProductAvailability availability : availabilities) {
if (availability.getProductQuantity() == null || availability.getProductQuantity().intValue() == 0) {
throw new Exception("Item with id " + p.getId() + " is not available");
}
}
if (!DateUtil.dateBeforeEqualsDate(p.getDateAvailable(), new Date())) {
throw new Exception("Item with id " + p.getId() + " is not available");
}
// end qty & availablility checks
// set attributes
List<com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute> attributes = shoppingCartItem.getAttributes();
if (!CollectionUtils.isEmpty(attributes)) {
for (com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute attribute : attributes) {
ProductAttribute productAttribute = productAttributeService.getById(attribute.getId());
if (productAttribute != null && productAttribute.getProduct().getId().longValue() == p.getId().longValue()) {
com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attributeItem = new com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem(item, productAttribute);
item.addAttributes(attributeItem);
}
}
}
items.add(item);
}
return items;
}
use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.
the class ProductServiceImpl method listByStore.
@Override
public Page<Product> listByStore(MerchantStore store, Language language, ProductCriteria criteria, int page, int count) {
criteria.setPageSize(page);
criteria.setPageSize(count);
criteria.setLegacyPagination(false);
ProductList productList = productRepository.listByStore(store, language, criteria);
PageRequest pageRequest = PageRequest.of(page, count);
@SuppressWarnings({ "rawtypes", "unchecked" }) Page<Product> p = new PageImpl(productList.getProducts(), pageRequest, productList.getTotalCount());
return p;
}
use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.
the class ProductServiceImpl method getProductForLocale.
@Override
public Product getProductForLocale(long productId, Language language, Locale locale) throws ServiceException {
Product product = productRepository.getProductForLocale(productId, language, locale);
if (product == null) {
return null;
}
CatalogServiceHelper.setToAvailability(product, locale);
CatalogServiceHelper.setToLanguage(product, language.getId());
return product;
}
use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.
the class CategoryServiceImpl method delete.
// @Override
public void delete(Category category) throws ServiceException {
// get category with lineage (subcategories)
StringBuilder lineage = new StringBuilder();
lineage.append(category.getLineage()).append(category.getId()).append(Constants.SLASH);
List<Category> categories = this.getListByLineage(category.getMerchantStore(), lineage.toString());
Category dbCategory = getById(category.getId(), category.getMerchantStore().getId());
if (dbCategory != null && dbCategory.getId().longValue() == category.getId().longValue()) {
categories.add(dbCategory);
Collections.reverse(categories);
List<Long> categoryIds = new ArrayList<Long>();
for (Category c : categories) {
categoryIds.add(c.getId());
}
List<Product> products = productService.getProducts(categoryIds);
for (Product product : products) {
// session.evict(product);// refresh product so we get all
// product categories
Product dbProduct = productService.getById(product.getId());
Set<Category> productCategories = dbProduct.getCategories();
if (productCategories.size() > 1) {
for (Category c : categories) {
productCategories.remove(c);
productService.update(dbProduct);
}
if (product.getCategories() == null || product.getCategories().size() == 0) {
productService.delete(dbProduct);
}
} else {
productService.delete(dbProduct);
}
}
Category categ = getById(category.getId(), category.getMerchantStore().getId());
categoryRepository.delete(categ);
}
}
use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.
the class ShoppingCartModelPopulator method createCartItem.
private com.salesmanager.core.model.shoppingcart.ShoppingCartItem createCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCart cart, ShoppingCartItem shoppingCartItem, MerchantStore store) throws Exception {
Product product = productService.getById(shoppingCartItem.getProductId());
if (product == null) {
throw new Exception("Item with id " + shoppingCartItem.getProductId() + " does not exist");
}
if (product.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new Exception("Item with id " + shoppingCartItem.getProductId() + " does not belong to merchant " + store.getId());
}
com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = new com.salesmanager.core.model.shoppingcart.ShoppingCartItem(cart, product);
item.setQuantity(shoppingCartItem.getQuantity());
item.setItemPrice(shoppingCartItem.getProductPrice());
item.setShoppingCart(cart);
// attributes
List<ShoppingCartAttribute> cartAttributes = shoppingCartItem.getShoppingCartAttributes();
if (!CollectionUtils.isEmpty(cartAttributes)) {
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> newAttributes = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem>();
for (ShoppingCartAttribute attribute : cartAttributes) {
ProductAttribute productAttribute = productAttributeService.getById(attribute.getAttributeId());
if (productAttribute != null && productAttribute.getProduct().getId().longValue() == product.getId().longValue()) {
com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attributeItem = new com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem(item, productAttribute);
if (attribute.getAttributeId() > 0) {
attributeItem.setId(attribute.getId());
}
item.addAttributes(attributeItem);
// newAttributes.add( attributeItem );
}
}
// item.setAttributes( newAttributes );
}
return item;
}
Aggregations