001: /*
002: * $Id: CatalogWorker.java,v 1.6 2003/09/02 02:17:15 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.product.catalog;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029: import java.util.LinkedList;
030: import java.util.List;
031: import java.util.Map;
032:
033: import javax.servlet.ServletRequest;
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpSession;
036:
037: import org.ofbiz.base.util.Debug;
038: import org.ofbiz.base.util.StringUtil;
039: import org.ofbiz.base.util.UtilHttp;
040: import org.ofbiz.base.util.UtilMisc;
041: import org.ofbiz.base.util.UtilValidate;
042: import org.ofbiz.content.website.WebSiteWorker;
043: import org.ofbiz.entity.GenericDelegator;
044: import org.ofbiz.entity.GenericEntityException;
045: import org.ofbiz.entity.GenericValue;
046: import org.ofbiz.entity.util.EntityUtil;
047: import org.ofbiz.product.category.CategoryWorker;
048: import org.ofbiz.product.store.ProductStoreWorker;
049:
050: /**
051: * CatalogWorker - Worker class for catalog related functionality
052: *
053: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
054: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
055: * @version $Revision: 1.6 $
056: * @since 2.0
057: */
058: public class CatalogWorker {
059:
060: public static final String module = CatalogWorker.class.getName();
061:
062: public static String getWebSiteId(ServletRequest request) {
063: return WebSiteWorker.getWebSiteId(request);
064: }
065:
066: public static GenericValue getWebSite(ServletRequest request) {
067: return WebSiteWorker.getWebSite(request);
068: }
069:
070: public static List getAllCatalogIds(ServletRequest request) {
071: List catalogIds = new ArrayList();
072: List catalogs = null;
073: GenericDelegator delegator = (GenericDelegator) request
074: .getAttribute("delegator");
075: try {
076: catalogs = delegator.findAll("ProdCatalog", UtilMisc
077: .toList("catalogName"));
078: } catch (GenericEntityException e) {
079: Debug.logError(e, "Error looking up all catalogs", module);
080: }
081: if (catalogs != null) {
082: Iterator i = catalogs.iterator();
083: while (i.hasNext()) {
084: GenericValue c = (GenericValue) i.next();
085: catalogIds.add(c.getString("prodCatalogId"));
086: }
087: }
088: return catalogIds;
089: }
090:
091: public static List getStoreCatalogs(ServletRequest request) {
092: String productStoreId = ProductStoreWorker
093: .getProductStoreId(request);
094: GenericDelegator delegator = (GenericDelegator) request
095: .getAttribute("delegator");
096:
097: try {
098: return EntityUtil.filterByDate(delegator.findByAndCache(
099: "ProductStoreCatalog", UtilMisc.toMap(
100: "productStoreId", productStoreId), UtilMisc
101: .toList("sequenceNum", "prodCatalogId")),
102: true);
103: } catch (GenericEntityException e) {
104: Debug.logError(e,
105: "Error looking up store catalogs for store with id "
106: + productStoreId, module);
107: }
108: return null;
109: }
110:
111: public static List getPartyCatalogs(ServletRequest request) {
112: HttpSession session = ((HttpServletRequest) request)
113: .getSession();
114: GenericValue userLogin = (GenericValue) session
115: .getAttribute("userLogin");
116: if (userLogin == null)
117: userLogin = (GenericValue) session
118: .getAttribute("autoUserLogin");
119: if (userLogin == null)
120: return null;
121: String partyId = userLogin.getString("partyId");
122: if (partyId == null)
123: return null;
124: GenericDelegator delegator = (GenericDelegator) request
125: .getAttribute("delegator");
126:
127: try {
128: return EntityUtil.filterByDate(delegator.findByAndCache(
129: "ProdCatalogRole", UtilMisc.toMap("partyId",
130: partyId, "roleTypeId", "CUSTOMER"),
131: UtilMisc.toList("sequenceNum", "prodCatalogId")),
132: true);
133: } catch (GenericEntityException e) {
134: Debug.logError(e,
135: "Error looking up ProdCatalog Roles for party with id "
136: + partyId, module);
137: }
138: return null;
139: }
140:
141: public static List getProdCatalogCategories(ServletRequest request,
142: String prodCatalogId, String prodCatalogCategoryTypeId) {
143: GenericDelegator delegator = (GenericDelegator) request
144: .getAttribute("delegator");
145: return getProdCatalogCategories(delegator, prodCatalogId,
146: prodCatalogCategoryTypeId);
147: }
148:
149: public static List getProdCatalogCategories(
150: GenericDelegator delegator, String prodCatalogId,
151: String prodCatalogCategoryTypeId) {
152: try {
153: List prodCatalogCategories = EntityUtil
154: .filterByDate(delegator.findByAndCache(
155: "ProdCatalogCategory", UtilMisc.toMap(
156: "prodCatalogId", prodCatalogId),
157: UtilMisc.toList("sequenceNum",
158: "productCategoryId")), true);
159:
160: if (UtilValidate.isNotEmpty(prodCatalogCategoryTypeId)
161: && prodCatalogCategories != null) {
162: prodCatalogCategories = EntityUtil.filterByAnd(
163: prodCatalogCategories, UtilMisc.toMap(
164: "prodCatalogCategoryTypeId",
165: prodCatalogCategoryTypeId));
166: }
167: return prodCatalogCategories;
168: } catch (GenericEntityException e) {
169: Debug.logError(e,
170: "Error looking up ProdCatalogCategories for prodCatalog with id "
171: + prodCatalogId, module);
172: }
173: return null;
174: }
175:
176: public static String getCurrentCatalogId(ServletRequest request) {
177: HttpSession session = ((HttpServletRequest) request)
178: .getSession();
179: Map requestParameters = UtilHttp
180: .getParameterMap((HttpServletRequest) request);
181: String prodCatalogId = null;
182: boolean fromSession = false;
183:
184: // first see if a new catalog was specified as a parameter
185: prodCatalogId = (String) requestParameters
186: .get("CURRENT_CATALOG_ID");
187: // if no parameter, try from session
188: if (prodCatalogId == null) {
189: prodCatalogId = (String) session
190: .getAttribute("CURRENT_CATALOG_ID");
191: if (prodCatalogId != null)
192: fromSession = true;
193: }
194: // get it from the database
195: if (prodCatalogId == null) {
196: List catalogIds = getCatalogIdsAvailable(request);
197: if (catalogIds != null && catalogIds.size() > 0)
198: prodCatalogId = (String) catalogIds.get(0);
199: }
200:
201: if (!fromSession) {
202: if (Debug.verboseOn())
203: Debug.logVerbose(
204: "[CatalogWorker.getCurrentCatalogId] Setting new catalog name: "
205: + prodCatalogId, module);
206: session.setAttribute("CURRENT_CATALOG_ID", prodCatalogId);
207: CategoryWorker.setTrail(request, new ArrayList());
208: }
209: return prodCatalogId;
210: }
211:
212: public static List getCatalogIdsAvailable(ServletRequest request) {
213: List categoryIds = new LinkedList();
214: List partyCatalogs = getPartyCatalogs(request);
215: List storeCatalogs = getStoreCatalogs(request);
216: List allCatalogLinks = new ArrayList((storeCatalogs == null ? 0
217: : storeCatalogs.size())
218: + (partyCatalogs == null ? 0 : partyCatalogs.size()));
219: if (partyCatalogs != null)
220: allCatalogLinks.addAll(partyCatalogs);
221: if (storeCatalogs != null)
222: allCatalogLinks.addAll(storeCatalogs);
223:
224: if (allCatalogLinks.size() > 0) {
225: Iterator aclIter = allCatalogLinks.iterator();
226: while (aclIter.hasNext()) {
227: GenericValue catalogLink = (GenericValue) aclIter
228: .next();
229: categoryIds.add(catalogLink.getString("prodCatalogId"));
230: }
231: }
232: return categoryIds;
233: }
234:
235: public static String getCatalogName(ServletRequest request) {
236: return getCatalogName(request, getCurrentCatalogId(request));
237: }
238:
239: public static String getCatalogName(ServletRequest request,
240: String prodCatalogId) {
241: if (prodCatalogId == null || prodCatalogId.length() <= 0)
242: return null;
243: GenericDelegator delegator = (GenericDelegator) request
244: .getAttribute("delegator");
245:
246: try {
247: GenericValue prodCatalog = delegator.findByPrimaryKeyCache(
248: "ProdCatalog", UtilMisc.toMap("prodCatalogId",
249: prodCatalogId));
250:
251: if (prodCatalog != null) {
252: return prodCatalog.getString("catalogName");
253: }
254: } catch (GenericEntityException e) {
255: Debug.logError(e,
256: "Error looking up name for prodCatalog with id "
257: + prodCatalogId, module);
258: }
259:
260: return null;
261: }
262:
263: public static String getContentPathPrefix(ServletRequest request) {
264: GenericValue prodCatalog = getProdCatalog(request,
265: getCurrentCatalogId(request));
266:
267: if (prodCatalog == null)
268: return "";
269: String contentPathPrefix = prodCatalog
270: .getString("contentPathPrefix");
271:
272: return StringUtil.cleanUpPathPrefix(contentPathPrefix);
273: }
274:
275: public static String getTemplatePathPrefix(ServletRequest request) {
276: GenericValue prodCatalog = getProdCatalog(request,
277: getCurrentCatalogId(request));
278:
279: if (prodCatalog == null)
280: return "";
281: String templatePathPrefix = prodCatalog
282: .getString("templatePathPrefix");
283:
284: return StringUtil.cleanUpPathPrefix(templatePathPrefix);
285: }
286:
287: public static GenericValue getProdCatalog(ServletRequest request) {
288: return getProdCatalog(request, getCurrentCatalogId(request));
289: }
290:
291: public static GenericValue getProdCatalog(ServletRequest request,
292: String prodCatalogId) {
293: if (prodCatalogId == null || prodCatalogId.length() <= 0)
294: return null;
295: GenericDelegator delegator = (GenericDelegator) request
296: .getAttribute("delegator");
297:
298: try {
299: return delegator.findByPrimaryKeyCache("ProdCatalog",
300: UtilMisc.toMap("prodCatalogId", prodCatalogId));
301: } catch (GenericEntityException e) {
302: Debug.logError(e,
303: "Error looking up name for prodCatalog with id "
304: + prodCatalogId, module);
305: return null;
306: }
307: }
308:
309: public static String getCatalogTopCategoryId(ServletRequest request) {
310: return getCatalogTopCategoryId(request,
311: getCurrentCatalogId(request));
312: }
313:
314: public static String getCatalogTopCategoryId(
315: ServletRequest request, String prodCatalogId) {
316: if (prodCatalogId == null || prodCatalogId.length() <= 0)
317: return null;
318:
319: List prodCatalogCategories = getProdCatalogCategories(request,
320: prodCatalogId, "PCCT_BROWSE_ROOT");
321:
322: if (prodCatalogCategories != null
323: && prodCatalogCategories.size() > 0) {
324: GenericValue prodCatalogCategory = EntityUtil
325: .getFirst(prodCatalogCategories);
326:
327: return prodCatalogCategory.getString("productCategoryId");
328: } else {
329: return null;
330: }
331: }
332:
333: public static String getCatalogSearchCategoryId(
334: ServletRequest request) {
335: return getCatalogSearchCategoryId(request,
336: getCurrentCatalogId(request));
337: }
338:
339: public static String getCatalogSearchCategoryId(
340: ServletRequest request, String prodCatalogId) {
341: return getCatalogSearchCategoryId((GenericDelegator) request
342: .getAttribute("delegator"), prodCatalogId);
343: }
344:
345: public static String getCatalogSearchCategoryId(
346: GenericDelegator delegator, String prodCatalogId) {
347: if (prodCatalogId == null || prodCatalogId.length() <= 0)
348: return null;
349:
350: List prodCatalogCategories = getProdCatalogCategories(
351: delegator, prodCatalogId, "PCCT_SEARCH");
352: if (prodCatalogCategories != null
353: && prodCatalogCategories.size() > 0) {
354: GenericValue prodCatalogCategory = EntityUtil
355: .getFirst(prodCatalogCategories);
356: return prodCatalogCategory.getString("productCategoryId");
357: } else {
358: return null;
359: }
360: }
361:
362: public static String getCatalogViewAllowCategoryId(
363: GenericDelegator delegator, String prodCatalogId) {
364: if (prodCatalogId == null || prodCatalogId.length() <= 0)
365: return null;
366:
367: List prodCatalogCategories = getProdCatalogCategories(
368: delegator, prodCatalogId, "PCCT_VIEW_ALLW");
369: if (prodCatalogCategories != null
370: && prodCatalogCategories.size() > 0) {
371: GenericValue prodCatalogCategory = EntityUtil
372: .getFirst(prodCatalogCategories);
373: return prodCatalogCategory.getString("productCategoryId");
374: } else {
375: return null;
376: }
377: }
378:
379: public static String getCatalogPurchaseAllowCategoryId(
380: GenericDelegator delegator, String prodCatalogId) {
381: if (prodCatalogId == null || prodCatalogId.length() <= 0)
382: return null;
383:
384: List prodCatalogCategories = getProdCatalogCategories(
385: delegator, prodCatalogId, "PCCT_PURCH_ALLW");
386: if (prodCatalogCategories != null
387: && prodCatalogCategories.size() > 0) {
388: GenericValue prodCatalogCategory = EntityUtil
389: .getFirst(prodCatalogCategories);
390: return prodCatalogCategory.getString("productCategoryId");
391: } else {
392: return null;
393: }
394: }
395:
396: public static String getCatalogPromotionsCategoryId(
397: ServletRequest request) {
398: return getCatalogPromotionsCategoryId(request,
399: getCurrentCatalogId(request));
400: }
401:
402: public static String getCatalogPromotionsCategoryId(
403: ServletRequest request, String prodCatalogId) {
404: if (prodCatalogId == null || prodCatalogId.length() <= 0)
405: return null;
406:
407: List prodCatalogCategories = getProdCatalogCategories(request,
408: prodCatalogId, "PCCT_PROMOTIONS");
409:
410: if (prodCatalogCategories != null
411: && prodCatalogCategories.size() > 0) {
412: GenericValue prodCatalogCategory = EntityUtil
413: .getFirst(prodCatalogCategories);
414:
415: return prodCatalogCategory.getString("productCategoryId");
416: } else {
417: return null;
418: }
419: }
420:
421: public static boolean getCatalogQuickaddUse(ServletRequest request) {
422: return getCatalogQuickaddUse(request,
423: getCurrentCatalogId(request));
424: }
425:
426: public static boolean getCatalogQuickaddUse(ServletRequest request,
427: String prodCatalogId) {
428: if (prodCatalogId == null || prodCatalogId.length() <= 0)
429: return false;
430: GenericDelegator delegator = (GenericDelegator) request
431: .getAttribute("delegator");
432:
433: try {
434: GenericValue prodCatalog = delegator.findByPrimaryKeyCache(
435: "ProdCatalog", UtilMisc.toMap("prodCatalogId",
436: prodCatalogId));
437:
438: if (prodCatalog != null) {
439: return "Y".equals(prodCatalog.getString("useQuickAdd"));
440: }
441: } catch (GenericEntityException e) {
442: Debug.logError(e,
443: "Error looking up name for prodCatalog with id "
444: + prodCatalogId, module);
445: }
446: return false;
447: }
448:
449: public static String getCatalogQuickaddCategoryPrimary(
450: ServletRequest request) {
451: return getCatalogQuickaddCategoryPrimary(request,
452: getCurrentCatalogId(request));
453: }
454:
455: public static String getCatalogQuickaddCategoryPrimary(
456: ServletRequest request, String prodCatalogId) {
457: if (prodCatalogId == null || prodCatalogId.length() <= 0)
458: return null;
459:
460: List prodCatalogCategories = getProdCatalogCategories(request,
461: prodCatalogId, "PCCT_QUICK_ADD");
462:
463: if (prodCatalogCategories != null
464: && prodCatalogCategories.size() > 0) {
465: GenericValue prodCatalogCategory = EntityUtil
466: .getFirst(prodCatalogCategories);
467:
468: return prodCatalogCategory.getString("productCategoryId");
469: } else {
470: return null;
471: }
472: }
473:
474: public static Collection getCatalogQuickaddCategories(
475: ServletRequest request) {
476: return getCatalogQuickaddCategories(request,
477: getCurrentCatalogId(request));
478: }
479:
480: public static Collection getCatalogQuickaddCategories(
481: ServletRequest request, String prodCatalogId) {
482: if (prodCatalogId == null || prodCatalogId.length() <= 0)
483: return null;
484:
485: Collection categoryIds = new LinkedList();
486:
487: Collection prodCatalogCategories = getProdCatalogCategories(
488: request, prodCatalogId, "PCCT_QUICK_ADD");
489:
490: if (prodCatalogCategories != null
491: && prodCatalogCategories.size() > 0) {
492: Iterator pccIter = prodCatalogCategories.iterator();
493:
494: while (pccIter.hasNext()) {
495: GenericValue prodCatalogCategory = (GenericValue) pccIter
496: .next();
497:
498: categoryIds.add(prodCatalogCategory
499: .getString("productCategoryId"));
500: }
501: }
502:
503: return categoryIds;
504: }
505: }
|