Search in sources :

Example 21 with ProductAttribute

use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute in project shopizer by shopizer-ecommerce.

the class ProductVariantApi method calculateVariant.

/**
 * Calculates the price based on selected options if any
 * @param id
 * @param options
 * @param merchantStore
 * @param language
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/products/{id}/variant", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ApiOperation(httpMethod = "POST", value = "Get product price variation based on selected product", notes = "", produces = "application/json", response = ReadableProductPrice.class)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableProductPrice calculateVariant(@PathVariable final Long id, @RequestBody ReadableSelectedProductVariant options, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletResponse response) throws Exception {
    Product product = productService.getById(id);
    if (product == null) {
        response.sendError(404, "Product not fount for id " + id);
        return null;
    }
    List<ReadableProductVariantValue> ids = options.getOptions();
    if (CollectionUtils.isEmpty(ids)) {
        return null;
    }
    List<ReadableProductVariantValue> variants = options.getOptions();
    List<ProductAttribute> attributes = new ArrayList<ProductAttribute>();
    Set<ProductAttribute> productAttributes = product.getAttributes();
    for (ProductAttribute attribute : productAttributes) {
        Long option = attribute.getProductOption().getId();
        Long optionValue = attribute.getProductOptionValue().getId();
        for (ReadableProductVariantValue v : variants) {
            if (v.getOption().longValue() == option.longValue() && v.getValue().longValue() == optionValue.longValue()) {
                attributes.add(attribute);
            }
        }
    }
    FinalPrice price = pricingService.calculateProductPrice(product, attributes);
    ReadableProductPrice readablePrice = new ReadableProductPrice();
    ReadableFinalPricePopulator populator = new ReadableFinalPricePopulator();
    populator.setPricingService(pricingService);
    populator.populate(price, readablePrice, merchantStore, language);
    return readablePrice;
}
Also used : ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ReadableFinalPricePopulator(com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ReadableProductVariantValue(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductVariantValue) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 22 with ProductAttribute

use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute 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);
    }
}
Also used : ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) 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) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Language(com.salesmanager.core.model.reference.language.Language) ReadableFinalPricePopulator(com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ProductInstanceService(com.salesmanager.core.business.services.catalog.product.instance.ProductInstanceService) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) CategoryService(com.salesmanager.core.business.services.catalog.category.CategoryService) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ProductAttributeService(com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Product(com.salesmanager.core.model.catalog.product.Product) ProductCriteria(com.salesmanager.core.model.catalog.product.ProductCriteria) ProductPriceRequest(com.salesmanager.shop.model.catalog.product.ProductPriceRequest) ProductRelationshipService(com.salesmanager.core.business.services.catalog.product.relationship.ProductRelationshipService) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) ProductAvailabilityService(com.salesmanager.core.business.services.catalog.product.availability.ProductAvailabilityService) Page(org.springframework.data.domain.Page) ProductInstance(com.salesmanager.core.model.catalog.product.instance.ProductInstance) Collectors(java.util.stream.Collectors) Profile(org.springframework.context.annotation.Profile) List(java.util.List) LocaleUtils(com.salesmanager.shop.utils.LocaleUtils) Validate(org.apache.commons.lang3.Validate) ProductRelationship(com.salesmanager.core.model.catalog.product.relationship.ProductRelationship) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ProductRelationshipType(com.salesmanager.core.model.catalog.product.relationship.ProductRelationshipType) ReadableProductMapper(com.salesmanager.shop.mapper.catalog.ReadableProductMapper) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) Comparator(java.util.Comparator) ReadableProductList(com.salesmanager.shop.model.catalog.product.ReadableProductList) ReadableProductInstanceMapper(com.salesmanager.shop.mapper.catalog.product.ReadableProductInstanceMapper) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ReadableFinalPricePopulator(com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) HashSet(java.util.HashSet) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Example 23 with ProductAttribute

use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute in project shopizer by shopizer-ecommerce.

the class ProductOptionFacadeImpl method createAttributes.

@Override
public List<CodeEntity> createAttributes(List<PersistableProductAttribute> attributes, Long productId, MerchantStore store) {
    Validate.notNull(productId, "Product id must not be null");
    Validate.notNull(store, "Merchant cannot be null");
    // convert to model
    List<ProductAttribute> modelAttributes = attributes.stream().map(attr -> persistableProductAttributeMapper.convert(attr, store, null)).collect(Collectors.toList());
    try {
        productAttributeService.saveAll(modelAttributes);
        // save to a product
        Product product = this.product(productId, store);
        product.getAttributes().addAll(modelAttributes);
        productService.save(product);
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("Exception while saving product with attributes", e);
    }
    return modelAttributes.stream().map(e -> codeEntity(e)).collect(Collectors.toList());
}
Also used : NotImplementedException(org.apache.commons.lang3.NotImplementedException) ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) ReadableProductAttributeList(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductAttributeList) ReadableProductOptionValueEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionValueEntity) Autowired(org.springframework.beans.factory.annotation.Autowired) ProductOptionValueService(com.salesmanager.core.business.services.catalog.product.attribute.ProductOptionValueService) ServiceException(com.salesmanager.core.business.exception.ServiceException) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ReadableProductOptionList(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionList) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Service(org.springframework.stereotype.Service) ReadableProductAttributeMapper(com.salesmanager.shop.mapper.catalog.ReadableProductAttributeMapper) ProductOption(com.salesmanager.core.model.catalog.product.attribute.ProductOption) FileContentType(com.salesmanager.core.model.content.FileContentType) ProductAttributeService(com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService) PersistableProductAttributeMapper(com.salesmanager.shop.mapper.catalog.PersistableProductAttributeMapper) ProductOptionFacade(com.salesmanager.shop.store.controller.product.facade.ProductOptionFacade) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Validate(org.jsoup.helper.Validate) Product(com.salesmanager.core.model.catalog.product.Product) ReadableProductOptionMapper(com.salesmanager.shop.mapper.catalog.ReadableProductOptionMapper) ProductOptionValue(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue) PersistableProductOptionEntity(com.salesmanager.shop.model.catalog.product.attribute.api.PersistableProductOptionEntity) CodeEntity(com.salesmanager.shop.model.entity.CodeEntity) PersistableProductAttribute(com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute) PersistableProductOptionValue(com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOptionValue) Page(org.springframework.data.domain.Page) InputContentFile(com.salesmanager.core.model.content.InputContentFile) Collectors(java.util.stream.Collectors) ContentService(com.salesmanager.core.business.services.content.ContentService) ReadableProductOptionValueList(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionValueList) List(java.util.List) PersistableProductOptionMapper(com.salesmanager.shop.mapper.catalog.PersistableProductOptionMapper) ReadableProductOptionValueMapper(com.salesmanager.shop.mapper.catalog.ReadableProductOptionValueMapper) ReadableProductAttributeEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductAttributeEntity) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) MultipartFile(org.springframework.web.multipart.MultipartFile) ProductOptionService(com.salesmanager.core.business.services.catalog.product.attribute.ProductOptionService) PersistableProductOptionValueMapper(com.salesmanager.shop.mapper.catalog.PersistableProductOptionValueMapper) ReadableProductOptionEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionEntity) InputStream(java.io.InputStream) ServiceException(com.salesmanager.core.business.exception.ServiceException) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) PersistableProductAttribute(com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 24 with ProductAttribute

use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute in project shopizer by shopizer-ecommerce.

the class ProductOptionFacadeImpl method deleteAttribute.

@Override
public void deleteAttribute(Long productId, Long attributeId, MerchantStore store) {
    try {
        ProductAttribute attr = productAttributeService.getById(attributeId);
        if (attr == null) {
            throw new ResourceNotFoundException("ProductAttribute not found for [" + attributeId + "] and store [" + store.getCode() + "]");
        }
        if (attr.getProduct().getId().longValue() != productId) {
            throw new ResourceNotFoundException("ProductAttribute not found for [" + attributeId + "] and product [" + productId + "]");
        }
        if (attr.getProduct().getMerchantStore().getId().intValue() != store.getId().intValue()) {
            throw new ResourceNotFoundException("ProductAttribute not found for [" + attributeId + "] and product [" + productId + "] and store [" + store.getCode() + "]");
        }
        productAttributeService.delete(attr);
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("An exception occured while deleting ProductAttribute [" + attributeId + "]", e);
    }
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) PersistableProductAttribute(com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 25 with ProductAttribute

use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute in project shopizer by shopizer-ecommerce.

the class ProductOptionFacadeImpl method getAttributesList.

@Override
public ReadableProductAttributeList getAttributesList(Long productId, MerchantStore store, Language language, int page, int count) {
    try {
        Product product = this.product(productId, store);
        ReadableProductAttributeList attrList = new ReadableProductAttributeList();
        Page<ProductAttribute> attr = null;
        if (language != null) {
            // all entry
            // attributes = productAttributeService.getByProductId(store, product, language);
            attr = productAttributeService.getByProductId(store, product, language, page, count);
            attrList.setRecordsTotal(attr.getTotalElements());
            attrList.setNumber(attr.getSize());
            attrList.setTotalPages(attr.getTotalPages());
        } else {
            attr = productAttributeService.getByProductId(store, product, page, count);
            attrList.setRecordsTotal(attr.getTotalElements());
            attrList.setNumber(attr.getSize());
            attrList.setTotalPages(attr.getTotalPages());
        }
        List<ReadableProductAttributeEntity> values = attr.getContent().stream().map(attribute -> readableProductAttributeMapper.convert(attribute, store, language)).collect(Collectors.toList());
        attrList.setAttributes(values);
        return attrList;
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("Error while getting attributes", e);
    }
}
Also used : NotImplementedException(org.apache.commons.lang3.NotImplementedException) ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) ReadableProductAttributeList(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductAttributeList) ReadableProductOptionValueEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionValueEntity) Autowired(org.springframework.beans.factory.annotation.Autowired) ProductOptionValueService(com.salesmanager.core.business.services.catalog.product.attribute.ProductOptionValueService) ServiceException(com.salesmanager.core.business.exception.ServiceException) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ReadableProductOptionList(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionList) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Service(org.springframework.stereotype.Service) ReadableProductAttributeMapper(com.salesmanager.shop.mapper.catalog.ReadableProductAttributeMapper) ProductOption(com.salesmanager.core.model.catalog.product.attribute.ProductOption) FileContentType(com.salesmanager.core.model.content.FileContentType) ProductAttributeService(com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService) PersistableProductAttributeMapper(com.salesmanager.shop.mapper.catalog.PersistableProductAttributeMapper) ProductOptionFacade(com.salesmanager.shop.store.controller.product.facade.ProductOptionFacade) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Validate(org.jsoup.helper.Validate) Product(com.salesmanager.core.model.catalog.product.Product) ReadableProductOptionMapper(com.salesmanager.shop.mapper.catalog.ReadableProductOptionMapper) ProductOptionValue(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue) PersistableProductOptionEntity(com.salesmanager.shop.model.catalog.product.attribute.api.PersistableProductOptionEntity) CodeEntity(com.salesmanager.shop.model.entity.CodeEntity) PersistableProductAttribute(com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute) PersistableProductOptionValue(com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOptionValue) Page(org.springframework.data.domain.Page) InputContentFile(com.salesmanager.core.model.content.InputContentFile) Collectors(java.util.stream.Collectors) ContentService(com.salesmanager.core.business.services.content.ContentService) ReadableProductOptionValueList(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionValueList) List(java.util.List) PersistableProductOptionMapper(com.salesmanager.shop.mapper.catalog.PersistableProductOptionMapper) ReadableProductOptionValueMapper(com.salesmanager.shop.mapper.catalog.ReadableProductOptionValueMapper) ReadableProductAttributeEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductAttributeEntity) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) MultipartFile(org.springframework.web.multipart.MultipartFile) ProductOptionService(com.salesmanager.core.business.services.catalog.product.attribute.ProductOptionService) PersistableProductOptionValueMapper(com.salesmanager.shop.mapper.catalog.PersistableProductOptionValueMapper) ReadableProductOptionEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionEntity) InputStream(java.io.InputStream) ServiceException(com.salesmanager.core.business.exception.ServiceException) ReadableProductAttributeList(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductAttributeList) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) PersistableProductAttribute(com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute) ReadableProductAttributeEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductAttributeEntity) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Aggregations

ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)41 Product (com.salesmanager.core.model.catalog.product.Product)25 ArrayList (java.util.ArrayList)20 Language (com.salesmanager.core.model.reference.language.Language)18 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)16 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)15 ServiceException (com.salesmanager.core.business.exception.ServiceException)13 ProductOptionValue (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue)12 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)12 BigDecimal (java.math.BigDecimal)12 HashSet (java.util.HashSet)12 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)11 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)11 List (java.util.List)11 ProductOption (com.salesmanager.core.model.catalog.product.attribute.ProductOption)10 Date (java.util.Date)10 Category (com.salesmanager.core.model.catalog.category.Category)9 ProductOptionDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription)9 ProductOptionValueDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)9 ProductDescription (com.salesmanager.core.model.catalog.product.description.ProductDescription)8