use of com.salesmanager.shop.store.api.exception.ConversionRuntimeException in project shopizer by shopizer-ecommerce.
the class ReadableShoppingCartMapper method merge.
@Override
public ReadableShoppingCart merge(ShoppingCart source, ReadableShoppingCart destination, MerchantStore store, Language language) {
Validate.notNull(source, "ShoppingCart cannot be null");
Validate.notNull(destination, "ReadableShoppingCart cannot be null");
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(language, "Language cannot be null");
destination.setCode(source.getShoppingCartCode());
int cartQuantity = 0;
destination.setCustomer(source.getCustomerId());
try {
if (!StringUtils.isBlank(source.getPromoCode())) {
// promo valid 1 day
Date promoDateAdded = source.getPromoAdded();
if (promoDateAdded == null) {
promoDateAdded = new Date();
}
Instant instant = promoDateAdded.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
LocalDate date = zdt.toLocalDate();
// date added < date + 1 day
LocalDate tomorrow = LocalDate.now().plusDays(1);
if (date.isBefore(tomorrow)) {
destination.setPromoCode(source.getPromoCode());
}
}
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = source.getLineItems();
if (items != null) {
for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem item : items) {
ReadableShoppingCartItem shoppingCartItem = new ReadableShoppingCartItem();
readableMinimalProductMapper.merge(item.getProduct(), shoppingCartItem, store, language);
// ReadableProductPopulator readableProductPopulator = new
// ReadableProductPopulator();
// readableProductPopulator.setPricingService(pricingService);
// readableProductPopulator.setimageUtils(imageUtils);
// readableProductPopulator.populate(item.getProduct(), shoppingCartItem, store,
// language);
shoppingCartItem.setPrice(item.getItemPrice());
shoppingCartItem.setFinalPrice(pricingService.getDisplayAmount(item.getItemPrice(), store));
shoppingCartItem.setQuantity(item.getQuantity());
cartQuantity = cartQuantity + item.getQuantity();
BigDecimal subTotal = pricingService.calculatePriceQuantity(item.getItemPrice(), item.getQuantity());
// calculate sub total (price * quantity)
shoppingCartItem.setSubTotal(subTotal);
shoppingCartItem.setDisplaySubTotal(pricingService.getDisplayAmount(subTotal, store));
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> attributes = item.getAttributes();
if (attributes != null) {
for (com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attribute : attributes) {
ProductAttribute productAttribute = productAttributeService.getById(attribute.getProductAttributeId());
if (productAttribute == null) {
LOG.warn("Product attribute with ID " + attribute.getId() + " not found, skipping cart attribute " + attribute.getId());
continue;
}
ReadableShoppingCartAttribute cartAttribute = new ReadableShoppingCartAttribute();
cartAttribute.setId(attribute.getId());
ProductOption option = productAttribute.getProductOption();
ProductOptionValue optionValue = productAttribute.getProductOptionValue();
List<ProductOptionDescription> optionDescriptions = option.getDescriptionsSettoList();
List<ProductOptionValueDescription> optionValueDescriptions = optionValue.getDescriptionsSettoList();
String optName = null;
String optValue = null;
if (!CollectionUtils.isEmpty(optionDescriptions) && !CollectionUtils.isEmpty(optionValueDescriptions)) {
optName = optionDescriptions.get(0).getName();
optValue = optionValueDescriptions.get(0).getName();
for (ProductOptionDescription optionDescription : optionDescriptions) {
if (optionDescription.getLanguage() != null && optionDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
optName = optionDescription.getName();
break;
}
}
for (ProductOptionValueDescription optionValueDescription : optionValueDescriptions) {
if (optionValueDescription.getLanguage() != null && optionValueDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
optValue = optionValueDescription.getName();
break;
}
}
}
if (optName != null) {
ReadableShoppingCartAttributeOption attributeOption = new ReadableShoppingCartAttributeOption();
attributeOption.setCode(option.getCode());
attributeOption.setId(option.getId());
attributeOption.setName(optName);
cartAttribute.setOption(attributeOption);
}
if (optValue != null) {
ReadableShoppingCartAttributeOptionValue attributeOptionValue = new ReadableShoppingCartAttributeOptionValue();
attributeOptionValue.setCode(optionValue.getCode());
attributeOptionValue.setId(optionValue.getId());
attributeOptionValue.setName(optValue);
cartAttribute.setOptionValue(attributeOptionValue);
}
shoppingCartItem.getCartItemattributes().add(cartAttribute);
}
}
destination.getProducts().add(shoppingCartItem);
}
}
// Calculate totals using shoppingCartService
// OrderSummary contains ShoppingCart items
OrderSummary summary = new OrderSummary();
List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> productsList = new ArrayList<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
productsList.addAll(source.getLineItems());
summary.setProducts(productsList);
// OrdetTotalSummary contains all calculations
OrderTotalSummary orderSummary = shoppingCartCalculationService.calculate(source, store, language);
if (CollectionUtils.isNotEmpty(orderSummary.getTotals())) {
if (orderSummary.getTotals().stream().filter(t -> Constants.OT_DISCOUNT_TITLE.equals(t.getOrderTotalCode())).count() == 0) {
// no promo coupon applied
destination.setPromoCode(null);
}
List<ReadableOrderTotal> totals = new ArrayList<ReadableOrderTotal>();
for (com.salesmanager.core.model.order.OrderTotal t : orderSummary.getTotals()) {
ReadableOrderTotal total = new ReadableOrderTotal();
total.setCode(t.getOrderTotalCode());
total.setValue(t.getValue());
total.setText(t.getText());
totals.add(total);
}
destination.setTotals(totals);
}
destination.setSubtotal(orderSummary.getSubTotal());
destination.setDisplaySubTotal(pricingService.getDisplayAmount(orderSummary.getSubTotal(), store));
destination.setTotal(orderSummary.getTotal());
destination.setDisplayTotal(pricingService.getDisplayAmount(orderSummary.getTotal(), store));
destination.setQuantity(cartQuantity);
destination.setId(source.getId());
if (source.getOrderId() != null) {
destination.setOrder(source.getOrderId());
}
} catch (Exception e) {
throw new ConversionRuntimeException("An error occured while converting ReadableShoppingCart", e);
}
return destination;
}
use of com.salesmanager.shop.store.api.exception.ConversionRuntimeException in project shopizer by shopizer-ecommerce.
the class PersistableProductAttributeMapper method merge.
@Override
public ProductAttribute merge(PersistableProductAttribute source, ProductAttribute destination, MerchantStore store, Language language) {
ProductOption productOption = null;
if (!StringUtils.isBlank(source.getOption().getCode())) {
productOption = productOptionService.getByCode(store, source.getOption().getCode());
} else {
Validate.notNull(source.getOption().getId(), "Product option id is null");
productOption = productOptionService.getById(source.getOption().getId());
}
if (productOption == null) {
throw new ConversionRuntimeException("Product option id " + source.getOption().getId() + " does not exist");
}
ProductOptionValue productOptionValue = null;
if (!StringUtils.isBlank(source.getOptionValue().getCode())) {
productOptionValue = productOptionValueService.getByCode(store, source.getOptionValue().getCode());
} else if (source.getProductId() != null && source.getOptionValue().getId().longValue() > 0) {
productOptionValue = productOptionValueService.getById(source.getOptionValue().getId());
} else {
// ProductOption value is text
productOptionValue = new ProductOptionValue();
productOptionValue.setProductOptionDisplayOnly(true);
productOptionValue.setCode(UUID.randomUUID().toString());
productOptionValue.setMerchantStore(store);
}
if (!CollectionUtils.isEmpty((source.getOptionValue().getDescriptions()))) {
productOptionValue = persistableProductOptionValueMapper.merge(source.getOptionValue(), productOptionValue, store, language);
try {
productOptionValueService.saveOrUpdate(productOptionValue);
} catch (ServiceException e) {
throw new ConversionRuntimeException("Error converting ProductOptionValue", e);
}
}
if (productOptionValue == null && !source.isAttributeDisplayOnly()) {
throw new ConversionRuntimeException("Product option value id " + source.getOptionValue().getId() + " does not exist");
}
if (productOption.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionRuntimeException("Invalid product option id ");
}
if (productOptionValue != null && productOptionValue.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionRuntimeException("Invalid product option value id ");
}
if (source.getProductId() != null && source.getProductId().longValue() > 0) {
Product p = productService.getById(source.getProductId());
if (p == null) {
throw new ConversionRuntimeException("Invalid product id ");
}
destination.setProduct(p);
}
if (destination.getId() != null && destination.getId().longValue() > 0) {
destination.setId(destination.getId());
} else {
destination.setId(null);
}
destination.setProductOption(productOption);
destination.setProductOptionValue(productOptionValue);
destination.setProductAttributePrice(source.getProductAttributePrice());
destination.setProductAttributeWeight(source.getProductAttributeWeight());
destination.setProductAttributePrice(source.getProductAttributePrice());
destination.setAttributeDisplayOnly(source.isAttributeDisplayOnly());
return destination;
}
use of com.salesmanager.shop.store.api.exception.ConversionRuntimeException in project shopizer by shopizer-ecommerce.
the class PersistableProductOptionSetMapper method merge.
@Override
public ProductOptionSet merge(PersistableProductOptionSet source, ProductOptionSet destination, MerchantStore store, Language language) {
Validate.notNull(destination, "ProductOptionSet must not be null");
destination.setId(source.getId());
destination.setCode(source.getCode());
destination.setOptionDisplayOnly(source.isReadOnly());
ProductOption option = productOptionService.getById(store, source.getOption());
destination.setOption(option);
if (!CollectionUtils.isEmpty(source.getOptionValues())) {
List<ProductOptionValue> values = source.getOptionValues().stream().map(id -> value(id, store)).collect(Collectors.toList());
destination.setValues(values);
}
if (!CollectionUtils.isEmpty(source.getProductTypes())) {
try {
List<ProductType> types = productTypeService.listProductTypes(source.getProductTypes(), store, language);
Set<ProductType> typesSet = new HashSet<ProductType>(types);
destination.setProductTypes(typesSet);
} catch (ServiceException e) {
throw new ConversionRuntimeException("Error while mpping ProductOptionSet", e);
}
}
return destination;
}
use of com.salesmanager.shop.store.api.exception.ConversionRuntimeException in project shopizer by shopizer-ecommerce.
the class ReadableCatalogCategoryEntryMapper method merge.
@Override
public ReadableCatalogCategoryEntry merge(CatalogCategoryEntry source, ReadableCatalogCategoryEntry destination, MerchantStore store, Language language) {
ReadableCatalogCategoryEntry convertedDestination = Optional.ofNullable(destination).orElse(new ReadableCatalogCategoryEntry());
try {
// ReadableProductPopulator readableProductPopulator = new ReadableProductPopulator();
// readableProductPopulator.setimageUtils(imageUtils);
// readableProductPopulator.setPricingService(pricingService);
// ReadableProduct readableProduct = readableProductPopulator.populate(source.getProduct(), store, language);
ReadableCategory readableCategory = readableCategoryMapper.convert(source.getCategory(), store, language);
convertedDestination.setCatalog(source.getCatalog().getCode());
convertedDestination.setId(source.getId());
convertedDestination.setVisible(source.isVisible());
convertedDestination.setCategory(readableCategory);
// destination.setProduct(readableProduct);
return convertedDestination;
} catch (Exception e) {
throw new ConversionRuntimeException("Error while creating ReadableCatalogEntry", e);
}
}
use of com.salesmanager.shop.store.api.exception.ConversionRuntimeException in project shopizer by shopizer-ecommerce.
the class PersistableProductDefinitionMapper method merge.
@Override
public Product merge(PersistableProductDefinition source, Product destination, MerchantStore store, Language language) {
Validate.notNull(destination, "Product must not be null");
try {
// core properties
destination.setSku(source.getIdentifier());
destination.setAvailable(source.isVisible());
destination.setDateAvailable(new Date());
destination.setRefSku(source.getIdentifier());
if (source.getId() != null && source.getId().longValue() == 0) {
destination.setId(null);
} else {
destination.setId(source.getId());
}
// MANUFACTURER
if (!StringUtils.isBlank(source.getManufacturer())) {
Manufacturer manufacturer = manufacturerService.getByCode(store, source.getManufacturer());
if (manufacturer == null) {
throw new ConversionException("Manufacturer [" + source.getManufacturer() + "] does not exist");
}
destination.setManufacturer(manufacturer);
}
// PRODUCT TYPE
if (!StringUtils.isBlank(source.getType())) {
ProductType type = productTypeService.getByCode(source.getType(), store, language);
if (type == null) {
throw new ConversionException("Product type [" + source.getType() + "] does not exist");
}
destination.setType(type);
}
if (!StringUtils.isBlank(source.getDateAvailable())) {
destination.setDateAvailable(DateUtil.getDate(source.getDateAvailable()));
}
destination.setMerchantStore(store);
List<Language> languages = new ArrayList<Language>();
Set<ProductDescription> descriptions = new HashSet<ProductDescription>();
if (!CollectionUtils.isEmpty(source.getDescriptions())) {
for (com.salesmanager.shop.model.catalog.product.ProductDescription description : source.getDescriptions()) {
ProductDescription productDescription = new ProductDescription();
Language lang = languageService.getByCode(description.getLanguage());
if (lang == null) {
throw new ConversionException("Language code " + description.getLanguage() + " is invalid, use ISO code (en, fr ...)");
}
if (!CollectionUtils.isEmpty(destination.getDescriptions())) {
for (ProductDescription desc : destination.getDescriptions()) {
if (desc.getLanguage().getCode().equals(description.getLanguage())) {
productDescription = desc;
break;
}
}
}
productDescription.setProduct(destination);
productDescription.setDescription(description.getDescription());
productDescription.setProductHighlight(description.getHighlights());
productDescription.setName(description.getName());
productDescription.setSeUrl(description.getFriendlyUrl());
productDescription.setMetatagKeywords(description.getKeyWords());
productDescription.setMetatagDescription(description.getMetaDescription());
productDescription.setTitle(description.getTitle());
languages.add(lang);
productDescription.setLanguage(lang);
descriptions.add(productDescription);
}
}
if (descriptions.size() > 0) {
destination.setDescriptions(descriptions);
}
// if(source.getRating() != null) {
// destination.setProductReviewAvg(new BigDecimal(source.getRating()));
// }
// destination.setProductReviewCount(source.getRatingCount());
/**
* Product definition
*/
ProductAvailability productAvailability = null;
ProductPrice defaultPrice = null;
if (!CollectionUtils.isEmpty(destination.getAvailabilities())) {
for (ProductAvailability avail : destination.getAvailabilities()) {
Set<ProductPrice> prices = avail.getPrices();
for (ProductPrice p : prices) {
if (p.isDefaultPrice()) {
if (productAvailability == null) {
productAvailability = avail;
defaultPrice = p;
productAvailability.setProductQuantity(source.getQuantity());
productAvailability.setProductStatus(source.isCanBePurchased());
p.setProductPriceAmount(source.getPrice());
break;
}
}
}
}
}
if (productAvailability == null) {
// create with default values
productAvailability = new ProductAvailability(destination, store);
destination.getAvailabilities().add(productAvailability);
productAvailability.setProductQuantity(source.getQuantity());
productAvailability.setProductQuantityOrderMin(1);
productAvailability.setProductQuantityOrderMax(1);
productAvailability.setRegion(Constants.ALL_REGIONS);
productAvailability.setAvailable(Boolean.valueOf(destination.isAvailable()));
productAvailability.setProductStatus(source.isCanBePurchased());
}
if (defaultPrice == null) {
BigDecimal defaultPriceAmount = new BigDecimal(0);
if (source.getPrice() != null) {
defaultPriceAmount = source.getPrice();
}
defaultPrice = new ProductPrice();
defaultPrice.setDefaultPrice(true);
defaultPrice.setProductPriceAmount(defaultPriceAmount);
defaultPrice.setCode(ProductPriceEntity.DEFAULT_PRICE_CODE);
defaultPrice.setProductAvailability(productAvailability);
productAvailability.getPrices().add(defaultPrice);
for (Language lang : languages) {
ProductPriceDescription ppd = new ProductPriceDescription();
ppd.setProductPrice(defaultPrice);
ppd.setLanguage(lang);
ppd.setName(ProductPriceDescription.DEFAULT_PRICE_DESCRIPTION);
defaultPrice.getDescriptions().add(ppd);
}
}
if (source.getProductSpecifications() != null) {
destination.setProductHeight(source.getProductSpecifications().getHeight());
destination.setProductLength(source.getProductSpecifications().getLength());
destination.setProductWeight(source.getProductSpecifications().getWeight());
destination.setProductWidth(source.getProductSpecifications().getWidth());
if (source.getProductSpecifications().getManufacturer() != null) {
Manufacturer manuf = null;
if (!StringUtils.isBlank(source.getProductSpecifications().getManufacturer())) {
manuf = manufacturerService.getByCode(store, source.getProductSpecifications().getManufacturer());
}
if (manuf == null) {
throw new ConversionException("Invalid manufacturer id");
}
if (manuf != null) {
if (manuf.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Invalid manufacturer id");
}
destination.setManufacturer(manuf);
}
}
}
destination.setSortOrder(source.getSortOrder());
destination.setProductVirtual(source.isVirtual());
destination.setProductShipeable(source.isShipeable());
// attributes
if (source.getProperties() != null) {
for (com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute attr : source.getProperties()) {
ProductAttribute attribute = persistableProductAttributeMapper.convert(attr, store, language);
attribute.setProduct(destination);
destination.getAttributes().add(attribute);
}
}
// categories
if (!CollectionUtils.isEmpty(source.getCategories())) {
for (com.salesmanager.shop.model.catalog.category.Category categ : source.getCategories()) {
Category c = null;
if (!StringUtils.isBlank(categ.getCode())) {
c = categoryService.getByCode(store, categ.getCode());
} else {
Validate.notNull(categ.getId(), "Category id nust not be null");
c = categoryService.getById(categ.getId(), store.getId());
}
if (c == null) {
throw new ConversionException("Category id " + categ.getId() + " does not exist");
}
if (c.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Invalid category id");
}
destination.getCategories().add(c);
}
}
return destination;
} catch (Exception e) {
throw new ConversionRuntimeException("Error converting product mapper", e);
}
}
Aggregations