Search in sources :

Example 6 with ProductOption

use of com.salesmanager.core.model.catalog.product.attribute.ProductOption 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);
    }
}
Also used : CategoryFacade(com.salesmanager.shop.store.controller.category.facade.CategoryFacade) PersistableCategoryPopulator(com.salesmanager.shop.populator.catalog.PersistableCategoryPopulator) HashMap(java.util.HashMap) ReadableProductVariantValue(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductVariantValue) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ServiceException(com.salesmanager.core.business.exception.ServiceException) Inject(javax.inject.Inject) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription) Language(com.salesmanager.core.model.reference.language.Language) ListCriteria(com.salesmanager.shop.model.entity.ListCriteria) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Service(org.springframework.stereotype.Service) Map(java.util.Map) ProductOption(com.salesmanager.core.model.catalog.product.attribute.ProductOption) ReadableProductVariant(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductVariant) ReadableCategoryPopulator(com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator) CategoryService(com.salesmanager.core.business.services.catalog.category.CategoryService) MerchantStoreService(com.salesmanager.core.business.services.merchant.MerchantStoreService) ProductAttributeService(com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Mapper(com.salesmanager.shop.mapper.Mapper) Iterator(java.util.Iterator) ProductOptionDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription) Collectors(java.util.stream.Collectors) ReadableCategoryList(com.salesmanager.shop.model.catalog.category.ReadableCategoryList) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) Objects(java.util.Objects) Category(com.salesmanager.core.model.catalog.category.Category) List(java.util.List) Validate(org.apache.commons.lang3.Validate) CollectionUtils(org.springframework.util.CollectionUtils) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) Entry(java.util.Map.Entry) Optional(java.util.Optional) ConversionException(com.salesmanager.core.business.exception.ConversionException) Comparator(java.util.Comparator) PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) Entry(java.util.Map.Entry) Category(com.salesmanager.core.model.catalog.category.Category) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ProductOptionDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) ReadableCategoryList(com.salesmanager.shop.model.catalog.category.ReadableCategoryList) List(java.util.List) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ProductOption(com.salesmanager.core.model.catalog.product.attribute.ProductOption) ReadableProductVariant(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductVariant) ServiceException(com.salesmanager.core.business.exception.ServiceException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) HashMap(java.util.HashMap) Map(java.util.Map) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription) ReadableProductVariantValue(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductVariantValue)

Example 7 with ProductOption

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

the class ProductOptionServiceImpl method delete.

@Override
public void delete(ProductOption entity) throws ServiceException {
    // remove all attributes having this option
    List<ProductAttribute> attributes = productAttributeService.getByOptionId(entity.getMerchantStore(), entity.getId());
    for (ProductAttribute attribute : attributes) {
        productAttributeService.delete(attribute);
    }
    ProductOption option = this.getById(entity.getId());
    // remove option
    super.delete(option);
}
Also used : ProductOption(com.salesmanager.core.model.catalog.product.attribute.ProductOption) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)

Example 8 with ProductOption

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

the class ProductOptionFacadeImpl method options.

@Override
public ReadableProductOptionList options(MerchantStore store, Language language, String name, int page, int count) {
    Validate.notNull(store, "MerchantStore should not be null");
    Page<ProductOption> options = productOptionService.getByMerchant(store, null, name, page, count);
    ReadableProductOptionList valueList = new ReadableProductOptionList();
    valueList.setTotalPages(options.getTotalPages());
    valueList.setRecordsTotal(options.getTotalElements());
    valueList.setNumber(options.getNumber());
    List<ReadableProductOptionEntity> values = options.getContent().stream().map(option -> readableMapper.convert(option, store, null)).collect(Collectors.toList());
    valueList.setOptions(values);
    return valueList;
}
Also used : ProductOption(com.salesmanager.core.model.catalog.product.attribute.ProductOption) 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) ReadableProductOptionList(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionList) ReadableProductOptionEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionEntity)

Example 9 with ProductOption

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

the class ProductOptionFacadeImpl method getOption.

@Override
public ReadableProductOptionEntity getOption(Long optionId, MerchantStore store, Language language) {
    Validate.notNull(optionId, "Option id cannot be null");
    Validate.notNull(store, "Store cannot be null");
    ProductOption option = productOptionService.getById(store, optionId);
    if (option == null) {
        throw new ResourceNotFoundException("Option id [" + optionId + "] not found");
    }
    return readableMapper.convert(option, store, language);
}
Also used : ProductOption(com.salesmanager.core.model.catalog.product.attribute.ProductOption) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException)

Example 10 with ProductOption

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

the class ProductOptionFacadeImpl method saveOption.

@Override
public ReadableProductOptionEntity saveOption(PersistableProductOptionEntity option, MerchantStore store, Language language) {
    Validate.notNull(option, "ProductOption cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    ProductOption optionModel = new ProductOption();
    if (option.getId() != null && option.getId().longValue() > 0) {
        optionModel = productOptionService.getById(store, option.getId());
        if (optionModel == null) {
            throw new ResourceNotFoundException("ProductOption not found for if [" + option.getId() + "] and store [" + store.getCode() + "]");
        }
    }
    optionModel = persistableeMapper.merge(option, optionModel, store, language);
    try {
        productOptionService.saveOrUpdate(optionModel);
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("An exception occured while saving ProductOption", e);
    }
    optionModel = productOptionService.getById(store, optionModel.getId());
    ReadableProductOptionEntity readable = readableMapper.convert(optionModel, store, language);
    return readable;
}
Also used : ProductOption(com.salesmanager.core.model.catalog.product.attribute.ProductOption) ServiceException(com.salesmanager.core.business.exception.ServiceException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ReadableProductOptionEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionEntity) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Aggregations

ProductOption (com.salesmanager.core.model.catalog.product.attribute.ProductOption)16 ProductOptionValue (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue)10 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)8 ProductOptionDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription)8 ProductOptionValueDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)7 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)7 Language (com.salesmanager.core.model.reference.language.Language)7 ServiceException (com.salesmanager.core.business.exception.ServiceException)5 Product (com.salesmanager.core.model.catalog.product.Product)4 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)4 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)4 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)4 BigDecimal (java.math.BigDecimal)4 HashSet (java.util.HashSet)4 Category (com.salesmanager.core.model.catalog.category.Category)3 ProductType (com.salesmanager.core.model.catalog.product.type.ProductType)3 Date (java.util.Date)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 ProductAttributeService (com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService)2