📄 productdisplayworker.java
字号:
/*
* Created on Aug 18, 2003
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package org.ofbiz.order.shoppingcart.product;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntity;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.order.shoppingcart.ShoppingCart;
import org.ofbiz.order.shoppingcart.ShoppingCartItem;
import org.ofbiz.product.catalog.CatalogWorker;
import org.ofbiz.product.category.CategoryWorker;
import org.ofbiz.product.store.ProductStoreWorker;
import org.ofbiz.service.LocalDispatcher;
/**
* @author ajzeneski
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class ProductDisplayWorker {
public static final String module = ProductDisplayWorker.class.getName();
public static boolean isStoreInventoryRequired(ShoppingCartItem item) {
return ProductStoreWorker.isStoreInventoryRequired(item.getProdCatalogId(), item.getProduct(), item.getDelegator());
}
public static boolean isStoreInventoryAvailable(ShoppingCartItem item, LocalDispatcher dispatcher) {
return ProductStoreWorker.isStoreInventoryAvailable(item.getProdCatalogId(), item.getProductId(), item.getQuantity(), item.getDelegator(), dispatcher);
}
/* ========================================================================================*/
/* ============================= Special Data Retreival Methods ===========================*/
public static List getRandomCartProductAssoc(ServletRequest request, boolean checkViewAllow) {
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
HttpServletRequest httpRequest = (HttpServletRequest) request;
ShoppingCart cart = (ShoppingCart) httpRequest.getSession().getAttribute("shoppingCart");
if (cart == null || cart.size() <= 0) return null;
ArrayList cartAssocs = null;
try {
Map products = new HashMap();
Iterator cartiter = cart.iterator();
while (cartiter != null && cartiter.hasNext()) {
ShoppingCartItem item = (ShoppingCartItem) cartiter.next();
// Collection upgradeProducts = delegator.findByAndCache("ProductAssoc", UtilMisc.toMap("productId", item.getProductId(), "productAssocTypeId", "PRODUCT_UPGRADE"), null);
List 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, true);
List productsCategories = delegator.findByAndCache("ProductCategoryMember", UtilMisc.toMap("productId", item.getProductId()), null);
productsCategories = EntityUtil.filterByDate(productsCategories, true);
if (productsCategories != null) {
Iterator productsCategoriesIter = productsCategories.iterator();
while (productsCategoriesIter.hasNext()) {
GenericValue productsCategoryMember = (GenericValue) productsCategoriesIter.next();
GenericValue productsCategory = productsCategoryMember.getRelatedOneCache("ProductCategory");
if ("CROSS_SELL_CATEGORY".equals(productsCategory.getString("productCategoryTypeId"))) {
List curPcms = productsCategory.getRelatedCache("ProductCategoryMember");
if (curPcms != null) {
Iterator curPcmsIter = curPcms.iterator();
while (curPcmsIter.hasNext()) {
GenericValue curPcm = (GenericValue) curPcmsIter.next();
if (!products.containsKey(curPcm.getString("productId"))) {
GenericValue product = curPcm.getRelatedOneCache("Product");
products.put(product.getString("productId"), product);
}
}
}
}
}
}
if (complementProducts != null && complementProducts.size() > 0) {
Iterator complIter = complementProducts.iterator();
while (complIter.hasNext()) {
GenericValue productAssoc = (GenericValue) 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 = (ShoppingCartItem) cartiter.next();
products.remove(item.getProductId());
}
// if desired check view allow category
if (checkViewAllow) {
String currentCatalogId = CatalogWorker.getCurrentCatalogId(request);
String viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, currentCatalogId);
if (viewProductCategoryId != null) {
List tempList = new ArrayList(products.values());
tempList = CategoryWorker.filterProductsInCategory(delegator, tempList, viewProductCategoryId, "productId");
cartAssocs = new ArrayList(tempList);
}
}
if (cartAssocs == null) {
cartAssocs = new ArrayList(products.values());
}
// randomly remove products while there are more than 3
while (cartAssocs.size() > 3) {
int toRemove = (int) (Math.random() * (double) (cartAssocs.size()));
cartAssocs.remove(toRemove);
}
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
}
if (cartAssocs != null && cartAssocs.size() > 0) {
return cartAssocs;
} else {
return null;
}
}
public static Map getQuickReorderProducts(ServletRequest request) {
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
HttpServletRequest httpRequest = (HttpServletRequest) request;
GenericValue userLogin = (GenericValue) httpRequest.getSession().getAttribute("userLogin");
Map results = new HashMap();
if (userLogin == null) userLogin = (GenericValue) httpRequest.getSession().getAttribute("autoUserLogin");
if (userLogin == null) return results;
try {
Map products = (Map) httpRequest.getSession().getAttribute("_QUICK_REORDER_PRODUCTS_");
Map productQuantities = (Map) httpRequest.getSession().getAttribute("_QUICK_REORDER_PRODUCT_QUANTITIES_");
Map productOccurances = (Map) httpRequest.getSession().getAttribute("_QUICK_REORDER_PRODUCT_OCCURANCES_");
if (products == null || productQuantities == null || productOccurances == null) {
products = new HashMap();
productQuantities = new HashMap();
// keep track of how many times a product occurs in order to find averages and rank by purchase amount
productOccurances = new HashMap();
// get all order role entities for user by customer role type
// final String[] USER_ORDER_ROLE_TYPES = {"END_USER_CUSTOMER", "SHIP_TO_CUSTOMER", "BILL_TO_CUSTOMER", "PLACING_CUSTOMER"};
final String[] USER_ORDER_ROLE_TYPES = {"PLACING_CUSTOMER"};
for (int i = 0; i < USER_ORDER_ROLE_TYPES.length; i++) {
Collection orderRoles = delegator.findByAnd("OrderRole", UtilMisc.toMap("partyId", userLogin.get("partyId"), "roleTypeId", USER_ORDER_ROLE_TYPES[i]), null);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -