KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > product > product > ProductContentWrapper


1 /*
2  * $Id: ProductContentWrapper.java 5467 2005-08-06 08:11:15Z jonesde $
3  *
4  * Copyright (c) 2003-2005 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.product;
25
26 import java.io.IOException JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.GeneralException;
38 import org.ofbiz.base.util.UtilHttp;
39 import org.ofbiz.base.util.UtilMisc;
40 import org.ofbiz.base.util.UtilValidate;
41 import org.ofbiz.base.util.cache.UtilCache;
42 import org.ofbiz.content.content.ContentWorker;
43 import org.ofbiz.entity.GenericDelegator;
44 import org.ofbiz.entity.GenericValue;
45 import org.ofbiz.entity.model.ModelEntity;
46 import org.ofbiz.entity.model.ModelUtil;
47 import org.ofbiz.entity.util.EntityUtil;
48
49 /**
50  * Product Content Worker: gets product content to display
51  *
52  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
53  * @version $Rev: 5467 $
54  * @since 3.0
55  */

56 public class ProductContentWrapper {
57     
58     public static final String JavaDoc module = ProductContentWrapper.class.getName();
59     public static final String JavaDoc SEPARATOR = "::"; // cache key separator
60

61     public static UtilCache productContentCache = new UtilCache("product.content.rendered", true);
62     
63     public static ProductContentWrapper makeProductContentWrapper(GenericValue product, HttpServletRequest JavaDoc request) {
64         return new ProductContentWrapper(product, request);
65     }
66     
67     protected GenericValue product;
68     protected Locale JavaDoc locale;
69     protected String JavaDoc mimeTypeId;
70     
71     public ProductContentWrapper(GenericValue product, Locale JavaDoc locale, String JavaDoc mimeTypeId) {
72         this.product = product;
73         this.locale = locale;
74         this.mimeTypeId = mimeTypeId;
75     }
76     
77     public ProductContentWrapper(GenericValue product, HttpServletRequest JavaDoc request) {
78         this.product = product;
79         this.locale = UtilHttp.getLocale(request);
80         this.mimeTypeId = "text/html";
81     }
82     
83     public String JavaDoc get(String JavaDoc productContentTypeId) {
84         return getProductContentAsText(product, productContentTypeId, locale, mimeTypeId, product.getDelegator());
85     }
86     
87     public static String JavaDoc getProductContentAsText(GenericValue product, String JavaDoc productContentTypeId, HttpServletRequest JavaDoc request) {
88         return getProductContentAsText(product, productContentTypeId, UtilHttp.getLocale(request), "text/html", product.getDelegator());
89     }
90
91     public static String JavaDoc getProductContentAsText(GenericValue product, String JavaDoc productContentTypeId, Locale JavaDoc locale) {
92         return getProductContentAsText(product, productContentTypeId, locale, null, null);
93     }
94     
95     public static String JavaDoc getProductContentAsText(GenericValue product, String JavaDoc productContentTypeId, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericDelegator delegator) {
96         if (product == null) {
97             return null;
98         }
99         
100         String JavaDoc candidateFieldName = ModelUtil.dbNameToVarName(productContentTypeId);
101         /* caching: there is one cache created, "product.content" Each product's content is cached with a key of
102          * contentTypeId::locale::mimeType::productId, or whatever the SEPARATOR is defined above to be.
103          */

104         String JavaDoc cacheKey = productContentTypeId + SEPARATOR + locale + SEPARATOR + mimeTypeId + SEPARATOR + product.get("productId");
105         try {
106             if (productContentCache.get(cacheKey) != null) {
107                 return (String JavaDoc) productContentCache.get(cacheKey);
108             }
109             
110             Writer JavaDoc outWriter = new StringWriter JavaDoc();
111             getProductContentAsText(null, product, productContentTypeId, locale, mimeTypeId, delegator, outWriter);
112             String JavaDoc outString = outWriter.toString();
113             if (outString.length() > 0) {
114                 if (productContentCache != null) {
115                     productContentCache.put(cacheKey, outString);
116                 }
117                 return outString;
118             } else {
119                 String JavaDoc candidateOut = product.getModelEntity().isField(candidateFieldName) ? product.getString(candidateFieldName): "";
120                 return candidateOut == null? "" : candidateOut;
121             }
122         } catch (GeneralException e) {
123             Debug.logError(e, "Error rendering ProductContent, inserting empty String", module);
124             String JavaDoc candidateOut = product.getModelEntity().isField(candidateFieldName) ? product.getString(candidateFieldName): "";
125             return candidateOut == null? "" : candidateOut;
126         } catch (IOException JavaDoc e) {
127             Debug.logError(e, "Error rendering ProductContent, inserting empty String", module);
128             String JavaDoc candidateOut = product.getModelEntity().isField(candidateFieldName) ? product.getString(candidateFieldName): "";
129             return candidateOut == null? "" : candidateOut;
130         }
131     }
132     
133     public static void getProductContentAsText(String JavaDoc productId, GenericValue product, String JavaDoc productContentTypeId, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericDelegator delegator, Writer JavaDoc outWriter) throws GeneralException, IOException JavaDoc {
134         if (productId == null && product != null) {
135             productId = product.getString("productId");
136         }
137         
138         if (delegator == null && product != null) {
139             delegator = product.getDelegator();
140         }
141         
142         if (UtilValidate.isEmpty(mimeTypeId)) {
143             mimeTypeId = "text/html";
144         }
145         
146         String JavaDoc candidateFieldName = ModelUtil.dbNameToVarName(productContentTypeId);
147         //Debug.logInfo("candidateFieldName=" + candidateFieldName, module);
148
ModelEntity productModel = delegator.getModelEntity("Product");
149         if (productModel.isField(candidateFieldName)) {
150             if (product == null) {
151                 product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", productId));
152             }
153             if (product != null) {
154                 String JavaDoc candidateValue = product.getString(candidateFieldName);
155                 if (UtilValidate.isNotEmpty(candidateValue)) {
156                     outWriter.write(candidateValue);
157                     return;
158                 }
159             }
160         }
161         
162         List JavaDoc productContentList = delegator.findByAndCache("ProductContent", UtilMisc.toMap("productId", productId, "productContentTypeId", productContentTypeId), UtilMisc.toList("-fromDate"));
163         productContentList = EntityUtil.filterByDate(productContentList);
164         GenericValue productContent = EntityUtil.getFirst(productContentList);
165         if (productContent != null) {
166             // when rendering the product content, always include the Product and ProductContent records that this comes from
167
Map JavaDoc inContext = new HashMap JavaDoc();
168             inContext.put("product", product);
169             inContext.put("productContent", productContent);
170             ContentWorker.renderContentAsText(delegator, productContent.getString("contentId"), outWriter, inContext, null, locale, mimeTypeId);
171         }
172     }
173 }
174
Popular Tags