1 24 package org.ofbiz.order.shoppingcart.product; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.Comparator ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 import javax.servlet.ServletRequest ; 36 import javax.servlet.http.HttpServletRequest ; 37 38 import org.ofbiz.base.util.Debug; 39 import org.ofbiz.base.util.UtilMisc; 40 import org.ofbiz.entity.GenericDelegator; 41 import org.ofbiz.entity.GenericEntity; 42 import org.ofbiz.entity.GenericEntityException; 43 import org.ofbiz.entity.GenericValue; 44 import org.ofbiz.entity.util.EntityUtil; 45 import org.ofbiz.order.shoppingcart.ShoppingCart; 46 import org.ofbiz.order.shoppingcart.ShoppingCartItem; 47 import org.ofbiz.product.catalog.CatalogWorker; 48 import org.ofbiz.product.category.CategoryWorker; 49 50 53 public class ProductDisplayWorker { 54 55 public static final String module = ProductDisplayWorker.class.getName(); 56 57 58 59 60 61 public static List getRandomCartProductAssoc(ServletRequest request, boolean checkViewAllow) { 62 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 63 HttpServletRequest httpRequest = (HttpServletRequest ) request; 64 ShoppingCart cart = (ShoppingCart) httpRequest.getSession().getAttribute("shoppingCart"); 65 66 if (cart == null || cart.size() <= 0) return null; 67 68 ArrayList cartAssocs = null; 69 try { 70 Map products = new HashMap (); 71 72 Iterator cartiter = cart.iterator(); 73 74 while (cartiter != null && cartiter.hasNext()) { 75 ShoppingCartItem item = (ShoppingCartItem) cartiter.next(); 76 List complementProducts = delegator.findByAndCache("ProductAssoc", UtilMisc.toMap("productId", item.getProductId(), "productAssocTypeId", "PRODUCT_COMPLEMENT"), null); 78 complementProducts = EntityUtil.filterByDate(complementProducts, true); 80 81 List productsCategories = delegator.findByAndCache("ProductCategoryMember", UtilMisc.toMap("productId", item.getProductId()), null); 82 productsCategories = EntityUtil.filterByDate(productsCategories, true); 83 if (productsCategories != null) { 84 Iterator productsCategoriesIter = productsCategories.iterator(); 85 while (productsCategoriesIter.hasNext()) { 86 GenericValue productsCategoryMember = (GenericValue) productsCategoriesIter.next(); 87 GenericValue productsCategory = productsCategoryMember.getRelatedOneCache("ProductCategory"); 88 if ("CROSS_SELL_CATEGORY".equals(productsCategory.getString("productCategoryTypeId"))) { 89 List curPcms = productsCategory.getRelatedCache("ProductCategoryMember"); 90 if (curPcms != null) { 91 Iterator curPcmsIter = curPcms.iterator(); 92 while (curPcmsIter.hasNext()) { 93 GenericValue curPcm = (GenericValue) curPcmsIter.next(); 94 if (!products.containsKey(curPcm.getString("productId"))) { 95 GenericValue product = curPcm.getRelatedOneCache("Product"); 96 products.put(product.getString("productId"), product); 97 } 98 } 99 } 100 } 101 } 102 } 103 104 if (complementProducts != null && complementProducts.size() > 0) { 105 Iterator complIter = complementProducts.iterator(); 106 while (complIter.hasNext()) { 107 GenericValue productAssoc = (GenericValue) complIter.next(); 108 if (!products.containsKey(productAssoc.getString("productIdTo"))) { 109 GenericValue product = productAssoc.getRelatedOneCache("AssocProduct"); 110 products.put(product.getString("productId"), product); 111 } 112 } 113 } 114 } 115 116 cartiter = cart.iterator(); 118 while (cartiter != null && cartiter.hasNext()) { 119 ShoppingCartItem item = (ShoppingCartItem) cartiter.next(); 120 products.remove(item.getProductId()); 121 } 122 123 if (checkViewAllow) { 125 String currentCatalogId = CatalogWorker.getCurrentCatalogId(request); 126 String viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, currentCatalogId); 127 if (viewProductCategoryId != null) { 128 List tempList = new ArrayList (products.values()); 129 tempList = CategoryWorker.filterProductsInCategory(delegator, tempList, viewProductCategoryId, "productId"); 130 cartAssocs = new ArrayList (tempList); 131 } 132 } 133 134 if (cartAssocs == null) { 135 cartAssocs = new ArrayList (products.values()); 136 } 137 138 while (cartAssocs.size() > 3) { 140 int toRemove = (int) (Math.random() * (double) (cartAssocs.size())); 141 cartAssocs.remove(toRemove); 142 } 143 } catch (GenericEntityException e) { 144 Debug.logWarning(e, module); 145 } 146 147 if (cartAssocs != null && cartAssocs.size() > 0) { 148 return cartAssocs; 149 } else { 150 return null; 151 } 152 } 153 154 public static Map getQuickReorderProducts(ServletRequest request) { 155 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 156 HttpServletRequest httpRequest = (HttpServletRequest ) request; 157 GenericValue userLogin = (GenericValue) httpRequest.getSession().getAttribute("userLogin"); 158 Map results = new HashMap (); 159 160 if (userLogin == null) userLogin = (GenericValue) httpRequest.getSession().getAttribute("autoUserLogin"); 161 if (userLogin == null) return results; 162 163 try { 164 Map products = (Map ) httpRequest.getSession().getAttribute("_QUICK_REORDER_PRODUCTS_"); 165 Map productQuantities = (Map ) httpRequest.getSession().getAttribute("_QUICK_REORDER_PRODUCT_QUANTITIES_"); 166 Map productOccurances = (Map ) httpRequest.getSession().getAttribute("_QUICK_REORDER_PRODUCT_OCCURANCES_"); 167 168 if (products == null || productQuantities == null || productOccurances == null) { 169 products = new HashMap (); 170 productQuantities = new HashMap (); 171 productOccurances = new HashMap (); 173 174 final String [] USER_ORDER_ROLE_TYPES = {"PLACING_CUSTOMER"}; 177 178 for (int i = 0; i < USER_ORDER_ROLE_TYPES.length; i++) { 179 Collection orderRoles = delegator.findByAnd("OrderRole", UtilMisc.toMap("partyId", userLogin.get("partyId"), "roleTypeId", USER_ORDER_ROLE_TYPES[i]), null); 180 Iterator ordersIter = UtilMisc.toIterator(orderRoles); 181 182 while (ordersIter != null && ordersIter.hasNext()) { 183 GenericValue orderRole = (GenericValue) ordersIter.next(); 184 Collection orderItems = orderRole.getRelated("OrderItem"); 186 Iterator orderItemsIter = UtilMisc.toIterator(orderItems); 187 188 while (orderItemsIter != null && orderItemsIter.hasNext()) { 189 GenericValue orderItem = (GenericValue) orderItemsIter.next(); 190 GenericValue product = orderItem.getRelatedOneCache("Product"); 192 193 products.put(product.get("productId"), product); 194 195 Integer curQuant = (Integer ) productQuantities.get(product.get("productId")); 196 197 if (curQuant == null) curQuant = new Integer (0); 198 Double orderQuant = orderItem.getDouble("quantity"); 199 200 if (orderQuant == null) orderQuant = new Double (0.0); 201 productQuantities.put(product.get("productId"), new Integer (curQuant.intValue() + orderQuant.intValue())); 202 203 Integer curOcc = (Integer ) productOccurances.get(product.get("productId")); 204 205 if (curOcc == null) curOcc = new Integer (0); 206 productOccurances.put(product.get("productId"), new Integer (curOcc.intValue() + 1)); 207 } 208 } 209 } 210 211 Iterator quantEntries = productQuantities.entrySet().iterator(); 213 214 while (quantEntries.hasNext()) { 215 Map.Entry entry = (Map.Entry ) quantEntries.next(); 216 Object prodId = entry.getKey(); 217 Integer quantity = (Integer ) entry.getValue(); 218 Integer occs = (Integer ) productOccurances.get(prodId); 219 int nqint = quantity.intValue() / occs.intValue(); 220 221 if (nqint < 1) nqint = 1; 222 productQuantities.put(prodId, new Integer (nqint)); 223 } 224 225 httpRequest.getSession().setAttribute("_QUICK_REORDER_PRODUCTS_", new HashMap (products)); 226 httpRequest.getSession().setAttribute("_QUICK_REORDER_PRODUCT_QUANTITIES_", new HashMap (productQuantities)); 227 httpRequest.getSession().setAttribute("_QUICK_REORDER_PRODUCT_OCCURANCES_", new HashMap (productOccurances)); 228 } else { 229 products = new HashMap (products); 231 productQuantities = new HashMap (productQuantities); 232 productOccurances = new HashMap (productOccurances); 233 } 234 235 ShoppingCart cart = (ShoppingCart) httpRequest.getSession().getAttribute("shoppingCart"); 237 if (cart != null && cart.size() > 0) { 238 Iterator cartiter = cart.iterator(); 239 while (cartiter.hasNext()) { 240 ShoppingCartItem item = (ShoppingCartItem) cartiter.next(); 241 String productId = item.getProductId(); 242 products.remove(productId); 243 productQuantities.remove(productId); 244 productOccurances.remove(productId); 245 } 246 } 247 248 List prodKeyList = new ArrayList (products.keySet()); 251 String currentCatalogId = CatalogWorker.getCurrentCatalogId(request); 253 String viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, currentCatalogId); 254 if (viewProductCategoryId != null) { 255 Iterator valIter = prodKeyList.iterator(); 256 while (valIter.hasNext()) { 257 String productId = (String ) valIter.next(); 258 if (!CategoryWorker.isProductInCategory(delegator, productId, viewProductCategoryId)) { 259 products.remove(productId); 260 productQuantities.remove(productId); 261 productOccurances.remove(productId); 262 } 263 } 264 } 265 267 List reorderProds = new ArrayList (products.values()); 268 269 276 277 double occurancesModifier = 1.0; 279 double quantityModifier = 1.0; 280 Map newMetric = new HashMap (); 281 Iterator occurEntries = productOccurances.entrySet().iterator(); 282 283 while (occurEntries.hasNext()) { 284 Map.Entry entry = (Map.Entry ) occurEntries.next(); 285 Object prodId = entry.getKey(); 286 Integer quantity = (Integer ) entry.getValue(); 287 Integer occs = (Integer ) productQuantities.get(prodId); 288 double nqdbl = quantity.doubleValue() * quantityModifier + occs.doubleValue() * occurancesModifier; 289 290 newMetric.put(prodId, new Double (nqdbl)); 291 } 292 reorderProds = productOrderByMap(reorderProds, newMetric, true); 293 294 while (reorderProds.size() > 5) { 296 reorderProds.remove(reorderProds.size() - 1); 297 } 298 299 results.put("products", reorderProds); 300 results.put("quantities", productQuantities); 301 } catch (GenericEntityException e) { 302 Debug.logWarning(e, module); 303 } 304 305 return results; 306 } 307 308 public static List productOrderByMap(Collection values, Map orderByMap, boolean descending) { 309 if (values == null) return null; 310 if (values.size() == 0) return UtilMisc.toList(values); 311 312 List result = new ArrayList (values); 313 314 Collections.sort(result, new ProductByMapComparator(orderByMap, descending)); 315 return result; 316 } 317 318 static class ProductByMapComparator implements Comparator { 319 private Map orderByMap; 320 private boolean descending; 321 322 ProductByMapComparator(Map orderByMap, boolean descending) { 323 this.orderByMap = orderByMap; 324 this.descending = descending; 325 } 326 327 public int compare(java.lang.Object prod1, java.lang.Object prod2) { 328 int result = compareAsc((GenericEntity) prod1, (GenericEntity) prod2); 329 330 if (descending) { 331 result = -result; 332 } 333 return result; 334 } 335 336 private int compareAsc(GenericEntity prod1, GenericEntity prod2) { 337 Object value = orderByMap.get(prod1.get("productId")); 338 Object value2 = orderByMap.get(prod2.get("productId")); 339 340 if (value == null) return value2 == null ? 0 : -1; 342 return ((Comparable ) value).compareTo(value2); 343 } 344 345 public boolean equals(java.lang.Object obj) { 346 if ((obj != null) && (obj instanceof ProductByMapComparator)) { 347 ProductByMapComparator that = (ProductByMapComparator) obj; 348 349 return this.orderByMap.equals(that.orderByMap) && this.descending == that.descending; 350 } else { 351 return false; 352 } 353 } 354 } 355 } 356 | Popular Tags |