KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > product > config > ProductConfigWorker


1 /*
2  * $Id: ProductConfigWorker.java 5472 2005-08-08 06:39:58Z jacopo $
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.config;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import org.ofbiz.base.util.Debug;
29 import org.ofbiz.base.util.UtilHttp;
30 import org.ofbiz.entity.GenericDelegator;
31 import org.ofbiz.entity.GenericValue;
32 import org.ofbiz.product.catalog.CatalogWorker;
33 import org.ofbiz.product.store.ProductStoreWorker;
34 import org.ofbiz.service.LocalDispatcher;
35 import org.ofbiz.base.util.cache.UtilCache;
36
37 /**
38  * Product Config Worker class to reduce code in templates.
39  *
40  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
41  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
42  * @author <a HREF="mailto:tiz@sastau.it">Jacopo Cappellato</a>
43  *
44  */

45 public class ProductConfigWorker {
46     
47     public static final String JavaDoc module = ProductConfigWorker.class.getName();
48     public static final String JavaDoc resource = "ProductUiLabels";
49     public static final String JavaDoc SEPARATOR = "::"; // cache key separator
50

51     public static UtilCache productConfigCache = new UtilCache("product.config", true); // use soft reference to free up memory if needed
52

53     public static ProductConfigWrapper getProductConfigWrapper(String JavaDoc productId, String JavaDoc currencyUomId, HttpServletRequest JavaDoc request) {
54         ProductConfigWrapper configWrapper = null;
55         String JavaDoc catalogId = CatalogWorker.getCurrentCatalogId(request);
56         String JavaDoc webSiteId = CatalogWorker.getWebSiteId(request);
57         String JavaDoc productStoreId = ProductStoreWorker.getProductStoreId(request);
58         GenericValue autoUserLogin = (GenericValue)request.getSession().getAttribute("autoUserLogin");
59         try {
60             /* caching: there is one cache created, "product.config" Each product's config wrapper is cached with a key of
61              * productId::catalogId::webSiteId::currencyUomId, or whatever the SEPARATOR is defined above to be.
62              */

63             String JavaDoc cacheKey = productId + SEPARATOR + productStoreId + SEPARATOR + catalogId + SEPARATOR + webSiteId + SEPARATOR + currencyUomId;
64             if (!productConfigCache.containsKey(cacheKey)) {
65                 configWrapper = new ProductConfigWrapper((GenericDelegator)request.getAttribute("delegator"),
66                                                          (LocalDispatcher)request.getAttribute("dispatcher"),
67                                                          productId, productStoreId, catalogId, webSiteId,
68                                                          currencyUomId, UtilHttp.getLocale(request),
69                                                          autoUserLogin);
70                 productConfigCache.put(cacheKey, new ProductConfigWrapper(configWrapper));
71             } else {
72                 configWrapper = new ProductConfigWrapper((ProductConfigWrapper)productConfigCache.get(cacheKey));
73             }
74         } catch(ProductConfigWrapperException we) {
75             configWrapper = null;
76         } catch(Exception JavaDoc e) {
77             Debug.logWarning(e.getMessage(), module);
78         }
79         return configWrapper;
80     }
81     
82     public static void fillProductConfigWrapper(ProductConfigWrapper configWrapper, HttpServletRequest JavaDoc request) {
83         int numOfQuestions = configWrapper.getQuestions().size();
84         for (int k = 0; k < numOfQuestions; k++) {
85             String JavaDoc[] opts = request.getParameterValues("" + k);
86             if (opts == null) {
87                 continue;
88             }
89             for (int h = 0; h < opts.length; h++) {
90                 int cnt = -1;
91                 try {
92                     cnt = Integer.parseInt(opts[h]);
93                     configWrapper.setSelected(k, cnt);
94                 } catch(Exception JavaDoc e) {
95                     Debug.logWarning(e.getMessage(), module);
96                 }
97             }
98         }
99     }
100 }
101
102
Popular Tags