Author: jleroux
Date: Sat Oct 31 05:39:13 2015
New Revision: 1711578

URL: http://svn.apache.org/viewvc?rev=1711578&view=rev
Log:
A modified patch from  Ritu Raj Lakhera for "Enhancement in the content 
wrappers" https://issues.apache.org/jira/browse/OFBIZ-6701

Some enhancements in the content wrapper JAVA classes. We used these 
enhancements in our project and observed good change in website performance. 

1. If content is empty string or null then method 'get*ContentAsText' are not 
caching the result. 
And thus every time this method is making SQL call even no content in database. 
Method should cache the empty string and null as result.
2. Overloaded method 'get*ContentAsText' is also using cache method of 
Delegator for '*Content' entity selection and passing 'true' to use cache for 
'*Worker.renderContentAsText' method. This approach is making unnecessary 
copies of cache for content.
        I Content wrapper cache
        II *Content entity cache
        III Content, Dataresource, ElectrocText and etc... entities 
cache(ContentWorker.renderContentAsText)

If I again call the 'get*ContentAsText' method then this will return the result 
from content wrapper cache. So there is no use of *Content, Content, 
Dataresource and ElectrocText cache. And these cache also filling the memory of 
application.



jleroux: Ritu Raj initially provided the changes only for the product content 
wrapper saying "We also applied the same logic for category content wrapper." I 
asked him if he could provide a patch for all content wrappers. I completed the 
parts for Category and Workeffort content wrapper which missed some lines.

Modified:
    
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderContentWrapper.java
    
ofbiz/trunk/applications/party/src/org/ofbiz/party/content/PartyContentWrapper.java
    
ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryContentWrapper.java
    
ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigItemContentWrapper.java
    
ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductContentWrapper.java
    
ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductPromoContentWrapper.java
    
ofbiz/trunk/applications/workeffort/src/org/ofbiz/workeffort/content/WorkEffortContentWrapper.java

Modified: 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderContentWrapper.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderContentWrapper.java?rev=1711578&r1=1711577&r2=1711578&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderContentWrapper.java
 (original)
+++ 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderContentWrapper.java
 Sat Oct 31 05:39:13 2015
@@ -104,10 +104,14 @@ public class OrderContentWrapper impleme
             }
 
             Writer outWriter = new StringWriter();
-            getOrderContentAsText(null, null, order, orderContentTypeId, 
locale, mimeTypeId, delegator, dispatcher, outWriter);
+            getOrderContentAsText(null, null, order, orderContentTypeId, 
locale, mimeTypeId, delegator, dispatcher, outWriter, false);
             String outString = outWriter.toString();
-            if (outString.length() > 0) {
-                outString = orderContentCache.putIfAbsentAndGet(cacheKey, 
encoder.encode(outString));
+            if (UtilValidate.isEmpty(outString)) {
+                outString = outString == null? "" : outString;
+            }
+            outString = encoder.encode(outString);
+            if (orderContentCache != null) {
+                orderContentCache.put(cacheKey, outString);
             }
             return outString;
 
@@ -121,6 +125,10 @@ public class OrderContentWrapper impleme
     }
 
     public static void getOrderContentAsText(String orderId, String 
orderItemSeqId, GenericValue order, String orderContentTypeId, Locale locale, 
String mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer 
outWriter) throws GeneralException, IOException {
+        getOrderContentAsText(orderId, orderItemSeqId, order, 
orderContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter, true);
+    }
+
+    public static void getOrderContentAsText(String orderId, String 
orderItemSeqId, GenericValue order, String orderContentTypeId, Locale locale, 
String mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer 
outWriter, boolean cache) throws GeneralException, IOException {
         if (orderId == null && order != null) {
             orderId = order.getString("orderId");
         }
@@ -141,13 +149,13 @@ public class OrderContentWrapper impleme
                         "orderItemSeqId", orderItemSeqId,
                         "orderContentTypeId", orderContentTypeId)
                 .orderBy("-fromDate")
-                .cache().filterByDate().queryFirst();
+                .cache(cache).filterByDate().queryFirst();
         if (orderContent != null) {
             // when rendering the order content, always include the 
OrderHeader/OrderItem and OrderContent records that this comes from
             Map<String, Object> inContext = new HashMap<String, Object>();
             inContext.put("order", order);
             inContext.put("orderContent", orderContent);
-            ContentWorker.renderContentAsText(dispatcher, delegator, 
orderContent.getString("contentId"), outWriter, inContext, locale, mimeTypeId, 
null, null, false);
+            ContentWorker.renderContentAsText(dispatcher, delegator, 
orderContent.getString("contentId"), outWriter, inContext, locale, mimeTypeId, 
null, null, cache);
         }
     }
 }

Modified: 
ofbiz/trunk/applications/party/src/org/ofbiz/party/content/PartyContentWrapper.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/party/src/org/ofbiz/party/content/PartyContentWrapper.java?rev=1711578&r1=1711577&r2=1711578&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/party/src/org/ofbiz/party/content/PartyContentWrapper.java
 (original)
+++ 
ofbiz/trunk/applications/party/src/org/ofbiz/party/content/PartyContentWrapper.java
 Sat Oct 31 05:39:13 2015
@@ -153,15 +153,18 @@ public class PartyContentWrapper impleme
             }
 
             Writer outWriter = new StringWriter();
-            getPartyContentAsText(contentId, party.getString("partyId"), 
party, partyContentTypeId, locale, mimeTypeId, delegator, dispatcher, 
outWriter);
+            getPartyContentAsText(contentId, party.getString("partyId"), 
party, partyContentTypeId, locale, mimeTypeId, delegator, dispatcher, 
outWriter, false);
 
             String outString = outWriter.toString();
-            if (outString.length() > 0) {
-                return partyContentCache.putIfAbsentAndGet(cacheKey, 
encoder.encode(outString));
-            } else {
-                String candidateOut = 
party.getModelEntity().isField(candidateFieldName) ? 
party.getString(candidateFieldName): "";
-                return candidateOut == null? "" : encoder.encode(candidateOut);
+            if (UtilValidate.isEmpty(outString)) {
+                outString = party.getModelEntity().isField(candidateFieldName) 
? party.getString(candidateFieldName): "";
+                outString = outString == null? "" : outString;
             }
+            outString = encoder.encode(outString);
+            if (partyContentCache != null) {
+                partyContentCache.put(cacheKey, outString);
+            }
+            return outString;
         } catch (GeneralException e) {
             Debug.logError(e, "Error rendering PartyContent, inserting empty 
String", module);
             String candidateOut = 
party.getModelEntity().isField(candidateFieldName) ? 
party.getString(candidateFieldName): "";
@@ -174,6 +177,10 @@ public class PartyContentWrapper impleme
     }
 
     public static void getPartyContentAsText(String contentId, String partyId, 
GenericValue party, String partyContentTypeId, Locale locale, String 
mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer outWriter) 
throws GeneralException, IOException {
+        getPartyContentAsText(contentId, partyId, party, partyContentTypeId, 
locale, mimeTypeId, delegator, dispatcher, outWriter, true);
+    }
+
+    public static void getPartyContentAsText(String contentId, String partyId, 
GenericValue party, String partyContentTypeId, Locale locale, String 
mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer outWriter, 
boolean cache) throws GeneralException, IOException {
         if (partyId == null && party != null) {
             partyId = party.getString("partyId");
         }
@@ -227,7 +234,7 @@ public class PartyContentWrapper impleme
         // otherwise a content field
         GenericValue partyContent;
         if (contentId != null) {
-            partyContent = 
EntityQuery.use(delegator).from("PartyContent").where("partyId", partyId, 
"contentId", contentId).cache().queryOne();
+            partyContent = 
EntityQuery.use(delegator).from("PartyContent").where("partyId", partyId, 
"contentId", contentId).cache(cache).queryOne();
         } else {
             partyContent = getFirstPartyContentByType(partyId, party, 
partyContentTypeId, delegator);
         }
@@ -236,7 +243,7 @@ public class PartyContentWrapper impleme
             Map<String, Object> inContext = new HashMap<String, Object>();
             inContext.put("party", party);
             inContext.put("partyContent", partyContent);
-            ContentWorker.renderContentAsText(dispatcher, delegator, 
partyContent.getString("contentId"), outWriter, inContext, locale, mimeTypeId, 
null, null, false);
+            ContentWorker.renderContentAsText(dispatcher, delegator, 
partyContent.getString("contentId"), outWriter, inContext, locale, mimeTypeId, 
null, null, cache);
         }
     }
 

Modified: 
ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryContentWrapper.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryContentWrapper.java?rev=1711578&r1=1711577&r2=1711578&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryContentWrapper.java
 (original)
+++ 
ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryContentWrapper.java
 Sat Oct 31 05:39:13 2015
@@ -102,14 +102,17 @@ public class CategoryContentWrapper impl
                 return cachedValue;
             }
             Writer outWriter = new StringWriter();
-            getProductCategoryContentAsText(null, productCategory, 
prodCatContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter);
+            getProductCategoryContentAsText(null, productCategory, 
prodCatContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter, 
false);
             String outString = outWriter.toString();
-            if (outString.length() > 0) {
-                outString = categoryContentCache.putIfAbsentAndGet(cacheKey, 
encoder.encode(outString));
-                return encoder.encode(outString);
-            } else {
-                return null;
+            if (UtilValidate.isEmpty(outString)) {
+                outString = 
productCategory.getModelEntity().isField(candidateFieldName) ? 
productCategory.getString(candidateFieldName): "";
+                outString = outString == null? "" : outString;
             }
+            outString = encoder.encode(outString);
+            if (categoryContentCache != null) {
+                categoryContentCache.put(cacheKey, outString);
+            }
+            return outString;
         } catch (GeneralException e) {
             Debug.logError(e, "Error rendering CategoryContent, inserting 
empty String", module);
             return productCategory.getString(candidateFieldName);
@@ -120,6 +123,10 @@ public class CategoryContentWrapper impl
     }
 
     public static void getProductCategoryContentAsText(String 
productCategoryId, GenericValue productCategory, String prodCatContentTypeId, 
Locale locale, String mimeTypeId, Delegator delegator, LocalDispatcher 
dispatcher, Writer outWriter) throws GeneralException, IOException {
+        getProductCategoryContentAsText(null, productCategory, 
prodCatContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter, 
true);
+    }
+
+    public static void getProductCategoryContentAsText(String 
productCategoryId, GenericValue productCategory, String prodCatContentTypeId, 
Locale locale, String mimeTypeId, Delegator delegator, LocalDispatcher 
dispatcher, Writer outWriter, boolean cache) throws GeneralException, 
IOException {
         if (productCategoryId == null && productCategory != null) {
             productCategoryId = productCategory.getString("productCategoryId");
         }
@@ -151,7 +158,7 @@ public class CategoryContentWrapper impl
             }
         }
 
-        List<GenericValue> categoryContentList = 
EntityQuery.use(delegator).from("ProductCategoryContent").where("productCategoryId",
 productCategoryId, "prodCatContentTypeId", 
prodCatContentTypeId).orderBy("-fromDate").cache(true).queryList();
+        List<GenericValue> categoryContentList = 
EntityQuery.use(delegator).from("ProductCategoryContent").where("productCategoryId",
 productCategoryId, "prodCatContentTypeId", 
prodCatContentTypeId).orderBy("-fromDate").cache(cache).queryList();
         categoryContentList = EntityUtil.filterByDate(categoryContentList);
         
         GenericValue categoryContent = null;
@@ -160,7 +167,7 @@ public class CategoryContentWrapper impl
         if ( sessionLocale == null ) sessionLocale = fallbackLocale;
         // look up all content found for locale
         for( GenericValue currentContent: categoryContentList ) {
-            GenericValue content = 
EntityQuery.use(delegator).from("Content").where("contentId", 
currentContent.getString("contentId")).cache().queryOne();
+            GenericValue content = 
EntityQuery.use(delegator).from("Content").where("contentId", 
currentContent.getString("contentId")).cache(cache).queryOne();
             if ( sessionLocale.equals(content.getString("localeString")) ) {
               // valid locale found
               categoryContent = currentContent;
@@ -175,7 +182,7 @@ public class CategoryContentWrapper impl
             Map<String, Object> inContext = new HashMap<String, Object>();
             inContext.put("productCategory", productCategory);
             inContext.put("categoryContent", categoryContent);
-            ContentWorker.renderContentAsText(dispatcher, delegator, 
categoryContent.getString("contentId"), outWriter, inContext, locale, 
mimeTypeId, null, null, true);
+            ContentWorker.renderContentAsText(dispatcher, delegator, 
categoryContent.getString("contentId"), outWriter, inContext, locale, 
mimeTypeId, null, null, cache);
         }
     }
 }

Modified: 
ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigItemContentWrapper.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigItemContentWrapper.java?rev=1711578&r1=1711577&r2=1711578&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigItemContentWrapper.java
 (original)
+++ 
ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigItemContentWrapper.java
 Sat Oct 31 05:39:13 2015
@@ -125,15 +125,17 @@ public class ProductConfigItemContentWra
                 return cachedValue;
             }
             Writer outWriter = new StringWriter();
-            getProductConfigItemContentAsText(null, productConfigItem, 
confItemContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter);
+            getProductConfigItemContentAsText(null, productConfigItem, 
confItemContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter, 
false);
             String outString = outWriter.toString();
-            if (outString.length() > 0) {
-                outString = configItemContentCache.putIfAbsentAndGet(cacheKey, 
encoder.encode(outString));
-                return encoder.encode(outString);
-            } else {
-                String candidateOut = 
productConfigItem.getModelEntity().isField(candidateFieldName) ? 
productConfigItem.getString(candidateFieldName): "";
-                return candidateOut == null? "" : encoder.encode(candidateOut);
+            if (UtilValidate.isEmpty(outString)) {
+                outString = 
productConfigItem.getModelEntity().isField(candidateFieldName) ? 
productConfigItem.getString(candidateFieldName): "";
+                outString = outString == null? "" : outString;
             }
+            outString = encoder.encode(outString);
+            if (configItemContentCache != null) {
+                configItemContentCache.put(cacheKey, outString);
+            }
+            return outString;
         } catch (GeneralException e) {
             Debug.logError(e, "Error rendering ProdConfItemContent, inserting 
empty String", module);
             String candidateOut = 
productConfigItem.getModelEntity().isField(candidateFieldName) ? 
productConfigItem.getString(candidateFieldName): "";
@@ -146,6 +148,10 @@ public class ProductConfigItemContentWra
     }
 
     public static void getProductConfigItemContentAsText(String configItemId, 
GenericValue productConfigItem, String confItemContentTypeId, Locale locale, 
String mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer 
outWriter) throws GeneralException, IOException {
+        getProductConfigItemContentAsText(configItemId, productConfigItem, 
confItemContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter, 
true);
+    }
+
+    public static void getProductConfigItemContentAsText(String configItemId, 
GenericValue productConfigItem, String confItemContentTypeId, Locale locale, 
String mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer 
outWriter, boolean cache) throws GeneralException, IOException {
         if (configItemId == null && productConfigItem != null) {
             configItemId = productConfigItem.getString("configItemId");
         }
@@ -177,7 +183,7 @@ public class ProductConfigItemContentWra
         GenericValue productConfigItemContent = 
EntityQuery.use(delegator).from("ProdConfItemContent")
                 .where("configItemId", configItemId, "confItemContentTypeId", 
confItemContentTypeId)
                 .orderBy("-fromDate")
-                .cache(true)
+                .cache(cache)
                 .filterByDate()
                 .queryFirst();
         if (productConfigItemContent != null) {
@@ -185,7 +191,7 @@ public class ProductConfigItemContentWra
             Map<String, Object> inContext = new HashMap<String, Object>();
             inContext.put("productConfigItem", productConfigItem);
             inContext.put("productConfigItemContent", 
productConfigItemContent);
-            ContentWorker.renderContentAsText(dispatcher, delegator, 
productConfigItemContent.getString("contentId"), outWriter, inContext, locale, 
mimeTypeId, null, null, false);
+            ContentWorker.renderContentAsText(dispatcher, delegator, 
productConfigItemContent.getString("contentId"), outWriter, inContext, locale, 
mimeTypeId, null, null, cache);
         }
     }
 }

Modified: 
ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductContentWrapper.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductContentWrapper.java?rev=1711578&r1=1711577&r2=1711578&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductContentWrapper.java
 (original)
+++ 
ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductContentWrapper.java
 Sat Oct 31 05:39:13 2015
@@ -115,14 +115,17 @@ public class ProductContentWrapper imple
             }
 
             Writer outWriter = new StringWriter();
-            getProductContentAsText(null, product, productContentTypeId, 
locale, mimeTypeId, partyId, roleTypeId, delegator, dispatcher, outWriter);
+            getProductContentAsText(null, product, productContentTypeId, 
locale, mimeTypeId, partyId, roleTypeId, delegator, dispatcher, outWriter, 
false);
             String outString = outWriter.toString();
-            if (outString.length() > 0) {
-                return productContentCache.putIfAbsentAndGet(cacheKey, 
encoder.encode(outString));
-            } else {
-                String candidateOut = 
product.getModelEntity().isField(candidateFieldName) ? 
product.getString(candidateFieldName): "";
-                return candidateOut == null? "" : encoder.encode(candidateOut);
+            if (UtilValidate.isEmpty(outString)) {
+                outString = 
product.getModelEntity().isField(candidateFieldName) ? 
product.getString(candidateFieldName): "";
+                outString = outString == null? "" : outString;
             }
+            outString = encoder.encode(outString);
+            if (productContentCache != null) {
+                productContentCache.put(cacheKey, outString);
+            }
+            return outString;
         } catch (GeneralException e) {
             Debug.logError(e, "Error rendering ProductContent, inserting empty 
String", module);
             String candidateOut = 
product.getModelEntity().isField(candidateFieldName) ? 
product.getString(candidateFieldName): "";
@@ -135,6 +138,10 @@ public class ProductContentWrapper imple
     }
 
     public static void getProductContentAsText(String productId, GenericValue 
product, String productContentTypeId, Locale locale, String mimeTypeId, String 
partyId, String roleTypeId, Delegator delegator, LocalDispatcher dispatcher, 
Writer outWriter) throws GeneralException, IOException {
+        getProductContentAsText(productId, product, productContentTypeId, 
locale, mimeTypeId, partyId, roleTypeId, delegator, dispatcher, outWriter, 
true);
+    }
+
+    public static void getProductContentAsText(String productId, GenericValue 
product, String productContentTypeId, Locale locale, String mimeTypeId, String 
partyId, String roleTypeId, Delegator delegator, LocalDispatcher dispatcher, 
Writer outWriter, boolean cache) throws GeneralException, IOException {
         if (productId == null && product != null) {
             productId = product.getString("productId");
         }
@@ -179,11 +186,11 @@ public class ProductContentWrapper imple
                 }
         }
 
-        List<GenericValue> productContentList = 
EntityQuery.use(delegator).from("ProductContent").where("productId", productId, 
"productContentTypeId", 
productContentTypeId).orderBy("-fromDate").cache(true).filterByDate().queryList();
+        List<GenericValue> productContentList = 
EntityQuery.use(delegator).from("ProductContent").where("productId", productId, 
"productContentTypeId", 
productContentTypeId).orderBy("-fromDate").cache(cache).filterByDate().queryList();
         if (UtilValidate.isEmpty(productContentList) && 
("Y".equals(product.getString("isVariant")))) {
             GenericValue parent = ProductWorker.getParentProduct(productId, 
delegator);
             if (UtilValidate.isNotEmpty(parent)) {
-                productContentList = 
EntityQuery.use(delegator).from("ProductContent").where("productId", 
parent.get("productId"), "productContentTypeId", 
productContentTypeId).orderBy("-fromDate").cache(true).filterByDate().queryList();
+                productContentList = 
EntityQuery.use(delegator).from("ProductContent").where("productId", 
parent.get("productId"), "productContentTypeId", 
productContentTypeId).orderBy("-fromDate").cache(cache).filterByDate().queryList();
             }
         }
         GenericValue productContent = EntityUtil.getFirst(productContentList);
@@ -192,7 +199,7 @@ public class ProductContentWrapper imple
             Map<String, Object> inContext = new HashMap<String, Object>();
             inContext.put("product", product);
             inContext.put("productContent", productContent);
-            ContentWorker.renderContentAsText(dispatcher, delegator, 
productContent.getString("contentId"), outWriter, inContext, locale, 
mimeTypeId, partyId, roleTypeId, true);
+            ContentWorker.renderContentAsText(dispatcher, delegator, 
productContent.getString("contentId"), outWriter, inContext, locale, 
mimeTypeId, partyId, roleTypeId, cache);
         }
     }
 }

Modified: 
ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductPromoContentWrapper.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductPromoContentWrapper.java?rev=1711578&r1=1711577&r2=1711578&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductPromoContentWrapper.java
 (original)
+++ 
ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductPromoContentWrapper.java
 Sat Oct 31 05:39:13 2015
@@ -118,14 +118,17 @@ public class ProductPromoContentWrapper
             }
 
             Writer outWriter = new StringWriter();
-            getProductPromoContentAsText(null, productPromo, 
productPromoContentTypeId, locale, mimeTypeId, partyId, roleTypeId, delegator, 
dispatcher, outWriter);
+            getProductPromoContentAsText(null, productPromo, 
productPromoContentTypeId, locale, mimeTypeId, partyId, roleTypeId, delegator, 
dispatcher, outWriter, false);
             String outString = outWriter.toString();
-            if (outString.length() > 0) {
-                return productPromoContentCache.putIfAbsentAndGet(cacheKey, 
encoder.encode(outString));
-            } else {
-                String candidateOut = 
productPromo.getModelEntity().isField(candidateFieldName) ? 
productPromo.getString(candidateFieldName): "";
-                return candidateOut == null? "" : encoder.encode(candidateOut);
+            if (UtilValidate.isEmpty(outString)) {
+                outString = 
productPromo.getModelEntity().isField(candidateFieldName) ? 
productPromo.getString(candidateFieldName): "";
+                outString = outString == null? "" : outString;
             }
+            outString = encoder.encode(outString);
+            if (productPromoContentCache != null) {
+                productPromoContentCache.put(cacheKey, outString);
+            }
+            return outString;
         } catch (GeneralException e) {
             Debug.logError(e, "Error rendering ProductPromoContent, inserting 
empty String", module);
             String candidateOut = 
productPromo.getModelEntity().isField(candidateFieldName) ? 
productPromo.getString(candidateFieldName): "";
@@ -138,6 +141,10 @@ public class ProductPromoContentWrapper
     }
 
     public static void getProductPromoContentAsText(String productPromoId, 
GenericValue productPromo, String productPromoContentTypeId, Locale locale, 
String mimeTypeId, String partyId, String roleTypeId, Delegator delegator, 
LocalDispatcher dispatcher, Writer outWriter) throws GeneralException, 
IOException {
+        getProductPromoContentAsText(productPromoId, productPromo, 
productPromoContentTypeId, locale, mimeTypeId, partyId, roleTypeId, delegator, 
dispatcher, outWriter, true);
+    }
+
+    public static void getProductPromoContentAsText(String productPromoId, 
GenericValue productPromo, String productPromoContentTypeId, Locale locale, 
String mimeTypeId, String partyId, String roleTypeId, Delegator delegator, 
LocalDispatcher dispatcher, Writer outWriter, boolean cache) throws 
GeneralException, IOException {
         if (UtilValidate.isEmpty(productPromoId) && 
UtilValidate.isNotEmpty(productPromo)) {
             productPromoId = productPromo.getString("productPromoId");
         }
@@ -173,7 +180,7 @@ public class ProductPromoContentWrapper
         exprs.add(EntityCondition.makeCondition("productPromoId", 
EntityOperator.EQUALS, productPromoId));
         exprs.add(EntityCondition.makeCondition("productPromoContentTypeId", 
EntityOperator.EQUALS, productPromoContentTypeId));
 
-        List<GenericValue> productPromoContentList = 
EntityQuery.use(delegator).from("ProductPromoContent").where(EntityCondition.makeCondition(exprs,
 EntityOperator.AND)).orderBy("-fromDate").cache(true).queryList();
+        List<GenericValue> productPromoContentList = 
EntityQuery.use(delegator).from("ProductPromoContent").where(EntityCondition.makeCondition(exprs,
 EntityOperator.AND)).orderBy("-fromDate").cache(cache).queryList();
         GenericValue productPromoContent = null;
         if (UtilValidate.isNotEmpty(productPromoContentList)) {
             productPromoContent = 
EntityUtil.getFirst(EntityUtil.filterByDate(productPromoContentList));
@@ -184,7 +191,7 @@ public class ProductPromoContentWrapper
             Map<String, Object> inContext = new HashMap<String, Object>();
             inContext.put("productPromo", productPromo);
             inContext.put("productPromoContent", productPromoContent);
-            ContentWorker.renderContentAsText(dispatcher, delegator, 
productPromoContent.getString("contentId"), outWriter, inContext, locale, 
mimeTypeId, partyId, roleTypeId, true);
+            ContentWorker.renderContentAsText(dispatcher, delegator, 
productPromoContent.getString("contentId"), outWriter, inContext, locale, 
mimeTypeId, partyId, roleTypeId, cache);
         }
     }
 }

Modified: 
ofbiz/trunk/applications/workeffort/src/org/ofbiz/workeffort/content/WorkEffortContentWrapper.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/workeffort/src/org/ofbiz/workeffort/content/WorkEffortContentWrapper.java?rev=1711578&r1=1711577&r2=1711578&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/workeffort/src/org/ofbiz/workeffort/content/WorkEffortContentWrapper.java
 (original)
+++ 
ofbiz/trunk/applications/workeffort/src/org/ofbiz/workeffort/content/WorkEffortContentWrapper.java
 Sat Oct 31 05:39:13 2015
@@ -95,7 +95,7 @@ public class WorkEffortContentWrapper im
      * @return String containing the contentId
      */
     public String getContentId(String contentTypeId) {
-        GenericValue workEffortContent = getFirstWorkEffortContentByType(null, 
workEffort, contentTypeId, workEffort.getDelegator());
+        GenericValue workEffortContent = getFirstWorkEffortContentByType(null, 
workEffort, contentTypeId, workEffort.getDelegator(), true);
         if (workEffortContent != null) {
             return workEffortContent.getString("contentId");
         } else {
@@ -109,7 +109,7 @@ public class WorkEffortContentWrapper im
      * @return String containing the name of the content record
      */
     public String getContentName(String contentTypeId) {
-        GenericValue workEffortContent = getFirstWorkEffortContentByType(null, 
workEffort, contentTypeId, workEffort.getDelegator());
+        GenericValue workEffortContent = getFirstWorkEffortContentByType(null, 
workEffort, contentTypeId, workEffort.getDelegator(), true);
         if (workEffortContent != null) {
             GenericValue content;
             try {
@@ -133,7 +133,7 @@ public class WorkEffortContentWrapper im
      * @return Timestamp of the fromDate field for this content type
      */
     public Timestamp getFromDate(String contentTypeId) {
-        GenericValue workEffortContent = getFirstWorkEffortContentByType(null, 
workEffort, contentTypeId, workEffort.getDelegator());
+        GenericValue workEffortContent = getFirstWorkEffortContentByType(null, 
workEffort, contentTypeId, workEffort.getDelegator(), true);
         if (workEffortContent != null) {
             return workEffortContent.getTimestamp("fromDate");
         } else {
@@ -142,7 +142,7 @@ public class WorkEffortContentWrapper im
     }
 
     public String getDataResourceId(String contentTypeId) {
-        GenericValue workEffortContent = getFirstWorkEffortContentByType(null, 
workEffort, contentTypeId, workEffort.getDelegator());
+        GenericValue workEffortContent = getFirstWorkEffortContentByType(null, 
workEffort, contentTypeId, workEffort.getDelegator(), true);
         if (workEffortContent != null) {
             GenericValue content;
             try {
@@ -248,14 +248,17 @@ public class WorkEffortContentWrapper im
             }
 
             Writer outWriter = new StringWriter();
-            getWorkEffortContentAsText(contentId, null, workEffort, 
workEffortContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter);
+            getWorkEffortContentAsText(contentId, null, workEffort, 
workEffortContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter, 
false);
             String outString = outWriter.toString();
-            if (outString.length() > 0) {
-                return workEffortContentCache.putIfAbsentAndGet(cacheKey, 
encoder.encode(outString));
-            } else {
-                String candidateOut = 
workEffort.getModelEntity().isField(candidateFieldName) ? 
workEffort.getString(candidateFieldName): "";
-                return candidateOut == null? "" : encoder.encode(candidateOut);
+            if (UtilValidate.isEmpty(outString)) {
+                outString = 
workEffort.getModelEntity().isField(candidateFieldName) ? 
workEffort.getString(candidateFieldName): "";
+                outString = outString == null? "" : outString;
+            }
+            outString = encoder.encode(outString);
+            if (workEffortContentCache != null) {
+                workEffortContentCache.put(cacheKey, outString);
             }
+            return outString;
         } catch (GeneralException e) {
             Debug.logError(e, "Error rendering WorkEffortContent, inserting 
empty String", module);
             String candidateOut = 
workEffort.getModelEntity().isField(candidateFieldName) ? 
workEffort.getString(candidateFieldName): "";
@@ -268,6 +271,10 @@ public class WorkEffortContentWrapper im
     }
 
     public static void getWorkEffortContentAsText(String contentId, String 
workEffortId, GenericValue workEffort, String workEffortContentTypeId, Locale 
locale, String mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, 
Writer outWriter) throws GeneralException, IOException {
+        getWorkEffortContentAsText(contentId, null, workEffort, 
workEffortContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter, 
true);
+    }
+
+    public static void getWorkEffortContentAsText(String contentId, String 
workEffortId, GenericValue workEffort, String workEffortContentTypeId, Locale 
locale, String mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, 
Writer outWriter, boolean cache) throws GeneralException, IOException {
         if (workEffortId == null && workEffort != null) {
             workEffortId = workEffort.getString("workEffortId");
         }
@@ -303,9 +310,9 @@ public class WorkEffortContentWrapper im
         // otherwise check content record
         GenericValue workEffortContent;
         if (contentId != null) {
-            workEffortContent = 
EntityQuery.use(delegator).from("WorkEffortContent").where("workEffortId", 
workEffortId, "contentId", contentId).cache().queryOne();
+            workEffortContent = 
EntityQuery.use(delegator).from("WorkEffortContent").where("workEffortId", 
workEffortId, "contentId", contentId).cache(cache).queryOne();
         } else {
-            workEffortContent = getFirstWorkEffortContentByType(workEffortId, 
workEffort, workEffortContentTypeId, delegator);
+            workEffortContent = getFirstWorkEffortContentByType(workEffortId, 
workEffort, workEffortContentTypeId, delegator, cache);
         }
         if (workEffortContent != null) {
             // when rendering the product content, always include the Product 
and ProductContent records that this comes from
@@ -339,7 +346,7 @@ public class WorkEffortContentWrapper im
         return contentList;
     }
 
-    public static GenericValue getFirstWorkEffortContentByType(String 
workEffortId, GenericValue workEffort, String workEffortContentTypeId, 
Delegator delegator) {
+    public static GenericValue getFirstWorkEffortContentByType(String 
workEffortId, GenericValue workEffort, String workEffortContentTypeId, 
Delegator delegator, boolean cache) {
         if (workEffortId == null && workEffort != null) {
             workEffortId = workEffort.getString("workEffortId");
         }
@@ -358,7 +365,7 @@ public class WorkEffortContentWrapper im
                                     .where("workEffortId", workEffortId, 
"workEffortContentTypeId", workEffortContentTypeId)
                                     .orderBy("-fromDate")
                                     .filterByDate()
-                                    .cache(true)
+                                    .cache(cache)
                                     .queryFirst();
         } catch (GeneralException e) {
             Debug.logError(e, module);


Reply via email to