1 24 package org.ofbiz.product.category; 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.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 56 public class CategoryContentWrapper { 57 58 public static final String module = CategoryContentWrapper.class.getName(); 59 60 protected GenericValue productCategory; 61 protected Locale locale; 62 protected String mimeTypeId; 63 64 65 66 public static CategoryContentWrapper makeCategoryContentWrapper(GenericValue productCategory, HttpServletRequest request) { 67 return new CategoryContentWrapper(productCategory, request); 68 } 69 70 public CategoryContentWrapper(GenericValue productCategory, Locale locale, String mimeTypeId) { 71 this.productCategory = productCategory; 72 this.locale = locale; 73 this.mimeTypeId = mimeTypeId; 74 } 75 76 public CategoryContentWrapper(GenericValue productCategory, HttpServletRequest request) { 77 this.productCategory = productCategory; 78 this.locale = UtilHttp.getLocale(request); 79 this.mimeTypeId = "text/html"; 80 } 81 82 public String get(String prodCatContentTypeId) { 83 return getProductCategoryContentAsText(productCategory, prodCatContentTypeId, locale, mimeTypeId, productCategory.getDelegator()); 84 } 85 86 public static String getProductCategoryContentAsText(GenericValue productCategory, String prodCatContentTypeId, HttpServletRequest request) { 87 return getProductCategoryContentAsText(productCategory, prodCatContentTypeId, UtilHttp.getLocale(request), "text/html", productCategory.getDelegator()); 88 } 89 90 public static String getProductCategoryContentAsText(GenericValue productCategory, String prodCatContentTypeId, Locale locale) { 91 return getProductCategoryContentAsText(productCategory, prodCatContentTypeId, locale, null, null); 92 } 93 94 public static String getProductCategoryContentAsText(GenericValue productCategory, String prodCatContentTypeId, Locale locale, String mimeTypeId, GenericDelegator delegator) { 95 String candidateFieldName = ModelUtil.dbNameToVarName(prodCatContentTypeId); 96 try { 97 Writer outWriter = new StringWriter (); 98 getProductCategoryContentAsText(null, productCategory, prodCatContentTypeId, locale, mimeTypeId, delegator, outWriter); 99 String 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 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 productCategoryId, GenericValue productCategory, String prodCatContentTypeId, Locale locale, String mimeTypeId, GenericDelegator delegator, Writer outWriter) throws GeneralException, IOException { 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 candidateFieldName = ModelUtil.dbNameToVarName(prodCatContentTypeId); 128 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 candidateValue = productCategory.getString(candidateFieldName); 136 if (UtilValidate.isNotEmpty(candidateValue)) { 137 outWriter.write(candidateValue); 138 return; 139 } 140 } 141 } 142 143 List 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 Map inContext = new HashMap (); 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 |