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();
}
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();
}
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();
}
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();
}
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 } };
}
Aggregations