1 24 package org.ofbiz.product.category; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import javax.servlet.ServletRequest ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpSession ; 37 import javax.servlet.jsp.PageContext ; 38 39 import org.ofbiz.base.util.*; 40 import org.ofbiz.entity.GenericDelegator; 41 import org.ofbiz.entity.GenericEntityException; 42 import org.ofbiz.entity.GenericValue; 43 import org.ofbiz.entity.condition.EntityExpr; 44 import org.ofbiz.entity.condition.EntityOperator; 45 import org.ofbiz.entity.condition.EntityConditionList; 46 import org.ofbiz.entity.condition.EntityCondition; 47 import org.ofbiz.entity.util.EntityUtil; 48 import org.ofbiz.product.product.ProductWorker; 49 50 58 public class CategoryWorker { 59 60 public static final String module = CategoryWorker.class.getName(); 61 62 public static String getCatalogTopCategory(PageContext pageContext, String defaultTopCategory) { 63 return getCatalogTopCategory(pageContext.getRequest(), defaultTopCategory); 64 } 65 66 public static String getCatalogTopCategory(ServletRequest request, String defaultTopCategory) { 67 HttpServletRequest httpRequest = (HttpServletRequest ) request; 68 Map requestParameters = UtilHttp.getParameterMap(httpRequest); 69 String topCatName = null; 70 boolean fromSession = false; 71 72 topCatName = (String ) requestParameters.get("CATALOG_TOP_CATEGORY"); 74 if (topCatName == null) { 76 topCatName = (String ) httpRequest.getSession().getAttribute("CATALOG_TOP_CATEGORY"); 77 if (topCatName != null) 78 fromSession = true; 79 } 80 if (topCatName == null) 82 topCatName = defaultTopCategory; 83 if (topCatName == null) 84 topCatName = "CATALOG1"; 85 86 if (!fromSession) { 87 if (Debug.infoOn()) Debug.logInfo("[CategoryWorker.getCatalogTopCategory] Setting new top category: " + topCatName, module); 88 httpRequest.getSession().setAttribute("CATALOG_TOP_CATEGORY", topCatName); 89 } 90 return topCatName; 91 } 92 93 public static void getCategoriesWithNoParent(PageContext pageContext, String attributeName) { 94 getCategoriesWithNoParent(pageContext.getRequest(), attributeName); 95 } 96 97 public static void getCategoriesWithNoParent(ServletRequest request, String attributeName) { 98 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 99 Collection results = new LinkedList (); 100 101 try { 102 Collection allCategories = delegator.findAll("ProductCategory"); 103 104 if (allCategories == null) 105 return; 106 Iterator aciter = allCategories.iterator(); 107 108 while (aciter.hasNext()) { 109 GenericValue curCat = (GenericValue) aciter.next(); 110 Collection parentCats = curCat.getRelatedCache("CurrentProductCategoryRollup"); 111 112 if (parentCats == null || parentCats.size() <= 0) 113 results.add(curCat); 114 } 115 } catch (GenericEntityException e) { 116 Debug.logWarning(e, module); 117 } 118 request.setAttribute(attributeName, results); 119 } 120 121 public static void getRelatedCategories(PageContext pageContext, String attributeName, boolean limitView) { 122 getRelatedCategories(pageContext.getRequest(), attributeName, limitView); 123 } 124 125 public static void getRelatedCategories(ServletRequest request, String attributeName, boolean limitView) { 126 Map requestParameters = UtilHttp.getParameterMap((HttpServletRequest ) request); 127 String requestId = null; 128 129 requestId = UtilFormatOut.checkNull((String )requestParameters.get("catalog_id"), (String )requestParameters.get("CATALOG_ID"), 130 (String )requestParameters.get("category_id"), (String )requestParameters.get("CATEGORY_ID")); 131 132 if (requestId.equals("")) 133 return; 134 if (Debug.infoOn()) Debug.logInfo("[CatalogHelper.getRelatedCategories] RequestID: " + requestId, module); 135 getRelatedCategories(request, attributeName, requestId, limitView); 136 } 137 138 public static void getRelatedCategories(PageContext pageContext, String attributeName, String parentId, boolean limitView) { 139 getRelatedCategories(pageContext.getRequest(), attributeName, parentId, limitView); 140 } 141 142 public static void getRelatedCategories(ServletRequest request, String attributeName, String parentId, boolean limitView) { 143 getRelatedCategories(request, attributeName, parentId, limitView, false); 144 } 145 146 public static void getRelatedCategories(ServletRequest request, String attributeName, String parentId, boolean limitView, boolean excludeEmpty) { 147 ArrayList categories = getRelatedCategoriesRet(request, attributeName, parentId, limitView, excludeEmpty); 148 149 if (categories.size() > 0) 150 request.setAttribute(attributeName, categories); 151 } 152 153 public static ArrayList getRelatedCategoriesRet(PageContext pageContext, String attributeName, String parentId, boolean limitView) { 154 return getRelatedCategoriesRet(pageContext.getRequest(), attributeName, parentId, limitView); 155 } 156 157 public static ArrayList getRelatedCategoriesRet(ServletRequest request, String attributeName, String parentId, boolean limitView) { 158 return getRelatedCategoriesRet(request, attributeName, parentId, limitView, false); 159 } 160 161 public static ArrayList getRelatedCategoriesRet(ServletRequest request, String attributeName, String parentId, boolean limitView, boolean excludeEmpty) { 162 ArrayList categories = new ArrayList (); 163 164 if (Debug.verboseOn()) Debug.logVerbose("[CatalogHelper.getRelatedCategories] ParentID: " + parentId, module); 165 166 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 167 List rollups = null; 168 169 try { 170 rollups = delegator.findByAndCache("ProductCategoryRollup", 171 UtilMisc.toMap("parentProductCategoryId", parentId), 172 UtilMisc.toList("sequenceNum")); 173 if (limitView) { 174 rollups = EntityUtil.filterByDate(rollups, true); 175 } 176 } catch (GenericEntityException e) { 177 Debug.logWarning(e.getMessage(), module); 178 rollups = null; 179 } 180 if (rollups != null && rollups.size() > 0) { 181 Iterator ri = rollups.iterator(); 183 184 while (ri.hasNext()) { 185 GenericValue parent = (GenericValue) ri.next(); 186 GenericValue cv = null; 188 189 try { 190 cv = parent.getRelatedOneCache("CurrentProductCategory"); 191 } catch (GenericEntityException e) { 192 Debug.logWarning(e.getMessage(), module); 193 cv = null; 194 } 195 if (cv != null) { 196 if (excludeEmpty) { 197 if (!isCategoryEmpty(cv)) { 198 categories.add(cv); 200 } 201 } else { 202 categories.add(cv); 203 } 204 } 205 } 206 } 207 return categories; 208 } 209 210 public static boolean isCategoryEmpty(GenericValue category) { 211 boolean empty = true; 212 long members = categoryMemberCount(category); 213 if (members > 0) { 215 empty = false; 216 } 217 218 if (empty) { 219 long rollups = categoryRollupCount(category); 220 if (rollups > 0) { 222 empty = false; 223 } 224 } 225 226 return empty; 227 } 228 229 public static long categoryMemberCount(GenericValue category) { 230 if (category == null) return 0; 231 GenericDelegator delegator = category.getDelegator(); 232 long count = 0; 233 try { 234 count = delegator.findCountByCondition("ProductCategoryMember", buildCountCondition("productCategoryId", category.getString("productCategoryId")), null); 235 } catch (GenericEntityException e) { 236 Debug.logError(e, module); 237 } 238 return count; 239 } 240 241 public static long categoryRollupCount(GenericValue category) { 242 if (category == null) return 0; 243 GenericDelegator delegator = category.getDelegator(); 244 long count = 0; 245 try { 246 count = delegator.findCountByCondition("ProductCategoryRollup", buildCountCondition("parentProductCategoryId", category.getString("productCategoryId")), null); 247 } catch (GenericEntityException e) { 248 Debug.logError(e, module); 249 } 250 return count; 251 } 252 253 private static EntityCondition buildCountCondition(String fieldName, String fieldValue) { 254 List orCondList = new ArrayList (); 255 orCondList.add(new EntityExpr("thruDate", EntityOperator.GREATER_THAN, UtilDateTime.nowTimestamp())); 256 orCondList.add(new EntityExpr("thruDate", EntityOperator.EQUALS, null)); 257 EntityCondition orCond = new EntityConditionList(orCondList, EntityOperator.OR); 258 259 List andCondList = new ArrayList (); 260 andCondList.add(new EntityExpr("fromDate", EntityOperator.LESS_THAN, UtilDateTime.nowTimestamp())); 261 andCondList.add(new EntityExpr(fieldName, EntityOperator.EQUALS, fieldValue)); 262 andCondList.add(orCond); 263 EntityCondition andCond = new EntityConditionList(andCondList, EntityOperator.AND); 264 265 return andCond; 266 } 267 268 public static void setTrail(PageContext pageContext, String currentCategory) { 269 setTrail(pageContext.getRequest(), currentCategory); 270 } 271 272 public static void setTrail(ServletRequest request, String currentCategory) { 273 Map requestParameters = UtilHttp.getParameterMap((HttpServletRequest ) request); 274 String previousCategory = (String ) requestParameters.get("pcategory"); 275 276 if (Debug.verboseOn()) Debug.logVerbose("[CatalogHelper.setTrail] Start: previousCategory=" + previousCategory + 277 " currentCategory=" + currentCategory, module); 278 279 if (currentCategory == null || currentCategory.length() <= 0) 281 return; 282 283 List crumb = getTrail(request); 285 286 if (crumb == null) 287 crumb = new ArrayList (); 288 289 if (previousCategory == null || previousCategory.length() <= 0) { 291 if (crumb.contains(currentCategory)) { 292 int cindex = crumb.lastIndexOf(currentCategory); 294 295 if (cindex < (crumb.size() - 1)) { 296 for (int i = crumb.size() - 1; i > cindex; i--) { 297 String deadCat = (String ) crumb.remove(i); 298 299 if (Debug.infoOn()) Debug.logInfo("[CatalogHelper.setTrail] Removed after current category index: " + i + 300 " catname: " + deadCat, module); 301 } 302 } 303 return; 304 } else { 305 previousCategory = "TOP"; 307 crumb.clear(); 308 crumb.add(previousCategory); 309 if (Debug.infoOn()) Debug.logInfo("[CatalogHelper.setTrail] Starting new list, added previousCategory: " + previousCategory, module); 310 } 311 } 312 313 if (!crumb.contains(previousCategory)) { 314 if (Debug.infoOn()) Debug.logInfo("[CatalogHelper.setTrail] ERROR: previousCategory (" + previousCategory + 316 ") was not in the crumb list, position is lost, starting over with TOP", module); 317 previousCategory = "TOP"; 318 crumb.clear(); 319 crumb.add(previousCategory); 320 } else { 321 int index = crumb.indexOf(previousCategory); 323 324 if (index < (crumb.size() - 1)) { 325 for (int i = crumb.size() - 1; i > index; i--) { 326 String deadCat = (String ) crumb.remove(i); 327 328 if (Debug.infoOn()) Debug.logInfo("[CatalogHelper.setTrail] Removed after previous category index: " + i + 329 " catname: " + deadCat, module); 330 } 331 } 332 } 333 334 crumb.add(currentCategory); 336 if (Debug.verboseOn()) Debug.logVerbose("[CatalogHelper.setTrail] Continuing list: Added currentCategory: " + currentCategory, module); 337 setTrail(request, crumb); 338 } 339 340 public static List getTrail(PageContext pageContext) { 341 return getTrail(pageContext.getRequest()); 342 } 343 344 public static List getTrail(ServletRequest request) { 345 HttpSession session = ((HttpServletRequest ) request).getSession(); 346 ArrayList crumb = (ArrayList ) session.getAttribute("_BREAD_CRUMB_TRAIL_"); 347 return crumb; 348 } 349 350 public static List setTrail(PageContext pageContext, List crumb) { 351 return setTrail(pageContext.getRequest(), crumb); 352 } 353 354 public static List setTrail(ServletRequest request, List crumb) { 355 HttpSession session = ((HttpServletRequest ) request).getSession(); 356 session.setAttribute("_BREAD_CRUMB_TRAIL_", crumb); 357 return crumb; 358 } 359 360 public static boolean checkTrailItem(PageContext pageContext, String category) { 361 return checkTrailItem(pageContext.getRequest(), category); 362 } 363 364 public static boolean checkTrailItem(ServletRequest request, String category) { 365 List crumb = getTrail(request); 366 367 if (crumb != null && crumb.contains(category)) 368 return true; 369 else 370 return false; 371 } 372 373 public static String lastTrailItem(PageContext pageContext) { 374 return lastTrailItem(pageContext.getRequest()); 375 } 376 377 public static String lastTrailItem(ServletRequest request) { 378 List crumb = getTrail(request); 379 380 if (crumb != null && crumb.size() > 0) { 381 return (String ) crumb.get(crumb.size() - 1); 382 } else { 383 return null; 384 } 385 } 386 387 public static boolean isProductInCategory(GenericDelegator delegator, String productId, String productCategoryId) throws GenericEntityException { 388 if (productCategoryId == null) return false; 389 if (productId == null || productId.length() == 0) return false; 390 391 List productCategoryMembers = EntityUtil.filterByDate(delegator.findByAndCache("ProductCategoryMember", 392 UtilMisc.toMap("productCategoryId", productCategoryId, "productId", productId)), true); 393 if (productCategoryMembers == null || productCategoryMembers.size() == 0) { 394 GenericValue product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); 396 List productAssocs = ProductWorker.getVariantVirtualAssocs(product); 397 if (productAssocs != null && productAssocs.size() > 0) { 399 Iterator pasIter = productAssocs.iterator(); 400 while (pasIter.hasNext()) { 401 GenericValue productAssoc = (GenericValue) pasIter.next(); 402 if (isProductInCategory(delegator, productAssoc.getString("productId"), productCategoryId)) { 403 return true; 404 } 405 } 406 } 407 408 return false; 409 } else { 410 return true; 411 } 412 } 413 414 public static List filterProductsInCategory(GenericDelegator delegator, List valueObjects, String productCategoryId) throws GenericEntityException { 415 return filterProductsInCategory(delegator, valueObjects, productCategoryId, "productId"); 416 } 417 418 public static List filterProductsInCategory(GenericDelegator delegator, List valueObjects, String productCategoryId, String productIdFieldName) throws GenericEntityException { 419 if (productCategoryId == null) return new LinkedList (); 420 if (valueObjects == null) return null; 421 422 List newList = new ArrayList (valueObjects.size()); 423 Iterator valIter = valueObjects.iterator(); 424 while (valIter.hasNext()) { 425 GenericValue curValue = (GenericValue) valIter.next(); 426 String productId = curValue.getString(productIdFieldName); 427 if (isProductInCategory(delegator, productId, productCategoryId)) { 428 newList.add(curValue); 429 } 430 } 431 return newList; 432 } 433 434 public static HashMap getCategoryContentWrappers(HashMap catContentWrappers, Iterator catIterator, HttpServletRequest request) throws GenericEntityException { 435 while(catIterator.hasNext()) { 436 GenericValue cat = (GenericValue) catIterator.next(); 437 CategoryContentWrapper catContentWrapper = new CategoryContentWrapper(cat, request); 438 String id = (String ) cat.get("productCategoryId"); 439 catContentWrappers.put(id, catContentWrapper); 440 ArrayList subCat = new ArrayList (); 441 subCat = getRelatedCategoriesRet(request, "subCatList", id, true); 442 if(subCat != null) { 443 Iterator subCatIterator = UtilMisc.toIterator(subCat); 444 if(subCatIterator != null) { 445 getCategoryContentWrappers(catContentWrappers, subCatIterator, request ); 446 } 447 } 448 } 449 return catContentWrappers; 450 } 451 } 452 | Popular Tags |