[ofbiz-framework] branch trunk updated: Improved: Refactor CatalogWorker.java to remove duplicate code(OFBIZ-11696)

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

[ofbiz-framework] branch trunk updated: Improved: Refactor CatalogWorker.java to remove duplicate code(OFBIZ-11696)

Pawan Verma-2
This is an automated email from the ASF dual-hosted git repository.

pawan pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 6c34232  Improved: Refactor CatalogWorker.java to remove duplicate code(OFBIZ-11696)
6c34232 is described below

commit 6c342323a521e0b363447c4ad183cae2d6867266
Author: Pawan Verma <[hidden email]>
AuthorDate: Tue Jun 2 16:27:52 2020 +0530

    Improved: Refactor CatalogWorker.java to remove duplicate code(OFBIZ-11696)
---
 .../ofbiz/product/catalog/CatalogWorker.java       | 111 ++++++---------------
 1 file changed, 30 insertions(+), 81 deletions(-)

diff --git a/applications/product/src/main/java/org/apache/ofbiz/product/catalog/CatalogWorker.java b/applications/product/src/main/java/org/apache/ofbiz/product/catalog/CatalogWorker.java
index 5051d4f..7c9b491 100644
--- a/applications/product/src/main/java/org/apache/ofbiz/product/catalog/CatalogWorker.java
+++ b/applications/product/src/main/java/org/apache/ofbiz/product/catalog/CatalogWorker.java
@@ -262,22 +262,28 @@ public final class CatalogWorker {
         }
     }
 
+    public static String getProdCatalogCategoryId(Delegator delegator, String prodCatalogId, String prodCatalogCategoryTypeId) {
+        if (UtilValidate.isNotEmpty(prodCatalogId) && UtilValidate.isNotEmpty(prodCatalogCategoryTypeId)) {
+            GenericValue prodCatalogCategory = EntityUtil.getFirst(getProdCatalogCategories(delegator, prodCatalogId
+                                                            , prodCatalogCategoryTypeId));
+
+            if (prodCatalogCategory != null) {
+                return prodCatalogCategory.getString("productCategoryId");
+            }
+        }
+        return null;
+    }
+
     public static String getCatalogTopCategoryId(ServletRequest request) {
         return getCatalogTopCategoryId(request, getCurrentCatalogId(request));
     }
 
     public static String getCatalogTopCategoryId(ServletRequest request, String prodCatalogId) {
-        if (UtilValidate.isEmpty(prodCatalogId)) return null;
-
-        List<GenericValue> prodCatalogCategories = getProdCatalogCategories(request, prodCatalogId, "PCCT_BROWSE_ROOT");
-
-        if (UtilValidate.isNotEmpty(prodCatalogCategories)) {
-            GenericValue prodCatalogCategory = EntityUtil.getFirst(prodCatalogCategories);
+        return getCatalogTopCategoryId((Delegator) request.getAttribute("delegator"), prodCatalogId);
+    }
 
-            return prodCatalogCategory.getString("productCategoryId");
-        } else {
-            return null;
-        }
+    public static String getCatalogTopCategoryId(Delegator delegator, String prodCatalogId) {
+        return getProdCatalogCategoryId(delegator, prodCatalogId, "PCCT_BROWSE_ROOT");
     }
 
     public static String getCatalogSearchCategoryId(ServletRequest request) {
@@ -288,39 +294,15 @@ public final class CatalogWorker {
         return getCatalogSearchCategoryId((Delegator) request.getAttribute("delegator"), prodCatalogId);
     }
     public static String getCatalogSearchCategoryId(Delegator delegator, String prodCatalogId) {
-        if (UtilValidate.isEmpty(prodCatalogId)) return null;
-
-        List<GenericValue> prodCatalogCategories = getProdCatalogCategories(delegator, prodCatalogId, "PCCT_SEARCH");
-        if (UtilValidate.isNotEmpty(prodCatalogCategories)) {
-            GenericValue prodCatalogCategory = EntityUtil.getFirst(prodCatalogCategories);
-            return prodCatalogCategory.getString("productCategoryId");
-        } else {
-            return null;
-        }
+        return getProdCatalogCategoryId(delegator, prodCatalogId, "PCCT_SEARCH");
     }
 
     public static String getCatalogViewAllowCategoryId(Delegator delegator, String prodCatalogId) {
-        if (UtilValidate.isEmpty(prodCatalogId)) return null;
-
-        List<GenericValue> prodCatalogCategories = getProdCatalogCategories(delegator, prodCatalogId, "PCCT_VIEW_ALLW");
-        if (UtilValidate.isNotEmpty(prodCatalogCategories)) {
-            GenericValue prodCatalogCategory = EntityUtil.getFirst(prodCatalogCategories);
-            return prodCatalogCategory.getString("productCategoryId");
-        } else {
-            return null;
-        }
+        return getProdCatalogCategoryId(delegator, prodCatalogId, "PCCT_VIEW_ALLW");
     }
 
     public static String getCatalogPurchaseAllowCategoryId(Delegator delegator, String prodCatalogId) {
-        if (UtilValidate.isEmpty(prodCatalogId)) return null;
-
-        List<GenericValue> prodCatalogCategories = getProdCatalogCategories(delegator, prodCatalogId, "PCCT_PURCH_ALLW");
-        if (UtilValidate.isNotEmpty(prodCatalogCategories)) {
-            GenericValue prodCatalogCategory = EntityUtil.getFirst(prodCatalogCategories);
-            return prodCatalogCategory.getString("productCategoryId");
-        } else {
-            return null;
-        }
+        return getProdCatalogCategoryId(delegator, prodCatalogId, "PCCT_PURCH_ALLW");
     }
 
     public static String getCatalogPromotionsCategoryId(ServletRequest request) {
@@ -328,17 +310,10 @@ public final class CatalogWorker {
     }
 
     public static String getCatalogPromotionsCategoryId(ServletRequest request, String prodCatalogId) {
-        if (UtilValidate.isEmpty(prodCatalogId)) return null;
-
-        List<GenericValue> prodCatalogCategories = getProdCatalogCategories(request, prodCatalogId, "PCCT_PROMOTIONS");
-
-        if (UtilValidate.isNotEmpty(prodCatalogCategories)) {
-            GenericValue prodCatalogCategory = EntityUtil.getFirst(prodCatalogCategories);
-
-            return prodCatalogCategory.getString("productCategoryId");
-        } else {
-            return null;
-        }
+        return getCatalogPromotionsCategoryId((Delegator) request.getAttribute("delegator"), prodCatalogId);
+    }
+    public static String getCatalogPromotionsCategoryId(Delegator delegator, String prodCatalogId) {
+        return getProdCatalogCategoryId(delegator, prodCatalogId, "PCCT_PROMOTIONS");
     }
 
     public static boolean getCatalogQuickaddUse(ServletRequest request) {
@@ -366,17 +341,7 @@ public final class CatalogWorker {
     }
 
     public static String getCatalogQuickaddCategoryPrimary(ServletRequest request, String prodCatalogId) {
-        if (UtilValidate.isEmpty(prodCatalogId)) return null;
-
-        List<GenericValue> prodCatalogCategories = getProdCatalogCategories(request, prodCatalogId, "PCCT_QUICK_ADD");
-
-        if (UtilValidate.isNotEmpty(prodCatalogCategories)) {
-            GenericValue prodCatalogCategory = EntityUtil.getFirst(prodCatalogCategories);
-
-            return prodCatalogCategory.getString("productCategoryId");
-        } else {
-            return null;
-        }
+        return getProdCatalogCategoryId((Delegator) request.getAttribute("delegator"), prodCatalogId, "PCCT_QUICK_ADD");
     }
 
     public static Collection<String> getCatalogQuickaddCategories(ServletRequest request) {
@@ -386,30 +351,14 @@ public final class CatalogWorker {
     public static Collection<String> getCatalogQuickaddCategories(ServletRequest request, String prodCatalogId) {
         if (UtilValidate.isEmpty(prodCatalogId)) return null;
 
-        Collection<String> categoryIds = new LinkedList<>();
-
-        Collection<GenericValue> prodCatalogCategories = getProdCatalogCategories(request, prodCatalogId, "PCCT_QUICK_ADD");
-
-        if (UtilValidate.isNotEmpty(prodCatalogCategories)) {
-            for (GenericValue prodCatalogCategory: prodCatalogCategories) {
-                categoryIds.add(prodCatalogCategory.getString("productCategoryId"));
-            }
-        }
-
-        return categoryIds;
+        return EntityUtil.getFieldListFromEntityList(
+                getProdCatalogCategories(request, prodCatalogId, "PCCT_QUICK_ADD"),
+                "productCategoryId",
+                true
+        );
     }
 
     public static String getCatalogTopEbayCategoryId(ServletRequest request, String prodCatalogId) {
-        if (UtilValidate.isEmpty(prodCatalogId)) return null;
-
-        List<GenericValue> prodCatalogCategories = getProdCatalogCategories(request, prodCatalogId, "PCCT_EBAY_ROOT");
-
-        if (UtilValidate.isNotEmpty(prodCatalogCategories)) {
-            GenericValue prodCatalogCategory = EntityUtil.getFirst(prodCatalogCategories);
-
-            return prodCatalogCategory.getString("productCategoryId");
-        } else {
-            return null;
-        }
+        return getProdCatalogCategoryId((Delegator) request.getAttribute("delegator"), prodCatalogId, "PCCT_EBAY_ROOT");
     }
 }