KickJava   Java API By Example, From Geeks To Geeks.

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


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

23 package org.ofbiz.product.config;
24
25 import java.io.IOException JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.GeneralException;
37 import org.ofbiz.base.util.UtilHttp;
38 import org.ofbiz.base.util.UtilMisc;
39 import org.ofbiz.base.util.UtilValidate;
40 import org.ofbiz.content.content.ContentWorker;
41 import org.ofbiz.entity.GenericDelegator;
42 import org.ofbiz.entity.GenericValue;
43 import org.ofbiz.entity.model.ModelEntity;
44 import org.ofbiz.entity.model.ModelUtil;
45 import org.ofbiz.entity.util.EntityUtil;
46
47 /**
48  * Product Config Item Content Worker: gets product content to display
49  *
50  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
51  * @author <a HREF="mailto:tiz@sastau.it">Jacopo Cappellato</a>
52  */

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