KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > order > order > OrderContentWrapper


1 /*
2  * $Id: OrderContentWrapper.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.order.order;
25
26 import java.io.IOException JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
48  * Order Content Worker: gets order content to display
49  *
50  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
51  * @author <a HREF="mailto:tiz@sastau.it">Jacopo Cappellato</a>
52  * @version $Rev: 5462 $
53  * @since 3.0
54  */

55 public class OrderContentWrapper {
56     
57     public static final String JavaDoc module = OrderContentWrapper.class.getName();
58     public static final String JavaDoc SEPARATOR = "::"; // cache key separator
59

60     public static UtilCache orderContentCache;
61     
62     public static OrderContentWrapper makeOrderContentWrapper(GenericValue order, HttpServletRequest JavaDoc request) {
63         return new OrderContentWrapper(order, request);
64     }
65     
66     protected GenericValue order;
67     protected Locale JavaDoc locale;
68     protected String JavaDoc mimeTypeId;
69     
70     public OrderContentWrapper(GenericValue order, Locale JavaDoc locale, String JavaDoc mimeTypeId) {
71         this.order = order;
72         this.locale = locale;
73         this.mimeTypeId = mimeTypeId;
74         if (orderContentCache == null) {
75             orderContentCache = new UtilCache("order.content", true); // use soft reference to free up memory if needed
76
}
77     }
78     
79     public OrderContentWrapper(GenericValue order, HttpServletRequest JavaDoc 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); // use soft reference to free up memory if needed
85
}
86     }
87     
88     public String JavaDoc get(String JavaDoc orderContentTypeId) {
89         return getOrderContentAsText(order, orderContentTypeId, locale, mimeTypeId, order.getDelegator());
90     }
91     
92     public static String JavaDoc getOrderContentAsText(GenericValue order, String JavaDoc orderContentTypeId, HttpServletRequest JavaDoc request) {
93         return getOrderContentAsText(order, orderContentTypeId, UtilHttp.getLocale(request), "text/html", order.getDelegator());
94     }
95
96     public static String JavaDoc getOrderContentAsText(GenericValue order, String JavaDoc orderContentTypeId, Locale JavaDoc locale) {
97         return getOrderContentAsText(order, orderContentTypeId, locale, null, null);
98     }
99     
100     public static String JavaDoc getOrderContentAsText(GenericValue order, String JavaDoc orderContentTypeId, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericDelegator delegator) {
101         /* caching: there is one cache created, "order.content" Each order's content is cached with a key of
102          * contentTypeId::locale::mimeType::orderId::orderItemSeqId, or whatever the SEPARATOR is defined above to be.
103          */

104         String JavaDoc orderItemSeqId = (order.getEntityName().equals("OrderItem")? order.getString("orderItemSeqId"): "_NA_");
105         
106         String JavaDoc 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 JavaDoc) orderContentCache.get(cacheKey);
110             }
111             
112             Writer JavaDoc outWriter = new StringWriter JavaDoc();
113             getOrderContentAsText(null, null, order, orderContentTypeId, locale, mimeTypeId, delegator, outWriter);
114             String JavaDoc 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 JavaDoc e) {
126             Debug.logError(e, "Error rendering OrderContent, inserting empty String", module);
127             return "";
128         }
129     }
130     
131     public static void getOrderContentAsText(String JavaDoc orderId, String JavaDoc orderItemSeqId, GenericValue order, String JavaDoc orderContentTypeId, Locale JavaDoc locale, String JavaDoc mimeTypeId, GenericDelegator delegator, Writer JavaDoc outWriter) throws GeneralException, IOException JavaDoc {
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 JavaDoc 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             // when rendering the order content, always include the OrderHeader/OrderItem and OrderContent records that this comes from
152
Map JavaDoc inContext = new HashMap JavaDoc();
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