Hi,
After my jira, I continue my improvement of generic function to search a
product by id.
I found some functions that do the same operation to find a product and
after is the product not found search on the view
GoodIdentificationAndProduct (in ProductWorker.java, Pos component and
hhfacility component).
I create a patch to refactoring search function, remove duplicated code
and create service to call the worker.
Do you have other remarks or suggestions that will be permit to improve
my solution ?
If my refactoring is in the good way, I continue to replace code in POS
and hhfacility.
Nicolas
--
Nicolas MALIN
Consultant
Tél : 06.17.66.40.06
Site projet : http://www.neogia.org/
-------
Société LibrenBerry
Tél : 02.48.02.56.12
Site : http://www.librenberry.net/
### Eclipse Workspace Patch 1.0
#P OFBiz
Index: applications/product/src/org/ofbiz/product/product/ProductServices.java
===================================================================
--- applications/product/src/org/ofbiz/product/product/ProductServices.java (revision 707362)
+++ applications/product/src/org/ofbiz/product/product/ProductServices.java (working copy)
@@ -1101,5 +1101,55 @@
}
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 = true;
+ boolean searchAllId = false;
+ if (UtilValidate.isNotEmpty(searchProductFirstContext)
+ && "N".equals(searchProductFirstContext)) {
+ searchProductFirst = false;
+ }
+
+ if (UtilValidate.isNotEmpty(searchAllIdContext)
+ && "Y".equals(searchAllIdContext)) {
+ searchAllId = true;
+ }
+
+ 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;
+ }
}
Index: applications/product/src/org/ofbiz/product/product/ProductWorker.java
===================================================================
--- applications/product/src/org/ofbiz/product/product/ProductWorker.java (revision 707362)
+++ applications/product/src/org/ofbiz/product/product/ProductWorker.java (working copy)
@@ -850,30 +850,65 @@
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;
+
+ /**
+ * 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));
+ }
- if (product == null) {
- // no product record found; no check good identification
- Map goodIdLookup = UtilMisc.toMap("idValue", idToFind);
+ if (searchAllId || (searchProductFirst && UtilValidate.isEmpty(product))) {
+ // 2) Retrieve product in GoodIdentification
+ Map<String, String> conditions = UtilMisc.toMap("idValue", idToFind);
if (UtilValidate.isNotEmpty(goodIdentificationTypeId)) {
- goodIdLookup.put("goodIdentificationTypeId", goodIdentificationTypeId);
+ conditions.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) {
- productId = lastGoodId.getString("productId");
- }
- } else {
- productId = product.getString("productId");
+ productsFound = delegator.findByAndCache("GoodIdentificationAndProduct", conditions, UtilMisc.toList("-createdStamp"));
+ }
+
+ 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.getString("productId"), 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 +919,26 @@
}
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;
}
Index: applications/product/servicedef/services.xml
===================================================================
--- applications/product/servicedef/services.xml (revision 707362)
+++ applications/product/servicedef/services.xml (working copy)
@@ -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 -->