1 24 package org.ofbiz.product.catalog; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import javax.servlet.ServletRequest ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpSession ; 36 37 import org.ofbiz.base.util.Debug; 38 import org.ofbiz.base.util.StringUtil; 39 import org.ofbiz.base.util.UtilHttp; 40 import org.ofbiz.base.util.UtilMisc; 41 import org.ofbiz.base.util.UtilValidate; 42 import org.ofbiz.webapp.website.WebSiteWorker; 43 import org.ofbiz.entity.GenericDelegator; 44 import org.ofbiz.entity.GenericEntityException; 45 import org.ofbiz.entity.GenericValue; 46 import org.ofbiz.entity.util.EntityUtil; 47 import org.ofbiz.product.category.CategoryWorker; 48 import org.ofbiz.product.store.ProductStoreWorker; 49 50 58 public class CatalogWorker { 59 60 public static final String module = CatalogWorker.class.getName(); 61 62 public static String getWebSiteId(ServletRequest request) { 63 return WebSiteWorker.getWebSiteId(request); 64 } 65 66 public static GenericValue getWebSite(ServletRequest request) { 67 return WebSiteWorker.getWebSite(request); 68 } 69 70 public static List getAllCatalogIds(ServletRequest request) { 71 List catalogIds = new ArrayList (); 72 List catalogs = null; 73 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 74 try { 75 catalogs = delegator.findAll("ProdCatalog", UtilMisc.toList("catalogName")); 76 } catch (GenericEntityException e) { 77 Debug.logError(e, "Error looking up all catalogs", module); 78 } 79 if (catalogs != null) { 80 Iterator i = catalogs.iterator(); 81 while (i.hasNext()) { 82 GenericValue c = (GenericValue) i.next(); 83 catalogIds.add(c.getString("prodCatalogId")); 84 } 85 } 86 return catalogIds; 87 } 88 89 public static List getStoreCatalogs(ServletRequest request) { 90 String productStoreId = ProductStoreWorker.getProductStoreId(request); 91 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 92 return getStoreCatalogs(delegator, productStoreId); 93 } 94 95 public static List getStoreCatalogs(GenericDelegator delegator, String productStoreId) { 96 try { 97 return EntityUtil.filterByDate(delegator.findByAndCache("ProductStoreCatalog", UtilMisc.toMap("productStoreId", productStoreId), UtilMisc.toList("sequenceNum", "prodCatalogId")), true); 98 } catch (GenericEntityException e) { 99 Debug.logError(e, "Error looking up store catalogs for store with id " + productStoreId, module); 100 } 101 return null; 102 } 103 104 public static List getPartyCatalogs(ServletRequest request) { 105 HttpSession session = ((HttpServletRequest ) request).getSession(); 106 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 107 if (userLogin == null) userLogin = (GenericValue) session.getAttribute("autoUserLogin"); 108 if (userLogin == null) return null; 109 String partyId = userLogin.getString("partyId"); 110 if (partyId == null) return null; 111 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 112 return getPartyCatalogs(delegator, partyId); 113 } 114 115 public static List getPartyCatalogs(GenericDelegator delegator, String partyId) { 116 if (delegator == null || partyId == null) { 117 return null; 118 } 119 120 try { 121 return EntityUtil.filterByDate(delegator.findByAndCache("ProdCatalogRole", UtilMisc.toMap("partyId", partyId, "roleTypeId", "CUSTOMER"), UtilMisc.toList("sequenceNum", "prodCatalogId")), true); 122 } catch (GenericEntityException e) { 123 Debug.logError(e, "Error looking up ProdCatalog Roles for party with id " + partyId, module); 124 } 125 return null; 126 } 127 128 public static List getProdCatalogCategories(ServletRequest request, String prodCatalogId, String prodCatalogCategoryTypeId) { 129 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 130 return getProdCatalogCategories(delegator, prodCatalogId, prodCatalogCategoryTypeId); 131 } 132 133 public static List getProdCatalogCategories(GenericDelegator delegator, String prodCatalogId, String prodCatalogCategoryTypeId) { 134 try { 135 List prodCatalogCategories = EntityUtil.filterByDate(delegator.findByAndCache("ProdCatalogCategory", 136 UtilMisc.toMap("prodCatalogId", prodCatalogId), 137 UtilMisc.toList("sequenceNum", "productCategoryId")), true); 138 139 if (UtilValidate.isNotEmpty(prodCatalogCategoryTypeId) && prodCatalogCategories != null) { 140 prodCatalogCategories = EntityUtil.filterByAnd(prodCatalogCategories, 141 UtilMisc.toMap("prodCatalogCategoryTypeId", prodCatalogCategoryTypeId)); 142 } 143 return prodCatalogCategories; 144 } catch (GenericEntityException e) { 145 Debug.logError(e, "Error looking up ProdCatalogCategories for prodCatalog with id " + prodCatalogId, module); 146 } 147 return null; 148 } 149 150 156 public static String getCurrentCatalogId(ServletRequest request) { 157 HttpSession session = ((HttpServletRequest ) request).getSession(); 158 Map requestParameters = UtilHttp.getParameterMap((HttpServletRequest ) request); 159 String prodCatalogId = null; 160 boolean fromSession = false; 161 162 prodCatalogId = (String ) requestParameters.get("CURRENT_CATALOG_ID"); 164 if (prodCatalogId == null) { 166 prodCatalogId = (String ) session.getAttribute("CURRENT_CATALOG_ID"); 167 if (prodCatalogId != null) fromSession = true; 168 } 169 if (prodCatalogId == null) { 171 List catalogIds = getCatalogIdsAvailable(request); 172 if (catalogIds != null && catalogIds.size() > 0) prodCatalogId = (String ) catalogIds.get(0); 173 } 174 175 if (!fromSession) { 176 if (Debug.verboseOn()) Debug.logVerbose("[CatalogWorker.getCurrentCatalogId] Setting new catalog name: " + prodCatalogId, module); 177 session.setAttribute("CURRENT_CATALOG_ID", prodCatalogId); 178 CategoryWorker.setTrail(request, new ArrayList ()); 179 } 180 return prodCatalogId; 181 } 182 183 public static List getCatalogIdsAvailable(ServletRequest request) { 184 List partyCatalogs = getPartyCatalogs(request); 185 List storeCatalogs = getStoreCatalogs(request); 186 return getCatalogIdsAvailable(partyCatalogs, storeCatalogs); 187 } 188 189 public static List getCatalogIdsAvailable(GenericDelegator delegator, String productStoreId, String partyId) { 190 List storeCatalogs = getStoreCatalogs(delegator, productStoreId); 191 List partyCatalogs = getPartyCatalogs(delegator, partyId); 192 return getCatalogIdsAvailable(partyCatalogs, storeCatalogs); 193 } 194 195 public static List getCatalogIdsAvailable(List partyCatalogs, List storeCatalogs) { 196 List categoryIds = new LinkedList (); 197 List allCatalogLinks = new ArrayList ((storeCatalogs == null ? 0 : storeCatalogs.size()) + (partyCatalogs == null ? 0 : partyCatalogs.size())); 198 if (partyCatalogs != null) allCatalogLinks.addAll(partyCatalogs); 199 if (storeCatalogs != null) allCatalogLinks.addAll(storeCatalogs); 200 201 if (allCatalogLinks.size() > 0) { 202 Iterator aclIter = allCatalogLinks.iterator(); 203 while (aclIter.hasNext()) { 204 GenericValue catalogLink = (GenericValue) aclIter.next(); 205 categoryIds.add(catalogLink.getString("prodCatalogId")); 206 } 207 } 208 return categoryIds; 209 } 210 211 public static String getCatalogName(ServletRequest request) { 212 return getCatalogName(request, getCurrentCatalogId(request)); 213 } 214 215 public static String getCatalogName(ServletRequest request, String prodCatalogId) { 216 if (prodCatalogId == null || prodCatalogId.length() <= 0) return null; 217 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 218 219 try { 220 GenericValue prodCatalog = delegator.findByPrimaryKeyCache("ProdCatalog", UtilMisc.toMap("prodCatalogId", prodCatalogId)); 221 222 if (prodCatalog != null) { 223 return prodCatalog.getString("catalogName"); 224 } 225 } catch (GenericEntityException e) { 226 Debug.logError(e, "Error looking up name for prodCatalog with id " + prodCatalogId, module); 227 } 228 229 return null; 230 } 231 232 public static String getContentPathPrefix(ServletRequest request) { 233 GenericValue prodCatalog = getProdCatalog(request, getCurrentCatalogId(request)); 234 235 if (prodCatalog == null) return ""; 236 String contentPathPrefix = prodCatalog.getString("contentPathPrefix"); 237 238 return StringUtil.cleanUpPathPrefix(contentPathPrefix); 239 } 240 241 public static String getTemplatePathPrefix(ServletRequest request) { 242 GenericValue prodCatalog = getProdCatalog(request, getCurrentCatalogId(request)); 243 244 if (prodCatalog == null) return ""; 245 String templatePathPrefix = prodCatalog.getString("templatePathPrefix"); 246 247 return StringUtil.cleanUpPathPrefix(templatePathPrefix); 248 } 249 250 public static GenericValue getProdCatalog(ServletRequest request) { 251 return getProdCatalog(request, getCurrentCatalogId(request)); 252 } 253 254 public static GenericValue getProdCatalog(ServletRequest request, String prodCatalogId) { 255 if (prodCatalogId == null || prodCatalogId.length() <= 0) return null; 256 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 257 258 try { 259 return delegator.findByPrimaryKeyCache("ProdCatalog", UtilMisc.toMap("prodCatalogId", prodCatalogId)); 260 } catch (GenericEntityException e) { 261 Debug.logError(e, "Error looking up name for prodCatalog with id " + prodCatalogId, module); 262 return null; 263 } 264 } 265 266 public static String getCatalogTopCategoryId(ServletRequest request) { 267 return getCatalogTopCategoryId(request, getCurrentCatalogId(request)); 268 } 269 270 public static String getCatalogTopCategoryId(ServletRequest request, String prodCatalogId) { 271 if (prodCatalogId == null || prodCatalogId.length() <= 0) return null; 272 273 List prodCatalogCategories = getProdCatalogCategories(request, prodCatalogId, "PCCT_BROWSE_ROOT"); 274 275 if (prodCatalogCategories != null && prodCatalogCategories.size() > 0) { 276 GenericValue prodCatalogCategory = EntityUtil.getFirst(prodCatalogCategories); 277 278 return prodCatalogCategory.getString("productCategoryId"); 279 } else { 280 return null; 281 } 282 } 283 284 public static String getCatalogSearchCategoryId(ServletRequest request) { 285 return getCatalogSearchCategoryId(request, getCurrentCatalogId(request)); 286 } 287 288 public static String getCatalogSearchCategoryId(ServletRequest request, String prodCatalogId) { 289 return getCatalogSearchCategoryId((GenericDelegator) request.getAttribute("delegator"), prodCatalogId); 290 } 291 public static String getCatalogSearchCategoryId(GenericDelegator delegator, String prodCatalogId) { 292 if (prodCatalogId == null || prodCatalogId.length() <= 0) return null; 293 294 List prodCatalogCategories = getProdCatalogCategories(delegator, prodCatalogId, "PCCT_SEARCH"); 295 if (prodCatalogCategories != null && prodCatalogCategories.size() > 0) { 296 GenericValue prodCatalogCategory = EntityUtil.getFirst(prodCatalogCategories); 297 return prodCatalogCategory.getString("productCategoryId"); 298 } else { 299 return null; 300 } 301 } 302 303 public static String getCatalogViewAllowCategoryId(GenericDelegator delegator, String prodCatalogId) { 304 if (prodCatalogId == null || prodCatalogId.length() <= 0) return null; 305 306 List prodCatalogCategories = getProdCatalogCategories(delegator, prodCatalogId, "PCCT_VIEW_ALLW"); 307 if (prodCatalogCategories != null && prodCatalogCategories.size() > 0) { 308 GenericValue prodCatalogCategory = EntityUtil.getFirst(prodCatalogCategories); 309 return prodCatalogCategory.getString("productCategoryId"); 310 } else { 311 return null; 312 } 313 } 314 315 public static String getCatalogPurchaseAllowCategoryId(GenericDelegator delegator, String prodCatalogId) { 316 if (prodCatalogId == null || prodCatalogId.length() <= 0) return null; 317 318 List prodCatalogCategories = getProdCatalogCategories(delegator, prodCatalogId, "PCCT_PURCH_ALLW"); 319 if (prodCatalogCategories != null && prodCatalogCategories.size() > 0) { 320 GenericValue prodCatalogCategory = EntityUtil.getFirst(prodCatalogCategories); 321 return prodCatalogCategory.getString("productCategoryId"); 322 } else { 323 return null; 324 } 325 } 326 327 public static String getCatalogPromotionsCategoryId(ServletRequest request) { 328 return getCatalogPromotionsCategoryId(request, getCurrentCatalogId(request)); 329 } 330 331 public static String getCatalogPromotionsCategoryId(ServletRequest request, String prodCatalogId) { 332 if (prodCatalogId == null || prodCatalogId.length() <= 0) return null; 333 334 List prodCatalogCategories = getProdCatalogCategories(request, prodCatalogId, "PCCT_PROMOTIONS"); 335 336 if (prodCatalogCategories != null && prodCatalogCategories.size() > 0) { 337 GenericValue prodCatalogCategory = EntityUtil.getFirst(prodCatalogCategories); 338 339 return prodCatalogCategory.getString("productCategoryId"); 340 } else { 341 return null; 342 } 343 } 344 345 public static boolean getCatalogQuickaddUse(ServletRequest request) { 346 return getCatalogQuickaddUse(request, getCurrentCatalogId(request)); 347 } 348 349 public static boolean getCatalogQuickaddUse(ServletRequest request, String prodCatalogId) { 350 if (prodCatalogId == null || prodCatalogId.length() <= 0) return false; 351 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 352 353 try { 354 GenericValue prodCatalog = delegator.findByPrimaryKeyCache("ProdCatalog", UtilMisc.toMap("prodCatalogId", prodCatalogId)); 355 356 if (prodCatalog != null) { 357 return "Y".equals(prodCatalog.getString("useQuickAdd")); 358 } 359 } catch (GenericEntityException e) { 360 Debug.logError(e, "Error looking up name for prodCatalog with id " + prodCatalogId, module); 361 } 362 return false; 363 } 364 365 public static String getCatalogQuickaddCategoryPrimary(ServletRequest request) { 366 return getCatalogQuickaddCategoryPrimary(request, getCurrentCatalogId(request)); 367 } 368 369 public static String getCatalogQuickaddCategoryPrimary(ServletRequest request, String prodCatalogId) { 370 if (prodCatalogId == null || prodCatalogId.length() <= 0) return null; 371 372 List prodCatalogCategories = getProdCatalogCategories(request, prodCatalogId, "PCCT_QUICK_ADD"); 373 374 if (prodCatalogCategories != null && prodCatalogCategories.size() > 0) { 375 GenericValue prodCatalogCategory = EntityUtil.getFirst(prodCatalogCategories); 376 377 return prodCatalogCategory.getString("productCategoryId"); 378 } else { 379 return null; 380 } 381 } 382 383 public static Collection getCatalogQuickaddCategories(ServletRequest request) { 384 return getCatalogQuickaddCategories(request, getCurrentCatalogId(request)); 385 } 386 387 public static Collection getCatalogQuickaddCategories(ServletRequest request, String prodCatalogId) { 388 if (prodCatalogId == null || prodCatalogId.length() <= 0) return null; 389 390 Collection categoryIds = new LinkedList (); 391 392 Collection prodCatalogCategories = getProdCatalogCategories(request, prodCatalogId, "PCCT_QUICK_ADD"); 393 394 if (prodCatalogCategories != null && prodCatalogCategories.size() > 0) { 395 Iterator pccIter = prodCatalogCategories.iterator(); 396 397 while (pccIter.hasNext()) { 398 GenericValue prodCatalogCategory = (GenericValue) pccIter.next(); 399 400 categoryIds.add(prodCatalogCategory.getString("productCategoryId")); 401 } 402 } 403 404 return categoryIds; 405 } 406 } 407 | Popular Tags |