You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by jl...@apache.org on 2008/11/01 18:19:33 UTC

svn commit: r709726 - in /ofbiz/trunk: applications/product/servicedef/ applications/product/src/org/ofbiz/product/product/ specialpurpose/hhfacility/src/org/ofbiz/hhfacility/ specialpurpose/pos/src/org/ofbiz/pos/

Author: jleroux
Date: Sat Nov  1 10:19:32 2008
New Revision: 709726

URL: http://svn.apache.org/viewvc?rev=709726&view=rev
Log:
A patch from Nicolas Malin "Refactoring product search by id" (https://issues.apache.org/jira/browse/OFBIZ-2027) - OFBIZ-2027

Modified:
    ofbiz/trunk/applications/product/servicedef/services.xml
    ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductServices.java
    ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java
    ofbiz/trunk/specialpurpose/hhfacility/src/org/ofbiz/hhfacility/FacilityServices.java
    ofbiz/trunk/specialpurpose/pos/src/org/ofbiz/pos/PosTransaction.java

Modified: ofbiz/trunk/applications/product/servicedef/services.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/servicedef/services.xml?rev=709726&r1=709725&r2=709726&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/servicedef/services.xml (original)
+++ ofbiz/trunk/applications/product/servicedef/services.xml Sat Nov  1 10:19:32 2008
@@ -203,6 +203,17 @@
         <auto-attributes mode="IN" include="pk" optional="false"/>
         <auto-attributes mode="IN" include="nonpk" optional="true"/>
     </service>
+    
+    <service name="findProductsById" engine="java"  auth="true"
+            location="org.ofbiz.product.ProductServices" invoke="findProductByGoodIdentification">
+        <description>Find the productId corresponding to a reference and a reference type</description>
+        <attribute type="String" mode="IN" name="idToFind" optional="false"/>
+        <attribute type="String" mode="IN" name="goodIdentificationTypeId" optional="true"/>
+        <attribute type="String" mode="IN" name="searchProductFirst" optional="true"/>
+        <attribute type="String" mode="IN" name="searchAllId" optional="true"/>
+        <attribute type="GenericValue" mode="OUT" name="product" optional="true"/>
+        <attribute type="List" mode="OUT" name="productsFound" optional="true"/>
+    </service>
 
     <!-- Product Association Services -->
 

Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductServices.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductServices.java?rev=709726&r1=709725&r2=709726&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductServices.java (original)
+++ ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductServices.java Sat Nov  1 10:19:32 2008
@@ -1101,5 +1101,46 @@
         }
     	   return ServiceUtil.returnSuccess();
     }
+    
+    /**
+     * Finds productId(s) corresponding to a product reference, productId or a GoodIdentification idValue
+     * @param dctx
+     * @param context
+     * @param context.productId use to search with productId or goodIdentification.idValue 
+     * @return a GenericValue with a productId and a List of complementary productId found
+     */
+    public static Map<String, Object> findProductById(DispatchContext ctx, Map<String, Object> context) { 
+        GenericDelegator delegator = ctx.getDelegator();
+        String idToFind = (String) context.get("idToFind");
+        String goodIdentificationTypeId = (String) context.get("goodIdentificationTypeId");
+        String searchProductFirstContext = (String) context.get("searchProductFirst");
+        String searchAllIdContext = (String) context.get("searchAllId");
+        
+        boolean searchProductFirst = UtilValidate.isNotEmpty(searchProductFirstContext) && "N".equals(searchProductFirstContext) ? false : true;
+        boolean searchAllId = UtilValidate.isNotEmpty(searchAllIdContext)&& "Y".equals(searchAllIdContext) ? true : false;
+        
+        GenericValue product = null;
+        List<GenericValue> productsFound = null;
+        try {
+            productsFound = ProductWorker.findProductsById(delegator, idToFind, goodIdentificationTypeId, searchProductFirst, searchAllId);   
+        } catch (GenericEntityException e) {
+            Debug.logError(e, module);
+            ServiceUtil.returnError(e.getMessage());
+        }
+                
+        if (UtilValidate.isNotEmpty(productsFound)) {          
+            LinkedList<GenericValue> productsList = new LinkedList<GenericValue>();
+            // gets the first productId of the List
+            product = EntityUtil.getFirst(productsFound);
+            // remove this productId
+            productsFound.remove(0);
+        }
+        
+        Map<String, Object> result = ServiceUtil.returnSuccess();
+        result.put("product", product);
+        result.put("productsList", productsFound);
+                               
+        return result;
+    }
 }
 

Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java?rev=709726&r1=709725&r2=709726&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java (original)
+++ ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java Sat Nov  1 10:19:32 2008
@@ -850,30 +850,64 @@
         return null;
     }
     
-    public static String findProductId(GenericDelegator delegator, String idToFind, String goodIdentificationTypeId) throws GenericEntityException {
-        // first lookup and see if this is the Product PK
-        GenericValue product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", idToFind));
-        String productId = null;
-
-        if (product == null) {
-            // no product record found; no check good identification
-            Map goodIdLookup = UtilMisc.toMap("idValue", idToFind);
-            if (UtilValidate.isNotEmpty(goodIdentificationTypeId)) {
-                goodIdLookup.put("goodIdentificationTypeId", goodIdentificationTypeId);
-            }
-            List goodIds = delegator.findByAndCache("GoodIdentification", goodIdLookup, UtilMisc.toList("-createdStamp"));
+    
+    /**
+     * Generic service to find product by id.
+     * By default return the product find by productId 
+     * but you can pass searchProductFirst at false if you want search in goodIdentification before 
+     * or pass searchAllId at true to find all product with this id (product.productId and goodIdentification.idValue) 
+     * @param delegator
+     * @param idToFind
+     * @param goodIdentificationTypeId
+     * @param searchProductFirst
+     * @param searchAllId
+     * @return
+     * @throws GenericEntityException
+     */
+    public static List<GenericValue> findProductsById( GenericDelegator delegator, 
+            String idToFind, String goodIdentificationTypeId,
+            boolean searchProductFirst, boolean searchAllId) throws GenericEntityException {
+        
+        if (Debug.infoOn()) Debug.logInfo("Analyze goodIdentification: entered id = " + idToFind + ", goodIdentificationTypeId = " + goodIdentificationTypeId, module);
+        
+        GenericValue product = null;
+        List<GenericValue> productsFound = null;
+        
+        // 1) look if the idToFind given is a real productId
+        if (searchProductFirst) {
+            product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", idToFind));
+        }
 
-            // sorted by createdStamp; so pull out the most current entry
-            GenericValue lastGoodId = EntityUtil.getFirst(goodIds);
-            if (lastGoodId != null) {
-                productId = lastGoodId.getString("productId");
+        if (searchAllId || (searchProductFirst && UtilValidate.isEmpty(product))) {
+            // 2) Retrieve product in GoodIdentification
+            Map<String, String> conditions = UtilMisc.toMap("idValue", idToFind);
+            if (UtilValidate.isNotEmpty(goodIdentificationTypeId)) {
+                conditions.put("goodIdentificationTypeId", goodIdentificationTypeId);
             }
-        } else {
-            productId = product.getString("productId");
+            productsFound = delegator.findByAndCache("GoodIdentificationAndProduct", conditions, UtilMisc.toList("productId"));
+        }
+        
+        if (! searchProductFirst) {
+            product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", idToFind));
+        }
+        
+        if (UtilValidate.isNotEmpty(product)){
+            if (UtilValidate.isNotEmpty(productsFound)) productsFound.add(product);
+            else productsFound = UtilMisc.toList(product);
         }
+        if (Debug.infoOn()) Debug.logInfo("Analyze goodIdentification: found product.productId = " + product + ", and list : " + productsFound, module);                       
+        return productsFound;
+    }
 
-        if (productId != null) {
-            return productId;
+    public static List<GenericValue> findProductsById( GenericDelegator delegator, String idToFind, String goodIdentificationTypeId)
+    throws GenericEntityException {
+        return findProductsById(delegator, idToFind, goodIdentificationTypeId, true, false);
+    }
+    
+    public static String findProductId(GenericDelegator delegator, String idToFind, String goodIdentificationTypeId) throws GenericEntityException {
+        GenericValue product = findProduct(delegator, idToFind, goodIdentificationTypeId);
+        if (UtilValidate.isNotEmpty(product)) {
+            return product.getString("productId");
         } else {
             return null;
         }
@@ -884,53 +918,30 @@
     }
 
     public static GenericValue findProduct(GenericDelegator delegator, String idToFind, String goodIdentificationTypeId) throws GenericEntityException {
-        // first lookup and see if this is the Product PK
-        GenericValue product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", idToFind));
-
-        if (product == null) {
-            // no product record found; no check good identification
-            Map goodIdLookup = UtilMisc.toMap("idValue", idToFind);
-            if (UtilValidate.isNotEmpty(goodIdentificationTypeId)) {
-                goodIdLookup.put("goodIdentificationTypeId", goodIdentificationTypeId);
-            }
-            List goodIds = delegator.findByAndCache("GoodIdentification", goodIdLookup, UtilMisc.toList("-createdStamp"));
-
-            // sorted by createdStamp; so pull out the most current entry
-            GenericValue lastGoodId = EntityUtil.getFirst(goodIds);
-            if (lastGoodId != null) {
-                product = lastGoodId.getRelatedOneCache("Product");
-            }
-        }
-
-        if (product != null) {
-            return product;
-        } else {
-            return null;
-        }
+        List<GenericValue> products = findProductsById(delegator, idToFind, goodIdentificationTypeId);
+        GenericValue product = EntityUtil.getFirst(products);
+        return product;
     }
 
-    public static List findProducts(GenericDelegator delegator, String idToFind, String goodIdentificationTypeId) throws GenericEntityException {
-        // first lookup and see if this is the Product PK
-        GenericValue product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", idToFind));
-        List products = FastList.newInstance();
-        if (product == null) {
-            // no product record found; no check good identification
-            Map goodIdLookup = UtilMisc.toMap("idValue", idToFind);
-            if (UtilValidate.isNotEmpty(goodIdentificationTypeId)) {
-                goodIdLookup.put("goodIdentificationTypeId", goodIdentificationTypeId);
-            }
-            List goodIds = delegator.findByAndCache("GoodIdentification", goodIdLookup, UtilMisc.toList("-createdStamp"));
-
-            // sorted by createdStamp; so pull out the most current entry
-            if (goodIds != null) {
-                Iterator<GenericValue> i = goodIds.iterator();
-                while (i.hasNext()) {
-                    GenericValue v = i.next();
-                    products.add(v.getRelatedOneCache("Product"));
+    public static List<GenericValue> findProducts(GenericDelegator delegator, String idToFind, String goodIdentificationTypeId) throws GenericEntityException {
+        List<GenericValue> productsByIds = findProductsById(delegator, idToFind, goodIdentificationTypeId);
+        List<GenericValue> products = null;
+        if (UtilValidate.isNotEmpty(productsByIds)){
+            for (GenericValue product : productsByIds){
+                GenericValue productToAdd = product;
+                //retreive product GV if the actual genericValue came from viewEntity
+                if (! "Product".equals(product.getEntityName())) {
+                    productToAdd = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", product.get("productId")));
+                }
+                
+                if (UtilValidate.isEmpty(products)) {
+                    products = UtilMisc.toList(productToAdd);
+                }
+                else {
+                    products.add(productToAdd);
                 }
             }            
         }
-
         return products;
     }
 

Modified: ofbiz/trunk/specialpurpose/hhfacility/src/org/ofbiz/hhfacility/FacilityServices.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/hhfacility/src/org/ofbiz/hhfacility/FacilityServices.java?rev=709726&r1=709725&r2=709726&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/hhfacility/src/org/ofbiz/hhfacility/FacilityServices.java (original)
+++ ofbiz/trunk/specialpurpose/hhfacility/src/org/ofbiz/hhfacility/FacilityServices.java Sat Nov  1 10:19:32 2008
@@ -32,6 +32,7 @@
 import org.ofbiz.entity.GenericDelegator;
 import org.ofbiz.entity.GenericEntityException;
 import org.ofbiz.entity.GenericValue;
+import org.ofbiz.product.product.ProductWorker;
 import org.ofbiz.service.DispatchContext;
 import org.ofbiz.service.GenericServiceException;
 import org.ofbiz.service.LocalDispatcher;
@@ -41,39 +42,19 @@
 
     public static final String module = FacilityServices.class.getName();
 
-    public static Map findProductsById(DispatchContext dctx, Map context) {
+    public static Map findProductsById(DispatchContext dctx, Map context) {        
         GenericDelegator delegator = dctx.getDelegator();
         String facilityId = (String) context.get("facilityId");
         String idValue = (String) context.get("idValue");
         GenericValue product = null;
-        List productsFound = null;
+        List<GenericValue> productsFound = null;
         
-        GenericValue productItem = null;
-        if (UtilValidate.isNotEmpty(idValue)) {
-            // First lets find the productId from the Sku(s)
-            try {
-                productsFound = delegator.findByAnd("GoodIdentificationAndProduct",
-                   UtilMisc.toMap("idValue", idValue), UtilMisc.toList("productId"));
-            } catch (GenericEntityException e) {
-                Debug.logError(e, module);
-                throw new GeneralRuntimeException(e.getMessage());
-            }
-        }
-
-        // Now do a direct lookup..
-        productItem = null;
         try {
-            productItem = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", idValue));
+            productsFound = ProductWorker.findProductsById(delegator, idValue, null, false, true);
         } catch (GenericEntityException e) {
             Debug.logError(e, module);
             throw new GeneralRuntimeException(e.getMessage());
         }
-        if (productItem != null) {
-            if (productsFound == null) {
-                productsFound = new ArrayList();
-            }
-            productsFound.add(productItem);
-        }
 
         // Send back the results
         Map result = ServiceUtil.returnSuccess();

Modified: ofbiz/trunk/specialpurpose/pos/src/org/ofbiz/pos/PosTransaction.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/pos/src/org/ofbiz/pos/PosTransaction.java?rev=709726&r1=709725&r2=709726&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/pos/src/org/ofbiz/pos/PosTransaction.java (original)
+++ ofbiz/trunk/specialpurpose/pos/src/org/ofbiz/pos/PosTransaction.java Sat Nov  1 10:19:32 2008
@@ -63,6 +63,7 @@
 import org.ofbiz.pos.screen.SaveSale;
 import org.ofbiz.product.config.ProductConfigWrapper;
 import org.ofbiz.product.config.ProductConfigWrapper.ConfigOption;
+import org.ofbiz.product.product.ProductWorker;
 import org.ofbiz.product.store.ProductStoreWorker;
 import org.ofbiz.service.GenericServiceException;
 import org.ofbiz.service.LocalDispatcher;
@@ -192,15 +193,7 @@
     }
 
     public List lookupItem(String sku) throws GeneralException {
-        // first check for the product
-        GenericValue product = session.getDelegator().findByPrimaryKey("Product", UtilMisc.toMap("productId", sku));
-        if (product != null) {
-            return UtilMisc.toList(product);
-        } else {
-            // not found; so we move on to GoodIdentification
-           return session.getDelegator().findByAnd("GoodIdentificationAndProduct",
-                   UtilMisc.toMap("idValue", sku), UtilMisc.toList("productId"));
-        }
+        return ProductWorker.findProductsById( session.getDelegator(), sku, null);
     }
 
     public String getOrderId() {