1 24 package org.ofbiz.product.product; 25 26 import java.io.IOException ; 27 import java.io.StringWriter ; 28 import java.io.Writer ; 29 import java.util.HashMap ; 30 import java.util.List ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 34 import javax.servlet.http.HttpServletRequest ; 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 56 public class ProductContentWrapper { 57 58 public static final String module = ProductContentWrapper.class.getName(); 59 public static final String SEPARATOR = "::"; 61 public static UtilCache productContentCache = new UtilCache("product.content.rendered", true); 62 63 public static ProductContentWrapper makeProductContentWrapper(GenericValue product, HttpServletRequest request) { 64 return new ProductContentWrapper(product, request); 65 } 66 67 protected GenericValue product; 68 protected Locale locale; 69 protected String mimeTypeId; 70 71 public ProductContentWrapper(GenericValue product, Locale locale, String mimeTypeId) { 72 this.product = product; 73 this.locale = locale; 74 this.mimeTypeId = mimeTypeId; 75 } 76 77 public ProductContentWrapper(GenericValue product, HttpServletRequest request) { 78 this.product = product; 79 this.locale = UtilHttp.getLocale(request); 80 this.mimeTypeId = "text/html"; 81 } 82 83 public String get(String productContentTypeId) { 84 return getProductContentAsText(product, productContentTypeId, locale, mimeTypeId, product.getDelegator()); 85 } 86 87 public static String getProductContentAsText(GenericValue product, String productContentTypeId, HttpServletRequest request) { 88 return getProductContentAsText(product, productContentTypeId, UtilHttp.getLocale(request), "text/html", product.getDelegator()); 89 } 90 91 public static String getProductContentAsText(GenericValue product, String productContentTypeId, Locale locale) { 92 return getProductContentAsText(product, productContentTypeId, locale, null, null); 93 } 94 95 public static String getProductContentAsText(GenericValue product, String productContentTypeId, Locale locale, String mimeTypeId, GenericDelegator delegator) { 96 if (product == null) { 97 return null; 98 } 99 100 String candidateFieldName = ModelUtil.dbNameToVarName(productContentTypeId); 101 104 String cacheKey = productContentTypeId + SEPARATOR + locale + SEPARATOR + mimeTypeId + SEPARATOR + product.get("productId"); 105 try { 106 if (productContentCache.get(cacheKey) != null) { 107 return (String ) productContentCache.get(cacheKey); 108 } 109 110 Writer outWriter = new StringWriter (); 111 getProductContentAsText(null, product, productContentTypeId, locale, mimeTypeId, delegator, outWriter); 112 String 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 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 candidateOut = product.getModelEntity().isField(candidateFieldName) ? product.getString(candidateFieldName): ""; 125 return candidateOut == null? "" : candidateOut; 126 } catch (IOException e) { 127 Debug.logError(e, "Error rendering ProductContent, inserting empty String", module); 128 String candidateOut = product.getModelEntity().isField(candidateFieldName) ? product.getString(candidateFieldName): ""; 129 return candidateOut == null? "" : candidateOut; 130 } 131 } 132 133 public static void getProductContentAsText(String productId, GenericValue product, String productContentTypeId, Locale locale, String mimeTypeId, GenericDelegator delegator, Writer outWriter) throws GeneralException, IOException { 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 candidateFieldName = ModelUtil.dbNameToVarName(productContentTypeId); 147 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 candidateValue = product.getString(candidateFieldName); 155 if (UtilValidate.isNotEmpty(candidateValue)) { 156 outWriter.write(candidateValue); 157 return; 158 } 159 } 160 } 161 162 List 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 Map inContext = new HashMap (); 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 |