KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > product > catalog > CatalogWorker


1 /*
2  * $Id: CatalogWorker.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.product.catalog;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.ServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
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 /**
51  * CatalogWorker - Worker class for catalog related functionality
52  *
53  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
54  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
55  * @version $Rev: 5462 $
56  * @since 2.0
57  */

58 public class CatalogWorker {
59     
60     public static final String JavaDoc module = CatalogWorker.class.getName();
61
62     public static String JavaDoc getWebSiteId(ServletRequest JavaDoc request) {
63         return WebSiteWorker.getWebSiteId(request);
64     }
65     
66     public static GenericValue getWebSite(ServletRequest JavaDoc request) {
67         return WebSiteWorker.getWebSite(request);
68     }
69
70     public static List JavaDoc getAllCatalogIds(ServletRequest JavaDoc request) {
71         List JavaDoc catalogIds = new ArrayList JavaDoc();
72         List JavaDoc 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 JavaDoc 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 JavaDoc getStoreCatalogs(ServletRequest JavaDoc request) {
90         String JavaDoc productStoreId = ProductStoreWorker.getProductStoreId(request);
91         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
92         return getStoreCatalogs(delegator, productStoreId);
93     }
94
95     public static List JavaDoc getStoreCatalogs(GenericDelegator delegator, String JavaDoc 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 JavaDoc getPartyCatalogs(ServletRequest JavaDoc request) {
105         HttpSession JavaDoc session = ((HttpServletRequest JavaDoc) 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 JavaDoc 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 JavaDoc getPartyCatalogs(GenericDelegator delegator, String JavaDoc 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 JavaDoc getProdCatalogCategories(ServletRequest JavaDoc request, String JavaDoc prodCatalogId, String JavaDoc prodCatalogCategoryTypeId) {
129         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
130         return getProdCatalogCategories(delegator, prodCatalogId, prodCatalogCategoryTypeId);
131     }
132
133     public static List JavaDoc getProdCatalogCategories(GenericDelegator delegator, String JavaDoc prodCatalogId, String JavaDoc prodCatalogCategoryTypeId) {
134         try {
135             List JavaDoc 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     /**
151      * Retrieves the current prodCatalogId. First it will attempt to find it from a special
152      * request parameter or session attribute named CURRENT_CATALOG_ID. Failing that, it will
153      * get the first catalog from the database as specified in getCatalogIdsAvailable().
154      * If this behavior is undesired, give the user a selectable list of catalogs.
155      */

156     public static String JavaDoc getCurrentCatalogId(ServletRequest JavaDoc request) {
157         HttpSession JavaDoc session = ((HttpServletRequest JavaDoc) request).getSession();
158         Map JavaDoc requestParameters = UtilHttp.getParameterMap((HttpServletRequest JavaDoc) request);
159         String JavaDoc prodCatalogId = null;
160         boolean fromSession = false;
161
162         // first see if a new catalog was specified as a parameter
163
prodCatalogId = (String JavaDoc) requestParameters.get("CURRENT_CATALOG_ID");
164         // if no parameter, try from session
165
if (prodCatalogId == null) {
166             prodCatalogId = (String JavaDoc) session.getAttribute("CURRENT_CATALOG_ID");
167             if (prodCatalogId != null) fromSession = true;
168         }
169         // get it from the database
170
if (prodCatalogId == null) {
171             List JavaDoc catalogIds = getCatalogIdsAvailable(request);
172             if (catalogIds != null && catalogIds.size() > 0) prodCatalogId = (String JavaDoc) 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 JavaDoc());
179         }
180         return prodCatalogId;
181     }
182     
183     public static List JavaDoc getCatalogIdsAvailable(ServletRequest JavaDoc request) {
184         List JavaDoc partyCatalogs = getPartyCatalogs(request);
185         List JavaDoc storeCatalogs = getStoreCatalogs(request);
186         return getCatalogIdsAvailable(partyCatalogs, storeCatalogs);
187     }
188
189     public static List JavaDoc getCatalogIdsAvailable(GenericDelegator delegator, String JavaDoc productStoreId, String JavaDoc partyId) {
190         List JavaDoc storeCatalogs = getStoreCatalogs(delegator, productStoreId);
191         List JavaDoc partyCatalogs = getPartyCatalogs(delegator, partyId);
192         return getCatalogIdsAvailable(partyCatalogs, storeCatalogs);
193     }
194     
195     public static List JavaDoc getCatalogIdsAvailable(List JavaDoc partyCatalogs, List JavaDoc storeCatalogs) {
196         List JavaDoc categoryIds = new LinkedList JavaDoc();
197         List JavaDoc allCatalogLinks = new ArrayList JavaDoc((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 JavaDoc 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 JavaDoc getCatalogName(ServletRequest JavaDoc request) {
212         return getCatalogName(request, getCurrentCatalogId(request));
213     }
214
215     public static String JavaDoc getCatalogName(ServletRequest JavaDoc request, String JavaDoc 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 JavaDoc getContentPathPrefix(ServletRequest JavaDoc request) {
233         GenericValue prodCatalog = getProdCatalog(request, getCurrentCatalogId(request));
234
235         if (prodCatalog == null) return "";
236         String JavaDoc contentPathPrefix = prodCatalog.getString("contentPathPrefix");
237
238         return StringUtil.cleanUpPathPrefix(contentPathPrefix);
239     }
240         
241     public static String JavaDoc getTemplatePathPrefix(ServletRequest JavaDoc request) {
242         GenericValue prodCatalog = getProdCatalog(request, getCurrentCatalogId(request));
243
244         if (prodCatalog == null) return "";
245         String JavaDoc templatePathPrefix = prodCatalog.getString("templatePathPrefix");
246
247         return StringUtil.cleanUpPathPrefix(templatePathPrefix);
248     }
249
250     public static GenericValue getProdCatalog(ServletRequest JavaDoc request) {
251         return getProdCatalog(request, getCurrentCatalogId(request));
252     }
253
254     public static GenericValue getProdCatalog(ServletRequest JavaDoc request, String JavaDoc 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 JavaDoc getCatalogTopCategoryId(ServletRequest JavaDoc request) {
267         return getCatalogTopCategoryId(request, getCurrentCatalogId(request));
268     }
269         
270     public static String JavaDoc getCatalogTopCategoryId(ServletRequest JavaDoc request, String JavaDoc prodCatalogId) {
271         if (prodCatalogId == null || prodCatalogId.length() <= 0) return null;
272
273         List JavaDoc 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 JavaDoc getCatalogSearchCategoryId(ServletRequest JavaDoc request) {
285         return getCatalogSearchCategoryId(request, getCurrentCatalogId(request));
286     }
287         
288     public static String JavaDoc getCatalogSearchCategoryId(ServletRequest JavaDoc request, String JavaDoc prodCatalogId) {
289         return getCatalogSearchCategoryId((GenericDelegator) request.getAttribute("delegator"), prodCatalogId);
290     }
291     public static String JavaDoc getCatalogSearchCategoryId(GenericDelegator delegator, String JavaDoc prodCatalogId) {
292         if (prodCatalogId == null || prodCatalogId.length() <= 0) return null;
293
294         List JavaDoc 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 JavaDoc getCatalogViewAllowCategoryId(GenericDelegator delegator, String JavaDoc prodCatalogId) {
304         if (prodCatalogId == null || prodCatalogId.length() <= 0) return null;
305
306         List JavaDoc 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 JavaDoc getCatalogPurchaseAllowCategoryId(GenericDelegator delegator, String JavaDoc prodCatalogId) {
316         if (prodCatalogId == null || prodCatalogId.length() <= 0) return null;
317
318         List JavaDoc 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 JavaDoc getCatalogPromotionsCategoryId(ServletRequest JavaDoc request) {
328         return getCatalogPromotionsCategoryId(request, getCurrentCatalogId(request));
329     }
330          
331     public static String JavaDoc getCatalogPromotionsCategoryId(ServletRequest JavaDoc request, String JavaDoc prodCatalogId) {
332         if (prodCatalogId == null || prodCatalogId.length() <= 0) return null;
333
334         List JavaDoc 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 JavaDoc request) {
346         return getCatalogQuickaddUse(request, getCurrentCatalogId(request));
347     }
348
349     public static boolean getCatalogQuickaddUse(ServletRequest JavaDoc request, String JavaDoc 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 JavaDoc getCatalogQuickaddCategoryPrimary(ServletRequest JavaDoc request) {
366         return getCatalogQuickaddCategoryPrimary(request, getCurrentCatalogId(request));
367     }
368                    
369     public static String JavaDoc getCatalogQuickaddCategoryPrimary(ServletRequest JavaDoc request, String JavaDoc prodCatalogId) {
370         if (prodCatalogId == null || prodCatalogId.length() <= 0) return null;
371
372         List JavaDoc 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 JavaDoc getCatalogQuickaddCategories(ServletRequest JavaDoc request) {
384         return getCatalogQuickaddCategories(request, getCurrentCatalogId(request));
385     }
386                 
387     public static Collection JavaDoc getCatalogQuickaddCategories(ServletRequest JavaDoc request, String JavaDoc prodCatalogId) {
388         if (prodCatalogId == null || prodCatalogId.length() <= 0) return null;
389
390         Collection JavaDoc categoryIds = new LinkedList JavaDoc();
391
392         Collection JavaDoc prodCatalogCategories = getProdCatalogCategories(request, prodCatalogId, "PCCT_QUICK_ADD");
393
394         if (prodCatalogCategories != null && prodCatalogCategories.size() > 0) {
395             Iterator JavaDoc 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