001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.order.order;
019:
020: import java.io.IOException;
021: import java.io.StringWriter;
022: import java.io.Writer;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Locale;
026: import java.util.Map;
027:
028: import javax.servlet.http.HttpServletRequest;
029:
030: import org.ofbiz.base.util.Debug;
031: import org.ofbiz.base.util.GeneralException;
032: import org.ofbiz.base.util.UtilHttp;
033: import org.ofbiz.base.util.UtilMisc;
034: import org.ofbiz.base.util.UtilValidate;
035: import org.ofbiz.base.util.cache.UtilCache;
036: import org.ofbiz.content.content.ContentWorker;
037: import org.ofbiz.entity.GenericDelegator;
038: import org.ofbiz.entity.GenericValue;
039: import org.ofbiz.entity.util.EntityUtil;
040: import org.ofbiz.service.LocalDispatcher;
041:
042: /**
043: * Order Content Worker: gets order content to display
044: *
045: */
046: public class OrderContentWrapper {
047:
048: public static final String module = OrderContentWrapper.class
049: .getName();
050: public static final String SEPARATOR = "::"; // cache key separator
051:
052: public static UtilCache orderContentCache;
053:
054: public static OrderContentWrapper makeOrderContentWrapper(
055: GenericValue order, HttpServletRequest request) {
056: return new OrderContentWrapper(order, request);
057: }
058:
059: protected LocalDispatcher dispatcher;
060: protected GenericValue order;
061: protected Locale locale;
062: protected String mimeTypeId;
063:
064: public OrderContentWrapper(LocalDispatcher dispatcher,
065: GenericValue order, Locale locale, String mimeTypeId) {
066: this .dispatcher = dispatcher;
067: this .order = order;
068: this .locale = locale;
069: this .mimeTypeId = mimeTypeId;
070: if (orderContentCache == null) {
071: orderContentCache = new UtilCache("order.content", true); // use soft reference to free up memory if needed
072: }
073: }
074:
075: public OrderContentWrapper(GenericValue order,
076: HttpServletRequest request) {
077: this .dispatcher = (LocalDispatcher) request
078: .getAttribute("dispatcher");
079: this .order = order;
080: this .locale = UtilHttp.getLocale(request);
081: this .mimeTypeId = "text/html";
082: if (orderContentCache == null) {
083: orderContentCache = new UtilCache("order.content", true); // use soft reference to free up memory if needed
084: }
085: }
086:
087: public String get(String orderContentTypeId) {
088: return getOrderContentAsText(order, orderContentTypeId, locale,
089: mimeTypeId, order.getDelegator(), dispatcher);
090: }
091:
092: public static String getOrderContentAsText(GenericValue order,
093: String orderContentTypeId, HttpServletRequest request) {
094: LocalDispatcher dispatcher = (LocalDispatcher) request
095: .getAttribute("dispatcher");
096: return getOrderContentAsText(order, orderContentTypeId,
097: UtilHttp.getLocale(request), "text/html", order
098: .getDelegator(), dispatcher);
099: }
100:
101: public static String getOrderContentAsText(GenericValue order,
102: String orderContentTypeId, Locale locale,
103: LocalDispatcher dispatcher) {
104: return getOrderContentAsText(order, orderContentTypeId, locale,
105: null, null, dispatcher);
106: }
107:
108: public static String getOrderContentAsText(GenericValue order,
109: String orderContentTypeId, Locale locale,
110: String mimeTypeId, GenericDelegator delegator,
111: LocalDispatcher dispatcher) {
112: /* caching: there is one cache created, "order.content" Each order's content is cached with a key of
113: * contentTypeId::locale::mimeType::orderId::orderItemSeqId, or whatever the SEPARATOR is defined above to be.
114: */
115: String orderItemSeqId = (order.getEntityName().equals(
116: "OrderItem") ? order.getString("orderItemSeqId")
117: : "_NA_");
118:
119: String cacheKey = orderContentTypeId + SEPARATOR + locale
120: + SEPARATOR + mimeTypeId + SEPARATOR
121: + order.get("orderId") + SEPARATOR + orderItemSeqId;
122: try {
123: if (orderContentCache != null
124: && orderContentCache.get(cacheKey) != null) {
125: return (String) orderContentCache.get(cacheKey);
126: }
127:
128: Writer outWriter = new StringWriter();
129: getOrderContentAsText(null, null, order,
130: orderContentTypeId, locale, mimeTypeId, delegator,
131: dispatcher, outWriter);
132: String outString = outWriter.toString();
133: if (outString.length() > 0) {
134: if (orderContentCache != null) {
135: orderContentCache.put(cacheKey, outString);
136: }
137: }
138: return outString;
139:
140: } catch (GeneralException e) {
141: Debug
142: .logError(
143: e,
144: "Error rendering OrderContent, inserting empty String",
145: module);
146: return "";
147: } catch (IOException e) {
148: Debug
149: .logError(
150: e,
151: "Error rendering OrderContent, inserting empty String",
152: module);
153: return "";
154: }
155: }
156:
157: public static void getOrderContentAsText(String orderId,
158: String orderItemSeqId, GenericValue order,
159: String orderContentTypeId, Locale locale,
160: String mimeTypeId, GenericDelegator delegator,
161: LocalDispatcher dispatcher, Writer outWriter)
162: throws GeneralException, IOException {
163: if (orderId == null && order != null) {
164: orderId = order.getString("orderId");
165: }
166: if (orderItemSeqId == null && order != null) {
167: orderItemSeqId = (order.getEntityName().equals("OrderItem") ? order
168: .getString("orderItemSeqId")
169: : "_NA_");
170: }
171:
172: if (delegator == null && order != null) {
173: delegator = order.getDelegator();
174: }
175:
176: if (UtilValidate.isEmpty(mimeTypeId)) {
177: mimeTypeId = "text/html";
178: }
179:
180: List orderContentList = delegator.findByAndCache(
181: "OrderContent", UtilMisc.toMap("orderId", orderId,
182: "orderItemSeqId", orderItemSeqId,
183: "orderContentTypeId", orderContentTypeId),
184: UtilMisc.toList("-fromDate"));
185: orderContentList = EntityUtil.filterByDate(orderContentList);
186: GenericValue orderContent = EntityUtil
187: .getFirst(orderContentList);
188: if (orderContent != null) {
189: // when rendering the order content, always include the OrderHeader/OrderItem and OrderContent records that this comes from
190: Map inContext = new HashMap();
191: inContext.put("order", order);
192: inContext.put("orderContent", orderContent);
193: ContentWorker.renderContentAsText(dispatcher, delegator,
194: orderContent.getString("contentId"), outWriter,
195: inContext, locale, mimeTypeId, false);
196: }
197: }
198: }
|