use of com.salesmanager.shop.store.api.exception.ServiceRuntimeException in project shopizer by shopizer-ecommerce.
the class ShippingFacadeImpl method shipToCountry.
@Override
public List<ReadableCountry> shipToCountry(MerchantStore store, Language language) {
try {
List<Country> countries = shippingService.getShipToCountryList(store, language);
List<ReadableCountry> countryList = new ArrayList<ReadableCountry>();
if (!CollectionUtils.isEmpty(countries)) {
countryList = countries.stream().map(c -> {
try {
return convert(c, store, language);
} catch (ConversionException e) {
throw new ConversionRuntimeException("Error converting Country to readable country,e");
}
}).sorted(Comparator.comparing(ReadableCountry::getName)).collect(Collectors.toList());
}
return countryList;
} catch (Exception e) {
throw new ServiceRuntimeException("Error getting shipping country", e);
}
}
use of com.salesmanager.shop.store.api.exception.ServiceRuntimeException in project shopizer by shopizer-ecommerce.
the class ShippingFacadeImpl method saveShippingOrigin.
@Override
public void saveShippingOrigin(PersistableAddress address, MerchantStore store) {
Validate.notNull(address, "PersistableAddress cannot be null");
try {
ShippingOrigin o = shippingOriginService.getByStore(store);
if (o == null) {
o = new ShippingOrigin();
}
o.setAddress(address.getAddress());
o.setCity(address.getCity());
o.setCountry(countryService.getByCode(address.getCountry()));
o.setMerchantStore(store);
o.setActive(address.isActive());
o.setPostalCode(address.getPostalCode());
Zone zone = zoneService.getByCode(address.getStateProvince());
if (zone == null) {
o.setState(address.getStateProvince());
} else {
o.setZone(zone);
}
shippingOriginService.save(o);
} catch (ServiceException e) {
LOGGER.error("Error while getting shipping origin for country [" + address.getCountry() + "]", e);
throw new ServiceRuntimeException("Error while getting shipping origin for country [" + address.getCountry() + "]", e);
}
}
use of com.salesmanager.shop.store.api.exception.ServiceRuntimeException in project shopizer by shopizer-ecommerce.
the class ProductImageApi method uploadImage.
/**
* To be used with MultipartFile
*
* @param id
* @param uploadfiles
* @param request
* @param response
* @throws Exception
*/
@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(value = { "/private/products/{id}/images", "/auth/products/{id}/images" }, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE }, method = RequestMethod.POST)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public void uploadImage(@PathVariable Long id, @RequestParam(value = "file", required = true) MultipartFile[] files, @RequestParam(value = "order", required = false, defaultValue = "0") Integer position, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) throws IOException {
try {
// get the product
Product product = productService.getById(id);
if (product == null) {
throw new ResourceNotFoundException("Product not found");
}
// product belongs to merchant store
if (product.getMerchantStore().getId().intValue() != merchantStore.getId().intValue()) {
throw new UnauthorizedException("Resource not authorized for this merchant");
}
boolean hasDefaultImage = false;
Set<ProductImage> images = product.getImages();
if (!CollectionUtils.isEmpty(images)) {
for (ProductImage image : images) {
if (image.isDefaultImage()) {
hasDefaultImage = true;
break;
}
}
}
List<ProductImage> contentImagesList = new ArrayList<ProductImage>();
int sortOrder = position;
for (MultipartFile multipartFile : files) {
if (!multipartFile.isEmpty()) {
ProductImage productImage = new ProductImage();
productImage.setImage(multipartFile.getInputStream());
productImage.setProductImage(multipartFile.getOriginalFilename());
productImage.setProduct(product);
if (!hasDefaultImage) {
productImage.setDefaultImage(true);
hasDefaultImage = true;
}
productImage.setSortOrder(sortOrder);
position++;
contentImagesList.add(productImage);
}
}
if (CollectionUtils.isNotEmpty(contentImagesList)) {
productImageService.addProductImages(product, contentImagesList);
}
} catch (Exception e) {
LOGGER.error("Error while creating ProductImage", e);
throw new ServiceRuntimeException("Error while creating image");
}
}
use of com.salesmanager.shop.store.api.exception.ServiceRuntimeException in project shopizer by shopizer-ecommerce.
the class ProductFacadeV2Impl method getProductPrice.
@Override
public ReadableProductPrice getProductPrice(Long id, ProductPriceRequest priceRequest, MerchantStore store, Language language) {
Validate.notNull(id, "Product id cannot be null");
Validate.notNull(priceRequest, "Product price request cannot be null");
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(language, "Language cannot be null");
try {
Product model = productService.findOne(id, store);
List<ProductAttribute> attributes = null;
if (!CollectionUtils.isEmpty(priceRequest.getOptions())) {
List<Long> attrinutesIds = priceRequest.getOptions().stream().map(p -> p.getId()).collect(Collectors.toList());
attributes = productAttributeService.getByAttributeIds(store, model, attrinutesIds);
for (ProductAttribute attribute : attributes) {
if (attribute.getProduct().getId().longValue() != id.longValue()) {
// throw unauthorized
throw new OperationNotAllowedException("Attribute with id [" + attribute.getId() + "] is not attached to product id [" + id + "]");
}
}
}
if (!StringUtils.isBlank(priceRequest.getSku())) {
// change default availability with sku (instance availability)
List<ProductAvailability> availabilityList = productAvailabilityService.getBySku(priceRequest.getSku(), store);
if (CollectionUtils.isNotEmpty(availabilityList)) {
model.setAvailabilities(new HashSet(availabilityList));
}
}
FinalPrice price;
// attributes can be null;
price = pricingService.calculateProductPrice(model, attributes);
ReadableProductPrice readablePrice = new ReadableProductPrice();
ReadableFinalPricePopulator populator = new ReadableFinalPricePopulator();
populator.setPricingService(pricingService);
return populator.populate(price, readablePrice, store, language);
} catch (Exception e) {
throw new ServiceRuntimeException("An error occured while getting product price", e);
}
}
use of com.salesmanager.shop.store.api.exception.ServiceRuntimeException in project shopizer by shopizer-ecommerce.
the class ProductInstanceFacadeImpl method create.
@Override
public Long create(PersistableProductInstance productInstance, Long productId, MerchantStore store, Language language) {
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(productInstance, "ProductInstance cannot be null");
Validate.notNull(productId, "Product id cannot be null");
// variation and variation value should not be of same product option code
if (productInstance.getVariant() != null && productInstance.getVariant().longValue() > 0 && productInstance.getVariantValue() != null && productInstance.getVariantValue().longValue() > 0) {
List<ProductVariation> variations = productVariationService.getByIds(Arrays.asList(productInstance.getVariant(), productInstance.getVariantValue()), store);
boolean differentOption = variations.stream().map(i -> i.getProductOption().getCode()).distinct().count() > 1;
if (!differentOption) {
throw new ConstraintException("Product option of instance.variant and instance.variantValue must be different");
}
}
productInstance.setProductId(productId);
productInstance.setId(null);
ProductInstance instance = persistableProductInstanceMapper.convert(productInstance, store, language);
try {
productInstanceService.save(instance);
} catch (ServiceException e) {
throw new ServiceRuntimeException("Cannot save product instance for store [" + store.getCode() + "] and productId [" + productId + "]", e);
}
return instance.getId();
}
Aggregations