use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.
the class ReadableProductDefinitionMapper method merge.
@Override
public ReadableProductDefinition merge(Product source, ReadableProductDefinition destination, MerchantStore store, Language language) {
Validate.notNull(source, "Product cannot be null");
Validate.notNull(destination, "Product destination cannot be null");
ReadableProductDefinition returnDestination = destination;
if (language == null) {
returnDestination = new ReadableProductDefinitionFull();
}
List<com.salesmanager.shop.model.catalog.product.ProductDescription> fulldescriptions = new ArrayList<com.salesmanager.shop.model.catalog.product.ProductDescription>();
returnDestination.setIdentifier(source.getSku());
returnDestination.setId(source.getId());
returnDestination.setVisible(source.isAvailable());
returnDestination.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
ProductDescription description = null;
if (source.getDescriptions() != null && source.getDescriptions().size() > 0) {
for (ProductDescription desc : source.getDescriptions()) {
if (language != null && desc.getLanguage() != null && desc.getLanguage().getId().intValue() == language.getId().intValue()) {
description = desc;
break;
} else {
fulldescriptions.add(populateDescription(desc));
}
}
}
if (description != null) {
com.salesmanager.shop.model.catalog.product.ProductDescription tragetDescription = populateDescription(description);
returnDestination.setDescription(tragetDescription);
}
if (source.getManufacturer() != null) {
ReadableManufacturer manufacturer = readableManufacturerMapper.convert(source.getManufacturer(), store, language);
returnDestination.setManufacturer(manufacturer);
}
if (!CollectionUtils.isEmpty(source.getCategories())) {
List<ReadableCategory> categoryList = new ArrayList<ReadableCategory>();
for (Category category : source.getCategories()) {
ReadableCategory readableCategory = readableCategoryMapper.convert(category, store, language);
categoryList.add(readableCategory);
}
returnDestination.setCategories(categoryList);
}
ProductSpecification specifications = new ProductSpecification();
specifications.setHeight(source.getProductHeight());
specifications.setLength(source.getProductLength());
specifications.setWeight(source.getProductWeight());
specifications.setWidth(source.getProductWidth());
if (!StringUtils.isBlank(store.getSeizeunitcode())) {
specifications.setDimensionUnitOfMeasure(DimensionUnitOfMeasure.valueOf(store.getSeizeunitcode().toLowerCase()));
}
if (!StringUtils.isBlank(store.getWeightunitcode())) {
specifications.setWeightUnitOfMeasure(WeightUnitOfMeasure.valueOf(store.getWeightunitcode().toLowerCase()));
}
returnDestination.setProductSpecifications(specifications);
if (source.getType() != null) {
ReadableProductType readableType = readableProductTypeMapper.convert(source.getType(), store, language);
returnDestination.setType(readableType);
}
returnDestination.setSortOrder(source.getSortOrder());
// images
Set<ProductImage> images = source.getImages();
if (CollectionUtils.isNotEmpty(images)) {
List<ReadableImage> imageList = images.stream().map(i -> this.convertImage(source, i, store)).collect(Collectors.toList());
returnDestination.setImages(imageList);
}
// quantity
ProductAvailability availability = null;
for (ProductAvailability a : source.getAvailabilities()) {
availability = a;
returnDestination.setCanBePurchased(availability.getProductStatus());
returnDestination.setQuantity(availability.getProductQuantity() == null ? 1 : availability.getProductQuantity());
}
FinalPrice price = null;
try {
price = pricingService.calculateProductPrice(source);
} catch (ServiceException e) {
throw new ConversionRuntimeException("Unable to get product price", e);
}
if (price != null) {
returnDestination.setPrice(price.getStringPrice());
}
if (returnDestination instanceof ReadableProductDefinitionFull) {
((ReadableProductDefinitionFull) returnDestination).setDescriptions(fulldescriptions);
}
return returnDestination;
}
use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.
the class ProductFacadeImpl method deleteProduct.
@Override
public void deleteProduct(Long id, MerchantStore store) {
Validate.notNull(id, "Product id cannot be null");
Validate.notNull(store, "store cannot be null");
Product p = productService.getById(id);
if (p == null) {
throw new ResourceNotFoundException("Product with id [" + id + " not found");
}
if (p.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ResourceNotFoundException("Product with id [" + id + " not found for store [" + store.getCode() + "]");
}
try {
productService.delete(p);
} catch (ServiceException e) {
throw new ServiceRuntimeException("Error while deleting ptoduct with id [" + id + "]", e);
}
}
use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.
the class ShoppingOrderController method commitOrder.
private Order commitOrder(ShopOrder order, HttpServletRequest request, Locale locale) throws Exception, ServiceException {
LOGGER.info("Entering comitOrder");
MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
Language language = (Language) request.getAttribute("LANGUAGE");
String userName = null;
String password = null;
PersistableCustomer customer = order.getCustomer();
/**
* set username and password to persistable object *
*/
LOGGER.info("Set username and password to customer");
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer authCustomer = null;
if (auth != null && request.isUserInRole("AUTH_CUSTOMER")) {
LOGGER.info("Customer authenticated");
authCustomer = customerFacade.getCustomerByUserName(auth.getName(), store);
// set id and authentication information
customer.setUserName(authCustomer.getNick());
// customer.setEncodedPassword(authCustomer.getPassword());
customer.setId(authCustomer.getId());
} else {
// set customer id to null
customer.setId(null);
}
// if the customer is new, generate a password
LOGGER.info("New customer generate password");
if (customer.getId() == null || customer.getId() == 0) {
// new customer
// password = UserReset.generateRandomString();
// String encodedPassword = passwordEncoder.encode(password);
// customer.setEncodedPassword(encodedPassword);
}
if (order.isShipToBillingAdress()) {
customer.setDelivery(customer.getBilling());
}
LOGGER.info("Before creating new volatile");
Customer modelCustomer = null;
try {
// set groups
if (authCustomer == null) {
// not authenticated, create a new volatile user
modelCustomer = customerFacade.getCustomerModel(customer, store, language);
customerFacade.setCustomerModelDefaultProperties(modelCustomer, store);
userName = modelCustomer.getNick();
LOGGER.debug("About to persist volatile customer to database.");
if (modelCustomer.getDefaultLanguage() == null) {
modelCustomer.setDefaultLanguage(languageService.toLanguage(locale));
}
customerService.saveOrUpdate(modelCustomer);
} else {
// use existing customer
LOGGER.info("Populate customer model");
modelCustomer = customerFacade.populateCustomerModel(authCustomer, customer, store, language);
}
} catch (Exception e) {
throw new ServiceException(e);
}
LOGGER.debug("About to save transaction");
Order modelOrder = null;
Transaction initialTransaction = (Transaction) super.getSessionAttribute(Constants.INIT_TRANSACTION_KEY, request);
if (initialTransaction != null) {
modelOrder = orderFacade.processOrder(order, modelCustomer, initialTransaction, store, language);
} else {
modelOrder = orderFacade.processOrder(order, modelCustomer, store, language);
}
// save order id in session
super.setSessionAttribute(Constants.ORDER_ID, modelOrder.getId(), request);
// set a unique token for confirmation
super.setSessionAttribute(Constants.ORDER_ID_TOKEN, modelOrder.getId(), request);
LOGGER.debug("Transaction ended and order saved");
LOGGER.debug("Remove cart");
// get cart
String cartCode = super.getSessionAttribute(Constants.SHOPPING_CART, request);
if (StringUtils.isNotBlank(cartCode)) {
try {
shoppingCartFacade.setOrderId(cartCode, modelOrder.getId(), store);
} catch (Exception e) {
LOGGER.error("Cannot update cart " + cartCode, e);
throw new ServiceException(e);
}
}
// cleanup the order objects
super.removeAttribute(Constants.ORDER, request);
super.removeAttribute(Constants.ORDER_SUMMARY, request);
super.removeAttribute(Constants.INIT_TRANSACTION_KEY, request);
super.removeAttribute(Constants.SHIPPING_OPTIONS, request);
super.removeAttribute(Constants.SHIPPING_SUMMARY, request);
super.removeAttribute(Constants.SHOPPING_CART, request);
LOGGER.debug("Refresh customer");
try {
// refresh customer --
modelCustomer = customerFacade.getCustomerByUserName(modelCustomer.getNick(), store);
// if has downloads, authenticate
// check if any downloads exist for this order6
List<OrderProductDownload> orderProductDownloads = orderProdctDownloadService.getByOrderId(modelOrder.getId());
if (CollectionUtils.isNotEmpty(orderProductDownloads)) {
LOGGER.debug("Is user authenticated ? ", auth.isAuthenticated());
if (auth != null && request.isUserInRole("AUTH_CUSTOMER")) {
// already authenticated
} else {
// authenticate
customerFacade.authenticate(modelCustomer, userName, password);
super.setSessionAttribute(Constants.CUSTOMER, modelCustomer, request);
}
// send new user registration template
if (order.getCustomer().getId() == null || order.getCustomer().getId().longValue() == 0) {
// send email for new customer
// set clear password for email
customer.setPassword(password);
customer.setUserName(userName);
emailTemplatesUtils.sendRegistrationEmail(customer, store, locale, request.getContextPath());
}
}
// send order confirmation email to customer
emailTemplatesUtils.sendOrderEmail(modelCustomer.getEmailAddress(), modelCustomer, modelOrder, locale, language, store, request.getContextPath());
if (orderService.hasDownloadFiles(modelOrder)) {
emailTemplatesUtils.sendOrderDownloadEmail(modelCustomer, modelOrder, store, locale, request.getContextPath());
}
// send order confirmation email to merchant
emailTemplatesUtils.sendOrderEmail(store.getStoreEmailAddress(), modelCustomer, modelOrder, locale, language, store, request.getContextPath());
} catch (Exception e) {
LOGGER.error("Error while post processing order", e);
}
return modelOrder;
}
use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.
the class PackingBox method getBoxPackagesDetails.
@Override
public List<PackageDetails> getBoxPackagesDetails(List<ShippingProduct> products, MerchantStore store) throws ServiceException {
if (products == null) {
throw new ServiceException("Product list cannot be null !!");
}
double width = 0;
double length = 0;
double height = 0;
double weight = 0;
double maxweight = 0;
// int treshold = 0;
ShippingConfiguration shippingConfiguration = shippingService.getShippingConfiguration(store);
if (shippingConfiguration == null) {
throw new ServiceException("ShippingConfiguration not found for merchant " + store.getCode());
}
width = (double) shippingConfiguration.getBoxWidth();
length = (double) shippingConfiguration.getBoxLength();
height = (double) shippingConfiguration.getBoxHeight();
weight = shippingConfiguration.getBoxWeight();
maxweight = shippingConfiguration.getMaxWeight();
List<PackageDetails> boxes = new ArrayList<PackageDetails>();
// maximum number of boxes
int maxBox = 100;
int iterCount = 0;
List<Product> individualProducts = new ArrayList<Product>();
// need to put items individually
for (ShippingProduct shippingProduct : products) {
Product product = shippingProduct.getProduct();
if (product.isProductVirtual()) {
continue;
}
int qty = shippingProduct.getQuantity();
Set<ProductAttribute> attrs = shippingProduct.getProduct().getAttributes();
// set attributes values
BigDecimal w = product.getProductWeight();
BigDecimal h = product.getProductHeight();
BigDecimal l = product.getProductLength();
BigDecimal wd = product.getProductWidth();
if (w == null) {
w = new BigDecimal(defaultWeight);
}
if (h == null) {
h = new BigDecimal(defaultHeight);
}
if (l == null) {
l = new BigDecimal(defaultLength);
}
if (wd == null) {
wd = new BigDecimal(defaultWidth);
}
if (attrs != null && attrs.size() > 0) {
for (ProductAttribute attribute : attrs) {
if (attribute.getProductAttributeWeight() != null) {
w = w.add(attribute.getProductAttributeWeight());
}
}
}
if (qty > 1) {
for (int i = 1; i <= qty; i++) {
Product temp = new Product();
temp.setProductHeight(h);
temp.setProductLength(l);
temp.setProductWidth(wd);
temp.setProductWeight(w);
temp.setAttributes(product.getAttributes());
temp.setDescriptions(product.getDescriptions());
individualProducts.add(temp);
}
} else {
Product temp = new Product();
temp.setProductHeight(h);
temp.setProductLength(l);
temp.setProductWidth(wd);
temp.setProductWeight(w);
temp.setAttributes(product.getAttributes());
temp.setDescriptions(product.getDescriptions());
individualProducts.add(temp);
}
iterCount++;
}
if (iterCount == 0) {
return null;
}
int productCount = individualProducts.size();
List<PackingBox> boxesList = new ArrayList<PackingBox>();
// start the creation of boxes
PackingBox box = new PackingBox();
// set box max volume
double maxVolume = width * length * height;
if (maxVolume == 0 || maxweight == 0) {
merchantLogService.save(new MerchantLog(store, "shipping", "Check shipping box configuration, it has a volume of " + maxVolume + " and a maximum weight of " + maxweight + ". Those values must be greater than 0."));
throw new ServiceException("Product configuration exceeds box configuraton");
}
box.setVolumeLeft(maxVolume);
box.setWeightLeft(maxweight);
// assign first box
boxesList.add(box);
// int boxCount = 1;
List<Product> assignedProducts = new ArrayList<Product>();
// calculate the volume for the next object
if (assignedProducts.size() > 0) {
individualProducts.removeAll(assignedProducts);
assignedProducts = new ArrayList<Product>();
}
boolean productAssigned = false;
for (Product p : individualProducts) {
// Set<ProductAttribute> attributes = p.getAttributes();
productAssigned = false;
double productWeight = p.getProductWeight().doubleValue();
// validate if product fits in the box
if (p.getProductWidth().doubleValue() > width || p.getProductHeight().doubleValue() > height || p.getProductLength().doubleValue() > length) {
// log message to customer
merchantLogService.save(new MerchantLog(store, "shipping", "Product " + p.getSku() + " has a demension larger than the box size specified. Will use per item calculation."));
throw new ServiceException("Product configuration exceeds box configuraton");
}
if (productWeight > maxweight) {
merchantLogService.save(new MerchantLog(store, "shipping", "Product " + p.getSku() + " has a weight larger than the box maximum weight specified. Will use per item calculation."));
throw new ServiceException("Product configuration exceeds box configuraton");
}
double productVolume = (p.getProductWidth().doubleValue() * p.getProductHeight().doubleValue() * p.getProductLength().doubleValue());
if (productVolume == 0) {
merchantLogService.save(new MerchantLog(store, "shipping", "Product " + p.getSku() + " has one of the dimension set to 0 and therefore cannot calculate the volume"));
throw new ServiceException("Product configuration exceeds box configuraton");
}
if (productVolume > maxVolume) {
throw new ServiceException("Product configuration exceeds box configuraton");
}
// Iterator boxIter = boxesList.iterator();
for (PackingBox pbox : boxesList) {
double volumeLeft = pbox.getVolumeLeft();
double weightLeft = pbox.getWeightLeft();
if ((volumeLeft * .75) >= productVolume && pbox.getWeightLeft() >= productWeight) {
// fit the item
// in this
// box
// fit in the current box
volumeLeft = volumeLeft - productVolume;
pbox.setVolumeLeft(volumeLeft);
weightLeft = weightLeft - productWeight;
pbox.setWeightLeft(weightLeft);
assignedProducts.add(p);
productCount--;
double w = pbox.getWeight();
w = w + productWeight;
pbox.setWeight(w);
productAssigned = true;
maxBox--;
break;
}
}
if (!productAssigned) {
// create a new box
box = new PackingBox();
// set box max volume
box.setVolumeLeft(maxVolume);
box.setWeightLeft(maxweight);
boxesList.add(box);
double volumeLeft = box.getVolumeLeft() - productVolume;
box.setVolumeLeft(volumeLeft);
double weightLeft = box.getWeightLeft() - productWeight;
box.setWeightLeft(weightLeft);
assignedProducts.add(p);
productCount--;
double w = box.getWeight();
w = w + productWeight;
box.setWeight(w);
maxBox--;
}
}
// now prepare the shipping info
// number of boxes
// Iterator ubIt = usedBoxesList.iterator();
System.out.println("###################################");
System.out.println("Number of boxes " + boxesList.size());
System.out.println("###################################");
for (PackingBox pb : boxesList) {
PackageDetails details = new PackageDetails();
details.setShippingHeight(height);
details.setShippingLength(length);
details.setShippingWeight(weight + box.getWeight());
details.setShippingWidth(width);
details.setItemName(store.getCode());
boxes.add(details);
}
return boxes;
}
use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.
the class S3ProductContentFileManager method removeProductImage.
@Override
public void removeProductImage(ProductImage productImage) throws ServiceException {
try {
// get buckets
String bucketName = bucketName();
final AmazonS3 s3 = s3Client();
s3.deleteObject(bucketName, nodePath(productImage.getProduct().getMerchantStore().getCode(), productImage.getProduct().getSku()) + productImage.getProductImage());
LOGGER.info("Remove file");
} catch (final Exception e) {
LOGGER.error("Error while removing file", e);
throw new ServiceException(e);
}
}
Aggregations