Search in sources :

Example 1 with ProductSpecification

use of com.salesmanager.shop.model.catalog.product.ProductSpecification in project shopizer by shopizer-ecommerce.

the class ReadableProductDefinitionMapper method merge.

@Override
public ReadableProductDefinition merge(Product source, ReadableProductDefinition destination, MerchantStore store, Language language) {
    Validate.notNull(source, "Product cannot be null");
    Validate.notNull(destination, "Product destination cannot be null");
    ReadableProductDefinition returnDestination = destination;
    if (language == null) {
        returnDestination = new ReadableProductDefinitionFull();
    }
    List<com.salesmanager.shop.model.catalog.product.ProductDescription> fulldescriptions = new ArrayList<com.salesmanager.shop.model.catalog.product.ProductDescription>();
    returnDestination.setIdentifier(source.getSku());
    returnDestination.setId(source.getId());
    returnDestination.setVisible(source.isAvailable());
    returnDestination.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;
            } else {
                fulldescriptions.add(populateDescription(desc));
            }
        }
    }
    if (description != null) {
        com.salesmanager.shop.model.catalog.product.ProductDescription tragetDescription = populateDescription(description);
        returnDestination.setDescription(tragetDescription);
    }
    if (source.getManufacturer() != null) {
        ReadableManufacturer manufacturer = readableManufacturerMapper.convert(source.getManufacturer(), store, language);
        returnDestination.setManufacturer(manufacturer);
    }
    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);
        }
        returnDestination.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()));
    }
    returnDestination.setProductSpecifications(specifications);
    if (source.getType() != null) {
        ReadableProductType readableType = readableProductTypeMapper.convert(source.getType(), store, language);
        returnDestination.setType(readableType);
    }
    returnDestination.setSortOrder(source.getSortOrder());
    // 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());
        returnDestination.setImages(imageList);
    }
    // quantity
    ProductAvailability availability = null;
    for (ProductAvailability a : source.getAvailabilities()) {
        availability = a;
        returnDestination.setCanBePurchased(availability.getProductStatus());
        returnDestination.setQuantity(availability.getProductQuantity() == null ? 1 : availability.getProductQuantity());
    }
    FinalPrice price = null;
    try {
        price = pricingService.calculateProductPrice(source);
    } catch (ServiceException e) {
        throw new ConversionRuntimeException("Unable to get product price", e);
    }
    if (price != null) {
        returnDestination.setPrice(price.getStringPrice());
    }
    if (returnDestination instanceof ReadableProductDefinitionFull) {
        ((ReadableProductDefinitionFull) returnDestination).setDescriptions(fulldescriptions);
    }
    return returnDestination;
}
Also used : 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) ServiceException(com.salesmanager.core.business.exception.ServiceException) 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) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ReadableProductDefinition(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinition) DimensionUnitOfMeasure(com.salesmanager.shop.model.references.DimensionUnitOfMeasure) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) ReadableProductDefinitionFull(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinitionFull) Mapper(com.salesmanager.shop.mapper.Mapper) Product(com.salesmanager.core.model.catalog.product.Product) ReadableProductType(com.salesmanager.shop.model.catalog.product.type.ReadableProductType) 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) 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) WeightUnitOfMeasure(com.salesmanager.shop.model.references.WeightUnitOfMeasure) 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) ArrayList(java.util.ArrayList) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) 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) ReadableProductDefinition(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinition) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ReadableProductDefinitionFull(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinitionFull) ServiceException(com.salesmanager.core.business.exception.ServiceException) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) ReadableProductType(com.salesmanager.shop.model.catalog.product.type.ReadableProductType)

Example 2 with ProductSpecification

use of com.salesmanager.shop.model.catalog.product.ProductSpecification in project shopizer by shopizer-ecommerce.

the class ServicesTestSupport method sampleProduct.

protected ReadableProduct sampleProduct(String code) {
    final PersistableCategory newCategory = new PersistableCategory();
    newCategory.setCode(code);
    newCategory.setSortOrder(1);
    newCategory.setVisible(true);
    newCategory.setDepth(4);
    final Category parent = new Category();
    newCategory.setParent(parent);
    final CategoryDescription description = new CategoryDescription();
    description.setLanguage("en");
    description.setName("test-cat");
    description.setFriendlyUrl("test-cat");
    description.setTitle("test-cat");
    final List<CategoryDescription> descriptions = new ArrayList<>();
    descriptions.add(description);
    newCategory.setDescriptions(descriptions);
    final HttpEntity<PersistableCategory> categoryEntity = new HttpEntity<>(newCategory, getHeader());
    final ResponseEntity<PersistableCategory> categoryResponse = testRestTemplate.postForEntity("/api/v1/private/category?store=" + Constants.DEFAULT_STORE, categoryEntity, PersistableCategory.class);
    final PersistableCategory cat = categoryResponse.getBody();
    assertThat(categoryResponse.getStatusCode(), is(CREATED));
    assertNotNull(cat.getId());
    final PersistableProduct product = new PersistableProduct();
    final ArrayList<Category> categories = new ArrayList<>();
    categories.add(cat);
    product.setCategories(categories);
    ProductSpecification specifications = new ProductSpecification();
    specifications.setManufacturer(com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer.DEFAULT_MANUFACTURER);
    product.setProductSpecifications(specifications);
    product.setAvailable(true);
    product.setPrice(BigDecimal.TEN);
    product.setSku(code);
    product.setQuantity(100);
    ProductDescription productDescription = new ProductDescription();
    productDescription.setDescription("TEST");
    productDescription.setName("TestName");
    productDescription.setLanguage("en");
    product.getDescriptions().add(productDescription);
    final HttpEntity<PersistableProduct> entity = new HttpEntity<>(product, getHeader());
    final ResponseEntity<PersistableProduct> response = testRestTemplate.postForEntity("/api/v1/private/product?store=" + Constants.DEFAULT_STORE, entity, PersistableProduct.class);
    assertThat(response.getStatusCode(), is(CREATED));
    final HttpEntity<String> httpEntity = new HttpEntity<>(getHeader());
    String apiUrl = "/api/v1/products/" + response.getBody().getId();
    ResponseEntity<ReadableProduct> readableProduct = testRestTemplate.exchange(apiUrl, HttpMethod.GET, httpEntity, ReadableProduct.class);
    assertThat(readableProduct.getStatusCode(), is(OK));
    return readableProduct.getBody();
}
Also used : PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) Category(com.salesmanager.shop.model.catalog.category.Category) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) PersistableProduct(com.salesmanager.shop.model.catalog.product.PersistableProduct) CategoryDescription(com.salesmanager.shop.model.catalog.category.CategoryDescription) ProductDescription(com.salesmanager.shop.model.catalog.product.ProductDescription)

Example 3 with ProductSpecification

use of com.salesmanager.shop.model.catalog.product.ProductSpecification in project shopizer by shopizer-ecommerce.

the class ReadableProductDefinitionMapper method merge.

@Override
public ReadableProductDefinition merge(Product source, ReadableProductDefinition destination, MerchantStore store, Language language) {
    Validate.notNull(source, "Product cannot be null");
    Validate.notNull(destination, "Product destination cannot be null");
    ReadableProductDefinition returnDestination = destination;
    if (language == null) {
        returnDestination = new ReadableProductDefinitionFull();
    }
    List<com.salesmanager.shop.model.catalog.product.ProductDescription> fulldescriptions = new ArrayList<com.salesmanager.shop.model.catalog.product.ProductDescription>();
    returnDestination.setIdentifier(source.getSku());
    returnDestination.setId(source.getId());
    returnDestination.setVisible(source.isAvailable());
    returnDestination.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;
            } else {
                fulldescriptions.add(populateDescription(desc));
            }
        }
    }
    if (description != null) {
        com.salesmanager.shop.model.catalog.product.ProductDescription tragetDescription = populateDescription(description);
        returnDestination.setDescription(tragetDescription);
    }
    if (source.getManufacturer() != null) {
        ReadableManufacturer manufacturer = readableManufacturerMapper.convert(source.getManufacturer(), store, language);
        returnDestination.setManufacturer(manufacturer);
    }
    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);
        }
        returnDestination.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()));
    }
    returnDestination.setProductSpecifications(specifications);
    if (source.getType() != null) {
        ReadableProductType readableType = readableProductTypeMapper.convert(source.getType(), store, language);
        returnDestination.setType(readableType);
    }
    returnDestination.setSortOrder(source.getSortOrder());
    // 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());
        returnDestination.setImages(imageList);
    }
    // quantity
    ProductAvailability availability = null;
    for (ProductAvailability a : source.getAvailabilities()) {
        availability = a;
        returnDestination.setCanBePurchased(availability.getProductStatus());
        returnDestination.setQuantity(availability.getProductQuantity() == null ? 1 : availability.getProductQuantity());
    }
    FinalPrice price = null;
    try {
        price = pricingService.calculateProductPrice(source);
    } catch (ServiceException e) {
        throw new ConversionRuntimeException("Unable to get product price", e);
    }
    if (price != null) {
        returnDestination.setPrice(price.getStringPrice());
    }
    if (returnDestination instanceof ReadableProductDefinitionFull) {
        ((ReadableProductDefinitionFull) returnDestination).setDescriptions(fulldescriptions);
    }
    return returnDestination;
}
Also used : DateUtil(com.salesmanager.shop.utils.DateUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) ReadableCategoryMapper(com.salesmanager.shop.mapper.catalog.ReadableCategoryMapper) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) ServiceException(com.salesmanager.core.business.exception.ServiceException) 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) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ReadableProductDefinition(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinition) DimensionUnitOfMeasure(com.salesmanager.shop.model.references.DimensionUnitOfMeasure) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) ReadableProductDefinitionFull(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinitionFull) Mapper(com.salesmanager.shop.mapper.Mapper) Product(com.salesmanager.core.model.catalog.product.Product) ReadableManufacturerMapper(com.salesmanager.shop.mapper.catalog.ReadableManufacturerMapper) ReadableProductType(com.salesmanager.shop.model.catalog.product.type.ReadableProductType) Set(java.util.Set) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) Collectors(java.util.stream.Collectors) Category(com.salesmanager.core.model.catalog.category.Category) ReadableProductTypeMapper(com.salesmanager.shop.mapper.catalog.ReadableProductTypeMapper) 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) 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) WeightUnitOfMeasure(com.salesmanager.shop.model.references.WeightUnitOfMeasure) 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) ArrayList(java.util.ArrayList) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) 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) ReadableProductDefinition(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinition) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ReadableProductDefinitionFull(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinitionFull) ServiceException(com.salesmanager.core.business.exception.ServiceException) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) ReadableProductType(com.salesmanager.shop.model.catalog.product.type.ReadableProductType)

Example 4 with ProductSpecification

use of com.salesmanager.shop.model.catalog.product.ProductSpecification in project shopizer by shopizer-ecommerce.

the class ProductCommonFacadeImpl method saveProduct.

@Override
public PersistableProduct saveProduct(MerchantStore store, PersistableProduct product, Language language) {
    String manufacturer = Manufacturer.DEFAULT_MANUFACTURER;
    if (product.getProductSpecifications() != null) {
        manufacturer = product.getProductSpecifications().getManufacturer();
    } else {
        ProductSpecification specifications = new ProductSpecification();
        specifications.setManufacturer(manufacturer);
    }
    Product target = null;
    if (product.getId() != null && product.getId().longValue() > 0) {
        target = productService.getById(product.getId());
    } else {
        target = new Product();
    }
    try {
        persistableProductPopulator.populate(product, target, store, language);
        if (target.getId() != null && target.getId() > 0) {
            productService.update(target);
        } else {
            productService.create(target);
            product.setId(target.getId());
        }
        return product;
    } catch (Exception e) {
        throw new ServiceRuntimeException(e);
    }
}
Also used : PersistableProduct(com.salesmanager.shop.model.catalog.product.PersistableProduct) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) LightPersistableProduct(com.salesmanager.shop.model.catalog.product.LightPersistableProduct) ServiceException(com.salesmanager.core.business.exception.ServiceException) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 5 with ProductSpecification

use of com.salesmanager.shop.model.catalog.product.ProductSpecification in project shopizer by shopizer-ecommerce.

the class ReadableProductPopulator method populate.

@Override
public ReadableProduct populate(Product source, ReadableProduct target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(pricingService, "Requires to set PricingService");
    Validate.notNull(imageUtils, "Requires to set imageUtils");
    try {
        List<com.salesmanager.shop.model.catalog.product.ProductDescription> fulldescriptions = new ArrayList<com.salesmanager.shop.model.catalog.product.ProductDescription>();
        if (language == null) {
            target = new ReadableProductFull();
        }
        if (target == null) {
            target = new ReadableProduct();
        }
        ProductDescription description = source.getProductDescription();
        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;
                } else {
                    fulldescriptions.add(populateDescription(desc));
                }
            }
        }
        if (target instanceof ReadableProductFull) {
            ((ReadableProductFull) target).setDescriptions(fulldescriptions);
        }
        if (language == null) {
            language = store.getDefaultLanguage();
        }
        final Language lang = language;
        target.setId(source.getId());
        target.setAvailable(source.isAvailable());
        target.setProductShipeable(source.isProductShipeable());
        ProductSpecification specifications = new ProductSpecification();
        specifications.setHeight(source.getProductHeight());
        specifications.setLength(source.getProductLength());
        specifications.setWeight(source.getProductWeight());
        specifications.setWidth(source.getProductWidth());
        target.setProductSpecifications(specifications);
        target.setPreOrder(source.isPreOrder());
        target.setRefSku(source.getRefSku());
        target.setSortOrder(source.getSortOrder());
        if (source.getType() != null) {
            target.setType(this.type(source.getType(), language));
        }
        if (source.getOwner() != null) {
            RentalOwner owner = new RentalOwner();
            owner.setId(source.getOwner().getId());
            owner.setEmailAddress(source.getOwner().getEmailAddress());
            owner.setFirstName(source.getOwner().getBilling().getFirstName());
            owner.setLastName(source.getOwner().getBilling().getLastName());
            com.salesmanager.shop.model.customer.address.Address address = new com.salesmanager.shop.model.customer.address.Address();
            address.setAddress(source.getOwner().getBilling().getAddress());
            address.setBillingAddress(true);
            address.setCity(source.getOwner().getBilling().getCity());
            address.setCompany(source.getOwner().getBilling().getCompany());
            address.setCountry(source.getOwner().getBilling().getCountry().getIsoCode());
            address.setZone(source.getOwner().getBilling().getZone().getCode());
            address.setLatitude(source.getOwner().getBilling().getLatitude());
            address.setLongitude(source.getOwner().getBilling().getLongitude());
            address.setPhone(source.getOwner().getBilling().getTelephone());
            address.setPostalCode(source.getOwner().getBilling().getPostalCode());
            owner.setAddress(address);
            target.setOwner(owner);
        }
        if (source.getDateAvailable() != null) {
            target.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
        }
        if (source.getAuditSection() != null) {
            target.setCreationDate(DateUtil.formatDate(source.getAuditSection().getDateCreated()));
        }
        /*			if(source.getProductReviewAvg()!=null) {
				double avg = source.getProductReviewAvg().doubleValue();
				double rating = Math.round(avg * 2) / 2.0f;
				target.setRating(rating);
			}*/
        target.setProductVirtual(source.getProductVirtual());
        /*			if(source.getProductReviewCount()!=null) {
				target.setRatingCount(source.getProductReviewCount().intValue());
			}*/
        if (description != null) {
            com.salesmanager.shop.model.catalog.product.ProductDescription tragetDescription = populateDescription(description);
            target.setDescription(tragetDescription);
        }
        if (source.getManufacturer() != null) {
            ManufacturerDescription manufacturer = source.getManufacturer().getDescriptions().iterator().next();
            ReadableManufacturer manufacturerEntity = new ReadableManufacturer();
            com.salesmanager.shop.model.catalog.manufacturer.ManufacturerDescription d = new com.salesmanager.shop.model.catalog.manufacturer.ManufacturerDescription();
            d.setName(manufacturer.getName());
            manufacturerEntity.setDescription(d);
            manufacturerEntity.setId(source.getManufacturer().getId());
            manufacturerEntity.setOrder(source.getManufacturer().getOrder());
            manufacturerEntity.setCode(source.getManufacturer().getCode());
            target.setManufacturer(manufacturerEntity);
        }
        /*			if(source.getType() != null) {
			  ReadableProductType type = new ReadableProductType();
			  type.setId(source.getType().getId());
			  type.setCode(source.getType().getCode());
			  type.setName(source.getType().getCode());//need name
			  target.setType(type);
			}*/
        /**
         * TODO use ProductImageMapper
         */
        Set<ProductImage> images = source.getImages();
        if (images != null && images.size() > 0) {
            List<ReadableImage> imageList = new ArrayList<ReadableImage>();
            String contextPath = imageUtils.getContextPath();
            for (ProductImage img : images) {
                ReadableImage prdImage = new ReadableImage();
                prdImage.setImageName(img.getProductImage());
                prdImage.setDefaultImage(img.isDefaultImage());
                prdImage.setOrder(img.getSortOrder() != null ? img.getSortOrder().intValue() : 0);
                if (img.getImageType() == 1 && img.getProductImageUrl() != null) {
                    prdImage.setImageUrl(img.getProductImageUrl());
                } else {
                    StringBuilder imgPath = new StringBuilder();
                    imgPath.append(contextPath).append(imageUtils.buildProductImageUtils(store, source.getSku(), img.getProductImage()));
                    prdImage.setImageUrl(imgPath.toString());
                }
                prdImage.setId(img.getId());
                prdImage.setImageType(img.getImageType());
                if (img.getProductImageUrl() != null) {
                    prdImage.setExternalUrl(img.getProductImageUrl());
                }
                if (img.getImageType() == 1 && img.getProductImageUrl() != null) {
                    // video
                    prdImage.setVideoUrl(img.getProductImageUrl());
                }
                if (prdImage.isDefaultImage()) {
                    target.setImage(prdImage);
                }
                imageList.add(prdImage);
            }
            imageList = imageList.stream().sorted(Comparator.comparingInt(ReadableImage::getOrder)).collect(Collectors.toList());
            target.setImages(imageList);
        }
        if (!CollectionUtils.isEmpty(source.getCategories())) {
            ReadableCategoryPopulator categoryPopulator = new ReadableCategoryPopulator();
            List<ReadableCategory> categoryList = new ArrayList<ReadableCategory>();
            for (Category category : source.getCategories()) {
                ReadableCategory readableCategory = new ReadableCategory();
                categoryPopulator.populate(category, readableCategory, store, language);
                categoryList.add(readableCategory);
            }
            target.setCategories(categoryList);
        }
        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();
                    if (attribute.getAttributeDisplayOnly()) {
                        // read only attribute = property
                        /*
								if(readOnlyAttributes==null) {
									readOnlyAttributes = new TreeMap<Long,ReadableProductAttribute>();
								}
								attr = readOnlyAttributes.get(attribute.getProductOption().getId());
								if(attr==null) {
									attr = createAttribute(attribute, language);
								}
								if(attr!=null) {
									readOnlyAttributes.put(attribute.getProductOption().getId(), attr);
								}


								attrValue.setDefaultValue(attribute.getAttributeDefault());
								if(attribute.getProductOptionValue()!=null) {
								  attrValue.setId(attribute.getProductOptionValue().getId());//id of the option value
								} else {
								  attrValue.setId(attribute.getId());
								}
								attrValue.setLang(language.getCode());


								attrValue.setSortOrder(0);
								if(attribute.getProductOptionSortOrder()!=null) {
									attrValue.setSortOrder(attribute.getProductOptionSortOrder().intValue());
								}

								List<ProductOptionValueDescription> podescriptions = optionValue.getDescriptionsSettoList();
								ProductOptionValueDescription podescription = null;
								if(podescriptions!=null && podescriptions.size()>0) {
									podescription = podescriptions.get(0);
									if(podescriptions.size()>1) {
										for(ProductOptionValueDescription optionValueDescription : podescriptions) {
											if(optionValueDescription.getLanguage().getId().intValue()==language.getId().intValue()) {
												podescription = optionValueDescription;
												break;
											}
										}
									}
								}
								attrValue.setName(podescription.getName());
								attrValue.setDescription(podescription.getDescription());

								if(attr!=null) {
									attr.getAttributeValues().add(attrValue);
								}
*/
                        // if(properties==null) {
                        // properties = new TreeMap<Long,ReadableProductProperty>();
                        // }
                        // property = properties.get(attribute.getProductOption().getId());
                        // if(property==null) {
                        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);
                        // } else{
                        // properties.put(attribute.getProductOption().getId(), property);
                        // }
                        /*								propertyValue.setCode(attribute.getProductOptionValue().getCode());
								propertyValue.setId(attribute.getProductOptionValue().getId());


								propertyValue.setSortOrder(0);
								if(attribute.getProductOptionSortOrder()!=null) {
									propertyValue.setSortOrder(attribute.getProductOptionSortOrder().intValue());
								}

								List<ProductOptionValueDescription> podescriptions = optionValue.getDescriptionsSettoList();
								if(podescriptions!=null && podescriptions.size()>0) {
									for(ProductOptionValueDescription optionValueDescription : podescriptions) {
										com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription desc = new com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription();
										desc.setId(optionValueDescription.getId());
										desc.setName(optionValueDescription.getName());
										propertyValue.getValues().add(desc);
									}
								}

								property.setPropertyValue(propertyValue);*/
                        // if(attr!=null) {
                        // attr.getAttributeValues().add(attrValue);
                        // }
                        target.getProperties().add(property);
                    } else {
                        if (selectableOptions == null) {
                            selectableOptions = new TreeMap<Long, ReadableProductOption>();
                        }
                        opt = selectableOptions.get(attribute.getProductOption().getId());
                        if (opt == null) {
                            opt = createOption(attribute, language);
                        }
                        if (opt != null) {
                            selectableOptions.put(attribute.getProductOption().getId(), opt);
                        }
                        optValue.setDefaultValue(attribute.getAttributeDefault());
                        // optValue.setId(attribute.getProductOptionValue().getId());
                        optValue.setId(attribute.getId());
                        optValue.setCode(attribute.getProductOptionValue().getCode());
                        com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription valueDescription = new com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription();
                        valueDescription.setLanguage(language.getCode());
                        // optValue.setLang(language.getCode());
                        if (attribute.getProductAttributePrice() != null && attribute.getProductAttributePrice().doubleValue() > 0) {
                            String formatedPrice = pricingService.getDisplayAmount(attribute.getProductAttributePrice(), store);
                            optValue.setPrice(formatedPrice);
                        }
                        if (!StringUtils.isBlank(attribute.getProductOptionValue().getProductOptionValueImage())) {
                            optValue.setImage(imageUtils.buildProductPropertyImageUtils(store, attribute.getProductOptionValue().getProductOptionValueImage()));
                        }
                        optValue.setSortOrder(0);
                        if (attribute.getProductOptionSortOrder() != null) {
                            optValue.setSortOrder(attribute.getProductOptionSortOrder().intValue());
                        }
                        List<ProductOptionValueDescription> podescriptions = optionValue.getDescriptionsSettoList();
                        ProductOptionValueDescription podescription = null;
                        if (podescriptions != null && podescriptions.size() > 0) {
                            podescription = podescriptions.get(0);
                            if (podescriptions.size() > 1) {
                                for (ProductOptionValueDescription optionValueDescription : podescriptions) {
                                    if (optionValueDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
                                        podescription = optionValueDescription;
                                        break;
                                    }
                                }
                            }
                        }
                        valueDescription.setName(podescription.getName());
                        valueDescription.setDescription(podescription.getDescription());
                        optValue.setDescription(valueDescription);
                        if (opt != null) {
                            opt.getOptionValues().add(optValue);
                        }
                    }
                }
            }
            if (selectableOptions != null) {
                List<ReadableProductOption> options = new ArrayList<ReadableProductOption>(selectableOptions.values());
                target.setOptions(options);
            }
        }
        // remove products from invisible category -> set visible = false
        /*			Set<Category> categories = source.getCategories();
			boolean isVisible = true;
			if(!CollectionUtils.isEmpty(categories)) {
				for(Category c : categories) {
					if(c.isVisible()) {
						isVisible = true;
						break;
					} else {
						isVisible = false;
					}
				}
			}*/
        // target.setVisible(isVisible);
        // 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;
            target.setQuantity(availability.getProductQuantity() == null ? 1 : availability.getProductQuantity());
            target.setQuantityOrderMaximum(availability.getProductQuantityOrderMax() == null ? 1 : availability.getProductQuantityOrderMax());
            target.setQuantityOrderMinimum(availability.getProductQuantityOrderMin() == null ? 1 : availability.getProductQuantityOrderMin());
            if (availability.getProductQuantity().intValue() > 0 && target.isAvailable()) {
                target.setCanBePurchased(true);
            }
        // }
        }
        target.setSku(source.getSku());
        FinalPrice price = pricingService.calculateProductPrice(source);
        if (price != null) {
            target.setFinalPrice(pricingService.getDisplayAmount(price.getFinalPrice(), store));
            target.setPrice(price.getFinalPrice());
            target.setOriginalPrice(pricingService.getDisplayAmount(price.getOriginalPrice(), store));
            if (price.isDiscounted()) {
                target.setDiscounted(true);
            }
            // price appender
            if (availability != null) {
                Set<ProductPrice> prices = availability.getPrices();
                if (!CollectionUtils.isEmpty(prices)) {
                    ReadableProductPrice readableProductPrice = new ReadableProductPrice();
                    readableProductPrice.setDiscounted(target.isDiscounted());
                    readableProductPrice.setFinalPrice(target.getFinalPrice());
                    readableProductPrice.setOriginalPrice(target.getOriginalPrice());
                    Optional<ProductPrice> pr = prices.stream().filter(p -> p.getCode().equals(ProductPrice.DEFAULT_PRICE_CODE)).findFirst();
                    target.setProductPrice(readableProductPrice);
                    if (pr.isPresent()) {
                        readableProductPrice.setId(pr.get().getId());
                        Optional<ProductPriceDescription> d = pr.get().getDescriptions().stream().filter(desc -> desc.getLanguage().getCode().equals(lang.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);
                        }
                    }
                }
            }
        }
        if (target instanceof ReadableProductFull) {
            ((ReadableProductFull) target).setDescriptions(fulldescriptions);
        }
        return target;
    } catch (Exception e) {
        throw new ConversionException(e);
    }
}
Also used : ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) RentalOwner(com.salesmanager.shop.model.catalog.product.RentalOwner) ReadableProductOptionValueEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionValueEntity) DateUtil(com.salesmanager.shop.utils.DateUtil) 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) ReadableProductFull(com.salesmanager.shop.model.catalog.product.ReadableProductFull) 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) ManufacturerDescription(com.salesmanager.core.model.catalog.product.manufacturer.ManufacturerDescription) Map(java.util.Map) 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) Product(com.salesmanager.core.model.catalog.product.Product) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription) AbstractDataPopulator(com.salesmanager.core.business.utils.AbstractDataPopulator) 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) Validate(org.apache.commons.lang3.Validate) TreeMap(java.util.TreeMap) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) Optional(java.util.Optional) ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductType(com.salesmanager.core.model.catalog.product.type.ProductType) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ProductTypeDescription(com.salesmanager.shop.model.catalog.product.type.ProductTypeDescription) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) Comparator(java.util.Comparator) 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) ReadableProductOption(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductOption) ArrayList(java.util.ArrayList) RentalOwner(com.salesmanager.shop.model.catalog.product.RentalOwner) 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) ConversionException(com.salesmanager.core.business.exception.ConversionException) ReadableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer) ReadableProductProperty(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductProperty) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) ReadableProductFull(com.salesmanager.shop.model.catalog.product.ReadableProductFull) ReadableProductOptionValueEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionValueEntity) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription) Category(com.salesmanager.core.model.catalog.category.Category) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ReadableProductAttribute(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductAttribute) 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) Language(com.salesmanager.core.model.reference.language.Language) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ReadableProductPropertyValue(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductPropertyValue) ManufacturerDescription(com.salesmanager.core.model.catalog.product.manufacturer.ManufacturerDescription) ReadableProductOption(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductOption) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription)

Aggregations

ProductSpecification (com.salesmanager.shop.model.catalog.product.ProductSpecification)12 ArrayList (java.util.ArrayList)10 PersistableProduct (com.salesmanager.shop.model.catalog.product.PersistableProduct)7 Product (com.salesmanager.core.model.catalog.product.Product)6 ServiceException (com.salesmanager.core.business.exception.ServiceException)5 ProductDescription (com.salesmanager.core.model.catalog.product.description.ProductDescription)5 ProductImage (com.salesmanager.core.model.catalog.product.image.ProductImage)5 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)5 Category (com.salesmanager.shop.model.catalog.category.Category)5 PersistableCategory (com.salesmanager.shop.model.catalog.category.PersistableCategory)5 ReadableCategory (com.salesmanager.shop.model.catalog.category.ReadableCategory)5 ReadableManufacturer (com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer)5 ReadableImage (com.salesmanager.shop.model.catalog.product.ReadableImage)5 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)5 List (java.util.List)5 PricingService (com.salesmanager.core.business.services.catalog.product.PricingService)4 Category (com.salesmanager.core.model.catalog.category.Category)4 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)4 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)4 Language (com.salesmanager.core.model.reference.language.Language)4