use of com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue in project shopizer by shopizer-ecommerce.
the class ProductOptionFacadeImpl method removeOptionValueImage.
@Override
public void removeOptionValueImage(Long optionValueId, MerchantStore store, Language language) {
Validate.notNull(optionValueId, "OptionValueId must not be null");
ProductOptionValue value = productOptionValueService.getById(store, optionValueId);
if (value == null) {
throw new ResourceNotFoundException("Product option value [" + optionValueId + "] not found");
}
try {
contentService.removeFile(store.getCode(), FileContentType.PROPERTY, value.getProductOptionValueImage());
value.setProductOptionValueImage(null);
productOptionValueService.saveOrUpdate(value);
} catch (Exception e) {
throw new ServiceRuntimeException("Exception while removing option value image", e);
}
return;
}
use of com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue in project shopizer by shopizer-ecommerce.
the class ReadableProductPopulator method populate.
@Override
public ReadableProduct populate(Product source, ReadableProduct target, MerchantStore store, Language language) throws ConversionException {
Validate.notNull(pricingService, "Requires to set PricingService");
Validate.notNull(imageUtils, "Requires to set imageUtils");
try {
List<com.salesmanager.shop.model.catalog.product.ProductDescription> fulldescriptions = new ArrayList<com.salesmanager.shop.model.catalog.product.ProductDescription>();
if (language == null) {
target = new ReadableProductFull();
}
if (target == null) {
target = new ReadableProduct();
}
ProductDescription description = source.getProductDescription();
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 (target instanceof ReadableProductFull) {
((ReadableProductFull) target).setDescriptions(fulldescriptions);
}
if (language == null) {
language = store.getDefaultLanguage();
}
final Language lang = language;
target.setId(source.getId());
target.setAvailable(source.isAvailable());
target.setProductShipeable(source.isProductShipeable());
ProductSpecification specifications = new ProductSpecification();
specifications.setHeight(source.getProductHeight());
specifications.setLength(source.getProductLength());
specifications.setWeight(source.getProductWeight());
specifications.setWidth(source.getProductWidth());
target.setProductSpecifications(specifications);
target.setPreOrder(source.isPreOrder());
target.setRefSku(source.getRefSku());
target.setSortOrder(source.getSortOrder());
if (source.getType() != null) {
target.setType(this.type(source.getType(), language));
}
if (source.getOwner() != null) {
RentalOwner owner = new RentalOwner();
owner.setId(source.getOwner().getId());
owner.setEmailAddress(source.getOwner().getEmailAddress());
owner.setFirstName(source.getOwner().getBilling().getFirstName());
owner.setLastName(source.getOwner().getBilling().getLastName());
com.salesmanager.shop.model.customer.address.Address address = new com.salesmanager.shop.model.customer.address.Address();
address.setAddress(source.getOwner().getBilling().getAddress());
address.setBillingAddress(true);
address.setCity(source.getOwner().getBilling().getCity());
address.setCompany(source.getOwner().getBilling().getCompany());
address.setCountry(source.getOwner().getBilling().getCountry().getIsoCode());
address.setZone(source.getOwner().getBilling().getZone().getCode());
address.setLatitude(source.getOwner().getBilling().getLatitude());
address.setLongitude(source.getOwner().getBilling().getLongitude());
address.setPhone(source.getOwner().getBilling().getTelephone());
address.setPostalCode(source.getOwner().getBilling().getPostalCode());
owner.setAddress(address);
target.setOwner(owner);
}
if (source.getDateAvailable() != null) {
target.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
}
if (source.getAuditSection() != null) {
target.setCreationDate(DateUtil.formatDate(source.getAuditSection().getDateCreated()));
}
/* if(source.getProductReviewAvg()!=null) {
double avg = source.getProductReviewAvg().doubleValue();
double rating = Math.round(avg * 2) / 2.0f;
target.setRating(rating);
}*/
target.setProductVirtual(source.getProductVirtual());
/* if(source.getProductReviewCount()!=null) {
target.setRatingCount(source.getProductReviewCount().intValue());
}*/
if (description != null) {
com.salesmanager.shop.model.catalog.product.ProductDescription tragetDescription = populateDescription(description);
target.setDescription(tragetDescription);
}
if (source.getManufacturer() != null) {
ManufacturerDescription manufacturer = source.getManufacturer().getDescriptions().iterator().next();
ReadableManufacturer manufacturerEntity = new ReadableManufacturer();
com.salesmanager.shop.model.catalog.manufacturer.ManufacturerDescription d = new com.salesmanager.shop.model.catalog.manufacturer.ManufacturerDescription();
d.setName(manufacturer.getName());
manufacturerEntity.setDescription(d);
manufacturerEntity.setId(source.getManufacturer().getId());
manufacturerEntity.setOrder(source.getManufacturer().getOrder());
manufacturerEntity.setCode(source.getManufacturer().getCode());
target.setManufacturer(manufacturerEntity);
}
/* if(source.getType() != null) {
ReadableProductType type = new ReadableProductType();
type.setId(source.getType().getId());
type.setCode(source.getType().getCode());
type.setName(source.getType().getCode());//need name
target.setType(type);
}*/
/**
* TODO use ProductImageMapper
*/
Set<ProductImage> images = source.getImages();
if (images != null && images.size() > 0) {
List<ReadableImage> imageList = new ArrayList<ReadableImage>();
String contextPath = imageUtils.getContextPath();
for (ProductImage img : images) {
ReadableImage prdImage = new ReadableImage();
prdImage.setImageName(img.getProductImage());
prdImage.setDefaultImage(img.isDefaultImage());
prdImage.setOrder(img.getSortOrder() != null ? img.getSortOrder().intValue() : 0);
if (img.getImageType() == 1 && img.getProductImageUrl() != null) {
prdImage.setImageUrl(img.getProductImageUrl());
} else {
StringBuilder imgPath = new StringBuilder();
imgPath.append(contextPath).append(imageUtils.buildProductImageUtils(store, source.getSku(), img.getProductImage()));
prdImage.setImageUrl(imgPath.toString());
}
prdImage.setId(img.getId());
prdImage.setImageType(img.getImageType());
if (img.getProductImageUrl() != null) {
prdImage.setExternalUrl(img.getProductImageUrl());
}
if (img.getImageType() == 1 && img.getProductImageUrl() != null) {
// video
prdImage.setVideoUrl(img.getProductImageUrl());
}
if (prdImage.isDefaultImage()) {
target.setImage(prdImage);
}
imageList.add(prdImage);
}
imageList = imageList.stream().sorted(Comparator.comparingInt(ReadableImage::getOrder)).collect(Collectors.toList());
target.setImages(imageList);
}
if (!CollectionUtils.isEmpty(source.getCategories())) {
ReadableCategoryPopulator categoryPopulator = new ReadableCategoryPopulator();
List<ReadableCategory> categoryList = new ArrayList<ReadableCategory>();
for (Category category : source.getCategories()) {
ReadableCategory readableCategory = new ReadableCategory();
categoryPopulator.populate(category, readableCategory, store, language);
categoryList.add(readableCategory);
}
target.setCategories(categoryList);
}
if (!CollectionUtils.isEmpty(source.getAttributes())) {
Set<ProductAttribute> attributes = source.getAttributes();
// split read only and options
// Map<Long,ReadableProductAttribute> readOnlyAttributes = null;
Map<Long, ReadableProductProperty> properties = null;
Map<Long, ReadableProductOption> selectableOptions = null;
if (!CollectionUtils.isEmpty(attributes)) {
for (ProductAttribute attribute : attributes) {
ReadableProductOption opt = null;
ReadableProductAttribute attr = null;
ReadableProductProperty property = null;
ReadableProductPropertyValue propertyValue = null;
ReadableProductOptionValueEntity optValue = new ReadableProductOptionValueEntity();
ReadableProductAttributeValue attrValue = new ReadableProductAttributeValue();
ProductOptionValue optionValue = attribute.getProductOptionValue();
if (attribute.getAttributeDisplayOnly()) {
// read only attribute = property
/*
if(readOnlyAttributes==null) {
readOnlyAttributes = new TreeMap<Long,ReadableProductAttribute>();
}
attr = readOnlyAttributes.get(attribute.getProductOption().getId());
if(attr==null) {
attr = createAttribute(attribute, language);
}
if(attr!=null) {
readOnlyAttributes.put(attribute.getProductOption().getId(), attr);
}
attrValue.setDefaultValue(attribute.getAttributeDefault());
if(attribute.getProductOptionValue()!=null) {
attrValue.setId(attribute.getProductOptionValue().getId());//id of the option value
} else {
attrValue.setId(attribute.getId());
}
attrValue.setLang(language.getCode());
attrValue.setSortOrder(0);
if(attribute.getProductOptionSortOrder()!=null) {
attrValue.setSortOrder(attribute.getProductOptionSortOrder().intValue());
}
List<ProductOptionValueDescription> podescriptions = optionValue.getDescriptionsSettoList();
ProductOptionValueDescription podescription = null;
if(podescriptions!=null && podescriptions.size()>0) {
podescription = podescriptions.get(0);
if(podescriptions.size()>1) {
for(ProductOptionValueDescription optionValueDescription : podescriptions) {
if(optionValueDescription.getLanguage().getId().intValue()==language.getId().intValue()) {
podescription = optionValueDescription;
break;
}
}
}
}
attrValue.setName(podescription.getName());
attrValue.setDescription(podescription.getDescription());
if(attr!=null) {
attr.getAttributeValues().add(attrValue);
}
*/
// if(properties==null) {
// properties = new TreeMap<Long,ReadableProductProperty>();
// }
// property = properties.get(attribute.getProductOption().getId());
// if(property==null) {
property = createProperty(attribute, language);
// that is the property
ReadableProductOption readableOption = new ReadableProductOption();
ReadableProductPropertyValue readableOptionValue = new ReadableProductPropertyValue();
readableOption.setCode(attribute.getProductOption().getCode());
readableOption.setId(attribute.getProductOption().getId());
Set<ProductOptionDescription> podescriptions = attribute.getProductOption().getDescriptions();
if (podescriptions != null && podescriptions.size() > 0) {
for (ProductOptionDescription optionDescription : podescriptions) {
if (optionDescription.getLanguage().getCode().equals(language.getCode())) {
readableOption.setName(optionDescription.getName());
}
}
}
property.setProperty(readableOption);
Set<ProductOptionValueDescription> povdescriptions = attribute.getProductOptionValue().getDescriptions();
readableOptionValue.setId(attribute.getProductOptionValue().getId());
if (povdescriptions != null && povdescriptions.size() > 0) {
for (ProductOptionValueDescription optionValueDescription : povdescriptions) {
if (optionValueDescription.getLanguage().getCode().equals(language.getCode())) {
readableOptionValue.setName(optionValueDescription.getName());
}
}
}
property.setPropertyValue(readableOptionValue);
// } else{
// properties.put(attribute.getProductOption().getId(), property);
// }
/* propertyValue.setCode(attribute.getProductOptionValue().getCode());
propertyValue.setId(attribute.getProductOptionValue().getId());
propertyValue.setSortOrder(0);
if(attribute.getProductOptionSortOrder()!=null) {
propertyValue.setSortOrder(attribute.getProductOptionSortOrder().intValue());
}
List<ProductOptionValueDescription> podescriptions = optionValue.getDescriptionsSettoList();
if(podescriptions!=null && podescriptions.size()>0) {
for(ProductOptionValueDescription optionValueDescription : podescriptions) {
com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription desc = new com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription();
desc.setId(optionValueDescription.getId());
desc.setName(optionValueDescription.getName());
propertyValue.getValues().add(desc);
}
}
property.setPropertyValue(propertyValue);*/
// if(attr!=null) {
// attr.getAttributeValues().add(attrValue);
// }
target.getProperties().add(property);
} else {
if (selectableOptions == null) {
selectableOptions = new TreeMap<Long, ReadableProductOption>();
}
opt = selectableOptions.get(attribute.getProductOption().getId());
if (opt == null) {
opt = createOption(attribute, language);
}
if (opt != null) {
selectableOptions.put(attribute.getProductOption().getId(), opt);
}
optValue.setDefaultValue(attribute.getAttributeDefault());
// optValue.setId(attribute.getProductOptionValue().getId());
optValue.setId(attribute.getId());
optValue.setCode(attribute.getProductOptionValue().getCode());
com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription valueDescription = new com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription();
valueDescription.setLanguage(language.getCode());
// optValue.setLang(language.getCode());
if (attribute.getProductAttributePrice() != null && attribute.getProductAttributePrice().doubleValue() > 0) {
String formatedPrice = pricingService.getDisplayAmount(attribute.getProductAttributePrice(), store);
optValue.setPrice(formatedPrice);
}
if (!StringUtils.isBlank(attribute.getProductOptionValue().getProductOptionValueImage())) {
optValue.setImage(imageUtils.buildProductPropertyImageUtils(store, attribute.getProductOptionValue().getProductOptionValueImage()));
}
optValue.setSortOrder(0);
if (attribute.getProductOptionSortOrder() != null) {
optValue.setSortOrder(attribute.getProductOptionSortOrder().intValue());
}
List<ProductOptionValueDescription> podescriptions = optionValue.getDescriptionsSettoList();
ProductOptionValueDescription podescription = null;
if (podescriptions != null && podescriptions.size() > 0) {
podescription = podescriptions.get(0);
if (podescriptions.size() > 1) {
for (ProductOptionValueDescription optionValueDescription : podescriptions) {
if (optionValueDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
podescription = optionValueDescription;
break;
}
}
}
}
valueDescription.setName(podescription.getName());
valueDescription.setDescription(podescription.getDescription());
optValue.setDescription(valueDescription);
if (opt != null) {
opt.getOptionValues().add(optValue);
}
}
}
}
if (selectableOptions != null) {
List<ReadableProductOption> options = new ArrayList<ReadableProductOption>(selectableOptions.values());
target.setOptions(options);
}
}
// remove products from invisible category -> set visible = false
/* Set<Category> categories = source.getCategories();
boolean isVisible = true;
if(!CollectionUtils.isEmpty(categories)) {
for(Category c : categories) {
if(c.isVisible()) {
isVisible = true;
break;
} else {
isVisible = false;
}
}
}*/
// target.setVisible(isVisible);
// availability
ProductAvailability availability = null;
for (ProductAvailability a : source.getAvailabilities()) {
// TODO validate region
// if(availability.getRegion().equals(Constants.ALL_REGIONS)) {//TODO REL 2.1 accept a region
availability = a;
target.setQuantity(availability.getProductQuantity() == null ? 1 : availability.getProductQuantity());
target.setQuantityOrderMaximum(availability.getProductQuantityOrderMax() == null ? 1 : availability.getProductQuantityOrderMax());
target.setQuantityOrderMinimum(availability.getProductQuantityOrderMin() == null ? 1 : availability.getProductQuantityOrderMin());
if (availability.getProductQuantity().intValue() > 0 && target.isAvailable()) {
target.setCanBePurchased(true);
}
// }
}
target.setSku(source.getSku());
FinalPrice price = pricingService.calculateProductPrice(source);
if (price != null) {
target.setFinalPrice(pricingService.getDisplayAmount(price.getFinalPrice(), store));
target.setPrice(price.getFinalPrice());
target.setOriginalPrice(pricingService.getDisplayAmount(price.getOriginalPrice(), store));
if (price.isDiscounted()) {
target.setDiscounted(true);
}
// price appender
if (availability != null) {
Set<ProductPrice> prices = availability.getPrices();
if (!CollectionUtils.isEmpty(prices)) {
ReadableProductPrice readableProductPrice = new ReadableProductPrice();
readableProductPrice.setDiscounted(target.isDiscounted());
readableProductPrice.setFinalPrice(target.getFinalPrice());
readableProductPrice.setOriginalPrice(target.getOriginalPrice());
Optional<ProductPrice> pr = prices.stream().filter(p -> p.getCode().equals(ProductPrice.DEFAULT_PRICE_CODE)).findFirst();
target.setProductPrice(readableProductPrice);
if (pr.isPresent()) {
readableProductPrice.setId(pr.get().getId());
Optional<ProductPriceDescription> d = pr.get().getDescriptions().stream().filter(desc -> desc.getLanguage().getCode().equals(lang.getCode())).findFirst();
if (d.isPresent()) {
com.salesmanager.shop.model.catalog.product.ProductPriceDescription priceDescription = new com.salesmanager.shop.model.catalog.product.ProductPriceDescription();
priceDescription.setLanguage(language.getCode());
priceDescription.setId(d.get().getId());
priceDescription.setPriceAppender(d.get().getPriceAppender());
readableProductPrice.setDescription(priceDescription);
}
}
}
}
}
if (target instanceof ReadableProductFull) {
((ReadableProductFull) target).setDescriptions(fulldescriptions);
}
return target;
} catch (Exception e) {
throw new ConversionException(e);
}
}
use of com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue 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.core.model.catalog.product.attribute.ProductOptionValue 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.core.model.catalog.product.attribute.ProductOptionValue 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;
}
Aggregations