1 24 package org.ofbiz.order.order; 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.util.EntityUtil; 46 47 55 public class OrderContentWrapper { 56 57 public static final String module = OrderContentWrapper.class.getName(); 58 public static final String SEPARATOR = "::"; 60 public static UtilCache orderContentCache; 61 62 public static OrderContentWrapper makeOrderContentWrapper(GenericValue order, HttpServletRequest request) { 63 return new OrderContentWrapper(order, request); 64 } 65 66 protected GenericValue order; 67 protected Locale locale; 68 protected String mimeTypeId; 69 70 public OrderContentWrapper(GenericValue order, Locale locale, String mimeTypeId) { 71 this.order = order; 72 this.locale = locale; 73 this.mimeTypeId = mimeTypeId; 74 if (orderContentCache == null) { 75 orderContentCache = new UtilCache("order.content", true); } 77 } 78 79 public OrderContentWrapper(GenericValue order, HttpServletRequest request) { 80 this.order = order; 81 this.locale = UtilHttp.getLocale(request); 82 this.mimeTypeId = "text/html"; 83 if (orderContentCache == null) { 84 orderContentCache = new UtilCache("order.content", true); } 86 } 87 88 public String get(String orderContentTypeId) { 89 return getOrderContentAsText(order, orderContentTypeId, locale, mimeTypeId, order.getDelegator()); 90 } 91 92 public static String getOrderContentAsText(GenericValue order, String orderContentTypeId, HttpServletRequest request) { 93 return getOrderContentAsText(order, orderContentTypeId, UtilHttp.getLocale(request), "text/html", order.getDelegator()); 94 } 95 96 public static String getOrderContentAsText(GenericValue order, String orderContentTypeId, Locale locale) { 97 return getOrderContentAsText(order, orderContentTypeId, locale, null, null); 98 } 99 100 public static String getOrderContentAsText(GenericValue order, String orderContentTypeId, Locale locale, String mimeTypeId, GenericDelegator delegator) { 101 104 String orderItemSeqId = (order.getEntityName().equals("OrderItem")? order.getString("orderItemSeqId"): "_NA_"); 105 106 String cacheKey = orderContentTypeId + SEPARATOR + locale + SEPARATOR + mimeTypeId + SEPARATOR + order.get("orderId") + SEPARATOR + orderItemSeqId; 107 try { 108 if (orderContentCache != null && orderContentCache.get(cacheKey) != null) { 109 return (String ) orderContentCache.get(cacheKey); 110 } 111 112 Writer outWriter = new StringWriter (); 113 getOrderContentAsText(null, null, order, orderContentTypeId, locale, mimeTypeId, delegator, outWriter); 114 String outString = outWriter.toString(); 115 if (outString.length() > 0) { 116 if (orderContentCache != null) { 117 orderContentCache.put(cacheKey, outString); 118 } 119 } 120 return outString; 121 122 } catch (GeneralException e) { 123 Debug.logError(e, "Error rendering OrderContent, inserting empty String", module); 124 return ""; 125 } catch (IOException e) { 126 Debug.logError(e, "Error rendering OrderContent, inserting empty String", module); 127 return ""; 128 } 129 } 130 131 public static void getOrderContentAsText(String orderId, String orderItemSeqId, GenericValue order, String orderContentTypeId, Locale locale, String mimeTypeId, GenericDelegator delegator, Writer outWriter) throws GeneralException, IOException { 132 if (orderId == null && order != null) { 133 orderId = order.getString("orderId"); 134 } 135 if (orderItemSeqId == null && order != null) { 136 orderItemSeqId = (order.getEntityName().equals("OrderItem")? order.getString("orderItemSeqId"): "_NA_"); 137 } 138 139 if (delegator == null && order != null) { 140 delegator = order.getDelegator(); 141 } 142 143 if (UtilValidate.isEmpty(mimeTypeId)) { 144 mimeTypeId = "text/html"; 145 } 146 147 List orderContentList = delegator.findByAndCache("OrderContent", UtilMisc.toMap("orderId", orderId, "orderItemSeqId", orderItemSeqId, "orderContentTypeId", orderContentTypeId), UtilMisc.toList("-fromDate")); 148 orderContentList = EntityUtil.filterByDate(orderContentList); 149 GenericValue orderContent = EntityUtil.getFirst(orderContentList); 150 if (orderContent != null) { 151 Map inContext = new HashMap (); 153 inContext.put("order", order); 154 inContext.put("orderContent", orderContent); 155 ContentWorker.renderContentAsText(delegator, orderContent.getString("contentId"), outWriter, inContext, null, locale, mimeTypeId); 156 } 157 } 158 } 159 160 | Popular Tags |