Search in sources :

Example 46 with Product

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

the class ShoppingCartFacadeImpl method createCartItems.

// used for api
private List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> createCartItems(ShoppingCart cartModel, List<PersistableShoppingCartItem> shoppingCartItems, MerchantStore store) throws Exception {
    List<Long> productIds = shoppingCartItems.stream().map(s -> Long.valueOf(s.getProduct())).collect(Collectors.toList());
    List<Product> products = productService.getProductsByIds(productIds);
    if (products == null || products.size() != shoppingCartItems.size()) {
        LOG.warn("----------------------- Items with in id-list " + productIds + " does not exist");
        throw new ResourceNotFoundException("Item with id " + productIds + " does not exist");
    }
    List<Product> wrongStoreProducts = products.stream().filter(p -> p.getMerchantStore().getId() != store.getId()).collect(Collectors.toList());
    if (wrongStoreProducts.size() > 0) {
        throw new ResourceNotFoundException("One or more of the items with id's " + wrongStoreProducts.stream().map(s -> Long.valueOf(s.getId())).collect(Collectors.toList()) + " does not belong to merchant " + store.getId());
    }
    List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = new ArrayList<>();
    for (Product p : products) {
        com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = shoppingCartService.populateShoppingCartItem(p);
        Optional<PersistableShoppingCartItem> oShoppingCartItem = shoppingCartItems.stream().filter(i -> i.getProduct() == p.getId()).findFirst();
        if (!oShoppingCartItem.isPresent()) {
            // Should never happen if not something is updated in realtime or user has item in local storage and add it long time after to cart!
            LOG.warn("Missing shoppingCartItem for product " + p.getSku() + " ( " + p.getId() + " )");
            continue;
        }
        PersistableShoppingCartItem shoppingCartItem = oShoppingCartItem.get();
        item.setQuantity(shoppingCartItem.getQuantity());
        item.setShoppingCart(cartModel);
        /**
         * Check if product is available
         * Check if product quantity is 0
         * Check if date available <= now
         */
        if (!p.isAvailable()) {
            throw new Exception("Item with id " + p.getId() + " is not available");
        }
        Set<ProductAvailability> availabilities = p.getAvailabilities();
        if (availabilities == null) {
            throw new Exception("Item with id " + p.getId() + " is not properly configured");
        }
        for (ProductAvailability availability : availabilities) {
            if (availability.getProductQuantity() == null || availability.getProductQuantity().intValue() == 0) {
                throw new Exception("Item with id " + p.getId() + " is not available");
            }
        }
        if (!DateUtil.dateBeforeEqualsDate(p.getDateAvailable(), new Date())) {
            throw new Exception("Item with id " + p.getId() + " is not available");
        }
        // end qty & availablility checks
        // set attributes
        List<com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute> attributes = shoppingCartItem.getAttributes();
        if (!CollectionUtils.isEmpty(attributes)) {
            for (com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute attribute : attributes) {
                ProductAttribute productAttribute = productAttributeService.getById(attribute.getId());
                if (productAttribute != null && productAttribute.getProduct().getId().longValue() == p.getId().longValue()) {
                    com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attributeItem = new com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem(item, productAttribute);
                    item.addAttributes(attributeItem);
                }
            }
        }
        items.add(item);
    }
    return items;
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) NoResultException(javax.persistence.NoResultException) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ServiceException(com.salesmanager.core.business.exception.ServiceException) RequestContextHolder(org.springframework.web.context.request.RequestContextHolder) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ShoppingCartDataPopulator(com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Set(java.util.Set) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) List(java.util.List) ShoppingCartCalculationService(com.salesmanager.core.business.services.shoppingcart.ShoppingCartCalculationService) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) LocalDate(java.time.LocalDate) Optional(java.util.Optional) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ProductPriceUtils(com.salesmanager.core.business.utils.ProductPriceUtils) ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) Constants(com.salesmanager.shop.constants.Constants) ReadableShoppingCartMapper(com.salesmanager.shop.mapper.cart.ReadableShoppingCartMapper) DateUtil(com.salesmanager.shop.utils.DateUtil) CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Language(com.salesmanager.core.model.reference.language.Language) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Nullable(org.springframework.lang.Nullable) ShoppingCartAttribute(com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute) ProductAttributeService(com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService) ShoppingCartService(com.salesmanager.core.business.services.shoppingcart.ShoppingCartService) Product(com.salesmanager.core.model.catalog.product.Product) Logger(org.slf4j.Logger) Customer(com.salesmanager.core.model.customer.Customer) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ShoppingCartData(com.salesmanager.shop.model.shoppingcart.ShoppingCartData) Validate(org.apache.commons.lang3.Validate) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) NoResultException(javax.persistence.NoResultException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Date(java.util.Date) LocalDate(java.time.LocalDate) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)

Example 47 with Product

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

the class ProductServiceImpl method listByStore.

@Override
public Page<Product> listByStore(MerchantStore store, Language language, ProductCriteria criteria, int page, int count) {
    criteria.setPageSize(page);
    criteria.setPageSize(count);
    criteria.setLegacyPagination(false);
    ProductList productList = productRepository.listByStore(store, language, criteria);
    PageRequest pageRequest = PageRequest.of(page, count);
    @SuppressWarnings({ "rawtypes", "unchecked" }) Page<Product> p = new PageImpl(productList.getProducts(), pageRequest, productList.getTotalCount());
    return p;
}
Also used : ProductList(com.salesmanager.core.model.catalog.product.ProductList) PageImpl(org.springframework.data.domain.PageImpl) PageRequest(org.springframework.data.domain.PageRequest) Product(com.salesmanager.core.model.catalog.product.Product)

Example 48 with Product

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

the class ProductServiceImpl method getProductForLocale.

@Override
public Product getProductForLocale(long productId, Language language, Locale locale) throws ServiceException {
    Product product = productRepository.getProductForLocale(productId, language, locale);
    if (product == null) {
        return null;
    }
    CatalogServiceHelper.setToAvailability(product, locale);
    CatalogServiceHelper.setToLanguage(product, language.getId());
    return product;
}
Also used : Product(com.salesmanager.core.model.catalog.product.Product)

Example 49 with Product

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

the class CategoryServiceImpl method delete.

// @Override
public void delete(Category category) throws ServiceException {
    // get category with lineage (subcategories)
    StringBuilder lineage = new StringBuilder();
    lineage.append(category.getLineage()).append(category.getId()).append(Constants.SLASH);
    List<Category> categories = this.getListByLineage(category.getMerchantStore(), lineage.toString());
    Category dbCategory = getById(category.getId(), category.getMerchantStore().getId());
    if (dbCategory != null && dbCategory.getId().longValue() == category.getId().longValue()) {
        categories.add(dbCategory);
        Collections.reverse(categories);
        List<Long> categoryIds = new ArrayList<Long>();
        for (Category c : categories) {
            categoryIds.add(c.getId());
        }
        List<Product> products = productService.getProducts(categoryIds);
        for (Product product : products) {
            // session.evict(product);// refresh product so we get all
            // product categories
            Product dbProduct = productService.getById(product.getId());
            Set<Category> productCategories = dbProduct.getCategories();
            if (productCategories.size() > 1) {
                for (Category c : categories) {
                    productCategories.remove(c);
                    productService.update(dbProduct);
                }
                if (product.getCategories() == null || product.getCategories().size() == 0) {
                    productService.delete(dbProduct);
                }
            } else {
                productService.delete(dbProduct);
            }
        }
        Category categ = getById(category.getId(), category.getMerchantStore().getId());
        categoryRepository.delete(categ);
    }
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product)

Example 50 with Product

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

the class ShoppingCartModelPopulator method createCartItem.

private com.salesmanager.core.model.shoppingcart.ShoppingCartItem createCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCart cart, ShoppingCartItem shoppingCartItem, MerchantStore store) throws Exception {
    Product product = productService.getById(shoppingCartItem.getProductId());
    if (product == null) {
        throw new Exception("Item with id " + shoppingCartItem.getProductId() + " does not exist");
    }
    if (product.getMerchantStore().getId().intValue() != store.getId().intValue()) {
        throw new Exception("Item with id " + shoppingCartItem.getProductId() + " does not belong to merchant " + store.getId());
    }
    com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = new com.salesmanager.core.model.shoppingcart.ShoppingCartItem(cart, product);
    item.setQuantity(shoppingCartItem.getQuantity());
    item.setItemPrice(shoppingCartItem.getProductPrice());
    item.setShoppingCart(cart);
    // attributes
    List<ShoppingCartAttribute> cartAttributes = shoppingCartItem.getShoppingCartAttributes();
    if (!CollectionUtils.isEmpty(cartAttributes)) {
        Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> newAttributes = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem>();
        for (ShoppingCartAttribute attribute : cartAttributes) {
            ProductAttribute productAttribute = productAttributeService.getById(attribute.getAttributeId());
            if (productAttribute != null && productAttribute.getProduct().getId().longValue() == product.getId().longValue()) {
                com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attributeItem = new com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem(item, productAttribute);
                if (attribute.getAttributeId() > 0) {
                    attributeItem.setId(attribute.getId());
                }
                item.addAttributes(attributeItem);
            // newAttributes.add( attributeItem );
            }
        }
    // item.setAttributes( newAttributes );
    }
    return item;
}
Also used : Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ConversionException(org.apache.commons.beanutils.ConversionException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ShoppingCartAttribute(com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) HashSet(java.util.HashSet)

Aggregations

Product (com.salesmanager.core.model.catalog.product.Product)120 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)53 ArrayList (java.util.ArrayList)45 Language (com.salesmanager.core.model.reference.language.Language)42 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)41 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)36 ServiceException (com.salesmanager.core.business.exception.ServiceException)35 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)35 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)33 ReadableProductPopulator (com.salesmanager.shop.populator.catalog.ReadableProductPopulator)33 PersistableProduct (com.salesmanager.shop.model.catalog.product.PersistableProduct)29 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)28 List (java.util.List)25 Date (java.util.Date)23 LightPersistableProduct (com.salesmanager.shop.model.catalog.product.LightPersistableProduct)22 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)21 Category (com.salesmanager.core.model.catalog.category.Category)20 Collectors (java.util.stream.Collectors)20 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)19 ConversionException (com.salesmanager.core.business.exception.ConversionException)17