Search in sources :

Example 6 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 {
        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) {
            defaultPrice = new ProductPrice();
            defaultPrice.setDefaultPrice(true);
            defaultPrice.setProductPriceAmount(source.getPrice());
            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) 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 7 with Category

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

the class CategoryRepositoryImpl method listByProduct.

@Override
public List<Category> listByProduct(MerchantStore store, Long product) {
    StringBuilder qs = new StringBuilder();
    qs.append("select category from Product product ");
    qs.append("inner join product.categories category inner join product.merchantStore pm ");
    qs.append("where product.id=:id and pm.id=:mid ");
    qs.append("group by category.id");
    String hql = qs.toString();
    Query q = this.em.createQuery(hql);
    q.setParameter("id", product);
    q.setParameter("mid", store.getId());
    @SuppressWarnings("unchecked") List<Category> c = q.getResultList();
    return c;
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) Query(javax.persistence.Query)

Example 8 with Category

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

the class CategoryTest method testCategory.

/**
 * This method creates multiple products using multiple catalog APIs
 * @throws ServiceException
 */
@Test
public void testCategory() throws Exception {
    Language en = languageService.getByCode("en");
    Language fr = languageService.getByCode("fr");
    MerchantStore store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);
    Category materingstuff = new Category();
    materingstuff.setMerchantStore(store);
    materingstuff.setCode("materingstuff");
    CategoryDescription bookEnglishDescription = new CategoryDescription();
    bookEnglishDescription.setName("Book");
    bookEnglishDescription.setCategory(materingstuff);
    bookEnglishDescription.setLanguage(en);
    CategoryDescription bookFrenchDescription = new CategoryDescription();
    bookFrenchDescription.setName("Livre");
    bookFrenchDescription.setCategory(materingstuff);
    bookFrenchDescription.setLanguage(fr);
    Set<CategoryDescription> descriptions = new HashSet<CategoryDescription>();
    descriptions.add(bookEnglishDescription);
    descriptions.add(bookFrenchDescription);
    materingstuff.setDescriptions(descriptions);
    categoryService.create(materingstuff);
    assertNotNull(materingstuff.getId());
    Long bookId = materingstuff.getId();
    Category fetchedBook = categoryService.getById(bookId, store.getId());
    Assert.assertEquals(2, fetchedBook.getDescriptions().size());
    // Clean up for other tests
    categoryService.delete(materingstuff);
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) Language(com.salesmanager.core.model.reference.language.Language) CategoryDescription(com.salesmanager.core.model.catalog.category.CategoryDescription) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with Category

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

the class ProductTest method testCreateRelationShip.

private void testCreateRelationShip(Product product) throws Exception {
    MerchantStore store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);
    Language en = languageService.getByCode("en");
    Manufacturer oreilley = manufacturerService.getByCode(store, "oreilley");
    ProductType generalType = productTypeService.getProductType(ProductType.GENERAL_TYPE);
    Category tech = categoryService.getByCode(store, "tech");
    // create new related product
    // PRODUCT 1
    Product related = new Product();
    related.setProductHeight(new BigDecimal(4));
    related.setProductLength(new BigDecimal(3));
    related.setProductWidth(new BigDecimal(1));
    related.setSku("TB67891");
    related.setManufacturer(oreilley);
    related.setType(generalType);
    related.setMerchantStore(store);
    // Product description
    ProductDescription description = new ProductDescription();
    description.setName("Spring 4 in Action");
    description.setLanguage(en);
    description.setProduct(related);
    product.getDescriptions().add(description);
    // add category
    product.getCategories().add(tech);
    // Availability
    ProductAvailability availability = new ProductAvailability();
    availability.setProductDateAvailable(date);
    availability.setProductQuantity(200);
    availability.setRegion("*");
    // associate with product
    availability.setProduct(related);
    // productAvailabilityService.create(availability);
    related.getAvailabilities().add(availability);
    ProductPrice dprice = new ProductPrice();
    dprice.setDefaultPrice(true);
    dprice.setProductPriceAmount(new BigDecimal(39.99));
    dprice.setProductAvailability(availability);
    ProductPriceDescription dpd = new ProductPriceDescription();
    dpd.setName("Base price");
    dpd.setProductPrice(dprice);
    dpd.setLanguage(en);
    dprice.getDescriptions().add(dpd);
    availability.getPrices().add(dprice);
    related.getAvailabilities().add(availability);
    productService.save(related);
    ProductRelationship relationship = new ProductRelationship();
    relationship.setActive(true);
    relationship.setCode("spring");
    relationship.setProduct(product);
    relationship.setRelatedProduct(related);
    relationship.setStore(store);
    // because relationships are nor joined fetched, make sure you query relationships first, then ad to an existing list
    // so relationship and review are they only objects not joined fetch when querying a product
    // need to do a subsequent query
    List<ProductRelationship> relationships = productRelationshipService.listByProduct(product);
    relationships.add(relationship);
    product.setRelationships(new HashSet<ProductRelationship>(relationships));
    productService.save(product);
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) ProductType(com.salesmanager.core.model.catalog.product.type.ProductType) Product(com.salesmanager.core.model.catalog.product.Product) ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) BigDecimal(java.math.BigDecimal) Language(com.salesmanager.core.model.reference.language.Language) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ProductRelationship(com.salesmanager.core.model.catalog.product.relationship.ProductRelationship) Manufacturer(com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription)

Example 10 with Category

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

the class ProductCommonFacadeImpl method addProductToCategory.

@Override
public ReadableProduct addProductToCategory(Category category, Product product, Language language) {
    Validate.notNull(category, "Category cannot be null");
    Validate.notNull(product, "Product cannot be null");
    // not alloweed if category already attached
    List<Category> assigned = product.getCategories().stream().filter(cat -> cat.getId().longValue() == category.getId().longValue()).collect(Collectors.toList());
    if (assigned.size() > 0) {
        throw new OperationNotAllowedException("Category with id [" + category.getId() + "] already attached to product [" + product.getId() + "]");
    }
    product.getCategories().add(category);
    ReadableProduct readableProduct = new ReadableProduct();
    try {
        productService.update(product);
        ReadableProductPopulator populator = new ReadableProductPopulator();
        populator.setPricingService(pricingService);
        populator.setimageUtils(imageUtils);
        populator.populate(product, readableProduct, product.getMerchantStore(), language);
    } catch (Exception e) {
        throw new RuntimeException("Exception when adding product [" + product.getId() + "] to category [" + category.getId() + "]", e);
    }
    return readableProduct;
}
Also used : ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) Date(java.util.Date) DateUtil(com.salesmanager.shop.utils.DateUtil) StringUtils(org.apache.commons.lang3.StringUtils) LanguageService(com.salesmanager.core.business.services.reference.language.LanguageService) PersistableProductPopulator(com.salesmanager.shop.populator.catalog.PersistableProductPopulator) ArrayList(java.util.ArrayList) ServiceException(com.salesmanager.core.business.exception.ServiceException) Inject(javax.inject.Inject) Language(com.salesmanager.core.model.reference.language.Language) PersistableProduct(com.salesmanager.shop.model.catalog.product.PersistableProduct) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) Manufacturer(com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer) ReadableProductReview(com.salesmanager.shop.model.catalog.product.ReadableProductReview) CategoryService(com.salesmanager.core.business.services.catalog.category.CategoryService) ProductAttributeService(com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService) CustomerService(com.salesmanager.core.business.services.customer.CustomerService) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ProductReviewService(com.salesmanager.core.business.services.catalog.product.review.ProductReviewService) Product(com.salesmanager.core.model.catalog.product.Product) LightPersistableProduct(com.salesmanager.shop.model.catalog.product.LightPersistableProduct) ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview) ProductRelationshipService(com.salesmanager.core.business.services.catalog.product.relationship.ProductRelationshipService) PersistableProductReviewPopulator(com.salesmanager.shop.populator.catalog.PersistableProductReviewPopulator) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) Collectors(java.util.stream.Collectors) ProductCommonFacade(com.salesmanager.shop.store.controller.product.facade.ProductCommonFacade) Category(com.salesmanager.core.model.catalog.category.Category) ReadableProductReviewPopulator(com.salesmanager.shop.populator.catalog.ReadableProductReviewPopulator) List(java.util.List) Validate(org.apache.commons.lang3.Validate) PersistableProductReview(com.salesmanager.shop.model.catalog.product.PersistableProductReview) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ProductPriceEntity(com.salesmanager.shop.model.catalog.product.ProductPriceEntity) Category(com.salesmanager.core.model.catalog.category.Category) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) 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)

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