svn commit: r821474 - /ofbiz/trunk/applications/content/src/org/ofbiz/content/content/ContentMapFacade.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r821474 - /ofbiz/trunk/applications/content/src/org/ofbiz/content/content/ContentMapFacade.java

jleroux@apache.org
Author: jleroux
Date: Sun Oct  4 07:53:56 2009
New Revision: 821474

URL: http://svn.apache.org/viewvc?rev=821474&view=rev
Log:
A patch from Patrick Antivackis "Improving the ContentMapFacade class in order to filter and sort results." (https://issues.apache.org/jira/browse/OFBIZ-2988) - OFBIZ-2988

Today's ContentMapFacade.class is mapped in the content application to the ${thisContent} variable in Freemarker templates.
Using {$thisContent.subcontent_all} return all the "subcontents" associated with thisContent sorting by -fromDate.
(in fact you usually use it like
<#list thisContent.subcontent_all as contentItem>
${contentItem}
</#list>
)
Using {$thisContent.subcontent.foo} return all the first "subcontent" associated with thisContent and mapped by foo mapKey (first sorted by -fromDate).
Unfortunatelly this facade don't bring any filter or sorting options.
I updated this class in order to allow to :
- sort the subcontents (both sucontent_all or subcontent.foo) by whatever field of the ContentAssocViewTo view. For this you need to write in the Freemarker template ${thisContent.setSortOrder(sortString)} before using the sucontent_all or subcontent .foo operations.
- filter the output of subcontent_all by mapKey. For this you need to write in the Freemarker template ${thisContent.setMapKeyFilter(mapKeyString)} before using the subcontent_all operation.
- filter the output of subcontent_all or subcontent.foo by statusId of the content. For this you need to write in the Freemarker template ${thisContent.setStatusFilter(CONTENT_STATUS)} before using the sbucontent_all or subcontent.foo operations.

Examples are given in the Jira issue

Modified:
    ofbiz/trunk/applications/content/src/org/ofbiz/content/content/ContentMapFacade.java

Modified: ofbiz/trunk/applications/content/src/org/ofbiz/content/content/ContentMapFacade.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/content/src/org/ofbiz/content/content/ContentMapFacade.java?rev=821474&r1=821473&r2=821474&view=diff
==============================================================================
--- ofbiz/trunk/applications/content/src/org/ofbiz/content/content/ContentMapFacade.java (original)
+++ ofbiz/trunk/applications/content/src/org/ofbiz/content/content/ContentMapFacade.java Sun Oct  4 07:53:56 2009
@@ -73,6 +73,9 @@
     protected boolean isDecorated = false;
 
     // internal objects
+    private String sortOrder="-fromDate";
+    private String mapKeyFilter="";
+    private String statusFilter="";
     private DataResource dataResource;
     private SubContent subContent;
     private MetaData metaData;
@@ -177,7 +180,33 @@
         Debug.logWarning("This method [entrySet()] is not implemented in ContentMapFacade", module);
         return null;
     }
+    
+    public void setSortOrder(Object obj) {
+        if (!(obj instanceof String)) {
+            Debug.logWarning("sortOrder parameters must be a string", module);
+            return;
+        }
+        this.sortOrder=(String) obj;
+        this.subContent.setSortOrder(obj);
+    }
+
+    public void setMapKeyFilter(Object obj) {
+        if (!(obj instanceof String)) {
+            Debug.logWarning("mapKeyFilter parameters must be a string", module);
+            return;
+        }
+        this.mapKeyFilter=(String) obj;
+    }
 
+    public void setStatusFilter(Object obj) {
+        if (!(obj instanceof String)) {
+            Debug.logWarning("statusFilter parameters must be a string", module);
+            return;
+        }
+        this.statusFilter=(String) obj;
+        this.subContent.setStatusFilter(obj);
+    }        
+    
     // implemented get method
     public Object get(Object obj) {
         if (!(obj instanceof String)) {
@@ -225,10 +254,19 @@
             List<ContentMapFacade> subContent = FastList.newInstance();
             List<GenericValue> subs = null;
             try {
+                Map expressions = FastMap.newInstance();
+                expressions.put("contentIdStart", contentId);
+                if(!this.mapKeyFilter.equals("")) {
+                    expressions.put("caMapKey", this.mapKeyFilter);
+                }
+                if(!this.statusFilter.equals("")) {
+                    expressions.put("statusId", this.statusFilter);
+                }
+
                 if (cache) {
-                    subs = delegator.findByAndCache("ContentAssoc", UtilMisc.toMap("contentId", contentId), UtilMisc.toList("-fromDate"));
+                 subs = delegator.findByAndCache("ContentAssocViewTo", expressions, UtilMisc.toList(this.sortOrder));
                 } else {
-                    subs = delegator.findByAnd("ContentAssoc", UtilMisc.toMap("contentId", contentId), UtilMisc.toList("-fromDate"));
+                 subs = delegator.findByAnd("ContentAssocViewTo", expressions, UtilMisc.toList(this.sortOrder));
                 }
             } catch (GenericEntityException e) {
                 Debug.logError(e, module);
@@ -237,7 +275,7 @@
                 subs = EntityUtil.filterByDate(subs);
 
                 for (GenericValue v: subs) {
-                    subContent.add(new ContentMapFacade(dispatcher, delegator, v.getString("contentIdTo"), context, locale, mimeType, cache));
+                    subContent.add(new ContentMapFacade(dispatcher, delegator, v.getString("contentId"), context, locale, mimeType, cache));
                 }
             }
             return subContent;
@@ -374,6 +412,8 @@
     }
 
     class SubContent extends AbstractInfo {
+     private String sortOrder="-fromDate";
+     private String statusFilter="";
         @Override
         public Object get(Object key) {
             if (!(key instanceof String)) {
@@ -388,10 +428,16 @@
             // key is the mapKey
             List subs = null;
             try {
+                Map expressions = FastMap.newInstance();
+                expressions.put("contentIdStart", contentId);
+                expressions.put("caMapKey", name);
+                if(!this.statusFilter.equals("")) {
+                    expressions.put("statusId", this.statusFilter);
+                }
                 if (cache) {
-                    subs = delegator.findByAndCache("ContentAssoc", UtilMisc.toMap("contentId", contentId, "mapKey", name), UtilMisc.toList("-fromDate"));
+                    subs = delegator.findByAndCache("ContentAssocViewTo", expressions, UtilMisc.toList(this.sortOrder));
                 } else {
-                    subs = delegator.findByAnd("ContentAssoc", UtilMisc.toMap("contentId", contentId, "mapKey", name), UtilMisc.toList("-fromDate"));
+                    subs = delegator.findByAnd("ContentAssocViewTo", expressions, UtilMisc.toList(this.sortOrder));
                 }
             } catch (GenericEntityException e) {
                 Debug.logError(e, module);
@@ -400,12 +446,26 @@
                 subs = EntityUtil.filterByDate(subs);
                 GenericValue v = EntityUtil.getFirst(subs);
                 if (v != null) {
-                    return new ContentMapFacade(dispatcher, delegator, v.getString("contentIdTo"), context, locale, mimeType, cache);
+                    return new ContentMapFacade(dispatcher, delegator, v.getString("contentId"), context, locale, mimeType, cache);
                 }
             }
 
             return null;
         }
+        public void setSortOrder(Object obj) {
+            if (!(obj instanceof String)) {
+                Debug.logWarning("sortOrder parameters must be a string", module);
+                return;
+            }
+            this.sortOrder=(String) obj;
+        }
+        public void setStatusFilter(Object obj) {
+            if (!(obj instanceof String)) {
+                Debug.logWarning("statusFilter parameters must be a string", module);
+                return;
+            }
+            this.statusFilter=(String) obj;
+        }  
     }
 
     class MetaData extends AbstractInfo {