/* ============================= Special Data Retrieval Methods ===========================*/
public static List<GenericValue> getRandomCartProductAssoc(ServletRequest request, boolean checkViewAllow) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
HttpServletRequest httpRequest = (HttpServletRequest) request;
ShoppingCart cart = (ShoppingCart) httpRequest.getSession().getAttribute("shoppingCart");
if (cart == null || cart.size() <= 0) return null;
List<GenericValue> cartAssocs = null;
try {
Map<String, GenericValue> products = FastMap.newInstance();
Iterator<ShoppingCartItem> cartiter = cart.iterator();
while (cartiter != null && cartiter.hasNext()) {
ShoppingCartItem item = cartiter.next();
// Collection upgradeProducts = delegator.findByAndCache("ProductAssoc", UtilMisc.toMap("productId", item.getProductId(), "productAssocTypeId", "PRODUCT_UPGRADE"), null);
List<GenericValue> complementProducts = delegator.findByAndCache("ProductAssoc", UtilMisc.toMap("productId", item.getProductId(), "productAssocTypeId", "PRODUCT_COMPLEMENT"), null);
// since ProductAssoc records have a fromDate and thruDate, we can filter by now so that only assocs in the date range are included
complementProducts = EntityUtil.filterByDate(complementProducts);
List<GenericValue> productsCategories = delegator.findByAndCache("ProductCategoryMember", UtilMisc.toMap("productId", item.getProductId()), null);
productsCategories = EntityUtil.filterByDate(productsCategories, true);
if (productsCategories != null) {
Iterator<GenericValue> productsCategoriesIter = productsCategories.iterator();
while (productsCategoriesIter.hasNext()) {
GenericValue productsCategoryMember = productsCategoriesIter.next();
GenericValue productsCategory = productsCategoryMember.getRelatedOneCache("ProductCategory");
if ("CROSS_SELL_CATEGORY".equals(productsCategory.getString("productCategoryTypeId"))) {
List<GenericValue> curPcms = productsCategory.getRelatedCache("ProductCategoryMember");
if (curPcms != null) {
Iterator<GenericValue> curPcmsIter = curPcms.iterator();
while (curPcmsIter.hasNext()) {
GenericValue curPcm = curPcmsIter.next();
if (!products.containsKey(curPcm.getString("productId"))) {
GenericValue product = curPcm.getRelatedOneCache("Product");
products.put(product.getString("productId"), product);
}
}
}
}
}
}
if (UtilValidate.isNotEmpty(complementProducts)) {
Iterator<GenericValue> complIter = complementProducts.iterator();
while (complIter.hasNext()) {
GenericValue productAssoc = complIter.next();
if (!products.containsKey(productAssoc.getString("productIdTo"))) {
GenericValue product = productAssoc.getRelatedOneCache("AssocProduct");
products.put(product.getString("productId"), product);
}
}
}
}
// remove all products that are already in the cart
cartiter = cart.iterator();
while (cartiter != null && cartiter.hasNext()) {
ShoppingCartItem item = cartiter.next();
products.remove(item.getProductId());
}