use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException 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.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method removeShoppingCartItem.
@Override
@Nullable
public ReadableShoppingCart removeShoppingCartItem(String cartCode, Long productId, MerchantStore merchant, Language language, boolean returnCart) throws Exception {
Validate.notNull(cartCode, "Shopping cart code must not be null");
Validate.notNull(productId, "product id must not be null");
Validate.notNull(merchant, "MerchantStore must not be null");
// get cart
ShoppingCart cart = getCartModel(cartCode, merchant);
if (cart == null) {
throw new ResourceNotFoundException("Cart code [ " + cartCode + " ] not found");
}
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
com.salesmanager.core.model.shoppingcart.ShoppingCartItem itemToDelete = null;
for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem shoppingCartItem : cart.getLineItems()) {
if (shoppingCartItem.getProduct().getId().longValue() == productId.longValue()) {
// get cart item
itemToDelete = getEntryToUpdate(shoppingCartItem.getId(), cart);
// break;
} else {
items.add(shoppingCartItem);
}
}
// delete item
if (itemToDelete != null) {
shoppingCartService.deleteShoppingCartItem(itemToDelete.getId());
}
// remaining items
if (items.size() > 0) {
cart.setLineItems(items);
} else {
cart.getLineItems().clear();
}
// update cart with remaining items
shoppingCartService.saveOrUpdate(cart);
if (items.size() > 0 & returnCart) {
return this.getByCode(cartCode, merchant, language);
}
return null;
}
use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.
the class StoreFacadeImpl method getMerchantStoresByCriteria.
private ReadableMerchantStoreList getMerchantStoresByCriteria(MerchantStoreCriteria criteria, Language language) {
try {
GenericEntityList<MerchantStore> stores = Optional.ofNullable(merchantStoreService.getByCriteria(criteria)).orElseThrow(() -> new ResourceNotFoundException("Criteria did not match any store"));
ReadableMerchantStoreList storeList = new ReadableMerchantStoreList();
storeList.setData((List<ReadableMerchantStore>) stores.getList().stream().map(s -> convertMerchantStoreToReadableMerchantStore(language, s)).collect(Collectors.toList()));
storeList.setTotalPages(stores.getTotalPages());
storeList.setRecordsTotal(stores.getTotalCount());
storeList.setNumber(stores.getList().size());
return storeList;
} catch (ServiceException e) {
throw new ServiceRuntimeException(e);
}
}
use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.
the class StoreFacadeImpl method getChildStores.
@Override
public ReadableMerchantStoreList getChildStores(Language language, String code, int page, int count) {
try {
// first check if store is retailer
MerchantStore retailer = this.getByCode(code);
if (retailer == null) {
throw new ResourceNotFoundException("Merchant [" + code + "] not found");
}
if (retailer.isRetailer() == null || !retailer.isRetailer().booleanValue()) {
throw new ResourceNotFoundException("Merchant [" + code + "] not a retailer");
}
Page<MerchantStore> children = merchantStoreService.listChildren(code, page, count);
List<ReadableMerchantStore> readableStores = new ArrayList<ReadableMerchantStore>();
ReadableMerchantStoreList readableList = new ReadableMerchantStoreList();
if (!CollectionUtils.isEmpty(children.getContent())) {
for (MerchantStore store : children) readableStores.add(convertMerchantStoreToReadableMerchantStore(language, store));
}
readableList.setData(readableStores);
readableList.setRecordsFiltered(children.getSize());
readableList.setTotalPages(children.getTotalPages());
readableList.setRecordsTotal(children.getTotalElements());
readableList.setNumber(children.getNumber());
return readableList;
/* List<MerchantStore> children = merchantStoreService.listChildren(code);
List<ReadableMerchantStore> readableStores = new ArrayList<ReadableMerchantStore>();
if (!CollectionUtils.isEmpty(children)) {
for (MerchantStore store : children)
readableStores.add(convertMerchantStoreToReadableMerchantStore(language, store));
}
return readableStores;*/
} catch (ServiceException e) {
throw new ServiceRuntimeException(e);
}
}
use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.
the class ContentFacadeImpl method getContent.
@Override
public ReadableContentFull getContent(String code, MerchantStore store, Language language) {
Validate.notNull(store, "MerchantStore not null");
Validate.notNull(code, "Content code must not be null");
try {
Content content = contentService.getByCode(code, store);
if (content == null) {
throw new ResourceNotFoundException("No content found with code [" + code + "] for store [" + store.getCode() + "]");
}
return this.convertContentToReadableContentFull(store, language, content);
} catch (ServiceException e) {
throw new ServiceRuntimeException("Error while getting content [" + code + "]", e);
}
}
Aggregations