Search in sources :

Example 1 with ProductList

use of com.salesmanager.core.model.catalog.product.ProductList 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 2 with ProductList

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

the class ProductRepositoryImpl method getProductsListForLocale.

/**
 * This query is used for category listings. All collections are not fully
 * loaded, only the required objects so the listing page can display
 * everything related to all products
 */
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
private ProductList getProductsListForLocale(MerchantStore store, Set categoryIds, Language language, Locale locale, int first, int max) {
    List regionList = new ArrayList();
    regionList.add(Constants.ALL_REGIONS);
    if (locale != null) {
        regionList.add(locale.getCountry());
    }
    ProductList productList = new ProductList();
    Query countQ = this.em.createQuery("select count(p) from Product as p INNER JOIN p.availabilities pa INNER JOIN p.categories categs where p.merchantStore.id=:mId and categs.id in (:cid) and pa.region in (:lid) and p.available=1 and p.dateAvailable<=:dt");
    countQ.setParameter("cid", categoryIds);
    countQ.setParameter("lid", regionList);
    countQ.setParameter("dt", new Date());
    countQ.setParameter("mId", store.getId());
    Number count = (Number) countQ.getSingleResult();
    productList.setTotalCount(count.intValue());
    if (count.intValue() == 0)
        return productList;
    StringBuilder qs = new StringBuilder();
    qs.append("select p from Product as p ");
    qs.append("join fetch p.merchantStore merch ");
    qs.append("join fetch p.availabilities pa ");
    qs.append("left join fetch pa.prices pap ");
    qs.append("join fetch p.descriptions pd ");
    qs.append("join fetch p.categories categs ");
    // not necessary
    // qs.append("join fetch pap.descriptions papd ");
    // images
    qs.append("left join fetch p.images images ");
    // options (do not need attributes for listings)
    // qs.append("left join fetch p.attributes pattr ");
    // qs.append("left join fetch pattr.productOption po ");
    // qs.append("left join fetch po.descriptions pod ");
    // qs.append("left join fetch pattr.productOptionValue pov ");
    // qs.append("left join fetch pov.descriptions povd ");
    // other lefts
    qs.append("left join fetch p.manufacturer manuf ");
    qs.append("left join fetch manuf.descriptions manufd ");
    qs.append("left join fetch p.type type ");
    qs.append("left join fetch p.taxClass tx ");
    // RENTAL
    qs.append("left join fetch p.owner owner ");
    // qs.append("where pa.region in (:lid) ");
    qs.append("where p.merchantStore.id=mId and categs.id in (:cid) and pa.region in (:lid) ");
    // qs.append("and p.available=true and p.dateAvailable<=:dt and
    // pd.language.id=:lang and manufd.language.id=:lang");
    qs.append("and p.available=true and p.dateAvailable<=:dt and pd.language.id=:lang");
    qs.append(" order by p.sortOrder asc");
    String hql = qs.toString();
    Query q = this.em.createQuery(hql);
    q.setParameter("cid", categoryIds);
    q.setParameter("lid", regionList);
    q.setParameter("dt", new Date());
    q.setParameter("lang", language.getId());
    q.setParameter("mId", store.getId());
    q.setFirstResult(first);
    if (max > 0) {
        int maxCount = first + max;
        q.setMaxResults(Math.min(maxCount, count.intValue()));
    }
    List<Product> products = q.getResultList();
    productList.setProducts(products);
    return productList;
}
Also used : ProductList(com.salesmanager.core.model.catalog.product.ProductList) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ProductList(com.salesmanager.core.model.catalog.product.ProductList) ArrayList(java.util.ArrayList) List(java.util.List) GenericEntityList(com.salesmanager.core.model.common.GenericEntityList) Date(java.util.Date)

Example 3 with ProductList

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

the class ProductRepositoryImpl method listByStore.

/**
 * This query is used for filtering products based on criterias
 *
 * @param store
 * @param first
 * @param max
 * @return
 */
@Override
public ProductList listByStore(MerchantStore store, Language language, ProductCriteria criteria) {
    ProductList productList = new ProductList();
    StringBuilder countBuilderSelect = new StringBuilder();
    countBuilderSelect.append("select count(distinct p) from Product as p");
    StringBuilder countBuilderWhere = new StringBuilder();
    countBuilderWhere.append(" where p.merchantStore.id=:mId");
    if (!CollectionUtils.isEmpty(criteria.getProductIds())) {
        countBuilderWhere.append(" and p.id in (:pId)");
    }
    countBuilderSelect.append(" inner join p.descriptions pd");
    if (criteria.getLanguage() != null && !criteria.getLanguage().equals("_all")) {
        countBuilderWhere.append(" and pd.language.code=:lang");
    }
    if (!StringUtils.isBlank(criteria.getProductName())) {
        countBuilderWhere.append(" and lower(pd.name) like:nm");
    }
    if (!CollectionUtils.isEmpty(criteria.getCategoryIds())) {
        countBuilderSelect.append(" INNER JOIN p.categories categs");
        countBuilderWhere.append(" and categs.id in (:cid)");
    }
    if (criteria.getManufacturerId() != null) {
        countBuilderSelect.append(" INNER JOIN p.manufacturer manuf");
        countBuilderWhere.append(" and manuf.id = :manufid");
    }
    if (!StringUtils.isBlank(criteria.getCode())) {
        countBuilderWhere.append(" and lower(p.sku) like :sku");
    }
    // RENTAL
    if (!StringUtils.isBlank(criteria.getStatus())) {
        countBuilderWhere.append(" and p.rentalStatus = :status");
    }
    if (criteria.getOwnerId() != null) {
        countBuilderSelect.append(" INNER JOIN p.owner owner");
        countBuilderWhere.append(" and owner.id = :ownerid");
    }
    // attribute or option values
    if (CollectionUtils.isNotEmpty(criteria.getAttributeCriteria()) || CollectionUtils.isNotEmpty(criteria.getOptionValueIds())) {
        countBuilderSelect.append(" INNER JOIN p.attributes pattr");
        countBuilderSelect.append(" INNER JOIN pattr.productOption po");
        countBuilderSelect.append(" INNER JOIN pattr.productOptionValue pov ");
        countBuilderSelect.append(" INNER JOIN pov.descriptions povd ");
        if (CollectionUtils.isNotEmpty(criteria.getAttributeCriteria())) {
            int count = 0;
            for (AttributeCriteria attributeCriteria : criteria.getAttributeCriteria()) {
                if (count == 0) {
                    countBuilderWhere.append(" and po.code =:").append(attributeCriteria.getAttributeCode());
                    countBuilderWhere.append(" and povd.description like :").append("val").append(count).append(attributeCriteria.getAttributeCode());
                }
                count++;
            }
            if (criteria.getLanguage() != null && !criteria.getLanguage().equals("_all")) {
                countBuilderWhere.append(" and povd.language.code=:lang");
            }
        }
        if (CollectionUtils.isNotEmpty(criteria.getOptionValueIds())) {
            countBuilderWhere.append(" and pov.id in (:povid)");
        }
    }
    if (criteria.getAvailable() != null) {
        if (criteria.getAvailable()) {
            countBuilderWhere.append(" and p.available=true and p.dateAvailable<=:dt");
        } else {
            countBuilderWhere.append(" and p.available=false or p.dateAvailable>:dt");
        }
    }
    Query countQ = this.em.createQuery(countBuilderSelect.toString() + countBuilderWhere.toString());
    countQ.setParameter("mId", store.getId());
    if (!CollectionUtils.isEmpty(criteria.getCategoryIds())) {
        countQ.setParameter("cid", criteria.getCategoryIds());
    }
    if (CollectionUtils.isNotEmpty(criteria.getOptionValueIds())) {
        countQ.setParameter("povid", criteria.getOptionValueIds());
    }
    if (criteria.getAvailable() != null) {
        countQ.setParameter("dt", new Date());
    }
    if (!StringUtils.isBlank(criteria.getCode())) {
        countQ.setParameter("sku", new StringBuilder().append("%").append(criteria.getCode().toLowerCase()).append("%").toString());
    }
    if (criteria.getManufacturerId() != null) {
        countQ.setParameter("manufid", criteria.getManufacturerId());
    }
    if (!CollectionUtils.isEmpty(criteria.getAttributeCriteria())) {
        int count = 0;
        for (AttributeCriteria attributeCriteria : criteria.getAttributeCriteria()) {
            countQ.setParameter(attributeCriteria.getAttributeCode(), attributeCriteria.getAttributeCode());
            countQ.setParameter("val" + count + attributeCriteria.getAttributeCode(), "%" + attributeCriteria.getAttributeValue() + "%");
            count++;
        }
    }
    if (criteria.getLanguage() != null && !criteria.getLanguage().equals("_all")) {
        countQ.setParameter("lang", language.getCode());
    }
    if (!StringUtils.isBlank(criteria.getProductName())) {
        countQ.setParameter("nm", new StringBuilder().append("%").append(criteria.getProductName().toLowerCase()).append("%").toString());
    }
    if (!CollectionUtils.isEmpty(criteria.getProductIds())) {
        countQ.setParameter("pId", criteria.getProductIds());
    }
    // RENTAL
    if (!StringUtils.isBlank(criteria.getStatus())) {
        countQ.setParameter("status", criteria.getStatus());
    }
    if (criteria.getOwnerId() != null) {
        countQ.setParameter("ownerid", criteria.getOwnerId());
    }
    Number count = (Number) countQ.getSingleResult();
    productList.setTotalCount(count.intValue());
    if (count.intValue() == 0)
        return productList;
    StringBuilder qs = new StringBuilder();
    qs.append("select distinct p from Product as p ");
    qs.append("join fetch p.merchantStore merch ");
    qs.append("join fetch p.availabilities pa ");
    qs.append("left join fetch pa.prices pap ");
    qs.append("join fetch p.descriptions pd ");
    qs.append("left join fetch p.categories categs ");
    qs.append("left join fetch categs.descriptions cd ");
    // images
    qs.append("left join fetch p.images images ");
    // other lefts
    qs.append("left join fetch p.manufacturer manuf ");
    qs.append("left join fetch manuf.descriptions manufd ");
    qs.append("left join fetch p.type type ");
    qs.append("left join fetch p.taxClass tx ");
    // RENTAL
    qs.append("left join fetch p.owner owner ");
    // attributes
    if (!CollectionUtils.isEmpty(criteria.getAttributeCriteria())) {
        qs.append(" inner join p.attributes pattr");
        qs.append(" inner join pattr.productOption po");
        qs.append(" inner join po.descriptions pod");
        qs.append(" inner join pattr.productOptionValue pov ");
        qs.append(" inner join pov.descriptions povd");
    } else {
        qs.append(" left join fetch p.attributes pattr");
        qs.append(" left join fetch pattr.productOption po");
        qs.append(" left join fetch po.descriptions pod");
        qs.append(" left join fetch pattr.productOptionValue pov");
        qs.append(" left join fetch pov.descriptions povd");
    }
    qs.append(" left join fetch p.relationships pr");
    qs.append(" where merch.id=:mId");
    if (criteria.getLanguage() != null && !criteria.getLanguage().equals("_all")) {
        qs.append(" and pd.language.code=:lang");
    }
    if (!CollectionUtils.isEmpty(criteria.getProductIds())) {
        qs.append(" and p.id in (:pId)");
    }
    if (!CollectionUtils.isEmpty(criteria.getCategoryIds())) {
        qs.append(" and categs.id in (:cid)");
    }
    if (criteria.getManufacturerId() != null) {
        qs.append(" and manuf.id = :manufid");
    }
    if (criteria.getAvailable() != null) {
        if (criteria.getAvailable()) {
            qs.append(" and p.available=true and p.dateAvailable<=:dt");
        } else {
            qs.append(" and p.available=false and p.dateAvailable>:dt");
        }
    }
    if (!StringUtils.isBlank(criteria.getProductName())) {
        qs.append(" and lower(pd.name) like :nm");
    }
    if (!StringUtils.isBlank(criteria.getCode())) {
        qs.append(" and lower(p.sku) like :sku");
    }
    // RENTAL
    if (!StringUtils.isBlank(criteria.getStatus())) {
        qs.append(" and p.rentalStatus = :status");
    }
    if (criteria.getOwnerId() != null) {
        qs.append(" and owner.id = :ownerid");
    }
    if (!CollectionUtils.isEmpty(criteria.getAttributeCriteria())) {
        int cnt = 0;
        for (AttributeCriteria attributeCriteria : criteria.getAttributeCriteria()) {
            qs.append(" and po.code =:").append(attributeCriteria.getAttributeCode());
            qs.append(" and povd.description like :").append("val").append(cnt).append(attributeCriteria.getAttributeCode());
            cnt++;
        }
        if (criteria.getLanguage() != null && !criteria.getLanguage().equals("_all")) {
            qs.append(" and povd.language.code=:lang");
        }
    }
    if (CollectionUtils.isNotEmpty(criteria.getOptionValueIds())) {
        qs.append(" and pov.id in (:povid)");
    }
    qs.append(" order by p.sortOrder asc");
    String hql = qs.toString();
    Query q = this.em.createQuery(hql);
    if (criteria.getLanguage() != null && !criteria.getLanguage().equals("_all")) {
        q.setParameter("lang", language.getCode());
    }
    q.setParameter("mId", store.getId());
    if (!CollectionUtils.isEmpty(criteria.getCategoryIds())) {
        q.setParameter("cid", criteria.getCategoryIds());
    }
    if (CollectionUtils.isNotEmpty(criteria.getOptionValueIds())) {
        q.setParameter("povid", criteria.getOptionValueIds());
    }
    if (!CollectionUtils.isEmpty(criteria.getProductIds())) {
        q.setParameter("pId", criteria.getProductIds());
    }
    if (criteria.getAvailable() != null) {
        q.setParameter("dt", new Date());
    }
    if (criteria.getManufacturerId() != null) {
        q.setParameter("manufid", criteria.getManufacturerId());
    }
    if (!StringUtils.isBlank(criteria.getCode())) {
        q.setParameter("sku", new StringBuilder().append("%").append(criteria.getCode().toLowerCase()).append("%").toString());
    }
    if (!CollectionUtils.isEmpty(criteria.getAttributeCriteria())) {
        int cnt = 0;
        for (AttributeCriteria attributeCriteria : criteria.getAttributeCriteria()) {
            q.setParameter(attributeCriteria.getAttributeCode(), attributeCriteria.getAttributeCode());
            q.setParameter("val" + cnt + attributeCriteria.getAttributeCode(), "%" + attributeCriteria.getAttributeValue() + "%");
            cnt++;
        }
    }
    // RENTAL
    if (!StringUtils.isBlank(criteria.getStatus())) {
        q.setParameter("status", criteria.getStatus());
    }
    if (criteria.getOwnerId() != null) {
        q.setParameter("ownerid", criteria.getOwnerId());
    }
    if (!StringUtils.isBlank(criteria.getProductName())) {
        q.setParameter("nm", new StringBuilder().append("%").append(criteria.getProductName().toLowerCase()).append("%").toString());
    }
    @SuppressWarnings("rawtypes") GenericEntityList entityList = new GenericEntityList();
    entityList.setTotalCount(count.intValue());
    q = RepositoryHelper.paginateQuery(q, count, entityList, criteria);
    @SuppressWarnings("unchecked") List<Product> products = q.getResultList();
    productList.setProducts(products);
    return productList;
}
Also used : ProductList(com.salesmanager.core.model.catalog.product.ProductList) AttributeCriteria(com.salesmanager.core.model.catalog.product.attribute.AttributeCriteria) Query(javax.persistence.Query) GenericEntityList(com.salesmanager.core.model.common.GenericEntityList) Product(com.salesmanager.core.model.catalog.product.Product) Date(java.util.Date)

Example 4 with ProductList

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

the class SearchFacadeImpl method convertToSearchProductList.

@Override
public SearchProductList convertToSearchProductList(SearchResponse searchResponse, MerchantStore merchantStore, int start, int count, Language language) {
    SearchProductList returnList = new SearchProductList();
    List<SearchEntry> entries = searchResponse.getEntries();
    if (CollectionUtils.isNotEmpty(entries)) {
        List<Long> ids = entries.stream().map(SearchEntry::getIndexProduct).map(IndexProduct::getId).map(Long::parseLong).collect(Collectors.toList());
        ProductCriteria searchCriteria = new ProductCriteria();
        searchCriteria.setMaxCount(count);
        searchCriteria.setStartIndex(start);
        searchCriteria.setProductIds(ids);
        searchCriteria.setAvailable(true);
        ProductList productList = productService.listByStore(merchantStore, language, searchCriteria);
        List<ReadableProduct> readableProducts = productList.getProducts().stream().map(product -> convertProductToReadableProduct(product, merchantStore, language)).collect(Collectors.toList());
        returnList.getProducts().addAll(readableProducts);
        returnList.setProductCount(productList.getProducts().size());
    }
    // Facets
    Map<String, List<SearchFacet>> facets = Optional.ofNullable(searchResponse.getFacets()).orElse(Collections.emptyMap());
    List<ReadableCategory> categoryProxies = getCategoryFacets(merchantStore, language, facets);
    returnList.setCategoryFacets(categoryProxies);
    List<SearchFacet> manufacturersFacets = facets.entrySet().stream().filter(e -> MANUFACTURER_FACET_NAME.equals(e.getKey())).findFirst().map(Entry::getValue).orElse(Collections.emptyList());
    if (CollectionUtils.isNotEmpty(manufacturersFacets)) {
    // TODO add manufacturer facets
    }
    return returnList;
}
Also used : ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) Async(org.springframework.scheduling.annotation.Async) LoggerFactory(org.slf4j.LoggerFactory) AutoCompleteRequest(com.salesmanager.shop.store.model.search.AutoCompleteRequest) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ServiceException(com.salesmanager.core.business.exception.ServiceException) Inject(javax.inject.Inject) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) Service(org.springframework.stereotype.Service) Map(java.util.Map) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) ReadableCategoryPopulator(com.salesmanager.shop.populator.catalog.ReadableCategoryPopulator) CategoryService(com.salesmanager.core.business.services.catalog.category.CategoryService) SearchKeywords(com.salesmanager.core.model.search.SearchKeywords) SearchProductRequest(com.salesmanager.shop.model.catalog.SearchProductRequest) SearchFacet(com.salesmanager.core.model.search.SearchFacet) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ProductCriteria(com.salesmanager.core.model.catalog.product.ProductCriteria) Logger(org.slf4j.Logger) SearchService(com.salesmanager.core.business.services.search.SearchService) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) ProductList(com.salesmanager.core.model.catalog.product.ProductList) Collectors(java.util.stream.Collectors) SearchEntry(com.salesmanager.core.model.search.SearchEntry) Category(com.salesmanager.core.model.catalog.category.Category) List(java.util.List) ValueList(com.salesmanager.shop.model.entity.ValueList) CoreConfiguration(com.salesmanager.core.business.utils.CoreConfiguration) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) SearchProductList(com.salesmanager.shop.model.catalog.SearchProductList) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) Entry(java.util.Map.Entry) SearchResponse(com.salesmanager.core.model.search.SearchResponse) Optional(java.util.Optional) ConversionException(com.salesmanager.core.business.exception.ConversionException) Collections(java.util.Collections) IndexProduct(com.salesmanager.core.model.search.IndexProduct) ProductCriteria(com.salesmanager.core.model.catalog.product.ProductCriteria) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) IndexProduct(com.salesmanager.core.model.search.IndexProduct) ProductList(com.salesmanager.core.model.catalog.product.ProductList) SearchProductList(com.salesmanager.shop.model.catalog.SearchProductList) SearchFacet(com.salesmanager.core.model.search.SearchFacet) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ProductList(com.salesmanager.core.model.catalog.product.ProductList) List(java.util.List) ValueList(com.salesmanager.shop.model.entity.ValueList) SearchProductList(com.salesmanager.shop.model.catalog.SearchProductList) SearchEntry(com.salesmanager.core.model.search.SearchEntry) SearchProductList(com.salesmanager.shop.model.catalog.SearchProductList)

Aggregations

Product (com.salesmanager.core.model.catalog.product.Product)4 ProductList (com.salesmanager.core.model.catalog.product.ProductList)4 GenericEntityList (com.salesmanager.core.model.common.GenericEntityList)2 Date (java.util.Date)2 List (java.util.List)2 Query (javax.persistence.Query)2 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 ServiceException (com.salesmanager.core.business.exception.ServiceException)1 CategoryService (com.salesmanager.core.business.services.catalog.category.CategoryService)1 PricingService (com.salesmanager.core.business.services.catalog.product.PricingService)1 ProductService (com.salesmanager.core.business.services.catalog.product.ProductService)1 SearchService (com.salesmanager.core.business.services.search.SearchService)1 CoreConfiguration (com.salesmanager.core.business.utils.CoreConfiguration)1 Category (com.salesmanager.core.model.catalog.category.Category)1 ProductCriteria (com.salesmanager.core.model.catalog.product.ProductCriteria)1 AttributeCriteria (com.salesmanager.core.model.catalog.product.attribute.AttributeCriteria)1 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)1 Language (com.salesmanager.core.model.reference.language.Language)1 IndexProduct (com.salesmanager.core.model.search.IndexProduct)1 SearchEntry (com.salesmanager.core.model.search.SearchEntry)1