use of com.salesmanager.core.business.exception.ConversionException in project shopizer by shopizer-ecommerce.
the class ReadableCustomerReviewPopulator method populate.
@Override
public ReadableCustomerReview populate(CustomerReview source, ReadableCustomerReview target, MerchantStore store, Language language) throws ConversionException {
try {
if (target == null) {
target = new ReadableCustomerReview();
}
if (source.getReviewDate() != null) {
target.setDate(DateUtil.formatDate(source.getReviewDate()));
}
ReadableCustomer reviewed = new ReadableCustomer();
reviewed.setId(source.getReviewedCustomer().getId());
reviewed.setFirstName(source.getReviewedCustomer().getBilling().getFirstName());
reviewed.setLastName(source.getReviewedCustomer().getBilling().getLastName());
target.setId(source.getId());
target.setCustomerId(source.getCustomer().getId());
target.setReviewedCustomer(reviewed);
target.setRating(source.getReviewRating());
target.setReviewedCustomer(reviewed);
target.setCustomerId(source.getCustomer().getId());
Set<CustomerReviewDescription> descriptions = source.getDescriptions();
if (CollectionUtils.isNotEmpty(descriptions)) {
CustomerReviewDescription description = null;
if (descriptions.size() > 1) {
for (CustomerReviewDescription desc : descriptions) {
if (desc.getLanguage().getCode().equals(language.getCode())) {
description = desc;
break;
}
}
} else {
description = descriptions.iterator().next();
}
if (description != null) {
target.setDescription(description.getDescription());
target.setLanguage(description.getLanguage().getCode());
}
}
} catch (Exception e) {
throw new ConversionException("Cannot populate ReadableCustomerReview", e);
}
return target;
}
use of com.salesmanager.core.business.exception.ConversionException in project shopizer by shopizer-ecommerce.
the class PersistableManufacturerPopulator method populate.
@Override
public Manufacturer populate(PersistableManufacturer source, Manufacturer target, MerchantStore store, Language language) throws ConversionException {
Validate.notNull(languageService, "Requires to set LanguageService");
try {
target.setMerchantStore(store);
target.setCode(source.getCode());
if (!CollectionUtils.isEmpty(source.getDescriptions())) {
Set<com.salesmanager.core.model.catalog.product.manufacturer.ManufacturerDescription> descriptions = new HashSet<com.salesmanager.core.model.catalog.product.manufacturer.ManufacturerDescription>();
for (ManufacturerDescription description : source.getDescriptions()) {
com.salesmanager.core.model.catalog.product.manufacturer.ManufacturerDescription desc = new com.salesmanager.core.model.catalog.product.manufacturer.ManufacturerDescription();
if (desc.getId() != null && desc.getId().longValue() > 0) {
desc.setId(description.getId());
}
if (target.getDescriptions() != null) {
for (com.salesmanager.core.model.catalog.product.manufacturer.ManufacturerDescription d : target.getDescriptions()) {
if (d.getLanguage().getCode().equals(description.getLanguage()) || desc.getId() != null && d.getId().longValue() == desc.getId().longValue()) {
desc = d;
}
}
}
desc.setManufacturer(target);
desc.setDescription(description.getDescription());
desc.setName(description.getName());
Language lang = languageService.getByCode(description.getLanguage());
if (lang == null) {
throw new ConversionException("Language is null for code " + description.getLanguage() + " use language ISO code [en, fr ...]");
}
desc.setLanguage(lang);
descriptions.add(desc);
}
target.setDescriptions(descriptions);
}
} catch (Exception e) {
throw new ConversionException(e);
}
return target;
}
use of com.salesmanager.core.business.exception.ConversionException in project shopizer by shopizer-ecommerce.
the class OrderProductPopulator method populate.
/**
* Converts a ShoppingCartItem carried in the ShoppingCart to an OrderProduct
* that will be saved in the system
*/
@Override
public OrderProduct populate(ShoppingCartItem source, OrderProduct target, MerchantStore store, Language language) throws ConversionException {
Validate.notNull(productService, "productService must be set");
Validate.notNull(digitalProductService, "digitalProductService must be set");
Validate.notNull(productAttributeService, "productAttributeService must be set");
try {
Product modelProduct = productService.getById(source.getProductId());
if (modelProduct == null) {
throw new ConversionException("Cannot get product with id (productId) " + source.getProductId());
}
if (modelProduct.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Invalid product id " + source.getProductId());
}
DigitalProduct digitalProduct = digitalProductService.getByProduct(store, modelProduct);
if (digitalProduct != null) {
OrderProductDownload orderProductDownload = new OrderProductDownload();
orderProductDownload.setOrderProductFilename(digitalProduct.getProductFileName());
orderProductDownload.setOrderProduct(target);
orderProductDownload.setDownloadCount(0);
orderProductDownload.setMaxdays(ApplicationConstants.MAX_DOWNLOAD_DAYS);
target.getDownloads().add(orderProductDownload);
}
target.setOneTimeCharge(source.getItemPrice());
target.setProductName(source.getProduct().getDescriptions().iterator().next().getName());
target.setProductQuantity(source.getQuantity());
target.setSku(source.getProduct().getSku());
FinalPrice finalPrice = source.getFinalPrice();
if (finalPrice == null) {
throw new ConversionException("Object final price not populated in shoppingCartItem (source)");
}
// Default price
OrderProductPrice orderProductPrice = orderProductPrice(finalPrice);
orderProductPrice.setOrderProduct(target);
Set<OrderProductPrice> prices = new HashSet<OrderProductPrice>();
prices.add(orderProductPrice);
// Other prices
List<FinalPrice> otherPrices = finalPrice.getAdditionalPrices();
if (otherPrices != null) {
for (FinalPrice otherPrice : otherPrices) {
OrderProductPrice other = orderProductPrice(otherPrice);
other.setOrderProduct(target);
prices.add(other);
}
}
target.setPrices(prices);
// OrderProductAttribute
Set<ShoppingCartAttributeItem> attributeItems = source.getAttributes();
if (!CollectionUtils.isEmpty(attributeItems)) {
Set<OrderProductAttribute> attributes = new HashSet<OrderProductAttribute>();
for (ShoppingCartAttributeItem attribute : attributeItems) {
OrderProductAttribute orderProductAttribute = new OrderProductAttribute();
orderProductAttribute.setOrderProduct(target);
Long id = attribute.getProductAttributeId();
ProductAttribute attr = productAttributeService.getById(id);
if (attr == null) {
throw new ConversionException("Attribute id " + id + " does not exists");
}
if (attr.getProduct().getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Attribute id " + id + " invalid for this store");
}
orderProductAttribute.setProductAttributeIsFree(attr.getProductAttributeIsFree());
orderProductAttribute.setProductAttributeName(attr.getProductOption().getDescriptionsSettoList().get(0).getName());
orderProductAttribute.setProductAttributeValueName(attr.getProductOptionValue().getDescriptionsSettoList().get(0).getName());
orderProductAttribute.setProductAttributePrice(attr.getProductAttributePrice());
orderProductAttribute.setProductAttributeWeight(attr.getProductAttributeWeight());
orderProductAttribute.setProductOptionId(attr.getProductOption().getId());
orderProductAttribute.setProductOptionValueId(attr.getProductOptionValue().getId());
attributes.add(orderProductAttribute);
}
target.setOrderAttributes(attributes);
}
} catch (Exception e) {
throw new ConversionException(e);
}
return target;
}
use of com.salesmanager.core.business.exception.ConversionException in project shopizer by shopizer-ecommerce.
the class PersistableOrderApiPopulator method populate.
@Override
public Order populate(PersistableOrder source, Order target, MerchantStore store, Language language) throws ConversionException {
/* Validate.notNull(currencyService,"currencyService must be set");
Validate.notNull(customerService,"customerService must be set");
Validate.notNull(shoppingCartService,"shoppingCartService must be set");
Validate.notNull(productService,"productService must be set");
Validate.notNull(productAttributeService,"productAttributeService must be set");
Validate.notNull(digitalProductService,"digitalProductService must be set");*/
Validate.notNull(source.getPayment(), "Payment cannot be null");
try {
if (target == null) {
target = new Order();
}
// target.setLocale(LocaleUtils.getLocale(store));
target.setLocale(LocaleUtils.getLocale(store));
Currency currency = null;
try {
currency = currencyService.getByCode(source.getCurrency());
} catch (Exception e) {
throw new ConversionException("Currency not found for code " + source.getCurrency());
}
if (currency == null) {
throw new ConversionException("Currency not found for code " + source.getCurrency());
}
// Customer
Customer customer = null;
if (source.getCustomerId() != null && source.getCustomerId().longValue() > 0) {
Long customerId = source.getCustomerId();
customer = customerService.getById(customerId);
if (customer == null) {
throw new ConversionException("Curstomer with id " + source.getCustomerId() + " does not exist");
}
target.setCustomerId(customerId);
} else {
if (source instanceof PersistableAnonymousOrder) {
PersistableCustomer persistableCustomer = ((PersistableAnonymousOrder) source).getCustomer();
customer = new Customer();
customer = customerPopulator.populate(persistableCustomer, customer, store, language);
} else {
throw new ConversionException("Curstomer details or id not set in request");
}
}
target.setCustomerEmailAddress(customer.getEmailAddress());
Delivery delivery = customer.getDelivery();
target.setDelivery(delivery);
Billing billing = customer.getBilling();
target.setBilling(billing);
if (source.getAttributes() != null && source.getAttributes().size() > 0) {
Set<OrderAttribute> attrs = new HashSet<OrderAttribute>();
for (com.salesmanager.shop.model.order.OrderAttribute attribute : source.getAttributes()) {
OrderAttribute attr = new OrderAttribute();
attr.setKey(attribute.getKey());
attr.setValue(attribute.getValue());
attr.setOrder(target);
attrs.add(attr);
}
target.setOrderAttributes(attrs);
}
target.setDatePurchased(new Date());
target.setCurrency(currency);
target.setCurrencyValue(new BigDecimal(0));
target.setMerchant(store);
target.setChannel(OrderChannel.API);
// need this
target.setStatus(OrderStatus.ORDERED);
target.setPaymentModuleCode(source.getPayment().getPaymentModule());
target.setPaymentType(PaymentType.valueOf(source.getPayment().getPaymentType()));
target.setCustomerAgreement(source.isCustomerAgreement());
// force this to true, cannot perform this activity from the API
target.setConfirmedAddress(true);
if (!StringUtils.isBlank(source.getComments())) {
OrderStatusHistory statusHistory = new OrderStatusHistory();
statusHistory.setStatus(null);
statusHistory.setOrder(target);
statusHistory.setComments(source.getComments());
target.getOrderHistory().add(statusHistory);
}
return target;
} catch (Exception e) {
throw new ConversionException(e);
}
}
use of com.salesmanager.core.business.exception.ConversionException in project shopizer by shopizer-ecommerce.
the class PersistableOrderPopulator method populate.
@Override
public Order populate(PersistableOrder source, Order target, MerchantStore store, Language language) throws ConversionException {
Validate.notNull(productService, "productService must be set");
Validate.notNull(digitalProductService, "digitalProductService must be set");
Validate.notNull(productAttributeService, "productAttributeService must be set");
Validate.notNull(customerService, "customerService must be set");
Validate.notNull(countryService, "countryService must be set");
Validate.notNull(zoneService, "zoneService must be set");
Validate.notNull(currencyService, "currencyService must be set");
try {
Map<String, Country> countriesMap = countryService.getCountriesMap(language);
Map<String, Zone> zonesMap = zoneService.getZones(language);
/**
* customer *
*/
PersistableCustomer customer = source.getCustomer();
if (customer != null) {
if (customer.getId() != null && customer.getId() > 0) {
Customer modelCustomer = customerService.getById(customer.getId());
if (modelCustomer == null) {
throw new ConversionException("Customer id " + customer.getId() + " does not exists");
}
if (modelCustomer.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Customer id " + customer.getId() + " does not exists for store " + store.getCode());
}
target.setCustomerId(modelCustomer.getId());
target.setBilling(modelCustomer.getBilling());
target.setDelivery(modelCustomer.getDelivery());
target.setCustomerEmailAddress(source.getCustomer().getEmailAddress());
}
}
target.setLocale(LocaleUtils.getLocale(store));
CreditCard creditCard = source.getCreditCard();
if (creditCard != null) {
String maskedNumber = CreditCardUtils.maskCardNumber(creditCard.getCcNumber());
creditCard.setCcNumber(maskedNumber);
target.setCreditCard(creditCard);
}
Currency currency = null;
try {
currency = currencyService.getByCode(source.getCurrency());
} catch (Exception e) {
throw new ConversionException("Currency not found for code " + source.getCurrency());
}
if (currency == null) {
throw new ConversionException("Currency not found for code " + source.getCurrency());
}
target.setCurrency(currency);
target.setDatePurchased(source.getDatePurchased());
// target.setCurrency(store.getCurrency());
target.setCurrencyValue(new BigDecimal(0));
target.setMerchant(store);
target.setStatus(source.getOrderStatus());
target.setPaymentModuleCode(source.getPaymentModule());
target.setPaymentType(source.getPaymentType());
target.setShippingModuleCode(source.getShippingModule());
target.setCustomerAgreement(source.isCustomerAgreed());
target.setConfirmedAddress(source.isConfirmedAddress());
if (source.getPreviousOrderStatus() != null) {
List<OrderStatus> orderStatusList = source.getPreviousOrderStatus();
for (OrderStatus status : orderStatusList) {
OrderStatusHistory statusHistory = new OrderStatusHistory();
statusHistory.setStatus(status);
statusHistory.setOrder(target);
target.getOrderHistory().add(statusHistory);
}
}
if (!StringUtils.isBlank(source.getComments())) {
OrderStatusHistory statusHistory = new OrderStatusHistory();
statusHistory.setStatus(null);
statusHistory.setOrder(target);
statusHistory.setComments(source.getComments());
target.getOrderHistory().add(statusHistory);
}
List<PersistableOrderProduct> products = source.getOrderProductItems();
if (CollectionUtils.isEmpty(products)) {
throw new ConversionException("Requires at least 1 PersistableOrderProduct");
}
com.salesmanager.shop.populator.order.PersistableOrderProductPopulator orderProductPopulator = new PersistableOrderProductPopulator();
orderProductPopulator.setProductAttributeService(productAttributeService);
orderProductPopulator.setProductService(productService);
orderProductPopulator.setDigitalProductService(digitalProductService);
for (PersistableOrderProduct orderProduct : products) {
OrderProduct modelOrderProduct = new OrderProduct();
orderProductPopulator.populate(orderProduct, modelOrderProduct, store, language);
target.getOrderProducts().add(modelOrderProduct);
}
List<OrderTotal> orderTotals = source.getTotals();
if (CollectionUtils.isNotEmpty(orderTotals)) {
for (OrderTotal total : orderTotals) {
com.salesmanager.core.model.order.OrderTotal totalModel = new com.salesmanager.core.model.order.OrderTotal();
totalModel.setModule(total.getModule());
totalModel.setOrder(target);
totalModel.setOrderTotalCode(total.getCode());
totalModel.setTitle(total.getTitle());
totalModel.setValue(total.getValue());
target.getOrderTotal().add(totalModel);
}
}
} catch (Exception e) {
throw new ConversionException(e);
}
return target;
}
Aggregations