use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.
the class ReadableProductInstanceMapper method merge.
@Override
public ReadableProductInstance merge(ProductInstance source, ReadableProductInstance destination, MerchantStore store, Language language) {
Validate.notNull(source, "Product instance cannot be null");
Validate.notNull(source.getProduct(), "Product cannot be null");
if (destination == null) {
destination = new ReadableProductInstance();
}
destination.setSortOrder(source.getSortOrder() != null ? source.getSortOrder().intValue() : 0);
destination.setAvailable(source.isAvailable());
destination.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
destination.setId(source.getId());
destination.setDefaultSelection(source.isDefaultSelection());
destination.setProductId(source.getProduct().getId());
destination.setSku(source.getSku());
destination.setSortOrder(source.getSortOrder());
destination.setCode(source.getCode());
// get product
Product baseProduct = source.getProduct();
if (baseProduct == null) {
throw new ResourceNotFoundException("Product instances do not include the parent product [" + destination.getSku() + "]");
}
destination.setProductShipeable(baseProduct.isProductShipeable());
// destination.setStore(null);
destination.setStore(store.getCode());
destination.setVariant(readableProductVariationMapper.convert(source.getVariant(), store, language));
destination.setVariantValue(readableProductVariationMapper.convert(source.getVariantValue(), store, language));
if (source.getProductInstanceGroup() != null) {
Set<String> nameSet = new HashSet<>();
List<ReadableImage> instanceImages = source.getProductInstanceGroup().getImages().stream().map(i -> this.image(i, store, language)).filter(e -> nameSet.add(e.getImageUrl())).collect(Collectors.toList());
destination.setImages(instanceImages);
}
if (!CollectionUtils.isEmpty(source.getAvailabilities())) {
List<ReadableInventory> inventories = source.getAvailabilities().stream().map(i -> readableInventoryMapper.convert(i, store, language)).collect(Collectors.toList());
destination.setInventory(inventories);
}
return destination;
}
use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException 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.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.
the class ContentFacadeImpl method getContentBox.
@Override
public ReadableContentBox getContentBox(String code, MerchantStore store, Language language) {
Validate.notNull(code, "Content code cannot be null");
Validate.notNull(store, "MerchantStore cannot be null");
try {
Content content = null;
ReadableContentBox box = new ReadableContentBox();
if (language != null) {
content = Optional.ofNullable(contentService.getByCode(code, store, language)).orElseThrow(() -> new ResourceNotFoundException("Resource not found [" + code + "] for store [" + store.getCode() + "]"));
Optional<ContentDescription> contentDescription = findAppropriateContentDescription(content.getDescriptions(), language);
if (contentDescription.isPresent()) {
com.salesmanager.shop.model.content.common.ContentDescription desc = this.contentDescription(// return cdata description
contentDescription.get());
desc.setDescription(this.fixContentDescription(desc.getDescription()));
box.setDescription(desc);
}
return box;
} else {
content = Optional.ofNullable(contentService.getByCode(code, store)).orElseThrow(() -> new ResourceNotFoundException("Resource not found [" + code + "] for store [" + store.getCode() + "]"));
// all languages
ReadableContentBoxFull full = new ReadableContentBoxFull();
List<com.salesmanager.shop.model.content.common.ContentDescription> descriptions = content.getDescriptions().stream().map(d -> this.contentDescription(d)).collect(Collectors.toList());
/**
* Optional<ContentDescription> contentDescription = findAppropriateContentDescription(
* content.getDescriptions(), store.getDefaultLanguage());
*
* if(contentDescription.isPresent()) {
* com.salesmanager.shop.model.content.common.ContentDescription desc = this
* .contentDescription(contentDescription.get());
* full.setDescription(desc);
* }
*/
full.setDescriptions(descriptions);
full.setCode(content.getCode());
full.setId(content.getId());
full.setVisible(content.isVisible());
return full;
}
} catch (ServiceException e) {
throw new ServiceRuntimeException(e);
}
}
use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.
the class ContentFacadeImpl method getContentPageByName.
@Override
public ReadableContentPage getContentPageByName(String name, MerchantStore store, Language language) {
Validate.notNull(name, "Content name cannot be null");
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(language, "Language cannot be null");
try {
ContentDescription contentDescription = Optional.ofNullable(contentService.getBySeUrl(store, name)).orElseThrow(() -> new ResourceNotFoundException("No page found : " + name));
return this.contentDescriptionToReadableContent(store, contentDescription.getContent(), contentDescription);
} catch (Exception e) {
throw new ServiceRuntimeException("Error while getting page " + e.getMessage(), e);
}
}
use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException 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;
}
Aggregations