use of com.salesmanager.core.model.catalog.product.instance.ProductInstance 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();
}
use of com.salesmanager.core.model.catalog.product.instance.ProductInstance in project shopizer by shopizer-ecommerce.
the class PersistableInventoryMapper method merge.
@Override
public ProductAvailability merge(PersistableInventory source, ProductAvailability destination, MerchantStore store, Language language) {
Validate.notNull(destination, "Product availability cannot be null");
try {
destination.setProductQuantity(source.getQuantity());
destination.setProductQuantityOrderMin(source.getProductQuantityOrderMax());
destination.setProductQuantityOrderMax(source.getProductQuantityOrderMin());
destination.setAvailable(source.isAvailable());
destination.setOwner(source.getOwner());
String region = getRegion(source);
destination.setRegion(region);
destination.setRegionVariant(source.getRegionVariant());
if (StringUtils.isNotBlank(source.getDateAvailable())) {
destination.setProductDateAvailable(DateUtil.getDate(source.getDateAvailable()));
}
if (source.getInstance() != null && source.getInstance() > 0) {
Optional<ProductInstance> instance = productInstanceService.getById(source.getInstance(), store);
if (instance.get() == null) {
throw new ResourceNotFoundException("ProductInstance with id [" + source.getInstance() + "] not found for store [" + store.getCode() + "]");
}
destination.setProductInstance(instance.get());
}
for (PersistableProductPrice priceEntity : source.getPrices()) {
ProductPrice price = new ProductPrice();
price.setId(null);
if (isPositive(priceEntity.getId())) {
price.setId(priceEntity.getId());
}
// .filter()
if (destination.getPrices() != null) {
for (ProductPrice pp : destination.getPrices()) {
if (isPositive(priceEntity.getId()) && priceEntity.getId().equals(pp.getId())) {
price = pp;
price.setId(pp.getId());
}
}
}
price.setProductAvailability(destination);
price.setDefaultPrice(priceEntity.isDefaultPrice());
price.setProductPriceAmount(priceEntity.getOriginalPrice());
price.setDefaultPrice(priceEntity.isDefaultPrice());
price.setCode(priceEntity.getCode());
price.setProductPriceSpecialAmount(priceEntity.getDiscountedPrice());
if (Objects.nonNull(priceEntity.getDiscountStartDate())) {
Date startDate = DateUtil.getDate(priceEntity.getDiscountStartDate());
price.setProductPriceSpecialStartDate(startDate);
}
if (Objects.nonNull(priceEntity.getDiscountEndDate())) {
Date endDate = DateUtil.getDate(priceEntity.getDiscountEndDate());
price.setProductPriceSpecialEndDate(endDate);
}
Set<ProductPriceDescription> descs = getProductPriceDescriptions(price, priceEntity.getDescriptions());
price.setDescriptions(descs);
Set<ProductPrice> prices = new HashSet<ProductPrice>();
prices.add(price);
destination.setPrices(prices);
}
return destination;
} catch (Exception e) {
throw new ConversionRuntimeException(e);
}
}
Aggregations