Search in sources :

Example 36 with Category

use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.

the class PersistableProductDefinitionMapper method merge.

@Override
public Product merge(PersistableProductDefinition source, Product destination, MerchantStore store, Language language) {
    Validate.notNull(destination, "Product must not be null");
    try {
        // core properties
        destination.setSku(source.getIdentifier());
        destination.setAvailable(source.isVisible());
        destination.setDateAvailable(new Date());
        destination.setRefSku(source.getIdentifier());
        if (source.getId() != null && source.getId().longValue() == 0) {
            destination.setId(null);
        } else {
            destination.setId(source.getId());
        }
        // MANUFACTURER
        if (!StringUtils.isBlank(source.getManufacturer())) {
            Manufacturer manufacturer = manufacturerService.getByCode(store, source.getManufacturer());
            if (manufacturer == null) {
                throw new ConversionException("Manufacturer [" + source.getManufacturer() + "] does not exist");
            }
            destination.setManufacturer(manufacturer);
        }
        // PRODUCT TYPE
        if (!StringUtils.isBlank(source.getType())) {
            ProductType type = productTypeService.getByCode(source.getType(), store, language);
            if (type == null) {
                throw new ConversionException("Product type [" + source.getType() + "] does not exist");
            }
            destination.setType(type);
        }
        if (!StringUtils.isBlank(source.getDateAvailable())) {
            destination.setDateAvailable(DateUtil.getDate(source.getDateAvailable()));
        }
        destination.setMerchantStore(store);
        List<Language> languages = new ArrayList<Language>();
        Set<ProductDescription> descriptions = new HashSet<ProductDescription>();
        if (!CollectionUtils.isEmpty(source.getDescriptions())) {
            for (com.salesmanager.shop.model.catalog.product.ProductDescription description : source.getDescriptions()) {
                ProductDescription productDescription = new ProductDescription();
                Language lang = languageService.getByCode(description.getLanguage());
                if (lang == null) {
                    throw new ConversionException("Language code " + description.getLanguage() + " is invalid, use ISO code (en, fr ...)");
                }
                if (!CollectionUtils.isEmpty(destination.getDescriptions())) {
                    for (ProductDescription desc : destination.getDescriptions()) {
                        if (desc.getLanguage().getCode().equals(description.getLanguage())) {
                            productDescription = desc;
                            break;
                        }
                    }
                }
                productDescription.setProduct(destination);
                productDescription.setDescription(description.getDescription());
                productDescription.setProductHighlight(description.getHighlights());
                productDescription.setName(description.getName());
                productDescription.setSeUrl(description.getFriendlyUrl());
                productDescription.setMetatagKeywords(description.getKeyWords());
                productDescription.setMetatagDescription(description.getMetaDescription());
                productDescription.setTitle(description.getTitle());
                languages.add(lang);
                productDescription.setLanguage(lang);
                descriptions.add(productDescription);
            }
        }
        if (descriptions.size() > 0) {
            destination.setDescriptions(descriptions);
        }
        // if(source.getRating() != null) {
        // destination.setProductReviewAvg(new BigDecimal(source.getRating()));
        // }
        // destination.setProductReviewCount(source.getRatingCount());
        /**
         * Product definition
         */
        ProductAvailability productAvailability = null;
        ProductPrice defaultPrice = null;
        if (!CollectionUtils.isEmpty(destination.getAvailabilities())) {
            for (ProductAvailability avail : destination.getAvailabilities()) {
                Set<ProductPrice> prices = avail.getPrices();
                for (ProductPrice p : prices) {
                    if (p.isDefaultPrice()) {
                        if (productAvailability == null) {
                            productAvailability = avail;
                            defaultPrice = p;
                            productAvailability.setProductQuantity(source.getQuantity());
                            productAvailability.setProductStatus(source.isCanBePurchased());
                            p.setProductPriceAmount(source.getPrice());
                            break;
                        }
                    }
                }
            }
        }
        if (productAvailability == null) {
            // create with default values
            productAvailability = new ProductAvailability(destination, store);
            destination.getAvailabilities().add(productAvailability);
            productAvailability.setProductQuantity(source.getQuantity());
            productAvailability.setProductQuantityOrderMin(1);
            productAvailability.setProductQuantityOrderMax(1);
            productAvailability.setRegion(Constants.ALL_REGIONS);
            productAvailability.setAvailable(Boolean.valueOf(destination.isAvailable()));
            productAvailability.setProductStatus(source.isCanBePurchased());
        }
        if (defaultPrice == null) {
            BigDecimal defaultPriceAmount = new BigDecimal(0);
            if (source.getPrice() != null) {
                defaultPriceAmount = source.getPrice();
            }
            defaultPrice = new ProductPrice();
            defaultPrice.setDefaultPrice(true);
            defaultPrice.setProductPriceAmount(defaultPriceAmount);
            defaultPrice.setCode(ProductPriceEntity.DEFAULT_PRICE_CODE);
            defaultPrice.setProductAvailability(productAvailability);
            productAvailability.getPrices().add(defaultPrice);
            for (Language lang : languages) {
                ProductPriceDescription ppd = new ProductPriceDescription();
                ppd.setProductPrice(defaultPrice);
                ppd.setLanguage(lang);
                ppd.setName(ProductPriceDescription.DEFAULT_PRICE_DESCRIPTION);
                defaultPrice.getDescriptions().add(ppd);
            }
        }
        if (source.getProductSpecifications() != null) {
            destination.setProductHeight(source.getProductSpecifications().getHeight());
            destination.setProductLength(source.getProductSpecifications().getLength());
            destination.setProductWeight(source.getProductSpecifications().getWeight());
            destination.setProductWidth(source.getProductSpecifications().getWidth());
            if (source.getProductSpecifications().getManufacturer() != null) {
                Manufacturer manuf = null;
                if (!StringUtils.isBlank(source.getProductSpecifications().getManufacturer())) {
                    manuf = manufacturerService.getByCode(store, source.getProductSpecifications().getManufacturer());
                }
                if (manuf == null) {
                    throw new ConversionException("Invalid manufacturer id");
                }
                if (manuf != null) {
                    if (manuf.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                        throw new ConversionException("Invalid manufacturer id");
                    }
                    destination.setManufacturer(manuf);
                }
            }
        }
        destination.setSortOrder(source.getSortOrder());
        destination.setProductVirtual(source.isVirtual());
        destination.setProductShipeable(source.isShipeable());
        // attributes
        if (source.getProperties() != null) {
            for (com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute attr : source.getProperties()) {
                ProductAttribute attribute = persistableProductAttributeMapper.convert(attr, store, language);
                attribute.setProduct(destination);
                destination.getAttributes().add(attribute);
            }
        }
        // categories
        if (!CollectionUtils.isEmpty(source.getCategories())) {
            for (com.salesmanager.shop.model.catalog.category.Category categ : source.getCategories()) {
                Category c = null;
                if (!StringUtils.isBlank(categ.getCode())) {
                    c = categoryService.getByCode(store, categ.getCode());
                } else {
                    Validate.notNull(categ.getId(), "Category id nust not be null");
                    c = categoryService.getById(categ.getId(), store.getId());
                }
                if (c == null) {
                    throw new ConversionException("Category id " + categ.getId() + " does not exist");
                }
                if (c.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                    throw new ConversionException("Invalid category id");
                }
                destination.getCategories().add(c);
            }
        }
        return destination;
    } catch (Exception e) {
        throw new ConversionRuntimeException("Error converting product mapper", e);
    }
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) ArrayList(java.util.ArrayList) ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Language(com.salesmanager.core.model.reference.language.Language) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) Manufacturer(com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription) HashSet(java.util.HashSet) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductType(com.salesmanager.core.model.catalog.product.type.ProductType) Date(java.util.Date) BigDecimal(java.math.BigDecimal) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription)

Example 37 with Category

use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.

the class ReadableCatalogMapper method processCategory.

/**
 * B
 * 	1
 * 	  D
 * 	2
 * C
 * 	1
 * 	4
 * A
 * @param parent
 * @param c
 * @param store
 * @param language
 * @param hierarchy
 */
// TODO it needs to cover by unit tests
private void processCategory(Category c, MerchantStore store, Language language, Map<Long, ReadableCategory> hierarchy, Map<Long, ReadableCategory> processed) {
    // build category hierarchy
    ReadableCategory rc = null;
    ReadableCategory rp = null;
    if (CollectionUtils.isNotEmpty(c.getCategories())) {
        c.getCategories().stream().forEach(element -> {
            processCategory(element, store, language, hierarchy, processed);
        });
    }
    Category parent = c.getParent();
    if (Objects.nonNull(parent)) {
        rp = hierarchy.computeIfAbsent(parent.getId(), i -> toReadableCategory(c.getParent(), store, language, processed));
    }
    rc = toReadableCategory(c, store, language, processed);
    if (Objects.nonNull(rp)) {
        rp.getChildren().add(rc);
    } else {
        hierarchy.put(c.getId(), rc);
    }
}
Also used : Mapper(com.salesmanager.shop.mapper.Mapper) DateUtil(com.salesmanager.shop.utils.DateUtil) ReadableCatalog(com.salesmanager.shop.model.catalog.catalog.ReadableCatalog) StoreFacade(com.salesmanager.shop.store.controller.store.facade.StoreFacade) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ReadableMerchantStore(com.salesmanager.shop.model.store.ReadableMerchantStore) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Category(com.salesmanager.core.model.catalog.category.Category) Language(com.salesmanager.core.model.reference.language.Language) Component(org.springframework.stereotype.Component) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) AuditSection(com.salesmanager.core.model.common.audit.AuditSection) Map(java.util.Map) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) Optional(java.util.Optional) Catalog(com.salesmanager.core.model.catalog.catalog.Catalog) Category(com.salesmanager.core.model.catalog.category.Category) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory)

Example 38 with Category

use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.

the class ReadableProductMapper method merge.

@Override
public ReadableProduct merge(Product source, ReadableProduct destination, MerchantStore store, Language language) {
    Validate.notNull(source, "Product cannot be null");
    Validate.notNull(destination, "Product destination cannot be null");
    destination.setSku(source.getSku());
    destination.setRefSku(source.getRefSku());
    destination.setId(source.getId());
    destination.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
    ProductDescription description = null;
    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;
            }
        }
        destination.setId(source.getId());
        destination.setAvailable(source.isAvailable());
        destination.setProductShipeable(source.isProductShipeable());
        ProductSpecification specifications = new ProductSpecification();
        specifications.setHeight(source.getProductHeight());
        specifications.setLength(source.getProductLength());
        specifications.setWeight(source.getProductWeight());
        specifications.setWidth(source.getProductWidth());
        destination.setProductSpecifications(specifications);
        destination.setPreOrder(source.isPreOrder());
        destination.setRefSku(source.getRefSku());
        destination.setSortOrder(source.getSortOrder());
        if (source.getType() != null) {
            ReadableProductType readableType = readableProductTypeMapper.convert(source.getType(), store, language);
            destination.setType(readableType);
        }
        if (source.getDateAvailable() != null) {
            destination.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
        }
        if (source.getAuditSection() != null) {
            destination.setCreationDate(DateUtil.formatDate(source.getAuditSection().getDateCreated()));
        }
        destination.setProductVirtual(source.getProductVirtual());
        if (source.getProductReviewCount() != null) {
            destination.setRatingCount(source.getProductReviewCount().intValue());
        }
        if (source.getManufacturer() != null) {
            ReadableManufacturer manufacturer = readableManufacturerMapper.convert(source.getManufacturer(), store, language);
            destination.setManufacturer(manufacturer);
        }
        // images
        Set<ProductImage> images = source.getImages();
        if (CollectionUtils.isNotEmpty(images)) {
            List<ReadableImage> imageList = images.stream().map(i -> this.convertImage(source, i, store)).collect(Collectors.toList());
            destination.setImages(imageList);
        }
        // read only product values
        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();
                    // we need to set readonly attributes only
                    if (attribute.getAttributeDisplayOnly()) {
                        // read only attribute = property
                        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);
                        destination.getProperties().add(property);
                    }
                    if (selectableOptions != null) {
                        List<ReadableProductOption> options = new ArrayList<ReadableProductOption>(selectableOptions.values());
                        destination.setOptions(options);
                    }
                }
            }
        }
    }
    // 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;
        destination.setQuantity(availability.getProductQuantity() == null ? 1 : availability.getProductQuantity());
        destination.setQuantityOrderMaximum(availability.getProductQuantityOrderMax() == null ? 1 : availability.getProductQuantityOrderMax());
        destination.setQuantityOrderMinimum(availability.getProductQuantityOrderMin() == null ? 1 : availability.getProductQuantityOrderMin());
        if (availability.getProductQuantity().intValue() > 0 && destination.isAvailable()) {
            destination.setCanBePurchased(true);
        }
    // }
    }
    destination.setSku(source.getSku());
    try {
        FinalPrice price = pricingService.calculateProductPrice(source);
        if (price != null) {
            destination.setFinalPrice(pricingService.getDisplayAmount(price.getFinalPrice(), store));
            destination.setPrice(price.getFinalPrice());
            destination.setOriginalPrice(pricingService.getDisplayAmount(price.getOriginalPrice(), store));
            if (price.isDiscounted()) {
                destination.setDiscounted(true);
            }
            // price appender
            if (availability != null) {
                Set<ProductPrice> prices = availability.getPrices();
                if (!CollectionUtils.isEmpty(prices)) {
                    ReadableProductPrice readableProductPrice = new ReadableProductPrice();
                    readableProductPrice.setDiscounted(destination.isDiscounted());
                    readableProductPrice.setFinalPrice(destination.getFinalPrice());
                    readableProductPrice.setOriginalPrice(destination.getOriginalPrice());
                    Optional<ProductPrice> pr = prices.stream().filter(p -> p.getCode().equals(ProductPrice.DEFAULT_PRICE_CODE)).findFirst();
                    destination.setProductPrice(readableProductPrice);
                    if (pr.isPresent()) {
                        readableProductPrice.setId(pr.get().getId());
                        Optional<ProductPriceDescription> d = pr.get().getDescriptions().stream().filter(desc -> desc.getLanguage().getCode().equals(language.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);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new ConversionRuntimeException("An error while converting product price", e);
    }
    if (source.getProductReviewAvg() != null) {
        double avg = source.getProductReviewAvg().doubleValue();
        double rating = Math.round(avg * 2) / 2.0f;
        destination.setRating(rating);
    }
    if (source.getProductReviewCount() != null) {
        destination.setRatingCount(source.getProductReviewCount().intValue());
    }
    if (description != null) {
        com.salesmanager.shop.model.catalog.product.ProductDescription tragetDescription = populateDescription(description);
        destination.setDescription(tragetDescription);
    }
    if (!CollectionUtils.isEmpty(source.getCategories())) {
        List<ReadableCategory> categoryList = new ArrayList<ReadableCategory>();
        for (Category category : source.getCategories()) {
            ReadableCategory readableCategory = readableCategoryMapper.convert(category, store, language);
            categoryList.add(readableCategory);
        }
        destination.setCategories(categoryList);
    }
    ProductSpecification specifications = new ProductSpecification();
    specifications.setHeight(source.getProductHeight());
    specifications.setLength(source.getProductLength());
    specifications.setWeight(source.getProductWeight());
    specifications.setWidth(source.getProductWidth());
    if (!StringUtils.isBlank(store.getSeizeunitcode())) {
        specifications.setDimensionUnitOfMeasure(DimensionUnitOfMeasure.valueOf(store.getSeizeunitcode().toLowerCase()));
    }
    if (!StringUtils.isBlank(store.getWeightunitcode())) {
        specifications.setWeightUnitOfMeasure(WeightUnitOfMeasure.valueOf(store.getWeightunitcode().toLowerCase()));
    }
    destination.setProductSpecifications(specifications);
    destination.setSortOrder(source.getSortOrder());
    return destination;
}
Also used : ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) ReadableProductOptionValueEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionValueEntity) DateUtil(com.salesmanager.shop.utils.DateUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ReadableProductAttributeValue(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductAttributeValue) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) DimensionUnitOfMeasure(com.salesmanager.shop.model.references.DimensionUnitOfMeasure) Map(java.util.Map) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Mapper(com.salesmanager.shop.mapper.Mapper) Product(com.salesmanager.core.model.catalog.product.Product) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription) ProductOptionValue(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue) ReadableProductType(com.salesmanager.shop.model.catalog.product.type.ReadableProductType) ProductOptionDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription) Set(java.util.Set) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) Collectors(java.util.stream.Collectors) Category(com.salesmanager.core.model.catalog.category.Category) ReadableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer) List(java.util.List) Component(org.springframework.stereotype.Component) Validate(org.apache.commons.lang3.Validate) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) Optional(java.util.Optional) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) ReadableProductPropertyValue(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductPropertyValue) ReadableProductAttribute(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductAttribute) ReadableProductProperty(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductProperty) WeightUnitOfMeasure(com.salesmanager.shop.model.references.WeightUnitOfMeasure) ReadableProductOption(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductOption) ArrayList(java.util.ArrayList) ProductOptionDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription) ProductOptionValue(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ReadableProductAttributeValue(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductAttributeValue) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ReadableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer) ReadableProductProperty(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductProperty) ReadableProductOptionValueEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionValueEntity) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription) ReadableProductAttribute(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductAttribute) Category(com.salesmanager.core.model.catalog.category.Category) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ReadableProductAttribute(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductAttribute) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ReadableProductPropertyValue(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductPropertyValue) ReadableProductOption(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductOption) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) ReadableProductType(com.salesmanager.shop.model.catalog.product.type.ReadableProductType)

Example 39 with Category

use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.

the class PersistableCategoryPopulator method populate.

@Override
public Category populate(PersistableCategory source, Category target, MerchantStore store, Language language) throws ConversionException {
    try {
        Validate.notNull(target, "Category target cannot be null");
        /*		Validate.notNull(categoryService, "Requires to set CategoryService");
		Validate.notNull(languageService, "Requires to set LanguageService");*/
        target.setMerchantStore(store);
        target.setCode(source.getCode());
        target.setSortOrder(source.getSortOrder());
        target.setVisible(source.isVisible());
        target.setFeatured(source.isFeatured());
        // children
        if (!CollectionUtils.isEmpty(source.getChildren())) {
        // no modifications to children category
        } else {
            target.getCategories().clear();
        }
        if (source.getParent() == null || (StringUtils.isBlank(source.getParent().getCode())) || source.getParent().getId() == null) {
            target.setParent(null);
            target.setDepth(0);
            target.setLineage(new StringBuilder().append("/").append(source.getId()).append("/").toString());
        } else {
            Category parent = null;
            if (!StringUtils.isBlank(source.getParent().getCode())) {
                parent = categoryService.getByCode(store.getCode(), source.getParent().getCode());
            } else if (source.getParent().getId() != null) {
                parent = categoryService.getById(source.getParent().getId(), store.getId());
            } else {
                throw new ConversionException("Category parent needs at least an id or a code for reference");
            }
            if (parent != null && parent.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                throw new ConversionException("Store id does not belong to specified parent id");
            }
            if (parent != null) {
                target.setParent(parent);
                String lineage = parent.getLineage();
                int depth = parent.getDepth();
                target.setDepth(depth + 1);
                target.setLineage(new StringBuilder().append(lineage).append(target.getId()).append("/").toString());
            }
        }
        if (!CollectionUtils.isEmpty(source.getChildren())) {
            for (PersistableCategory cat : source.getChildren()) {
                Category persistCategory = this.populate(cat, new Category(), store, language);
                target.getCategories().add(persistCategory);
            }
        }
        if (!CollectionUtils.isEmpty(source.getDescriptions())) {
            Set<com.salesmanager.core.model.catalog.category.CategoryDescription> descriptions = new HashSet<com.salesmanager.core.model.catalog.category.CategoryDescription>();
            if (CollectionUtils.isNotEmpty(target.getDescriptions())) {
                for (com.salesmanager.core.model.catalog.category.CategoryDescription description : target.getDescriptions()) {
                    for (CategoryDescription d : source.getDescriptions()) {
                        if (StringUtils.isBlank(d.getLanguage())) {
                            throw new ConversionException("Source category description has no language");
                        }
                        if (d.getLanguage().equals(description.getLanguage().getCode())) {
                            description.setCategory(target);
                            description = buildDescription(d, description);
                            descriptions.add(description);
                        }
                    }
                }
            } else {
                for (CategoryDescription d : source.getDescriptions()) {
                    com.salesmanager.core.model.catalog.category.CategoryDescription t = new com.salesmanager.core.model.catalog.category.CategoryDescription();
                    this.buildDescription(d, t);
                    t.setCategory(target);
                    descriptions.add(t);
                }
            }
            target.setDescriptions(descriptions);
        }
        return target;
    } catch (Exception e) {
        throw new ConversionException(e);
    }
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) Category(com.salesmanager.core.model.catalog.category.Category) PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) ConversionException(com.salesmanager.core.business.exception.ConversionException) CategoryDescription(com.salesmanager.shop.model.catalog.category.CategoryDescription) HashSet(java.util.HashSet)

Example 40 with Category

use of com.salesmanager.core.model.catalog.category.Category in project shopizer by shopizer-ecommerce.

the class ProductServiceImpl method getProductsForLocale.

@Override
public List<Product> getProductsForLocale(Category category, Language language, Locale locale) throws ServiceException {
    if (category == null) {
        throw new ServiceException("The category is null");
    }
    // Get the category list
    StringBuilder lineage = new StringBuilder().append(category.getLineage()).append(category.getId()).append("/");
    List<Category> categories = categoryService.getListByLineage(category.getMerchantStore(), lineage.toString());
    Set<Long> categoryIds = new HashSet<Long>();
    for (Category c : categories) {
        categoryIds.add(c.getId());
    }
    categoryIds.add(category.getId());
    return productRepository.getProductsForLocale(category.getMerchantStore(), categoryIds, language, locale);
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) ServiceException(com.salesmanager.core.business.exception.ServiceException) HashSet(java.util.HashSet)

Aggregations

Category (com.salesmanager.core.model.catalog.category.Category)52 Language (com.salesmanager.core.model.reference.language.Language)30 ReadableCategory (com.salesmanager.shop.model.catalog.category.ReadableCategory)28 ArrayList (java.util.ArrayList)28 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)26 Product (com.salesmanager.core.model.catalog.product.Product)20 ServiceException (com.salesmanager.core.business.exception.ServiceException)15 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)14 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)14 ConversionException (com.salesmanager.core.business.exception.ConversionException)13 ProductDescription (com.salesmanager.core.model.catalog.product.description.ProductDescription)13 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)13 List (java.util.List)13 Collectors (java.util.stream.Collectors)13 ReadableCategoryPopulator (com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator)12 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)12 Validate (org.apache.commons.lang3.Validate)12 Manufacturer (com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer)11 ProductPrice (com.salesmanager.core.model.catalog.product.price.ProductPrice)11 PersistableCategory (com.salesmanager.shop.model.catalog.category.PersistableCategory)11