Search in sources :

Example 11 with ProductInstance

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();
}
Also used : ReadableEntityUtil.createReadableList(com.salesmanager.shop.util.ReadableEntityUtil.createReadableList) Arrays(java.util.Arrays) ReadableProductInstance(com.salesmanager.shop.model.catalog.product.product.instance.ReadableProductInstance) Autowired(org.springframework.beans.factory.annotation.Autowired) ProductFacade(com.salesmanager.shop.store.controller.product.facade.ProductFacade) ProductInstanceFacade(com.salesmanager.shop.store.controller.product.facade.ProductInstanceFacade) ServiceException(com.salesmanager.core.business.exception.ServiceException) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ProductVariationService(com.salesmanager.core.business.services.catalog.product.variation.ProductVariationService) ProductInstanceService(com.salesmanager.core.business.services.catalog.product.instance.ProductInstanceService) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ProductVariation(com.salesmanager.core.model.catalog.product.variation.ProductVariation) ConstraintException(com.salesmanager.shop.store.api.exception.ConstraintException) PersistableProductInstanceMapper(com.salesmanager.shop.mapper.catalog.product.PersistableProductInstanceMapper) Page(org.springframework.data.domain.Page) ProductInstance(com.salesmanager.core.model.catalog.product.instance.ProductInstance) Collectors(java.util.stream.Collectors) ProductCommonFacade(com.salesmanager.shop.store.controller.product.facade.ProductCommonFacade) List(java.util.List) Component(org.springframework.stereotype.Component) Validate(org.apache.commons.lang3.Validate) PersistableProductInstance(com.salesmanager.shop.model.catalog.product.product.instance.PersistableProductInstance) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) Optional(java.util.Optional) ReadableProductInstanceMapper(com.salesmanager.shop.mapper.catalog.product.ReadableProductInstanceMapper) ReadableEntityList(com.salesmanager.shop.model.entity.ReadableEntityList) ServiceException(com.salesmanager.core.business.exception.ServiceException) ProductVariation(com.salesmanager.core.model.catalog.product.variation.ProductVariation) ReadableProductInstance(com.salesmanager.shop.model.catalog.product.product.instance.ReadableProductInstance) ProductInstance(com.salesmanager.core.model.catalog.product.instance.ProductInstance) PersistableProductInstance(com.salesmanager.shop.model.catalog.product.product.instance.PersistableProductInstance) ConstraintException(com.salesmanager.shop.store.api.exception.ConstraintException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 12 with ProductInstance

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);
    }
}
Also used : PersistableProductPrice(com.salesmanager.shop.model.catalog.product.PersistableProductPrice) ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) PersistableProductPrice(com.salesmanager.shop.model.catalog.product.PersistableProductPrice) ProductInstance(com.salesmanager.core.model.catalog.product.instance.ProductInstance) Date(java.util.Date) ServiceException(com.salesmanager.core.business.exception.ServiceException) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription) HashSet(java.util.HashSet) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException)

Aggregations

ProductInstance (com.salesmanager.core.model.catalog.product.instance.ProductInstance)12 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)10 ReadableProductInstance (com.salesmanager.shop.model.catalog.product.product.instance.ReadableProductInstance)9 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)7 ServiceException (com.salesmanager.core.business.exception.ServiceException)6 Product (com.salesmanager.core.model.catalog.product.Product)5 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)5 Language (com.salesmanager.core.model.reference.language.Language)5 PersistableProductInstance (com.salesmanager.shop.model.catalog.product.product.instance.PersistableProductInstance)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 ProductInstanceService (com.salesmanager.core.business.services.catalog.product.instance.ProductInstanceService)4 ReadableProductInstanceMapper (com.salesmanager.shop.mapper.catalog.product.ReadableProductInstanceMapper)4 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)4 ProductFacade (com.salesmanager.shop.store.controller.product.facade.ProductFacade)4 HashSet (java.util.HashSet)4 Validate (org.apache.commons.lang3.Validate)4 Page (org.springframework.data.domain.Page)4 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)3