001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.product.catalog;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Map;
026:
027: import javax.servlet.ServletRequest;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpSession;
030:
031: import org.ofbiz.base.util.Debug;
032: import org.ofbiz.base.util.StringUtil;
033: import org.ofbiz.base.util.UtilHttp;
034: import org.ofbiz.base.util.UtilMisc;
035: import org.ofbiz.base.util.UtilValidate;
036: import org.ofbiz.webapp.website.WebSiteWorker;
037: import org.ofbiz.entity.GenericDelegator;
038: import org.ofbiz.entity.GenericEntityException;
039: import org.ofbiz.entity.GenericValue;
040: import org.ofbiz.entity.util.EntityUtil;
041: import org.ofbiz.product.category.CategoryWorker;
042: import org.ofbiz.product.store.ProductStoreWorker;
043:
044: /**
045: * CatalogWorker - Worker class for catalog related functionality
046: */
047: public class CatalogWorker {
048:
049: public static final String module = CatalogWorker.class.getName();
050:
051: public static String getWebSiteId(ServletRequest request) {
052: return WebSiteWorker.getWebSiteId(request);
053: }
054:
055: public static GenericValue getWebSite(ServletRequest request) {
056: return WebSiteWorker.getWebSite(request);
057: }
058:
059: public static List getAllCatalogIds(ServletRequest request) {
060: List catalogIds = new ArrayList();
061: List catalogs = null;
062: GenericDelegator delegator = (GenericDelegator) request
063: .getAttribute("delegator");
064: try {
065: catalogs = delegator.findAll("ProdCatalog", UtilMisc
066: .toList("catalogName"));
067: } catch (GenericEntityException e) {
068: Debug.logError(e, "Error looking up all catalogs", module);
069: }
070: if (catalogs != null) {
071: Iterator i = catalogs.iterator();
072: while (i.hasNext()) {
073: GenericValue c = (GenericValue) i.next();
074: catalogIds.add(c.getString("prodCatalogId"));
075: }
076: }
077: return catalogIds;
078: }
079:
080: public static List getStoreCatalogs(ServletRequest request) {
081: String productStoreId = ProductStoreWorker
082: .getProductStoreId(request);
083: GenericDelegator delegator = (GenericDelegator) request
084: .getAttribute("delegator");
085: return getStoreCatalogs(delegator, productStoreId);
086: }
087:
088: public static List getStoreCatalogs(GenericDelegator delegator,
089: String productStoreId) {
090: try {
091: return EntityUtil.filterByDate(delegator.findByAndCache(
092: "ProductStoreCatalog", UtilMisc.toMap(
093: "productStoreId", productStoreId), UtilMisc
094: .toList("sequenceNum", "prodCatalogId")),
095: true);
096: } catch (GenericEntityException e) {
097: Debug.logError(e,
098: "Error looking up store catalogs for store with id "
099: + productStoreId, module);
100: }
101: return null;
102: }
103:
104: public static List getPartyCatalogs(ServletRequest request) {
105: HttpSession session = ((HttpServletRequest) request)
106: .getSession();
107: GenericValue userLogin = (GenericValue) session
108: .getAttribute("userLogin");
109: if (userLogin == null)
110: userLogin = (GenericValue) session
111: .getAttribute("autoUserLogin");
112: if (userLogin == null)
113: return null;
114: String partyId = userLogin.getString("partyId");
115: if (partyId == null)
116: return null;
117: GenericDelegator delegator = (GenericDelegator) request
118: .getAttribute("delegator");
119: return getPartyCatalogs(delegator, partyId);
120: }
121:
122: public static List getPartyCatalogs(GenericDelegator delegator,
123: String partyId) {
124: if (delegator == null || partyId == null) {
125: return null;
126: }
127:
128: try {
129: return EntityUtil.filterByDate(delegator.findByAndCache(
130: "ProdCatalogRole", UtilMisc.toMap("partyId",
131: partyId, "roleTypeId", "CUSTOMER"),
132: UtilMisc.toList("sequenceNum", "prodCatalogId")),
133: true);
134: } catch (GenericEntityException e) {
135: Debug.logError(e,
136: "Error looking up ProdCatalog Roles for party with id "
137: + partyId, module);
138: }
139: return null;
140: }
141:
142: public static List getProdCatalogCategories(ServletRequest request,
143: String prodCatalogId, String prodCatalogCategoryTypeId) {
144: GenericDelegator delegator = (GenericDelegator) request
145: .getAttribute("delegator");
146: return getProdCatalogCategories(delegator, prodCatalogId,
147: prodCatalogCategoryTypeId);
148: }
149:
150: public static List getProdCatalogCategories(
151: GenericDelegator delegator, String prodCatalogId,
152: String prodCatalogCategoryTypeId) {
153: try {
154: List prodCatalogCategories = EntityUtil
155: .filterByDate(delegator.findByAndCache(
156: "ProdCatalogCategory", UtilMisc.toMap(
157: "prodCatalogId", prodCatalogId),
158: UtilMisc.toList("sequenceNum",
159: "productCategoryId")), true);
160:
161: if (UtilValidate.isNotEmpty(prodCatalogCategoryTypeId)
162: && prodCatalogCategories != null) {
163: prodCatalogCategories = EntityUtil.filterByAnd(
164: prodCatalogCategories, UtilMisc.toMap(
165: "prodCatalogCategoryTypeId",
166: prodCatalogCategoryTypeId));
167: }
168: return prodCatalogCategories;
169: } catch (GenericEntityException e) {
170: Debug.logError(e,
171: "Error looking up ProdCatalogCategories for prodCatalog with id "
172: + prodCatalogId, module);
173: }
174: return null;
175: }
176:
177: /**
178: * Retrieves the current prodCatalogId. First it will attempt to find it from a special
179: * request parameter or session attribute named CURRENT_CATALOG_ID. Failing that, it will
180: * get the first catalog from the database as specified in getCatalogIdsAvailable().
181: * If this behavior is undesired, give the user a selectable list of catalogs.
182: */
183: public static String getCurrentCatalogId(ServletRequest request) {
184: HttpSession session = ((HttpServletRequest) request)
185: .getSession();
186: Map requestParameters = UtilHttp
187: .getParameterMap((HttpServletRequest) request);
188: String prodCatalogId = null;
189: boolean fromSession = false;
190:
191: // first see if a new catalog was specified as a parameter
192: prodCatalogId = (String) requestParameters
193: .get("CURRENT_CATALOG_ID");
194: // if no parameter, try from session
195: if (prodCatalogId == null) {
196: prodCatalogId = (String) session
197: .getAttribute("CURRENT_CATALOG_ID");
198: if (prodCatalogId != null)
199: fromSession = true;
200: }
201: // get it from the database
202: if (prodCatalogId == null) {
203: List catalogIds = getCatalogIdsAvailable(request);
204: if (catalogIds != null && catalogIds.size() > 0)
205: prodCatalogId = (String) catalogIds.get(0);
206: }
207:
208: if (!fromSession) {
209: if (Debug.verboseOn())
210: Debug.logVerbose(
211: "[CatalogWorker.getCurrentCatalogId] Setting new catalog name: "
212: + prodCatalogId, module);
213: session.setAttribute("CURRENT_CATALOG_ID", prodCatalogId);
214: CategoryWorker.setTrail(request, new ArrayList());
215: }
216: return prodCatalogId;
217: }
218:
219: public static List getCatalogIdsAvailable(ServletRequest request) {
220: List partyCatalogs = getPartyCatalogs(request);
221: List storeCatalogs = getStoreCatalogs(request);
222: return getCatalogIdsAvailable(partyCatalogs, storeCatalogs);
223: }
224:
225: public static List getCatalogIdsAvailable(
226: GenericDelegator delegator, String productStoreId,
227: String partyId) {
228: List storeCatalogs = getStoreCatalogs(delegator, productStoreId);
229: List partyCatalogs = getPartyCatalogs(delegator, partyId);
230: return getCatalogIdsAvailable(partyCatalogs, storeCatalogs);
231: }
232:
233: public static List getCatalogIdsAvailable(List partyCatalogs,
234: List storeCatalogs) {
235: List categoryIds = new LinkedList();
236: List allCatalogLinks = new ArrayList((storeCatalogs == null ? 0
237: : storeCatalogs.size())
238: + (partyCatalogs == null ? 0 : partyCatalogs.size()));
239: if (partyCatalogs != null)
240: allCatalogLinks.addAll(partyCatalogs);
241: if (storeCatalogs != null)
242: allCatalogLinks.addAll(storeCatalogs);
243:
244: if (allCatalogLinks.size() > 0) {
245: Iterator aclIter = allCatalogLinks.iterator();
246: while (aclIter.hasNext()) {
247: GenericValue catalogLink = (GenericValue) aclIter
248: .next();
249: categoryIds.add(catalogLink.getString("prodCatalogId"));
250: }
251: }
252: return categoryIds;
253: }
254:
255: public static String getCatalogName(ServletRequest request) {
256: return getCatalogName(request, getCurrentCatalogId(request));
257: }
258:
259: public static String getCatalogName(ServletRequest request,
260: String prodCatalogId) {
261: if (prodCatalogId == null || prodCatalogId.length() <= 0)
262: return null;
263: GenericDelegator delegator = (GenericDelegator) request
264: .getAttribute("delegator");
265:
266: try {
267: GenericValue prodCatalog = delegator.findByPrimaryKeyCache(
268: "ProdCatalog", UtilMisc.toMap("prodCatalogId",
269: prodCatalogId));
270:
271: if (prodCatalog != null) {
272: return prodCatalog.getString("catalogName");
273: }
274: } catch (GenericEntityException e) {
275: Debug.logError(e,
276: "Error looking up name for prodCatalog with id "
277: + prodCatalogId, module);
278: }
279:
280: return null;
281: }
282:
283: public static String getContentPathPrefix(ServletRequest request) {
284: GenericValue prodCatalog = getProdCatalog(request,
285: getCurrentCatalogId(request));
286:
287: if (prodCatalog == null)
288: return "";
289: String contentPathPrefix = prodCatalog
290: .getString("contentPathPrefix");
291:
292: return StringUtil.cleanUpPathPrefix(contentPathPrefix);
293: }
294:
295: public static String getTemplatePathPrefix(ServletRequest request) {
296: GenericValue prodCatalog = getProdCatalog(request,
297: getCurrentCatalogId(request));
298:
299: if (prodCatalog == null)
300: return "";
301: String templatePathPrefix = prodCatalog
302: .getString("templatePathPrefix");
303:
304: return StringUtil.cleanUpPathPrefix(templatePathPrefix);
305: }
306:
307: public static GenericValue getProdCatalog(ServletRequest request) {
308: return getProdCatalog(request, getCurrentCatalogId(request));
309: }
310:
311: public static GenericValue getProdCatalog(ServletRequest request,
312: String prodCatalogId) {
313: if (prodCatalogId == null || prodCatalogId.length() <= 0)
314: return null;
315: GenericDelegator delegator = (GenericDelegator) request
316: .getAttribute("delegator");
317:
318: try {
319: return delegator.findByPrimaryKeyCache("ProdCatalog",
320: UtilMisc.toMap("prodCatalogId", prodCatalogId));
321: } catch (GenericEntityException e) {
322: Debug.logError(e,
323: "Error looking up name for prodCatalog with id "
324: + prodCatalogId, module);
325: return null;
326: }
327: }
328:
329: public static String getCatalogTopCategoryId(ServletRequest request) {
330: return getCatalogTopCategoryId(request,
331: getCurrentCatalogId(request));
332: }
333:
334: public static String getCatalogTopCategoryId(
335: ServletRequest request, String prodCatalogId) {
336: if (prodCatalogId == null || prodCatalogId.length() <= 0)
337: return null;
338:
339: List prodCatalogCategories = getProdCatalogCategories(request,
340: prodCatalogId, "PCCT_BROWSE_ROOT");
341:
342: if (prodCatalogCategories != null
343: && prodCatalogCategories.size() > 0) {
344: GenericValue prodCatalogCategory = EntityUtil
345: .getFirst(prodCatalogCategories);
346:
347: return prodCatalogCategory.getString("productCategoryId");
348: } else {
349: return null;
350: }
351: }
352:
353: public static String getCatalogSearchCategoryId(
354: ServletRequest request) {
355: return getCatalogSearchCategoryId(request,
356: getCurrentCatalogId(request));
357: }
358:
359: public static String getCatalogSearchCategoryId(
360: ServletRequest request, String prodCatalogId) {
361: return getCatalogSearchCategoryId((GenericDelegator) request
362: .getAttribute("delegator"), prodCatalogId);
363: }
364:
365: public static String getCatalogSearchCategoryId(
366: GenericDelegator delegator, String prodCatalogId) {
367: if (prodCatalogId == null || prodCatalogId.length() <= 0)
368: return null;
369:
370: List prodCatalogCategories = getProdCatalogCategories(
371: delegator, prodCatalogId, "PCCT_SEARCH");
372: if (prodCatalogCategories != null
373: && prodCatalogCategories.size() > 0) {
374: GenericValue prodCatalogCategory = EntityUtil
375: .getFirst(prodCatalogCategories);
376: return prodCatalogCategory.getString("productCategoryId");
377: } else {
378: return null;
379: }
380: }
381:
382: public static String getCatalogViewAllowCategoryId(
383: GenericDelegator delegator, String prodCatalogId) {
384: if (prodCatalogId == null || prodCatalogId.length() <= 0)
385: return null;
386:
387: List prodCatalogCategories = getProdCatalogCategories(
388: delegator, prodCatalogId, "PCCT_VIEW_ALLW");
389: if (prodCatalogCategories != null
390: && prodCatalogCategories.size() > 0) {
391: GenericValue prodCatalogCategory = EntityUtil
392: .getFirst(prodCatalogCategories);
393: return prodCatalogCategory.getString("productCategoryId");
394: } else {
395: return null;
396: }
397: }
398:
399: public static String getCatalogPurchaseAllowCategoryId(
400: GenericDelegator delegator, String prodCatalogId) {
401: if (prodCatalogId == null || prodCatalogId.length() <= 0)
402: return null;
403:
404: List prodCatalogCategories = getProdCatalogCategories(
405: delegator, prodCatalogId, "PCCT_PURCH_ALLW");
406: if (prodCatalogCategories != null
407: && prodCatalogCategories.size() > 0) {
408: GenericValue prodCatalogCategory = EntityUtil
409: .getFirst(prodCatalogCategories);
410: return prodCatalogCategory.getString("productCategoryId");
411: } else {
412: return null;
413: }
414: }
415:
416: public static String getCatalogPromotionsCategoryId(
417: ServletRequest request) {
418: return getCatalogPromotionsCategoryId(request,
419: getCurrentCatalogId(request));
420: }
421:
422: public static String getCatalogPromotionsCategoryId(
423: ServletRequest request, String prodCatalogId) {
424: if (prodCatalogId == null || prodCatalogId.length() <= 0)
425: return null;
426:
427: List prodCatalogCategories = getProdCatalogCategories(request,
428: prodCatalogId, "PCCT_PROMOTIONS");
429:
430: if (prodCatalogCategories != null
431: && prodCatalogCategories.size() > 0) {
432: GenericValue prodCatalogCategory = EntityUtil
433: .getFirst(prodCatalogCategories);
434:
435: return prodCatalogCategory.getString("productCategoryId");
436: } else {
437: return null;
438: }
439: }
440:
441: public static boolean getCatalogQuickaddUse(ServletRequest request) {
442: return getCatalogQuickaddUse(request,
443: getCurrentCatalogId(request));
444: }
445:
446: public static boolean getCatalogQuickaddUse(ServletRequest request,
447: String prodCatalogId) {
448: if (prodCatalogId == null || prodCatalogId.length() <= 0)
449: return false;
450: GenericDelegator delegator = (GenericDelegator) request
451: .getAttribute("delegator");
452:
453: try {
454: GenericValue prodCatalog = delegator.findByPrimaryKeyCache(
455: "ProdCatalog", UtilMisc.toMap("prodCatalogId",
456: prodCatalogId));
457:
458: if (prodCatalog != null) {
459: return "Y".equals(prodCatalog.getString("useQuickAdd"));
460: }
461: } catch (GenericEntityException e) {
462: Debug.logError(e,
463: "Error looking up name for prodCatalog with id "
464: + prodCatalogId, module);
465: }
466: return false;
467: }
468:
469: public static String getCatalogQuickaddCategoryPrimary(
470: ServletRequest request) {
471: return getCatalogQuickaddCategoryPrimary(request,
472: getCurrentCatalogId(request));
473: }
474:
475: public static String getCatalogQuickaddCategoryPrimary(
476: ServletRequest request, String prodCatalogId) {
477: if (prodCatalogId == null || prodCatalogId.length() <= 0)
478: return null;
479:
480: List prodCatalogCategories = getProdCatalogCategories(request,
481: prodCatalogId, "PCCT_QUICK_ADD");
482:
483: if (prodCatalogCategories != null
484: && prodCatalogCategories.size() > 0) {
485: GenericValue prodCatalogCategory = EntityUtil
486: .getFirst(prodCatalogCategories);
487:
488: return prodCatalogCategory.getString("productCategoryId");
489: } else {
490: return null;
491: }
492: }
493:
494: public static Collection getCatalogQuickaddCategories(
495: ServletRequest request) {
496: return getCatalogQuickaddCategories(request,
497: getCurrentCatalogId(request));
498: }
499:
500: public static Collection getCatalogQuickaddCategories(
501: ServletRequest request, String prodCatalogId) {
502: if (prodCatalogId == null || prodCatalogId.length() <= 0)
503: return null;
504:
505: Collection categoryIds = new LinkedList();
506:
507: Collection prodCatalogCategories = getProdCatalogCategories(
508: request, prodCatalogId, "PCCT_QUICK_ADD");
509:
510: if (prodCatalogCategories != null
511: && prodCatalogCategories.size() > 0) {
512: Iterator pccIter = prodCatalogCategories.iterator();
513:
514: while (pccIter.hasNext()) {
515: GenericValue prodCatalogCategory = (GenericValue) pccIter
516: .next();
517:
518: categoryIds.add(prodCatalogCategory
519: .getString("productCategoryId"));
520: }
521: }
522:
523: return categoryIds;
524: }
525: }
|