1 23 package org.ofbiz.product.product; 24 25 import java.sql.Timestamp ; 26 import java.util.*; 27 28 import org.apache.commons.collections.map.LinkedMap; 29 import org.apache.commons.collections.set.ListOrderedSet; 30 31 import org.ofbiz.base.util.Debug; 32 import org.ofbiz.base.util.UtilDateTime; 33 import org.ofbiz.base.util.UtilMisc; 34 import org.ofbiz.base.util.UtilProperties; 35 import org.ofbiz.base.util.UtilValidate; 36 import org.ofbiz.entity.GenericDelegator; 37 import org.ofbiz.entity.GenericEntityException; 38 import org.ofbiz.entity.GenericValue; 39 import org.ofbiz.entity.util.EntityUtil; 40 import org.ofbiz.product.catalog.CatalogWorker; 41 import org.ofbiz.product.category.CategoryWorker; 42 import org.ofbiz.service.DispatchContext; 43 import org.ofbiz.service.GenericServiceException; 44 import org.ofbiz.service.LocalDispatcher; 45 import org.ofbiz.service.ModelService; 46 import org.ofbiz.service.ServiceUtil; 47 48 56 public class ProductServices { 57 58 public static final String module = ProductServices.class.getName(); 59 public static final String resource = "ProductUiLabels"; 60 61 64 public static Map prodFindAllVariants(DispatchContext dctx, Map context) { 65 context.put("type", "PRODUCT_VARIANT"); 67 return prodFindAssociatedByType(dctx, context); 68 } 69 70 73 public static Map prodFindSelectedVariant(DispatchContext dctx, Map context) { 74 GenericDelegator delegator = dctx.getDelegator(); 77 Locale locale = (Locale) context.get("locale"); 78 String productId = (String ) context.get("productId"); 79 Map selectedFeatures = (Map) context.get("selectedFeatures"); 80 ArrayList products = new ArrayList(); 81 Map resVariants = prodFindAllVariants(dctx, context); 83 List variants = (List)resVariants.get("assocProducts"); 84 GenericValue oneVariant = null; 85 Iterator variantsIt = variants.iterator(); 86 while (variantsIt.hasNext()) { 87 oneVariant = (GenericValue)variantsIt.next(); 89 Map feaContext = new HashMap(); 90 feaContext.put("productId", oneVariant.get("productIdTo")); 91 feaContext.put("type", "STANDARD_FEATURE"); 92 Map resFeatures = prodGetFeatures(dctx, feaContext); 93 List features = (List)resFeatures.get("productFeatures"); 94 Iterator featuresIt = features.iterator(); 95 GenericValue oneFeature = null; 96 boolean variantFound = true; 97 while (featuresIt.hasNext()) { 106 oneFeature = (GenericValue)featuresIt.next(); 107 if (selectedFeatures.containsKey(oneFeature.getString("productFeatureTypeId"))) { 108 if (!selectedFeatures.containsValue(oneFeature.getString("productFeatureId"))) { 109 variantFound = false; 110 break; 111 } 112 } 113 } 114 if (variantFound) { 115 try { 116 products.add(delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", oneVariant.getString("productIdTo")))); 117 } catch (GenericEntityException e) { 118 Map messageMap = UtilMisc.toMap("errProductFeatures", e.toString()); 119 String errMsg = UtilProperties.getMessage(resource,"productservices.problem_reading_product_features_errors", messageMap, locale); 120 Debug.logError(e, errMsg, module); 121 return ServiceUtil.returnError(errMsg); 122 } 123 } 124 } 125 126 Map result = ServiceUtil.returnSuccess(); 127 result.put("products", products); 128 return result; 129 } 130 131 134 public static Map prodFindDistinctVariants(DispatchContext dctx, Map context) { 135 GenericDelegator delegator = dctx.getDelegator(); 138 String productId = (String ) context.get("productId"); 139 String feature = (String ) context.get("feature"); 140 141 return ServiceUtil.returnError("This service has not yet been implemented."); 142 } 143 144 147 public static Map prodFindFeatureTypes(DispatchContext dctx, Map context) { 148 GenericDelegator delegator = dctx.getDelegator(); 150 String productId = (String ) context.get("productId"); 151 Locale locale = (Locale) context.get("locale"); 152 String errMsg=null; 153 Set featureSet = new ListOrderedSet(); 154 155 try { 156 Map fields = UtilMisc.toMap("productId", productId, "productFeatureApplTypeId", "SELECTABLE_FEATURE"); 157 List order = UtilMisc.toList("sequenceNum", "productFeatureTypeId"); 158 List features = delegator.findByAndCache("ProductFeatureAndAppl", fields, order); 159 Iterator i = features.iterator(); 160 while (i.hasNext()) { 161 featureSet.add(((GenericValue) i.next()).getString("productFeatureTypeId")); 162 } 163 } catch (GenericEntityException e) { 165 Map messageMap = UtilMisc.toMap("errProductFeatures", e.toString()); 166 errMsg = UtilProperties.getMessage(resource,"productservices.problem_reading_product_features_errors", messageMap, locale); 167 Debug.logError(e, errMsg, module); 168 return ServiceUtil.returnError(errMsg); 169 } 170 171 if (featureSet.size() == 0) { 172 errMsg = UtilProperties.getMessage(resource,"productservices.problem_reading_product_features", locale); 173 Debug.logWarning(errMsg + " for product " + productId, module); 175 } 177 Map result = ServiceUtil.returnSuccess(); 178 result.put("featureSet", featureSet); 179 return result; 180 } 181 182 185 public static Map prodMakeFeatureTree(DispatchContext dctx, Map context) { 186 String productStoreId = (String ) context.get("productStoreId"); 190 Locale locale = (Locale) context.get("locale"); 191 192 GenericDelegator delegator = dctx.getDelegator(); 193 LocalDispatcher dispatcher = dctx.getDispatcher(); 194 Map result = new HashMap(); 195 List featureOrder = new LinkedList((Collection) context.get("featureOrder")); 196 197 if (featureOrder == null || featureOrder.size() == 0) { 198 return ServiceUtil.returnError("Empty list of features passed"); 199 } 200 201 Collection variants = (Collection) prodFindAllVariants(dctx, context).get("assocProducts"); 202 List virtualVariant = new ArrayList(); 203 204 if (variants == null || variants.size() == 0) { 205 return ServiceUtil.returnSuccess(); 206 } 207 List items = new ArrayList(); 208 Iterator i = variants.iterator(); 209 210 while (i.hasNext()) { 211 String productIdTo = (String ) ((GenericValue) i.next()).get("productIdTo"); 212 213 GenericValue productTo = null; 215 216 try { 217 productTo = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", productIdTo)); 218 } catch (GenericEntityException e) { 219 Debug.logError(e, module); 220 Map messageMap = UtilMisc.toMap("productIdTo", productIdTo, "errMessage", e.toString()); 221 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "productservices.error_finding_associated_variant_with_ID_error", messageMap, locale)); 222 } 223 if (productTo == null) { 224 Debug.logWarning("Could not find associated variant with ID " + productIdTo + ", not showing in list", module); 225 continue; 226 } 227 228 java.sql.Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); 229 230 if (productTo.get("introductionDate") != null && nowTimestamp.before(productTo.getTimestamp("introductionDate"))) { 232 if (Debug.verboseOn()) { 233 String excMsg = "Tried to view the Product " + productTo.getString("productName") + 234 " (productId: " + productTo.getString("productId") + ") as a variant. This product has not yet been made available for sale, so not adding for view."; 235 236 Debug.logVerbose(excMsg, module); 237 } 238 continue; 239 } 240 241 if (productTo.get("salesDiscontinuationDate") != null && nowTimestamp.after(productTo.getTimestamp("salesDiscontinuationDate"))) { 243 if (Debug.verboseOn()) { 244 String excMsg = "Tried to view the Product " + productTo.getString("productName") + 245 " (productId: " + productTo.getString("productId") + ") as a variant. This product is no longer available for sale, so not adding for view."; 246 247 Debug.logVerbose(excMsg, module); 248 } 249 continue; 250 } 251 252 try { 254 Map invReqResult = dispatcher.runSync("isStoreInventoryAvailableOrNotRequired", UtilMisc.toMap("productStoreId", productStoreId, "productId", productIdTo, "quantity", new Double (1.0))); 255 if (ServiceUtil.isError(invReqResult)) { 256 return ServiceUtil.returnError("Error calling the isStoreInventoryRequired when building the variant product tree.", null, null, invReqResult); 257 } else if ("Y".equals((String ) invReqResult.get("availableOrNotRequired"))) { 258 items.add(productIdTo); 259 if (productTo.getString("isVirtual") != null && productTo.getString("isVirtual").equals("Y")) { 260 virtualVariant.add(productIdTo); 261 } 262 } 263 } catch (GenericServiceException e) { 264 String errMsg = "Error calling the isStoreInventoryRequired when building the variant product tree: " + e.toString(); 265 Debug.logError(e, errMsg, module); 266 return ServiceUtil.returnError(errMsg); 267 } 268 } 269 270 String productId = (String ) context.get("productId"); 271 272 List selectableFeatures = null; 274 try { 275 Map fields = UtilMisc.toMap("productId", productId, "productFeatureApplTypeId", "SELECTABLE_FEATURE"); 276 List sort = UtilMisc.toList("sequenceNum"); 277 278 selectableFeatures = delegator.findByAndCache("ProductFeatureAndAppl", fields, sort); 279 selectableFeatures = EntityUtil.filterByDate(selectableFeatures, true); 280 } catch (GenericEntityException e) { 281 Debug.logError(e, module); 282 return ServiceUtil.returnError(UtilProperties.getMessage(resource,"productservices.empty_list_of_selectable_features_found", locale)); 283 } 284 Map features = new HashMap(); 285 Iterator sFIt = selectableFeatures.iterator(); 286 287 while (sFIt.hasNext()) { 288 GenericValue v = (GenericValue) sFIt.next(); 289 String featureType = v.getString("productFeatureTypeId"); 290 String feature = v.getString("description"); 291 292 if (!features.containsKey(featureType)) { 293 List featureList = new LinkedList(); 294 featureList.add(feature); 295 features.put(featureType, featureList); 296 } else { 297 List featureList = (LinkedList) features.get(featureType); 298 featureList.add(feature); 299 features.put(featureType, featureList); 300 } 301 } 302 303 Map tree = null; 304 try { 305 tree = makeGroup(delegator, features, items, featureOrder, 0); 306 } catch (Exception e) { 307 Debug.logError(e, module); 308 return ServiceUtil.returnError(e.getMessage()); 309 } 310 if (tree == null || tree.size() == 0) { 311 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 312 result.put(ModelService.ERROR_MESSAGE, UtilProperties.getMessage(resource,"productservices.feature_grouping_came_back_empty", locale)); 313 } else { 314 result.put("variantTree", tree); 315 result.put("virtualVariant", virtualVariant); 316 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 317 } 318 319 Map sample = null; 320 try { 321 sample = makeVariantSample(dctx.getDelegator(), features, items, (String ) featureOrder.get(0)); 322 } catch (Exception e) { 323 return ServiceUtil.returnError(e.getMessage()); 324 } 325 326 if (sample == null || sample.size() == 0) { 327 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 328 result.put(ModelService.ERROR_MESSAGE, UtilProperties.getMessage(resource,"productservices.feature_sample_came_back_empty", locale)); 329 } else { 330 result.put("variantSample", sample); 331 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 332 } 333 334 return result; 335 } 336 337 340 public static Map prodGetFeatures(DispatchContext dctx, Map context) { 341 GenericDelegator delegator = dctx.getDelegator(); 345 Map result = new HashMap(); 346 String productId = (String ) context.get("productId"); 347 String distinct = (String ) context.get("distinct"); 348 String type = (String ) context.get("type"); 349 Locale locale = (Locale) context.get("locale"); 350 String errMsg=null; 351 Collection features = null; 352 353 try { 354 Map fields = UtilMisc.toMap("productId", productId); 355 List order = UtilMisc.toList("sequenceNum", "productFeatureTypeId"); 356 357 if (distinct != null) fields.put("productFeatureTypeId", distinct); 358 if (type != null) fields.put("productFeatureApplTypeId", type); 359 features = delegator.findByAndCache("ProductFeatureAndAppl", fields, order); 360 result.put("productFeatures", features); 361 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 362 } catch (GenericEntityException e) { 363 Map messageMap = UtilMisc.toMap("errMessage", e.toString()); 364 errMsg = UtilProperties.getMessage(resource,"productservices.problem_reading_product_feature_entity", messageMap, locale); 365 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 366 result.put(ModelService.ERROR_MESSAGE, errMsg); 367 } 368 return result; 369 } 370 371 374 public static Map prodFindProduct(DispatchContext dctx, Map context) { 375 GenericDelegator delegator = dctx.getDelegator(); 377 Map result = new HashMap(); 378 String productId = (String ) context.get("productId"); 379 Locale locale = (Locale) context.get("locale"); 380 String errMsg = null; 381 382 if (productId == null || productId.length() == 0) { 383 errMsg = UtilProperties.getMessage(resource,"productservices.invalid_productId_passed", locale); 384 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 385 result.put(ModelService.ERROR_MESSAGE, errMsg); 386 return result; 387 } 388 389 try { 390 GenericValue product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", productId)); 391 GenericValue mainProduct = product; 392 393 if (product.get("isVariant") != null && product.getString("isVariant").equalsIgnoreCase("Y")) { 394 List c = product.getRelatedByAndCache("AssocProductAssoc", 395 UtilMisc.toMap("productAssocTypeId", "PRODUCT_VARIANT")); 396 397 if (c != null) { 398 c = EntityUtil.filterByDate(c, true); 400 if (c.size() > 0) { 402 GenericValue asV = (GenericValue) c.iterator().next(); 403 404 mainProduct = asV.getRelatedOneCache("MainProduct"); 406 } 408 } 409 } 410 result.put("product", mainProduct); 411 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 412 } catch (GenericEntityException e) { 413 e.printStackTrace(); 414 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 415 errMsg = UtilProperties.getMessage(resource,"productservices.problems_reading_product_entity", messageMap, locale); 416 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 417 result.put(ModelService.ERROR_MESSAGE, errMsg); 418 } 419 420 return result; 421 } 422 423 426 public static Map prodFindAssociatedByType(DispatchContext dctx, Map context) { 427 GenericDelegator delegator = dctx.getDelegator(); 430 Map result = new HashMap(); 431 String productId = (String ) context.get("productId"); 432 String productIdTo = (String ) context.get("productIdTo"); 433 String type = (String ) context.get("type"); 434 Locale locale = (Locale) context.get("locale"); 435 String errMsg = null; 436 437 Boolean cvaBool = (Boolean ) context.get("checkViewAllow"); 438 boolean checkViewAllow = (cvaBool == null ? false : cvaBool.booleanValue()); 439 String prodCatalogId = (String ) context.get("prodCatalogId"); 440 441 if (productId == null && productIdTo == null) { 442 errMsg = UtilProperties.getMessage(resource,"productservices.both_productId_and_productIdTo_cannot_be_null", locale); 443 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 444 result.put(ModelService.ERROR_MESSAGE, errMsg); 445 return result; 446 } 447 448 if (productId != null && productIdTo != null) { 449 errMsg = UtilProperties.getMessage(resource,"productservices.both_productId_and_productIdTo_cannot_be_defined", locale); 450 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 451 result.put(ModelService.ERROR_MESSAGE, errMsg); 452 return result; 453 } 454 455 productId = productId == null ? productIdTo : productId; 456 GenericValue product = null; 457 458 try { 459 product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", productId)); 460 } catch (GenericEntityException e) { 461 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 462 errMsg = UtilProperties.getMessage(resource,"productservices.productservices.problems_reading_product_entity", messageMap, locale); 463 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 464 result.put(ModelService.ERROR_MESSAGE, errMsg); 465 return result; 466 } 467 468 if (product == null) { 469 errMsg = UtilProperties.getMessage(resource,"productservices.problems_getting_product_entity", locale); 470 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 471 result.put(ModelService.ERROR_MESSAGE, errMsg); 472 return result; 473 } 474 475 try { 476 List productAssocs = null; 477 478 if (productIdTo == null) { 479 productAssocs = product.getRelatedCache("MainProductAssoc", UtilMisc.toMap("productAssocTypeId", type), UtilMisc.toList("sequenceNum")); 480 } else { 481 productAssocs = product.getRelatedCache("AssocProductAssoc", UtilMisc.toMap("productAssocTypeId", type), UtilMisc.toList("sequenceNum")); 482 } 483 productAssocs = EntityUtil.filterByDate(productAssocs, true); 485 if (checkViewAllow && prodCatalogId != null && productAssocs != null && productAssocs.size() > 0) { 487 String viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, prodCatalogId); 488 if (viewProductCategoryId != null) { 489 if (productIdTo == null) { 490 productAssocs = CategoryWorker.filterProductsInCategory(delegator, productAssocs, viewProductCategoryId, "productIdTo"); 491 } else { 492 productAssocs = CategoryWorker.filterProductsInCategory(delegator, productAssocs, viewProductCategoryId, "productId"); 493 } 494 } 495 } 496 497 498 result.put("assocProducts", productAssocs); 499 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 500 } catch (GenericEntityException e) { 501 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 502 errMsg = UtilProperties.getMessage(resource,"productservices.problems_product_association_relation_error", messageMap, locale); 503 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 504 result.put(ModelService.ERROR_MESSAGE, errMsg); 505 return result; 506 } 507 508 return result; 509 } 510 511 private static Map makeGroup(GenericDelegator delegator, Map featureList, List items, List order, int index) 513 throws IllegalArgumentException , IllegalStateException { 514 Map tempGroup = new HashMap(); 516 Map group = new LinkedMap(); 517 String orderKey = (String ) order.get(index); 518 519 if (featureList == null) { 520 throw new IllegalArgumentException ("Cannot build feature tree: featureList is null"); 521 } 522 523 if (index < 0) { 524 throw new IllegalArgumentException ("Invalid index '" + index + "' min index '0'"); 525 } 526 if (index + 1 > order.size()) { 527 throw new IllegalArgumentException ("Invalid index '" + index + "' max index '" + (order.size() - 1) + "'"); 528 } 529 530 Iterator itemIterator = items.iterator(); 532 533 while (itemIterator.hasNext()) { 534 String thisItem = (String ) itemIterator.next(); 538 539 if (Debug.verboseOn()) Debug.logVerbose("ThisItem: " + thisItem, module); 540 List features = null; 541 542 try { 543 Map fields = UtilMisc.toMap("productId", thisItem, "productFeatureTypeId", orderKey, 544 "productFeatureApplTypeId", "STANDARD_FEATURE"); 545 List sort = UtilMisc.toList("sequenceNum"); 546 547 features = delegator.findByAndCache("ProductFeatureAndAppl", fields, sort); 549 features = EntityUtil.filterByDate(features, true); 550 } catch (GenericEntityException e) { 551 throw new IllegalStateException ("Problem reading relation: " + e.getMessage()); 552 } 553 if (Debug.verboseOn()) Debug.logVerbose("Features: " + features, module); 554 555 Iterator featuresIterator = features.iterator(); 557 558 while (featuresIterator.hasNext()) { 559 GenericValue item = (GenericValue) featuresIterator.next(); 560 Object itemKey = item.get("description"); 561 562 if (tempGroup.containsKey(itemKey)) { 563 List itemList = (List) tempGroup.get(itemKey); 564 565 if (!itemList.contains(thisItem)) 566 itemList.add(thisItem); 567 } else { 568 List itemList = UtilMisc.toList(thisItem); 569 570 tempGroup.put(itemKey, itemList); 571 } 572 } 573 } 574 if (Debug.verboseOn()) Debug.logVerbose("TempGroup: " + tempGroup, module); 575 576 List orderFeatureList = (List) featureList.get(orderKey); 578 579 if (orderFeatureList == null) { 580 throw new IllegalArgumentException ("Cannot build feature tree: orderFeatureList is null for orderKey=" + orderKey); 581 } 582 583 Iterator featureListIt = orderFeatureList.iterator(); 584 585 while (featureListIt.hasNext()) { 586 String featureStr = (String ) featureListIt.next(); 587 588 if (tempGroup.containsKey(featureStr)) 589 group.put(featureStr, tempGroup.get(featureStr)); 590 } 591 592 if (Debug.verboseOn()) Debug.logVerbose("Group: " + group, module); 593 594 if (group.size() == 0) { 596 return group; 597 } 599 600 if (index + 1 == order.size()) { 601 return group; 602 } 603 604 Iterator groupIterator = group.keySet().iterator(); 606 while (groupIterator.hasNext()) { 607 Object key = groupIterator.next(); 608 List itemList = (List) group.get(key); 609 610 if (itemList != null && itemList.size() > 0) { 611 Map subGroup = makeGroup(delegator, featureList, itemList, order, index + 1); 612 group.put(key, subGroup); 613 } else { 614 } 617 } 618 return group; 619 } 620 621 private static Map makeVariantSample(GenericDelegator delegator, Map featureList, List items, String feature) { 623 Map tempSample = new HashMap(); 624 Map sample = new LinkedMap(); 625 Iterator itemIt = items.iterator(); 626 627 while (itemIt.hasNext()) { 628 String productId = (String ) itemIt.next(); 629 List features = null; 630 631 try { 632 Map fields = UtilMisc.toMap("productId", productId, "productFeatureTypeId", feature, 633 "productFeatureApplTypeId", "STANDARD_FEATURE"); 634 List sort = UtilMisc.toList("sequenceNum", "description"); 635 636 features = delegator.findByAndCache("ProductFeatureAndAppl", fields, sort); 638 features = EntityUtil.filterByDate(features, true); 639 } catch (GenericEntityException e) { 640 throw new IllegalStateException ("Problem reading relation: " + e.getMessage()); 641 } 642 Iterator featureIt = features.iterator(); 643 644 while (featureIt.hasNext()) { 645 GenericValue featureAppl = (GenericValue) featureIt.next(); 646 647 try { 648 GenericValue product = delegator.findByPrimaryKeyCache("Product", 649 UtilMisc.toMap("productId", productId)); 650 651 tempSample.put(featureAppl.getString("description"), product); 652 } catch (GenericEntityException e) { 653 throw new RuntimeException ("Cannot get product entity: " + e.getMessage()); 654 } 655 } 656 } 657 658 List features = (LinkedList) featureList.get(feature); 660 Iterator fi = features.iterator(); 661 662 while (fi.hasNext()) { 663 String f = (String ) fi.next(); 664 665 if (tempSample.containsKey(f)) 666 sample.put(f, tempSample.get(f)); 667 } 668 669 return sample; 670 } 671 672 public static Map quickAddVariant(DispatchContext dctx, Map context) { 673 GenericDelegator delegator = dctx.getDelegator(); 674 Map result = new HashMap(); 675 Locale locale = (Locale) context.get("locale"); 676 String errMsg=null; 677 String productId = (String ) context.get("productId"); 678 String variantProductId = (String ) context.get("productVariantId"); 679 String productFeatureIds = (String ) context.get("productFeatureIds"); 680 681 try { 682 GenericValue product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); 684 if (product == null) { 685 Map messageMap = UtilMisc.toMap("productId", productId); 686 errMsg = UtilProperties.getMessage(resource,"productservices.product_not_found_with_ID", messageMap, locale); 687 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 688 result.put(ModelService.ERROR_MESSAGE, errMsg); 689 return result; 690 } 691 GenericValue variantProduct = delegator.findByPrimaryKey("Product",UtilMisc.toMap("productId", variantProductId)); 693 boolean variantProductExists = (variantProduct != null); 694 if (variantProduct == null) { 695 variantProduct = GenericValue.create(product); 697 variantProduct.set("productId", variantProductId); 698 variantProduct.set("isVirtual", "N"); 699 variantProduct.set("isVariant", "Y"); 700 variantProduct.set("primaryProductCategoryId", null); 701 variantProduct.create(); 703 } else { 704 variantProduct.set("isVirtual", "N"); 706 variantProduct.set("isVariant", "Y"); 707 variantProduct.set("primaryProductCategoryId", null); 708 variantProduct.store(); 710 } 711 GenericValue variantProductAssoc = null; 712 if (variantProductExists) { 713 delegator.removeByAnd("ProductAssoc", UtilMisc.toMap("productIdTo", variantProductId, 716 "productAssocTypeId", "PRODUCT_VARIANT")); 717 delegator.removeByAnd("ProductFeatureAppl", UtilMisc.toMap("productId", variantProductId, 718 "productFeatureApplTypeId", "STANDARD_FEATURE")); 719 } 720 GenericValue productAssoc = delegator.makeValue("ProductAssoc", 722 UtilMisc.toMap("productId", productId, "productIdTo", variantProductId, 723 "productAssocTypeId", "PRODUCT_VARIANT", "fromDate", UtilDateTime.nowTimestamp())); 724 productAssoc.create(); 725 726 java.util.StringTokenizer st = new java.util.StringTokenizer (productFeatureIds, "|"); 728 while (st.hasMoreTokens()) { 729 String productFeatureId = st.nextToken(); 730 731 GenericValue productFeature = delegator.findByPrimaryKey("ProductFeature", UtilMisc.toMap("productFeatureId", productFeatureId)); 732 733 GenericValue productFeatureAppl = delegator.makeValue("ProductFeatureAppl", 734 UtilMisc.toMap("productId", variantProductId, "productFeatureId", productFeatureId, 735 "productFeatureApplTypeId", "STANDARD_FEATURE", "fromDate", UtilDateTime.nowTimestamp())); 736 737 if (productFeature != null) { 739 productFeatureAppl.set("sequenceNum", productFeature.get("defaultSequenceNum")); 740 } 741 742 productFeatureAppl.create(); 743 } 744 745 } catch (GenericEntityException e) { 746 Debug.logError(e, "Entity error creating quick add variant data", module); 747 Map messageMap = UtilMisc.toMap("errMessage", e.toString()); 748 errMsg = UtilProperties.getMessage(resource,"productservices.entity_error_quick_add_variant_data", messageMap, locale); 749 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 750 result.put(ModelService.ERROR_MESSAGE, errMsg); 751 return result; 752 } 753 result.put("productVariantId", variantProductId); 754 return result; 755 } 756 757 761 public static Map quickCreateVirtualWithVariants(DispatchContext dctx, Map context) { 762 GenericDelegator delegator = dctx.getDelegator(); 763 Locale locale = (Locale) context.get("locale"); 764 Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); 765 766 String variantProductIdsBag = (String ) context.get("variantProductIdsBag"); 768 String productFeatureIdOne = (String ) context.get("productFeatureIdOne"); 769 String productFeatureIdTwo = (String ) context.get("productFeatureIdTwo"); 770 String productFeatureIdThree = (String ) context.get("productFeatureIdThree"); 771 772 Map successResult = ServiceUtil.returnSuccess(); 773 774 try { 775 String productId = (String ) context.get("productId"); 777 778 if (UtilValidate.isEmpty(productId)) { 779 productId = "VP" + delegator.getNextSeqId("VirtualProduct"); 780 GenericValue product = delegator.makeValue("Product", null); 782 product.set("productId", productId); 783 product.set("isVirtual", "Y"); 785 product.set("isVariant", "N"); 786 product.set("productTypeId", "FINISHED_GOOD"); 787 product.set("introductionDate", nowTimestamp); 788 product.set("returnable", "Y"); 790 product.set("taxable", "Y"); 791 product.set("chargeShipping", "Y"); 792 product.set("autoCreateKeywords", "Y"); 793 product.set("includeInPromotions", "Y"); 794 product.create(); 796 } 797 successResult.put("productId", productId); 798 799 Set prelimVariantProductIds = new HashSet(); 802 List splitIds = Arrays.asList(variantProductIdsBag.split("[,\\p{Space}]")); 803 Debug.logInfo("Variants: bag=" + variantProductIdsBag, module); 804 Debug.logInfo("Variants: split=" + splitIds, module); 805 prelimVariantProductIds.addAll(splitIds); 806 808 Map variantProductsById = new HashMap(); 809 Iterator variantProductIdIter = prelimVariantProductIds.iterator(); 810 while (variantProductIdIter.hasNext()) { 811 String variantProductId = (String ) variantProductIdIter.next(); 812 if (UtilValidate.isEmpty(variantProductId)) { 813 continue; 815 } 816 GenericValue variantProduct = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", variantProductId)); 818 if (variantProduct != null) { 819 variantProductsById.put(variantProductId, variantProduct); 820 } else { 821 List goodIdentificationList = delegator.findByAnd("GoodIdentification", UtilMisc.toMap("idValue", variantProductId)); 823 if (goodIdentificationList == null || goodIdentificationList.size() == 0) { 824 return ServiceUtil.returnError("Error creating a virtual with variants: the ID [" + variantProductId + "] is not a valid Product.productId or a GoodIdentification.idValue"); 826 } 827 828 if (goodIdentificationList.size() > 1) { 829 Debug.logWarning("Warning creating a virtual with variants: the ID [" + variantProductId + "] was not a productId and resulted in [" + goodIdentificationList.size() + "] GoodIdentification records: " + goodIdentificationList, module); 831 } 832 833 Iterator goodIdentificationIter = goodIdentificationList.iterator(); 834 while (goodIdentificationIter.hasNext()) { 835 GenericValue goodIdentification = (GenericValue) goodIdentificationIter.next(); 836 GenericValue giProduct = goodIdentification.getRelatedOne("Product"); 837 if (giProduct != null) { 838 variantProductsById.put(giProduct.get("productId"), giProduct); 839 } 840 } 841 } 842 } 843 844 Set featureProductIds = new HashSet(); 846 featureProductIds.add(productId); 847 featureProductIds.addAll(variantProductsById.keySet()); 848 Set productFeatureIds = new HashSet(); 849 productFeatureIds.add(productFeatureIdOne); 850 productFeatureIds.add(productFeatureIdTwo); 851 productFeatureIds.add(productFeatureIdThree); 852 853 Iterator featureProductIdIter = featureProductIds.iterator(); 854 while (featureProductIdIter.hasNext()) { 855 Iterator productFeatureIdIter = productFeatureIds.iterator(); 856 String featureProductId = (String ) featureProductIdIter.next(); 857 while (productFeatureIdIter.hasNext()) { 858 String productFeatureId = (String ) productFeatureIdIter.next(); 859 if (UtilValidate.isNotEmpty(productFeatureId)) { 860 GenericValue productFeatureAppl = delegator.makeValue("ProductFeatureAppl", 861 UtilMisc.toMap("productId", featureProductId, "productFeatureId", productFeatureId, 862 "productFeatureApplTypeId", "STANDARD_FEATURE", "fromDate", nowTimestamp)); 863 productFeatureAppl.create(); 864 } 865 } 866 } 867 868 Iterator variantProductIter = variantProductsById.values().iterator(); 869 while (variantProductIter.hasNext()) { 870 GenericValue variantProduct = (GenericValue) variantProductIter.next(); 872 variantProduct.set("isVirtual", "N"); 873 variantProduct.set("isVariant", "Y"); 874 variantProduct.set("introductionDate", nowTimestamp); 875 variantProduct.store(); 876 877 GenericValue productAssoc = delegator.makeValue("ProductAssoc", 879 UtilMisc.toMap("productId", productId, "productIdTo", variantProduct.get("productId"), 880 "productAssocTypeId", "PRODUCT_VARIANT", "fromDate", nowTimestamp)); 881 productAssoc.create(); 882 } 883 } catch (GenericEntityException e) { 884 String errMsg = "Error creating new virtual product from variant products: " + e.toString(); 885 Debug.logError(e, errMsg, module); 886 return ServiceUtil.returnError(errMsg); 887 } 888 return successResult; 889 } 890 891 public static Map updateProductIfAvailableFromShipment(DispatchContext dctx, Map context) { 892 if ("Y".equals(UtilProperties.getPropertyValue("catalog.properties", "reactivate.product.from.receipt", "N"))) { 893 LocalDispatcher dispatcher = dctx.getDispatcher(); 894 GenericDelegator delegator = dctx.getDelegator(); 895 GenericValue userLogin = (GenericValue) context.get("userLogin"); 896 String inventoryItemId = (String ) context.get("inventoryItemId"); 897 898 GenericValue inventoryItem = null; 899 try { 900 inventoryItem = delegator.findByPrimaryKeyCache("InventoryItem", UtilMisc.toMap("inventoryItemId", inventoryItemId)); 901 } catch (GenericEntityException e) { 902 Debug.logError(e, module); 903 return ServiceUtil.returnError(e.getMessage()); 904 } 905 906 if (inventoryItem != null) { 907 String productId = inventoryItem.getString("productId"); 908 GenericValue product = null; 909 try { 910 product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", productId)); 911 } catch (GenericEntityException e) { 912 Debug.logError(e, module); 913 return ServiceUtil.returnError(e.getMessage()); 914 } 915 916 if (product != null) { 917 Timestamp salesDiscontinuationDate = product.getTimestamp("salesDiscontinuationDate"); 918 if (salesDiscontinuationDate != null && salesDiscontinuationDate.before(UtilDateTime.nowTimestamp())) { 919 Map invRes = null; 920 try { 921 invRes = dispatcher.runSync("getProductInventoryAvailable", UtilMisc.toMap("productId", productId, "userLogin", userLogin)); 922 } catch (GenericServiceException e) { 923 Debug.logError(e, module); 924 return ServiceUtil.returnError(e.getMessage()); 925 } 926 927 Double availableToPromiseTotal = (Double ) invRes.get("availableToPromiseTotal"); 928 if (availableToPromiseTotal != null && availableToPromiseTotal.doubleValue() > 0) { 929 GenericValue productToUpdate = null; 931 try { 932 productToUpdate = delegator.findByPrimaryKey("Product", product.getPrimaryKey()); 933 } catch (GenericEntityException e) { 934 Debug.logError(e, module); 935 return ServiceUtil.returnError(e.getMessage()); 936 } 937 938 productToUpdate.set("salesDiscontinuationDate", null); 940 try { 941 delegator.store(productToUpdate); 942 } catch (GenericEntityException e) { 943 Debug.logError(e, module); 944 return ServiceUtil.returnError(e.getMessage()); 945 } 946 } 947 } 948 } 949 } 950 } 951 952 return ServiceUtil.returnSuccess(); 953 } 954 } 955 956 | Popular Tags |