1 23 package org.ofbiz.product.config; 24 25 import java.io.IOException ; 26 import java.io.StringWriter ; 27 import java.io.Writer ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 33 import javax.servlet.http.HttpServletRequest ; 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 53 public class ProductConfigItemContentWrapper implements java.io.Serializable { 54 55 public static final String module = ProductConfigItemContentWrapper.class.getName(); 56 57 protected GenericValue productConfigItem; 58 protected Locale locale; 59 protected String mimeTypeId; 60 61 public static ProductConfigItemContentWrapper makeProductConfigItemContentWrapper(GenericValue productConfigItem, HttpServletRequest request) { 62 return new ProductConfigItemContentWrapper(productConfigItem, request); 63 } 64 65 public ProductConfigItemContentWrapper(GenericValue productConfigItem, Locale locale, String mimeTypeId) { 66 this.productConfigItem = productConfigItem; 67 this.locale = locale; 68 this.mimeTypeId = mimeTypeId; 69 } 70 71 public ProductConfigItemContentWrapper(GenericValue productConfigItem, HttpServletRequest request) { 72 this.productConfigItem = productConfigItem; 73 this.locale = UtilHttp.getLocale(request); 74 this.mimeTypeId = "text/html"; 75 } 76 77 public String get(String confItemContentTypeId) { 78 return getProductConfigItemContentAsText(productConfigItem, confItemContentTypeId, locale, mimeTypeId, productConfigItem.getDelegator()); 79 } 80 81 public static String getProductConfigItemContentAsText(GenericValue productConfigItem, String confItemContentTypeId, HttpServletRequest request) { 82 return getProductConfigItemContentAsText(productConfigItem, confItemContentTypeId, UtilHttp.getLocale(request), "text/html", productConfigItem.getDelegator()); 83 } 84 85 public static String getProductConfigItemContentAsText(GenericValue productConfigItem, String confItemContentTypeId, Locale locale) { 86 return getProductConfigItemContentAsText(productConfigItem, confItemContentTypeId, locale, null, null); 87 } 88 89 public static String getProductConfigItemContentAsText(GenericValue productConfigItem, String confItemContentTypeId, Locale locale, String mimeTypeId, GenericDelegator delegator) { 90 String candidateFieldName = ModelUtil.dbNameToVarName(confItemContentTypeId); 91 try { 92 Writer outWriter = new StringWriter (); 93 getProductConfigItemContentAsText(null, productConfigItem, confItemContentTypeId, locale, mimeTypeId, delegator, outWriter); 94 String 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 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 configItemId, GenericValue productConfigItem, String confItemContentTypeId, Locale locale, String mimeTypeId, GenericDelegator delegator, Writer outWriter) throws GeneralException, IOException { 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 candidateFieldName = ModelUtil.dbNameToVarName(confItemContentTypeId); 123 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 candidateValue = productConfigItem.getString(candidateFieldName); 131 if (UtilValidate.isNotEmpty(candidateValue)) { 132 outWriter.write(candidateValue); 133 return; 134 } 135 } 136 } 137 138 List 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 Map inContext = new HashMap (); 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 |