1 24 package org.ofbiz.product.product; 25 26 import java.util.ArrayList ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 import java.util.Set ; 34 import javax.servlet.ServletRequest ; 35 import javax.servlet.jsp.PageContext ; 36 37 import org.apache.commons.collections.map.LinkedMap; 38 39 import org.ofbiz.base.util.Debug; 40 import org.ofbiz.base.util.UtilFormatOut; 41 import org.ofbiz.base.util.UtilMisc; 42 import org.ofbiz.base.util.UtilValidate; 43 import org.ofbiz.entity.GenericDelegator; 44 import org.ofbiz.entity.GenericEntityException; 45 import org.ofbiz.entity.GenericValue; 46 import org.ofbiz.entity.util.EntityUtil; 47 import org.ofbiz.product.config.ProductConfigWrapper; 48 import org.ofbiz.product.config.ProductConfigWrapper.ConfigOption; 49 import org.ofbiz.service.GenericServiceException; 50 import org.ofbiz.service.LocalDispatcher; 51 import org.ofbiz.service.ModelService; 52 53 61 public class ProductWorker { 62 63 public static final String module = ProductWorker.class.getName(); 64 public static final String resource = "ProductUiLabels"; 65 66 public static void getProduct(PageContext pageContext, String attributeName) { 67 getProduct(pageContext, attributeName, null); 68 } 69 70 public static boolean shippingApplies(GenericValue product) { 71 String errMsg = null; 72 if (product != null) { 73 String productTypeId = product.getString("productTypeId"); 74 if ("SERVICE".equals(productTypeId) || (ProductWorker.isDigital(product) && !ProductWorker.isPhysical(product))) { 75 return false; 77 } 78 Boolean chargeShipping = product.getBoolean("chargeShipping"); 79 80 if (chargeShipping == null) { 81 return true; 82 } else { 83 return chargeShipping.booleanValue(); 84 } 85 } else { 86 throw new IllegalArgumentException (errMsg); 89 } 90 } 91 92 public static boolean taxApplies(GenericValue product) { 93 String errMsg = null; 94 if (product != null) { 95 Boolean taxable = product.getBoolean("taxable"); 96 97 if (taxable == null) { 98 return true; 99 } else { 100 return taxable.booleanValue(); 101 } 102 } else { 103 throw new IllegalArgumentException (errMsg); 106 } 107 } 108 109 public static void getProduct(PageContext pageContext, String attributeName, String productId) { 110 GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator"); 111 ServletRequest request = pageContext.getRequest(); 112 113 if (productId == null) 114 productId = UtilFormatOut.checkNull(request.getParameter("product_id"), request.getParameter("PRODUCT_ID")); 115 116 if (productId.equals("")) 117 return; 118 119 GenericValue product = null; 120 121 try { 122 product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); 123 } catch (GenericEntityException e) { 124 Debug.logWarning(e.getMessage(), module); 125 product = null; 126 } 127 if (product != null) 128 pageContext.setAttribute(attributeName, product); 129 } 130 131 public static String getVariantVirtualId(GenericValue variantProduct) throws GenericEntityException { 132 List productAssocs = getVariantVirtualAssocs(variantProduct); 133 if (productAssocs == null) 134 { 135 return null; 136 } 137 GenericValue productAssoc = EntityUtil.getFirst(productAssocs); 138 if (productAssoc != null) { 139 return productAssoc.getString("productId"); 140 } else { 141 return null; 142 } 143 } 144 145 public static List getVariantVirtualAssocs(GenericValue variantProduct) throws GenericEntityException { 146 if (variantProduct != null && "Y".equals(variantProduct.getString("isVariant"))) { 147 List productAssocs = EntityUtil.filterByDate(variantProduct.getRelatedByAndCache("AssocProductAssoc", 148 UtilMisc.toMap("productAssocTypeId", "PRODUCT_VARIANT")), true); 149 return productAssocs; 150 } 151 return null; 152 } 153 154 158 private static boolean isProductInventoryAvailableByFacility(String productId, String inventoryFacilityId, double quantity, LocalDispatcher dispatcher) throws GenericServiceException { 159 Double availableToPromise = null; 160 161 try { 162 Map result = dispatcher.runSync("getInventoryAvailableByFacility", 163 UtilMisc.toMap("productId", productId, "facilityId", inventoryFacilityId)); 164 165 availableToPromise = (Double ) result.get("availableToPromiseTotal"); 166 167 if (availableToPromise == null) { 168 Debug.logWarning("The getInventoryAvailableByFacility service returned a null availableToPromise, the error message was:\n" + result.get(ModelService.ERROR_MESSAGE), module); 169 return false; 170 } 171 } catch (GenericServiceException e) { 172 Debug.logWarning(e, "Error invoking getInventoryAvailableByFacility service in isCatalogInventoryAvailable", module); 173 return false; 174 } 175 176 if (availableToPromise.doubleValue() >= quantity) { 178 if (Debug.infoOn()) Debug.logInfo("Inventory IS available in facility with id " + inventoryFacilityId + " for product id " + productId + "; desired quantity is " + quantity + ", available quantity is " + availableToPromise, module); 179 return true; 180 } else { 181 if (Debug.infoOn()) Debug.logInfo("Returning false because there is insufficient inventory available in facility with id " + inventoryFacilityId + " for product id " + productId + "; desired quantity is " + quantity + ", available quantity is " + availableToPromise, module); 182 return false; 183 } 184 } 185 186 190 public static boolean isProductInventoryAvailableByFacility(ProductConfigWrapper productConfig, String inventoryFacilityId, double quantity, LocalDispatcher dispatcher) throws GenericServiceException { 191 boolean available = true; 192 List options = productConfig.getSelectedOptions(); 193 Iterator optionsIt = options.iterator(); 194 while (optionsIt.hasNext()) { 195 ConfigOption ci = (ConfigOption)optionsIt.next(); 196 List products = ci.getComponents(); 197 Iterator productsIt = products.iterator(); 198 while (productsIt.hasNext()) { 199 GenericValue product = (GenericValue)productsIt.next(); 200 String productId = product.getString("productId"); 201 Double cmpQuantity = product.getDouble("quantity"); 202 double neededQty = 1.0; 203 if (cmpQuantity != null) { 204 neededQty = quantity * cmpQuantity.doubleValue(); 205 } 206 if (!isProductInventoryAvailableByFacility(productId, inventoryFacilityId, neededQty, dispatcher)) { 207 ci.setAvailable(false); 208 } 209 } 210 if (!ci.isAvailable()) { 211 available = false; 212 } 213 } 214 return available; 215 } 216 217 public static void getAssociatedProducts(PageContext pageContext, String productAttributeName, String assocPrefix) { 218 GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator"); 219 GenericValue product = (GenericValue) pageContext.getAttribute(productAttributeName); 220 221 if (product == null) 222 return; 223 224 try { 225 List upgradeProducts = product.getRelatedByAndCache("MainProductAssoc", 226 UtilMisc.toMap("productAssocTypeId", "PRODUCT_UPGRADE")); 227 228 List complementProducts = product.getRelatedByAndCache("MainProductAssoc", 229 UtilMisc.toMap("productAssocTypeId", "PRODUCT_COMPLEMENT")); 230 231 List obsolescenceProducts = product.getRelatedByAndCache("AssocProductAssoc", 232 UtilMisc.toMap("productAssocTypeId", "PRODUCT_OBSOLESCENCE")); 233 234 List obsoleteByProducts = product.getRelatedByAndCache("MainProductAssoc", 235 UtilMisc.toMap("productAssocTypeId", "PRODUCT_OBSOLESCENCE")); 236 237 upgradeProducts = EntityUtil.filterByDate(upgradeProducts, true); 239 complementProducts = EntityUtil.filterByDate(complementProducts, true); 240 obsolescenceProducts = EntityUtil.filterByDate(obsolescenceProducts, true); 241 obsoleteByProducts = EntityUtil.filterByDate(obsoleteByProducts, true); 242 243 if (upgradeProducts != null && upgradeProducts.size() > 0) 244 pageContext.setAttribute(assocPrefix + "upgrade", upgradeProducts); 245 if (complementProducts != null && complementProducts.size() > 0) 246 pageContext.setAttribute(assocPrefix + "complement", complementProducts); 247 if (obsolescenceProducts != null && obsolescenceProducts.size() > 0) 248 pageContext.setAttribute(assocPrefix + "obsolescence", obsolescenceProducts); 249 if (obsoleteByProducts != null && obsoleteByProducts.size() > 0) 250 pageContext.setAttribute(assocPrefix + "obsoleteby", obsoleteByProducts); 251 } catch (GenericEntityException e) { 252 Debug.logWarning(e, module); 253 } 254 } 255 256 260 public static Set getVariantDistinguishingFeatures(GenericValue variantProduct) throws GenericEntityException { 261 if (variantProduct == null) { 262 return new HashSet (); 263 } 264 if (!"Y".equals(variantProduct.getString("isVariant"))) { 265 throw new IllegalArgumentException ("Cannot get distinguishing features for a product that is not a variant (ie isVariant!=Y)."); 266 } 267 GenericDelegator delegator = variantProduct.getDelegator(); 268 String virtualProductId = getVariantVirtualId(variantProduct); 269 270 Set distFeatures = new HashSet (); 272 273 List variantDistinguishingFeatures = delegator.findByAndCache("ProductFeatureAndAppl", UtilMisc.toMap("productId", variantProduct.get("productId"), "productFeatureApplTypeId", "DISTINGUISHING_FEAT")); 274 276 Iterator variantDistinguishingFeatureIter = UtilMisc.toIterator(EntityUtil.filterByDate(variantDistinguishingFeatures)); 277 while (variantDistinguishingFeatureIter != null && variantDistinguishingFeatureIter.hasNext()) { 278 GenericValue variantDistinguishingFeature = (GenericValue) variantDistinguishingFeatureIter.next(); 279 GenericValue dummyFeature = delegator.makeValue("ProductFeature", null); 280 dummyFeature.setAllFields(variantDistinguishingFeature, true, null, null); 281 distFeatures.add(dummyFeature); 282 } 283 284 List virtualSelectableFeatures = delegator.findByAndCache("ProductFeatureAndAppl", UtilMisc.toMap("productId", virtualProductId, "productFeatureApplTypeId", "SELECTABLE_FEATURE")); 285 287 Iterator virtualSelectableFeatureIter = UtilMisc.toIterator(EntityUtil.filterByDate(virtualSelectableFeatures)); 288 Set virtualSelectableFeatureIds = new HashSet (); 289 while (virtualSelectableFeatureIter != null && virtualSelectableFeatureIter.hasNext()) { 290 GenericValue virtualSelectableFeature = (GenericValue) virtualSelectableFeatureIter.next(); 291 virtualSelectableFeatureIds.add(virtualSelectableFeature.get("productFeatureId")); 292 } 293 294 List variantStandardFeatures = delegator.findByAndCache("ProductFeatureAndAppl", UtilMisc.toMap("productId", variantProduct.get("productId"), "productFeatureApplTypeId", "STANDARD_FEATURE")); 295 297 Iterator variantStandardFeatureIter = UtilMisc.toIterator(EntityUtil.filterByDate(variantStandardFeatures)); 298 while (variantStandardFeatureIter != null && variantStandardFeatureIter.hasNext()) { 299 GenericValue variantStandardFeature = (GenericValue) variantStandardFeatureIter.next(); 300 if (virtualSelectableFeatureIds.contains(variantStandardFeature.get("productFeatureId"))) { 301 GenericValue dummyFeature = delegator.makeValue("ProductFeature", null); 302 dummyFeature.setAllFields(variantStandardFeature, true, null, null); 303 distFeatures.add(dummyFeature); 304 } 305 } 306 307 return distFeatures; 308 } 309 310 314 public static String getGwpAlternativeOptionName(GenericDelegator delegator, String alternativeOptionProductId, Locale locale) { 315 try { 316 GenericValue alternativeOptionProduct = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", alternativeOptionProductId)); 317 if (alternativeOptionProduct != null) { 318 if ("Y".equals(alternativeOptionProduct.getString("isVariant"))) { 319 Set distFeatures = getVariantDistinguishingFeatures(alternativeOptionProduct); 320 if (distFeatures != null && distFeatures.size() > 0) { 321 323 StringBuffer nameBuf = new StringBuffer (); 324 Iterator distFeatIter = distFeatures.iterator(); 325 while (distFeatIter.hasNext()) { 326 GenericValue productFeature = (GenericValue) distFeatIter.next(); 327 GenericValue productFeatureType = productFeature.getRelatedOneCache("ProductFeatureType"); 328 if (productFeatureType != null) { 329 nameBuf.append(productFeatureType.get("description", locale)); 330 nameBuf.append(":"); 331 } 332 nameBuf.append(productFeature.get("description", locale)); 333 if (distFeatIter.hasNext()) { 334 nameBuf.append(", "); 335 } 336 } 337 return nameBuf.toString(); 338 } 339 } 340 341 String alternativeProductName = ProductContentWrapper.getProductContentAsText(alternativeOptionProduct, "PRODUCT_NAME", locale); 343 return alternativeProductName; 345 } 346 } catch (GenericEntityException e) { 347 Debug.logError(e, module); 348 } catch (Exception e) { 349 Debug.logError(e, module); 350 } 351 return "[" + alternativeOptionProductId + "]"; 353 } 354 355 362 public static List getProductFeaturesByApplTypeId(GenericDelegator delegator, String productId, String productFeatureApplTypeId) { 363 if (productId == null) { 364 return null; 365 } 366 try { 367 return getProductFeaturesByApplTypeId(delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)), 368 productFeatureApplTypeId); 369 } catch (GenericEntityException e) { 370 Debug.logError(e, module); 371 } 372 return null; 373 } 374 375 public static List getProductFeaturesByApplTypeId(GenericValue product, String productFeatureApplTypeId) { 376 if (product == null) { 377 return null; 378 } 379 List features = new ArrayList (); 380 try { 381 if (product != null) { 382 List productAppls; 383 if (productFeatureApplTypeId == null) { 384 productAppls = product.getRelated("ProductFeatureAppl"); 385 } else { 386 productAppls = product.getRelatedByAnd("ProductFeatureAppl", 387 UtilMisc.toMap("productFeatureApplTypeId", productFeatureApplTypeId)); 388 } 389 for (int i = 0; i < productAppls.size(); i++) { 390 GenericValue productAppl = (GenericValue)productAppls.get(i); 391 features.add(productAppl.getRelatedOne("ProductFeature")); 392 } 393 features = EntityUtil.orderBy(features, UtilMisc.toList("description")); 394 } 395 } catch (GenericEntityException e) { 396 Debug.logError(e, module); 397 } 398 return features; 399 } 400 401 public static Map getOptionalProductFeatures(GenericDelegator delegator, String productId) { 402 Map featureMap = new LinkedMap(); 403 404 List productFeatureAppls = null; 405 try { 406 productFeatureAppls = delegator.findByAnd("ProductFeatureAndAppl", UtilMisc.toMap("productId", productId, "productFeatureApplTypeId", "OPTIONAL_FEATURE"), UtilMisc.toList("productFeatureTypeId", "sequenceNum")); 407 } catch (GenericEntityException e) { 408 Debug.logError(e, module); 409 } 410 411 if (productFeatureAppls != null) { 412 Iterator i = productFeatureAppls.iterator(); 413 while (i.hasNext()) { 414 GenericValue appl = (GenericValue) i.next(); 415 String featureType = appl.getString("productFeatureTypeId"); 416 List features = (List ) featureMap.get(featureType); 417 if (features == null) { 418 features = new LinkedList (); 419 } 420 features.add(appl); 421 featureMap.put(featureType, features); 422 } 423 } 424 425 return featureMap; 426 } 427 428 430 public static double calcOrderAdjustments(List orderHeaderAdjustments, double subTotal, boolean includeOther, boolean includeTax, boolean includeShipping) { 431 double adjTotal = 0.0; 432 433 if (orderHeaderAdjustments != null && orderHeaderAdjustments.size() > 0) { 434 List filteredAdjs = filterOrderAdjustments(orderHeaderAdjustments, includeOther, includeTax, includeShipping, false, false); 435 Iterator adjIt = filteredAdjs.iterator(); 436 437 while (adjIt.hasNext()) { 438 GenericValue orderAdjustment = (GenericValue) adjIt.next(); 439 440 adjTotal += calcOrderAdjustment(orderAdjustment, subTotal); 441 } 442 } 443 return adjTotal; 444 } 445 446 public static double calcOrderAdjustment(GenericValue orderAdjustment, double orderSubTotal) { 447 double adjustment = 0.0; 448 449 if (orderAdjustment.get("amount") != null) { 450 adjustment += orderAdjustment.getDouble("amount").doubleValue(); 451 } 452 if (orderAdjustment.get("percentage") != null) { 453 adjustment += (orderAdjustment.getDouble("percentage").doubleValue() * orderSubTotal); 454 } 455 return adjustment; 456 } 457 458 public static List filterOrderAdjustments(List adjustments, boolean includeOther, boolean includeTax, boolean includeShipping, boolean forTax, boolean forShipping) { 459 List newOrderAdjustmentsList = new LinkedList (); 460 461 if (adjustments != null && adjustments.size() > 0) { 462 Iterator adjIt = adjustments.iterator(); 463 464 while (adjIt.hasNext()) { 465 GenericValue orderAdjustment = (GenericValue) adjIt.next(); 466 467 boolean includeAdjustment = false; 468 469 if ("SALES_TAX".equals(orderAdjustment.getString("orderAdjustmentTypeId"))) { 470 if (includeTax) includeAdjustment = true; 471 } else if ("SHIPPING_CHARGES".equals(orderAdjustment.getString("orderAdjustmentTypeId"))) { 472 if (includeShipping) includeAdjustment = true; 473 } else { 474 if (includeOther) includeAdjustment = true; 475 } 476 477 if (forTax && "N".equals(orderAdjustment.getString("includeInTax"))) { 479 includeAdjustment = false; 480 } 481 482 if (forShipping && "N".equals(orderAdjustment.getString("includeInShipping"))) { 484 includeAdjustment = false; 485 } 486 487 if (includeAdjustment) { 488 newOrderAdjustmentsList.add(orderAdjustment); 489 } 490 } 491 } 492 return newOrderAdjustmentsList; 493 } 494 495 public static double getAverageProductRating(GenericDelegator delegator, String productId) { 496 return getAverageProductRating(delegator, productId, null); 497 } 498 499 public static double getAverageProductRating(GenericDelegator delegator, String productId, String productStoreId) { 500 GenericValue product = null; 501 try { 502 product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", productId)); 503 } catch (GenericEntityException e) { 504 Debug.logError(e, module); 505 } 506 return ProductWorker.getAverageProductRating(product, productStoreId); 507 } 508 509 public static double getAverageProductRating(GenericValue product, String productStoreId) { 510 return getAverageProductRating(product, null, productStoreId); 511 } 512 513 public static double getAverageProductRating(GenericValue product, List reviews, String productStoreId) { 514 if (product == null) { 515 Debug.logWarning("Invalid product entity passed; unable to obtain valid product rating", module); 516 return 0.00; 517 } 518 519 double productRating = 0.00; 520 Double productEntityRating = product.getDouble("productRating"); 521 String entityFieldType = product.getString("ratingTypeEnum"); 522 523 if (productEntityRating == null) { 525 productEntityRating = new Double (0); 526 } 527 if (entityFieldType == null) { 528 entityFieldType = new String (); 529 } 530 531 if ("PRDR_FLAT".equals(entityFieldType)) { 532 productRating = productEntityRating.doubleValue(); 533 } else { 534 Map reviewByAnd = UtilMisc.toMap("statusId", "PRR_APPROVED"); 536 if (productStoreId != null) { 537 reviewByAnd.put("productStoreId", productStoreId); 538 } 539 540 if (reviews == null) { 542 try { 543 reviews = product.getRelatedCache("ProductReview", reviewByAnd, UtilMisc.toList("-postedDateTime")); 544 } catch (GenericEntityException e) { 545 Debug.logError(e, module); 546 } 547 } 548 549 double ratingTally = 0; 551 double numRatings = 0; 552 if (reviews != null) { 553 Iterator i = reviews.iterator(); 554 while (i.hasNext()) { 555 GenericValue productReview = (GenericValue) i.next(); 556 Double rating = productReview.getDouble("productRating"); 557 if (rating != null) { 558 ratingTally += rating.doubleValue(); 559 numRatings++; 560 } 561 } 562 } 563 if (ratingTally > 0 && numRatings > 0) { 564 productRating = ratingTally / numRatings; 565 } 566 567 if ("PRDR_MIN".equals(entityFieldType)) { 568 if (productEntityRating.doubleValue() > productRating) { 570 productRating = productEntityRating.doubleValue(); 571 } 572 } else if ("PRDR_MAX".equals(entityFieldType)) { 573 if (productRating > productEntityRating.doubleValue()) { 575 productRating = productEntityRating.doubleValue(); 576 } 577 } 578 } 579 580 return productRating; 581 } 582 583 public static List getCurrentProductCategories(GenericDelegator delegator, String productId) { 584 GenericValue product = null; 585 try { 586 product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); 587 } catch (GenericEntityException e) { 588 Debug.logError(e, module); 589 } 590 return getCurrentProductCategories(delegator, product); 591 } 592 593 public static List getCurrentProductCategories(GenericDelegator delegator, GenericValue product) { 594 if (product == null) { 595 return null; 596 } 597 List categories = new ArrayList (); 598 try { 599 List categoryMembers = product.getRelated("ProductCategoryMember"); 600 categoryMembers = EntityUtil.filterByDate(categoryMembers); 601 categories = EntityUtil.getRelated("ProductCategory", categoryMembers); 602 } catch (GenericEntityException e) { 603 Debug.logError(e, module); 604 } 605 return categories; 606 } 607 608 public static GenericValue getParentProduct(String productId, GenericDelegator delegator) { 610 GenericValue _parentProduct = null; 611 if (productId == null) { 612 Debug.logWarning("Bad product id", module); 613 } 614 615 try { 616 List virtualProductAssocs = delegator.findByAndCache("ProductAssoc", UtilMisc.toMap("productIdTo", productId, "productAssocTypeId", "PRODUCT_VARIANT"), UtilMisc.toList("-fromDate")); 617 virtualProductAssocs = EntityUtil.filterByDate(virtualProductAssocs, true); 618 if (virtualProductAssocs == null || virtualProductAssocs.size() == 0) { 619 virtualProductAssocs = delegator.findByAndCache("ProductAssoc", UtilMisc.toMap("productIdTo", productId, "productAssocTypeId", "UNIQUE_ITEM"), UtilMisc.toList("-fromDate")); 621 virtualProductAssocs = EntityUtil.filterByDate(virtualProductAssocs, true); 622 } 623 if (virtualProductAssocs != null && virtualProductAssocs.size() > 0) { 624 GenericValue productAssoc = EntityUtil.getFirst(virtualProductAssocs); 626 _parentProduct = productAssoc.getRelatedOneCache("MainProduct"); 627 } 628 } catch (GenericEntityException e) { 629 throw new RuntimeException ("Entity Engine error getting Parent Product (" + e.getMessage() + ")"); 630 } 631 return _parentProduct; 632 } 633 634 public static boolean isDigital(GenericValue product) { 635 boolean isDigital = false; 636 if (product != null) { 637 GenericValue productType = null; 638 try { 639 productType = product.getRelatedOneCache("ProductType"); 640 } catch (GenericEntityException e) { 641 Debug.logWarning(e.getMessage(), module); 642 } 643 String isDigitalValue = (productType != null? productType.getString("isDigital"): null); 644 isDigital = isDigitalValue != null && "Y".equalsIgnoreCase(isDigitalValue); 645 } 646 return isDigital; 647 } 648 649 public static boolean isPhysical(GenericValue product) { 650 boolean isPhysical = false; 651 if (product != null) { 652 GenericValue productType = null; 653 try { 654 productType = product.getRelatedOneCache("ProductType"); 655 } catch (GenericEntityException e) { 656 Debug.logWarning(e.getMessage(), module); 657 } 658 String isPhysicalValue = (productType != null? productType.getString("isPhysical"): null); 659 isPhysical = isPhysicalValue != null && "Y".equalsIgnoreCase(isPhysicalValue); 660 } 661 return isPhysical; 662 } 663 664 public static String findProductId(GenericDelegator delegator, String idToFind, String goodIdentificationTypeId) throws GenericEntityException { 665 GenericValue product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", idToFind)); 667 String productId = null; 668 669 if (product == null) { 670 Map goodIdLookup = UtilMisc.toMap("idValue", idToFind); 672 if (UtilValidate.isNotEmpty(goodIdentificationTypeId)) { 673 goodIdLookup.put("goodIdentificationTypeId", goodIdentificationTypeId); 674 } 675 List goodIds = delegator.findByAndCache("GoodIdentification", goodIdLookup, UtilMisc.toList("-createdStamp")); 676 677 GenericValue lastGoodId = EntityUtil.getFirst(goodIds); 679 if (lastGoodId != null) { 680 productId = lastGoodId.getString("productId"); 681 } 682 } else { 683 productId = product.getString("productId"); 684 } 685 686 if (productId != null) { 687 return productId; 688 } else { 689 return null; 690 } 691 } 692 693 public static String findProductId(GenericDelegator delegator, String idToFind) throws GenericEntityException { 694 return findProductId(delegator, idToFind, null); 695 } 696 697 public static GenericValue findProduct(GenericDelegator delegator, String idToFind, String goodIdentificationTypeId) throws GenericEntityException { 698 GenericValue product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", idToFind)); 700 String productId = null; 701 702 if (product == null) { 703 Map goodIdLookup = UtilMisc.toMap("idValue", idToFind); 705 if (UtilValidate.isNotEmpty(goodIdentificationTypeId)) { 706 goodIdLookup.put("goodIdentificationTypeId", goodIdentificationTypeId); 707 } 708 List goodIds = delegator.findByAndCache("GoodIdentification", goodIdLookup, UtilMisc.toList("-createdStamp")); 709 710 GenericValue lastGoodId = EntityUtil.getFirst(goodIds); 712 if (lastGoodId != null) { 713 product = lastGoodId.getRelatedOneCache("Product"); 714 } 715 } 716 717 if (product != null) { 718 return product; 719 } else { 720 return null; 721 } 722 } 723 724 public static GenericValue findProduct(GenericDelegator delegator, String idToFind) throws GenericEntityException { 725 return findProduct(delegator, idToFind, null); 726 } 727 } 728 729 | Popular Tags |