Search in sources :

Example 6 with ProductImpl

use of org.broadleafcommerce.core.catalog.domain.ProductImpl in project BroadleafCommerce by BroadleafCommerce.

the class SearchFacetDaoImpl method readDistinctValuesForField.

@Override
public <T> List<T> readDistinctValuesForField(String fieldName, Class<T> fieldValueClass) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<T> criteria = builder.createQuery(fieldValueClass);
    Root<ProductImpl> product = criteria.from(ProductImpl.class);
    Path<Sku> sku = product.get("defaultSku");
    Path<?> pathToUse;
    if (fieldName.contains("defaultSku.")) {
        pathToUse = sku;
        fieldName = fieldName.substring("defaultSku.".length());
    } else if (fieldName.contains("productAttributes.")) {
        pathToUse = product.join("productAttributes");
        fieldName = fieldName.substring("productAttributes.".length());
        criteria.where(builder.equal(builder.lower(pathToUse.get("name").as(String.class)), fieldName.toLowerCase()));
        fieldName = "value";
    } else if (fieldName.contains("product.")) {
        pathToUse = product;
        fieldName = fieldName.substring("product.".length());
    } else {
        throw new IllegalArgumentException("Invalid facet fieldName specified: " + fieldName);
    }
    criteria.where(pathToUse.get(fieldName).as(fieldValueClass).isNotNull());
    criteria.distinct(true).select(pathToUse.get(fieldName).as(fieldValueClass));
    TypedQuery<T> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Search");
    return query.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ProductImpl(org.broadleafcommerce.core.catalog.domain.ProductImpl) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 7 with ProductImpl

use of org.broadleafcommerce.core.catalog.domain.ProductImpl in project BroadleafCommerce by BroadleafCommerce.

the class ProductDaoImpl method readCountAllActiveProductsInternal.

protected Long readCountAllActiveProductsInternal(Date currentDate) {
    // Set up the criteria query that specifies we want to return a Long
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
    // The root of our search is Product
    Root<ProductImpl> product = criteria.from(ProductImpl.class);
    // We need to filter on active date on the sku
    Join<Product, Sku> sku = product.join("defaultSku");
    // We want the count of products
    criteria.select(builder.count(product));
    // Ensure the product is currently active
    List<Predicate> restrictions = new ArrayList<Predicate>();
    attachActiveRestriction(currentDate, product, sku, restrictions);
    // Add the restrictions to the criteria query
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
    TypedQuery<Long> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");
    return query.getSingleResult();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ProductImpl(org.broadleafcommerce.core.catalog.domain.ProductImpl) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Predicate(javax.persistence.criteria.Predicate)

Example 8 with ProductImpl

use of org.broadleafcommerce.core.catalog.domain.ProductImpl in project BroadleafCommerce by BroadleafCommerce.

the class ProductDaoImpl method readFilteredActiveProductsByQueryInternal.

protected List<Product> readFilteredActiveProductsByQueryInternal(String query, Date currentDate, SearchCriteria searchCriteria) {
    // Set up the criteria query that specifies we want to return Products
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    // The root of our search is Product since we are searching
    Root<ProductImpl> product = criteria.from(ProductImpl.class);
    // We also want to filter on attributes from sku and productAttributes
    Join<Product, Sku> sku = product.join("defaultSku");
    // Product objects are what we want back
    criteria.select(product);
    // We only want results that match the search query
    List<Predicate> restrictions = new ArrayList<Predicate>();
    if (query != null) {
        String lq = query.toLowerCase();
        restrictions.add(builder.or(builder.like(builder.lower(sku.get("name").as(String.class)), '%' + lq + '%'), builder.like(builder.lower(sku.get("longDescription").as(String.class)), '%' + lq + '%')));
    }
    attachSearchCriteria(searchCriteria, product, sku, restrictions);
    attachActiveRestriction(currentDate, product, sku, restrictions);
    attachOrderBy(searchCriteria, product, sku, criteria);
    // Execute the query with the restrictions
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
    TypedQuery<Product> typedQuery = em.createQuery(criteria);
    return typedQuery.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ProductImpl(org.broadleafcommerce.core.catalog.domain.ProductImpl) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Predicate(javax.persistence.criteria.Predicate)

Example 9 with ProductImpl

use of org.broadleafcommerce.core.catalog.domain.ProductImpl in project BroadleafCommerce by BroadleafCommerce.

the class ProductDaoImpl method readProductsByIds.

@Override
public List<Product> readProductsByIds(List<Long> productIds) {
    if (productIds == null || productIds.size() == 0) {
        return null;
    }
    if (productIds.size() > 100) {
        logger.warn("Not recommended to use the readProductsByIds method for long lists of productIds, since " + "Hibernate is required to transform the distinct results. The list of requested" + "product ids was (" + productIds.size() + ") in length.");
    }
    // Set up the criteria query that specifies we want to return Products
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    Root<ProductImpl> product = criteria.from(ProductImpl.class);
    product.fetch("defaultSku", JoinType.LEFT);
    criteria.select(product);
    // We only want results that match the product IDs
    criteria.where(product.get("id").as(Long.class).in(sandBoxHelper.mergeCloneIds(ProductImpl.class, productIds.toArray(new Long[productIds.size()]))));
    TypedQuery<Product> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");
    return query.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ProductImpl(org.broadleafcommerce.core.catalog.domain.ProductImpl) Product(org.broadleafcommerce.core.catalog.domain.Product)

Example 10 with ProductImpl

use of org.broadleafcommerce.core.catalog.domain.ProductImpl in project BroadleafCommerce by BroadleafCommerce.

the class ProductDataProvider method provideBasicProduct.

/**
 * A basic product is actually a Product and a Sku
 */
@DataProvider(name = "basicProduct")
public static Object[][] provideBasicProduct() {
    Product ci = new ProductImpl();
    Sku defaultSku = new SkuImpl();
    defaultSku.setName("setOfAggieDominoes");
    defaultSku.setDescription("a fine set of bones for 42");
    ci.setDefaultSku(defaultSku);
    return new Object[][] { { ci } };
}
Also used : SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) ProductImpl(org.broadleafcommerce.core.catalog.domain.ProductImpl) Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku) DataProvider(org.testng.annotations.DataProvider)

Aggregations

ProductImpl (org.broadleafcommerce.core.catalog.domain.ProductImpl)18 Product (org.broadleafcommerce.core.catalog.domain.Product)16 Sku (org.broadleafcommerce.core.catalog.domain.Sku)16 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)11 ArrayList (java.util.ArrayList)9 Money (org.broadleafcommerce.common.money.Money)8 Category (org.broadleafcommerce.core.catalog.domain.Category)7 CategoryImpl (org.broadleafcommerce.core.catalog.domain.CategoryImpl)7 CategoryProductXref (org.broadleafcommerce.core.catalog.domain.CategoryProductXref)5 CategoryProductXrefImpl (org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl)5 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)5 Calendar (java.util.Calendar)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 OrderImpl (org.broadleafcommerce.core.order.domain.OrderImpl)4 PromotableOrder (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder)3 PromotableOrderImpl (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderImpl)3 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)3 Order (org.broadleafcommerce.core.order.domain.Order)3 OrderItemPriceDetail (org.broadleafcommerce.core.order.domain.OrderItemPriceDetail)3 OrderItemPriceDetailImpl (org.broadleafcommerce.core.order.domain.OrderItemPriceDetailImpl)3