use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute 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.product.attribute.ProductAttribute in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method createCartItem.
// used for api
private com.salesmanager.core.model.shoppingcart.ShoppingCartItem createCartItem(ShoppingCart cartModel, PersistableShoppingCartItem shoppingCartItem, MerchantStore store) throws Exception {
Product product = productService.getById(shoppingCartItem.getProduct());
if (product == null) {
throw new ResourceNotFoundException("Item with id " + shoppingCartItem.getProduct() + " does not exist");
}
if (product.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ResourceNotFoundException("Item with id " + shoppingCartItem.getProduct() + " does not belong to merchant " + store.getId());
}
/**
* Check if product quantity is 0
* Check if product is available
* Check if date available <= now
*/
Set<ProductAvailability> availabilities = product.getAvailabilities();
if (availabilities == null) {
throw new Exception("Item with id " + product.getId() + " is not properly configured");
}
for (ProductAvailability availability : availabilities) {
if (availability.getProductQuantity() == null || availability.getProductQuantity().intValue() == 0) {
throw new Exception("Item with id " + product.getId() + " is not available");
}
}
if (!product.isAvailable()) {
throw new Exception("Item with id " + product.getId() + " is not available");
}
if (!DateUtil.dateBeforeEqualsDate(product.getDateAvailable(), new Date())) {
throw new Exception("Item with id " + product.getId() + " is not available");
}
com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = shoppingCartService.populateShoppingCartItem(product);
item.setQuantity(shoppingCartItem.getQuantity());
item.setShoppingCart(cartModel);
// 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() == product.getId().longValue()) {
com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attributeItem = new com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem(item, productAttribute);
item.addAttributes(attributeItem);
}
}
}
return item;
}
use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method updateCartItems.
// TODO promoCode request parameter
@Override
public ShoppingCartData updateCartItems(Optional<String> promoCode, final List<ShoppingCartItem> shoppingCartItems, final MerchantStore store, final Language language) throws Exception {
Validate.notEmpty(shoppingCartItems, "shoppingCartItems null or empty");
ShoppingCart cartModel = null;
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> cartItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
for (ShoppingCartItem item : shoppingCartItems) {
if (item.getQuantity() < 1) {
throw new CartModificationException("Quantity must not be less than one");
}
if (cartModel == null) {
cartModel = getCartModel(item.getCode(), store);
}
com.salesmanager.core.model.shoppingcart.ShoppingCartItem entryToUpdate = getEntryToUpdate(item.getId(), cartModel);
if (entryToUpdate == null) {
throw new CartModificationException("Unknown entry number.");
}
entryToUpdate.getProduct();
LOG.info("Updating cart entry quantity to" + item.getQuantity());
entryToUpdate.setQuantity((int) item.getQuantity());
List<ProductAttribute> productAttributes = new ArrayList<ProductAttribute>();
productAttributes.addAll(entryToUpdate.getProduct().getAttributes());
final FinalPrice finalPrice = productPriceUtils.getFinalProductPrice(entryToUpdate.getProduct(), productAttributes);
entryToUpdate.setItemPrice(finalPrice.getFinalPrice());
cartItems.add(entryToUpdate);
}
cartModel.setPromoCode(null);
if (promoCode.isPresent()) {
cartModel.setPromoCode(promoCode.get());
cartModel.setPromoAdded(new Date());
}
cartModel.setLineItems(cartItems);
shoppingCartService.saveOrUpdate(cartModel);
LOG.info("Cart entry updated with desired quantity");
ShoppingCartDataPopulator shoppingCartDataPopulator = new ShoppingCartDataPopulator();
shoppingCartDataPopulator.setShoppingCartCalculationService(shoppingCartCalculationService);
shoppingCartDataPopulator.setPricingService(pricingService);
shoppingCartDataPopulator.setimageUtils(imageUtils);
return shoppingCartDataPopulator.populate(cartModel, store, language);
}
use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method createCartItem.
private com.salesmanager.core.model.shoppingcart.ShoppingCartItem createCartItem(final ShoppingCart cartModel, final ShoppingCartItem shoppingCartItem, final 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());
}
/**
* Check if product quantity is 0
* Check if product is available
* Check if date available <= now
*/
Set<ProductAvailability> availabilities = product.getAvailabilities();
if (availabilities == null) {
throw new Exception("Item with id " + product.getId() + " is not properly configured");
}
for (ProductAvailability availability : availabilities) {
if (availability.getProductQuantity() == null || availability.getProductQuantity().intValue() == 0) {
throw new Exception("Item with id " + product.getId() + " is not available");
}
}
if (!product.isAvailable()) {
throw new Exception("Item with id " + product.getId() + " is not available");
}
if (!DateUtil.dateBeforeEqualsDate(product.getDateAvailable(), new Date())) {
throw new Exception("Item with id " + product.getId() + " is not available");
}
com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = shoppingCartService.populateShoppingCartItem(product);
item.setQuantity(shoppingCartItem.getQuantity());
item.setShoppingCart(cartModel);
// attributes
List<ShoppingCartAttribute> cartAttributes = shoppingCartItem.getShoppingCartAttributes();
if (!CollectionUtils.isEmpty(cartAttributes)) {
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);
item.addAttributes(attributeItem);
}
}
}
return item;
}
use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute 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;
}
Aggregations