KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > product > category > CategoryContentWrapper


1 /*
2  * $Id: CategoryContentWrapper.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.category;
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.content.content.ContentWorker;
42 import org.ofbiz.entity.GenericDelegator;
43 import org.ofbiz.entity.GenericValue;
44 import org.ofbiz.entity.model.ModelEntity;
45 import org.ofbiz.entity.model.ModelUtil;
46 import org.ofbiz.entity.util.EntityUtil;
47
48 /**
49  * Category Content Worker: gets category content to display
50  *
51  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
52  * @author <a HREF="mailto:arukala@gmx.de">Arukala</a>
53  * @version $Rev: 5462 $
54  * @since 3.0
55  */

56 public class CategoryContentWrapper {
57     
58     public static final String JavaDoc module = CategoryContentWrapper.class.getName();
59     
60     protected GenericValue productCategory;
61     protected Locale JavaDoc locale;
62     protected String JavaDoc mimeTypeId;
63
64
65     
66     public static CategoryContentWrapper makeCategoryContentWrapper(GenericValue productCategory, HttpServletRequest JavaDoc request) {
67         return new CategoryContentWrapper(productCategory, request);
68     }
69     
70     public CategoryContentWrapper(GenericValue productCategory, Locale JavaDoc locale, String JavaDoc mimeTypeId) {
71         this.productCategory = productCategory;
72         this.locale = locale;
73         this.mimeTypeId = mimeTypeId;
74     }
75     
76     public CategoryContentWrapper(GenericValue productCategory, HttpServletRequest JavaDoc request) {
77         this.productCategory = productCategory;
78         this.locale = UtilHttp.getLocale(request);
79         this.mimeTypeId = "text/html";
80     }
81     
82     public String JavaDoc get(String JavaDoc prodCatContentTypeId) {
83         return getProductCategoryContentAsText(productCategory, prodCatContentTypeId, locale, mimeTypeId, productCategory.getDelegator());
84     }
85     
86     public static String JavaDoc getProductCategoryContentAsText(GenericValue productCategory, String JavaDoc prodCatContentTypeId, HttpServletRequest JavaDoc request) {
87         return getProductCategoryContentAsText(productCategory, prodCatContentTypeId, UtilHttp.getLocale(request), "text/html", productCategory.getDelegator());
88     }
89
90     public static String JavaDoc getProductCategoryContentAsText(GenericValue productCategory, String JavaDoc prodCatContentTypeId, Locale JavaDoc locale) {
91         return getProductCategoryContentAsText(productCategory, prodCatContentTypeId, locale, null, null);
92     }
93     
94     public static String JavaDoc getProductCategoryContentAsText(GenericValue productCategory, String JavaDoc prodCatContentTypeId, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericDelegator delegator) {
95         String JavaDoc candidateFieldName = ModelUtil.dbNameToVarName(prodCatContentTypeId);
96         try {
97             Writer JavaDoc outWriter = new StringWriter JavaDoc();
98             getProductCategoryContentAsText(null, productCategory, prodCatContentTypeId, locale, mimeTypeId, delegator, outWriter);
99             String JavaDoc outString = outWriter.toString();
100             if (outString.length() > 0) {
101                 return outString;
102             } else {
103                 return null;
104             }
105         } catch (GeneralException e) {
106             Debug.logError(e, "Error rendering CategoryContent, inserting empty String", module);
107             return productCategory.getString(candidateFieldName);
108         } catch (IOException JavaDoc e) {
109             Debug.logError(e, "Error rendering CategoryContent, inserting empty String", module);
110             return productCategory.getString(candidateFieldName);
111         }
112     }
113     
114     public static void getProductCategoryContentAsText(String JavaDoc productCategoryId, GenericValue productCategory, String JavaDoc prodCatContentTypeId, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericDelegator delegator, Writer JavaDoc outWriter) throws GeneralException, IOException JavaDoc {
115         if (productCategoryId == null && productCategory != null) {
116             productCategoryId = productCategory.getString("productCategoryId");
117         }
118         
119         if (delegator == null && productCategory != null) {
120             delegator = productCategory.getDelegator();
121         }
122         
123         if (UtilValidate.isEmpty(mimeTypeId)) {
124             mimeTypeId = "text/html";
125         }
126         
127         String JavaDoc candidateFieldName = ModelUtil.dbNameToVarName(prodCatContentTypeId);
128 // Debug.logInfo("candidateFieldName=" + candidateFieldName, module);
129
ModelEntity categoryModel = delegator.getModelEntity("ProductCategory");
130         if (categoryModel.isField(candidateFieldName)) {
131             if (productCategory == null) {
132                 productCategory = delegator.findByPrimaryKeyCache("ProductCategory", UtilMisc.toMap("productCategoryId", productCategoryId));
133             }
134             if (productCategory != null) {
135                 String JavaDoc candidateValue = productCategory.getString(candidateFieldName);
136                 if (UtilValidate.isNotEmpty(candidateValue)) {
137                     outWriter.write(candidateValue);
138                     return;
139                 }
140             }
141         }
142         
143         List JavaDoc categoryContentList = delegator.findByAndCache("ProductCategoryContent", UtilMisc.toMap("productCategoryId", productCategoryId, "prodCatContentTypeId", prodCatContentTypeId), UtilMisc.toList("-fromDate"));
144         categoryContentList = EntityUtil.filterByDate(categoryContentList);
145         GenericValue categoryContent = EntityUtil.getFirst(categoryContentList);
146         if (categoryContent != null) {
147             // when rendering the category content, always include the Product Category and ProductCategoryContent records that this comes from
148
Map JavaDoc inContext = new HashMap JavaDoc();
149             inContext.put("productCategory", productCategory);
150             inContext.put("categoryContent", categoryContent);
151             ContentWorker.renderContentAsText(delegator, categoryContent.getString("contentId"), outWriter, inContext, null, locale, mimeTypeId);
152         }
153     }
154 }
155
156
Popular Tags