1 25 package org.ofbiz.party.party; 26 27 import java.sql.Timestamp ; 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Locale ; 34 import java.util.Map ; 35 36 import javolution.util.FastList; 37 import org.ofbiz.base.util.Debug; 38 import org.ofbiz.base.util.UtilDateTime; 39 import org.ofbiz.base.util.UtilMisc; 40 import org.ofbiz.base.util.UtilProperties; 41 import org.ofbiz.base.util.UtilValidate; 42 import org.ofbiz.entity.GenericDelegator; 43 import org.ofbiz.entity.GenericEntityException; 44 import org.ofbiz.entity.GenericValue; 45 import org.ofbiz.entity.condition.EntityCondition; 46 import org.ofbiz.entity.condition.EntityConditionList; 47 import org.ofbiz.entity.condition.EntityExpr; 48 import org.ofbiz.entity.condition.EntityFieldValue; 49 import org.ofbiz.entity.condition.EntityFunction; 50 import org.ofbiz.entity.condition.EntityOperator; 51 import org.ofbiz.entity.model.DynamicViewEntity; 52 import org.ofbiz.entity.model.ModelKeyMap; 53 import org.ofbiz.entity.util.EntityFindOptions; 54 import org.ofbiz.entity.util.EntityListIterator; 55 import org.ofbiz.entity.util.EntityTypeUtil; 56 import org.ofbiz.entity.util.EntityUtil; 57 import org.ofbiz.security.Security; 58 import org.ofbiz.service.DispatchContext; 59 import org.ofbiz.service.ModelService; 60 import org.ofbiz.service.ServiceUtil; 61 import org.ofbiz.service.LocalDispatcher; 62 import org.ofbiz.service.GenericServiceException; 63 64 72 public class PartyServices { 73 74 public static final String module = PartyServices.class.getName(); 75 public static final String resource = "PartyUiLabels"; 76 77 83 public static Map deleteParty(DispatchContext ctx, Map context) { 84 85 Locale locale = (Locale ) context.get("locale"); 86 87 100 101 String errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_delete_party_not_implemented", locale); 102 return ServiceUtil.returnError(errMsg); 103 } 104 105 112 public static Map createPerson(DispatchContext ctx, Map context) { 113 Map result = new HashMap (); 114 GenericDelegator delegator = ctx.getDelegator(); 115 Timestamp now = UtilDateTime.nowTimestamp(); 116 List toBeStored = new LinkedList (); 117 Locale locale = (Locale ) context.get("locale"); 118 GenericValue userLogin = (GenericValue) context.get("userLogin"); 120 121 String partyId = (String ) context.get("partyId"); 122 123 if (partyId != null && partyId.length() > 0 && Character.isDigit(partyId.charAt(0))) { 125 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "party.id_is_digit", locale)); 126 } 127 128 if (partyId == null || partyId.length() == 0) { 130 try { 131 partyId = delegator.getNextSeqId("Party"); 132 } catch (IllegalArgumentException e) { 133 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "party.id_generation_failure", locale)); 134 } 135 } 136 137 GenericValue party = null; 139 140 try { 141 party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId)); 142 } catch (GenericEntityException e) { 143 Debug.logWarning(e.getMessage(), module); 144 } 145 146 if (party != null) { 147 if (!"PERSON".equals(party.getString("partyTypeId"))) { 148 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.create.party_exists_not_person_type", locale)); 149 } 150 } else { 151 String statusId = (String ) context.get("statusId"); 153 Map newPartyMap = UtilMisc.toMap("partyId", partyId, "partyTypeId", "PERSON", "createdDate", now, "lastModifiedDate", now, "statusId", statusId); 154 if (userLogin != null) { 155 newPartyMap.put("createdByUserLogin", userLogin.get("userLoginId")); 156 newPartyMap.put("lastModifiedByUserLogin", userLogin.get("userLoginId")); 157 } 158 party = delegator.makeValue("Party", newPartyMap); 159 toBeStored.add(party); 160 } 161 162 GenericValue person = null; 163 164 try { 165 person = delegator.findByPrimaryKey("Person", UtilMisc.toMap("partyId", partyId)); 166 } catch (GenericEntityException e) { 167 Debug.logWarning(e.getMessage(), module); 168 } 169 170 if (person != null) { 171 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.create.person_exists", locale)); 172 } 173 174 person = delegator.makeValue("Person", UtilMisc.toMap("partyId", partyId)); 175 person.setNonPKFields(context); 176 toBeStored.add(person); 177 178 try { 179 delegator.storeAll(toBeStored); 180 } catch (GenericEntityException e) { 181 Debug.logWarning(e.getMessage(), module); 182 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.create.db_error", new Object [] { e.getMessage() }, locale)); 183 } 184 185 result.put("partyId", partyId); 186 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 187 return result; 188 } 189 190 194 public static Map setPartyStatus(DispatchContext ctx, Map context) { 195 Map result = new HashMap (); 196 GenericDelegator delegator = ctx.getDelegator(); 197 Security security = ctx.getSecurity(); 198 GenericValue userLogin = (GenericValue) context.get("userLogin"); 199 Locale locale = (Locale ) context.get("locale"); 200 201 String partyId = (String ) context.get("partyId"); 202 String statusId = (String ) context.get("statusId"); 203 Timestamp statusDate = (Timestamp ) context.get("statusDate"); 204 if (statusDate == null) statusDate = UtilDateTime.nowTimestamp(); 205 206 if (!security.hasEntityPermission("PARTYMGR", "_STS_UPDATE", userLogin)) { 208 String errorMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.no_permission_to_operation", locale) + "."; 209 Debug.logWarning(errorMsg, module); 210 return ServiceUtil.returnError(errorMsg); 211 } 212 try { 213 GenericValue party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId)); 214 215 GenericValue statusValidChange = delegator.findByPrimaryKey("StatusValidChange", UtilMisc.toMap("statusId", party.getString("statusId"), "statusIdTo", statusId)); 217 if (statusValidChange == null) { 218 String errorMsg = "Cannot change party status from " + party.getString("statusId") + " to " + statusId; 219 Debug.logWarning(errorMsg, module); 220 return ServiceUtil.returnError(errorMsg); 221 } 222 223 String oldStatusId = party.getString("statusId"); 225 party.set("statusId", statusId); 226 party.store(); 227 228 GenericValue partyStatus = delegator.makeValue("PartyStatus", UtilMisc.toMap("partyId", partyId, "statusId", statusId, "statusDate", statusDate)); 230 partyStatus.create(); 231 232 Map results = ServiceUtil.returnSuccess(); 233 results.put("oldStatusId", oldStatusId); 234 return results; 235 } catch (GenericEntityException e) { 236 Debug.logError(e, e.getMessage(), module); 237 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.update.write_failure", new Object [] { e.getMessage() }, locale)); 238 } 239 } 240 241 248 public static Map updatePerson(DispatchContext ctx, Map context) { 249 Map result = new HashMap (); 250 GenericDelegator delegator = ctx.getDelegator(); 251 Security security = ctx.getSecurity(); 252 GenericValue userLogin = (GenericValue) context.get("userLogin"); 253 Locale locale = (Locale ) context.get("locale"); 254 255 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_GRP_UPDATE"); 256 257 if (result.size() > 0) 258 return result; 259 260 GenericValue person = null; 261 GenericValue party = null; 262 263 try { 264 person = delegator.findByPrimaryKey("Person", UtilMisc.toMap("partyId", partyId)); 265 party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId)); 266 } catch (GenericEntityException e) { 267 Debug.logWarning(e, module); 268 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.update.read_failure", new Object [] { e.getMessage() }, locale)); 269 } 270 271 if (person == null || party == null) { 272 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.update.not_found", locale)); 273 } 274 275 person.setNonPKFields(context); 276 party.setNonPKFields(context); 277 278 try { 279 person.store(); 280 party.store(); 281 } catch (GenericEntityException e) { 282 Debug.logWarning(e.getMessage(), module); 283 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.update.write_failure", new Object [] { e.getMessage() }, locale)); 284 } 285 286 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 287 result.put(ModelService.SUCCESS_MESSAGE, UtilProperties.getMessage(resource, "person.update.success", locale)); 288 return result; 289 } 290 291 298 public static Map createPartyGroup(DispatchContext ctx, Map context) { 299 Map result = new HashMap (); 300 GenericDelegator delegator = ctx.getDelegator(); 301 GenericValue userLogin = (GenericValue) context.get("userLogin"); 302 Timestamp now = UtilDateTime.nowTimestamp(); 303 304 String partyId = (String ) context.get("partyId"); 305 Locale locale = (Locale ) context.get("locale"); 306 String errMsg = null; 307 308 if (partyId == null || partyId.length() == 0) { 310 try { 311 partyId = delegator.getNextSeqId("Party"); 312 } catch (IllegalArgumentException e) { 313 errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_create_party_group_generation_failure", locale); 314 return ServiceUtil.returnError(errMsg); 315 } 316 } else { 317 if (Character.isDigit(partyId.charAt(0))) { 319 errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_create_party_ID_digit", locale); 320 return ServiceUtil.returnError(errMsg); 321 } 322 } 323 324 try { 325 GenericValue party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId)); 327 GenericValue partyGroupPartyType = delegator.findByPrimaryKeyCache("PartyType", UtilMisc.toMap("partyTypeId", "PARTY_GROUP")); 328 329 if (partyGroupPartyType == null) { 330 errMsg = UtilProperties.getMessage(resource,"partyservices.party_type_not_found_in_database_cannot_create_party_group", locale); 331 return ServiceUtil.returnError(errMsg); 332 } 333 334 if (party != null) { 335 GenericValue partyType = party.getRelatedOneCache("PartyType"); 336 337 if (!EntityTypeUtil.isType(partyType, partyGroupPartyType)) { 338 errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_create_party_group_already_exists_not_PARTY_GROUP_type", locale); 339 return ServiceUtil.returnError(errMsg); 340 } 341 } else { 342 String partyTypeId = "PARTY_GROUP"; 344 345 if (UtilValidate.isNotEmpty(((String ) context.get("partyTypeId")))) { 346 GenericValue desiredPartyType = delegator.findByPrimaryKeyCache("PartyType", UtilMisc.toMap("partyTypeId", context.get("partyTypeId"))); 347 if (desiredPartyType != null && EntityTypeUtil.isType(desiredPartyType, partyGroupPartyType)) { 348 partyTypeId = desiredPartyType.getString("partyTypeId"); 349 } else { 350 return ServiceUtil.returnError("The specified partyTypeId [" + context.get("partyTypeId") + "] could not be found or is not a sub-type of PARTY_GROUP"); 351 } 352 } 353 354 Map newPartyMap = UtilMisc.toMap("partyId", partyId, "partyTypeId", partyTypeId, "createdDate", now, "lastModifiedDate", now); 355 if (userLogin != null) { 356 newPartyMap.put("createdByUserLogin", userLogin.get("userLoginId")); 357 newPartyMap.put("lastModifiedByUserLogin", userLogin.get("userLoginId")); 358 } 359 party = delegator.makeValue("Party", newPartyMap); 360 party.setNonPKFields(context); 361 party.create(); 362 } 363 364 GenericValue partyGroup = delegator.findByPrimaryKey("PartyGroup", UtilMisc.toMap("partyId", partyId)); 365 if (partyGroup != null) { 366 errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_create_party_group_already_exists", locale); 367 return ServiceUtil.returnError(errMsg); 368 } 369 370 partyGroup = delegator.makeValue("PartyGroup", UtilMisc.toMap("partyId", partyId)); 371 partyGroup.setNonPKFields(context); 372 partyGroup.create(); 373 } catch (GenericEntityException e) { 374 Debug.logWarning(e, module); 375 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 376 errMsg = UtilProperties.getMessage(resource,"partyservices.data_source_error_adding_party_group", messageMap, locale); 377 return ServiceUtil.returnError(errMsg); 378 } 379 380 result.put("partyId", partyId); 381 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 382 return result; 383 } 384 385 391 public static Map updatePartyGroup(DispatchContext ctx, Map context) { 392 Map result = new HashMap (); 393 GenericDelegator delegator = ctx.getDelegator(); 394 Security security = ctx.getSecurity(); 395 GenericValue userLogin = (GenericValue) context.get("userLogin"); 396 397 String partyId = null; 399 if (security.hasEntityPermission("PARTYMGR", "_GRP_UPDATE", userLogin)) { 400 partyId = (String ) context.get("partyId"); 401 } else { 402 partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_UPDATE"); 403 } 404 Locale locale = (Locale ) context.get("locale"); 405 String errMsg = null; 406 407 if (result.size() > 0) 408 return result; 409 410 GenericValue partyGroup = null; 411 GenericValue party = null; 412 413 try { 414 partyGroup = delegator.findByPrimaryKey("PartyGroup", UtilMisc.toMap("partyId", partyId)); 415 party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId)); 416 } catch (GenericEntityException e) { 417 Debug.logWarning(e, module); 418 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 419 errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_update_party_information_read", messageMap, locale); 420 return ServiceUtil.returnError(errMsg); 421 } 422 423 if (partyGroup == null || party == null) { 424 errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_update_party_information_not_found", locale); 425 return ServiceUtil.returnError(errMsg); 426 } 427 428 partyGroup.setNonPKFields(context); 429 party.setNonPKFields(context); 430 431 try { 432 partyGroup.store(); 433 party.store(); 434 } catch (GenericEntityException e) { 435 Debug.logWarning(e.getMessage(), module); 436 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 437 errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_update_party_information_write", messageMap, locale); 438 return ServiceUtil.returnError(errMsg); 439 } 440 441 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 442 return result; 443 } 444 445 451 public static Map createAffiliate(DispatchContext ctx, Map context) { 452 Map result = new HashMap (); 453 GenericDelegator delegator = ctx.getDelegator(); 454 GenericValue userLogin = (GenericValue) context.get("userLogin"); 455 Timestamp now = UtilDateTime.nowTimestamp(); 456 457 String partyId = (String ) context.get("partyId"); 458 Locale locale = (Locale ) context.get("locale"); 459 String errMsg = null; 460 461 if (partyId == null || partyId.length() == 0) { 462 partyId = userLogin.getString("partyId"); 463 } 464 465 if (Character.isDigit(partyId.charAt(0))) { 467 errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_create_affiliate_digit", locale); 468 return ServiceUtil.returnError(errMsg); 469 } 470 471 if (partyId == null || partyId.length() == 0) { 473 try { 474 partyId = delegator.getNextSeqId("Party"); 475 } catch (IllegalArgumentException e) { 476 errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_create_affiliate_generation_failure", locale); 477 return ServiceUtil.returnError(errMsg); 478 } 479 } 480 481 GenericValue party = null; 483 484 try { 485 party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId)); 486 } catch (GenericEntityException e) { 487 Debug.logWarning(e.getMessage(), module); 488 } 489 490 if (party == null) { 491 errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_create_affiliate_no_party_entity", locale); 492 return ServiceUtil.returnError(errMsg); 493 } 494 495 GenericValue affiliate = null; 496 497 try { 498 affiliate = delegator.findByPrimaryKey("Affiliate", UtilMisc.toMap("partyId", partyId)); 499 } catch (GenericEntityException e) { 500 Debug.logWarning(e.getMessage(), module); 501 } 502 503 if (affiliate != null) { 504 errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_create_affiliate_ID_already_exists", locale); 505 return ServiceUtil.returnError(errMsg); 506 } 507 508 affiliate = delegator.makeValue("Affiliate", UtilMisc.toMap("partyId", partyId)); 509 affiliate.setNonPKFields(context); 510 affiliate.set("dateTimeCreated", now, false); 511 512 try { 513 delegator.create(affiliate); 514 } catch (GenericEntityException e) { 515 Debug.logWarning(e.getMessage(), module); 516 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 517 errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_add_affiliate_info_write", messageMap, locale); 518 return ServiceUtil.returnError(errMsg); 519 } 520 521 result.put("partyId", partyId); 522 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 523 return result; 524 } 525 526 533 public static Map updateAffiliate(DispatchContext ctx, Map context) { 534 Map result = new HashMap (); 535 GenericDelegator delegator = ctx.getDelegator(); 536 Security security = ctx.getSecurity(); 537 GenericValue userLogin = (GenericValue) context.get("userLogin"); 538 539 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_UPDATE"); 540 Locale locale = (Locale ) context.get("locale"); 541 String errMsg = null; 542 543 if (result.size() > 0) 544 return result; 545 546 GenericValue affiliate = null; 547 548 try { 549 affiliate = delegator.findByPrimaryKey("Affiliate", UtilMisc.toMap("partyId", partyId)); 550 } catch (GenericEntityException e) { 551 Debug.logWarning(e, module); 552 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 553 errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_update_affiliate_information_read", messageMap, locale); 554 return ServiceUtil.returnError(errMsg); 555 } 556 557 if (affiliate == null) { 558 errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_update_affiliate_information_not_found", locale); 559 return ServiceUtil.returnError(errMsg); 560 } 561 562 affiliate.setNonPKFields(context); 563 564 try { 565 affiliate.store(); 566 } catch (GenericEntityException e) { 567 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 568 errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_update_affiliate_information_write", messageMap, locale); 569 return ServiceUtil.returnError(errMsg); 570 } 571 return ServiceUtil.returnSuccess(); 572 } 573 574 580 public static Map createPartyNote(DispatchContext dctx, Map context) { 581 Map result = new HashMap (); 582 GenericDelegator delegator = dctx.getDelegator(); 583 LocalDispatcher dispatcher = dctx.getDispatcher(); 584 GenericValue userLogin = (GenericValue) context.get("userLogin"); 585 String noteString = (String ) context.get("note"); 586 String partyId = (String ) context.get("partyId"); 587 String noteId = (String ) context.get("noteId"); 588 String errMsg = null; 589 Locale locale = (Locale ) context.get("locale"); 590 592 if (noteId == null) { 594 Map noteRes = null; 595 try { 596 noteRes = dispatcher.runSync("createNote", UtilMisc.toMap("partyId", userLogin.getString("partyId"), "note", noteString, "userLogin", userLogin, "locale", locale)); 597 } catch (GenericServiceException e) { 598 Debug.logError(e, e.getMessage(), module); 599 return ServiceUtil.returnError("Unable to create Note: " + e.getMessage()); 600 } 601 602 if (noteRes.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) 603 return noteRes; 604 605 noteId = (String ) noteRes.get("noteId"); 606 607 if (noteId == null || noteId.length() == 0) { 608 errMsg = UtilProperties.getMessage(resource,"partyservices.problem_creating_note_no_noteId_returned", locale); 609 return ServiceUtil.returnError(errMsg); 610 } 611 } 612 result.put("noteId", noteId); 613 614 try { 616 Map fields = UtilMisc.toMap("partyId", partyId, "noteId", noteId); 617 GenericValue v = delegator.makeValue("PartyNote", fields); 618 619 delegator.create(v); 620 } catch (GenericEntityException ee) { 621 Debug.logError(ee, module); 622 Map messageMap = UtilMisc.toMap("errMessage", ee.getMessage()); 623 errMsg = UtilProperties.getMessage(resource,"partyservices.problem_associating_note_with_party", messageMap, locale); 624 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 625 result.put(ModelService.ERROR_MESSAGE, errMsg); 626 } 627 return result; 628 } 629 630 636 public static Map getPartyFromEmail(DispatchContext dctx, Map context) { 637 Map result = new HashMap (); 638 GenericDelegator delegator = dctx.getDelegator(); 639 Collection parties = new LinkedList (); 640 String email = (String ) context.get("email"); 641 Locale locale = (Locale ) context.get("locale"); 642 String errMsg = null; 643 644 if (email.length() == 0){ 645 errMsg = UtilProperties.getMessage(resource,"partyservices.required_parameter_email_cannot_be_empty", locale); 646 return ServiceUtil.returnError(errMsg); 647 } 648 649 try { 650 List exprs = new LinkedList (); 651 652 exprs.add(new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue("infoString")), EntityOperator.LIKE, new EntityFunction.UPPER("%" + email.toUpperCase() + "%"))); 653 List c = EntityUtil.filterByDate(delegator.findByAnd("PartyAndContactMech", exprs, UtilMisc.toList("infoString")), true); 654 655 if (Debug.verboseOn()) Debug.logVerbose("List: " + c, module); 656 if (Debug.infoOn()) Debug.logInfo("PartyFromEmail number found: " + c.size(), module); 657 if (c != null) { 658 Iterator i = c.iterator(); 659 660 while (i.hasNext()) { 661 GenericValue pacm = (GenericValue) i.next(); 662 GenericValue party = delegator.makeValue("Party", UtilMisc.toMap("partyId", pacm.get("partyId"), "partyTypeId", pacm.get("partyTypeId"))); 663 664 parties.add(UtilMisc.toMap("party", party)); 665 } 666 } 667 } catch (GenericEntityException e) { 668 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 669 errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_get_party_entities_read", messageMap, locale); 670 return ServiceUtil.returnError(errMsg); 671 } 672 if (parties.size() > 0) 673 result.put("parties", parties); 674 return result; 675 } 676 677 683 public static Map getPartyFromUserLogin(DispatchContext dctx, Map context) { 684 Debug.logWarning("Running the getPartyFromUserLogin Service...", module); 685 Map result = new HashMap (); 686 GenericDelegator delegator = dctx.getDelegator(); 687 Collection parties = new LinkedList (); 688 String userLoginId = (String ) context.get("userLoginId"); 689 Locale locale = (Locale ) context.get("locale"); 690 691 if (userLoginId.length() == 0) 692 return ServiceUtil.returnError("Required parameter 'userLoginId' cannot be empty."); 693 694 try { 695 List exprs = new LinkedList (); 696 697 exprs.add(new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue("userLoginId")), EntityOperator.LIKE, new EntityFunction.UPPER("%" + userLoginId.toUpperCase() + "%"))); 698 Collection ulc = delegator.findByAnd("PartyAndUserLogin", exprs, UtilMisc.toList("userloginId")); 699 700 if (Debug.verboseOn()) Debug.logVerbose("Collection: " + ulc, module); 701 if (Debug.infoOn()) Debug.logInfo("PartyFromUserLogin number found: " + ulc.size(), module); 702 if (ulc != null) { 703 Iterator i = ulc.iterator(); 704 705 while (i.hasNext()) { 706 GenericValue ul = (GenericValue) i.next(); 707 GenericValue party = delegator.makeValue("Party", UtilMisc.toMap("partyId", ul.get("partyId"), "partyTypeId", ul.get("partyTypeId"))); 708 709 parties.add(UtilMisc.toMap("party", party)); 710 } 711 } 712 } catch (GenericEntityException e) { 713 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 714 String errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_get_party_entities_read", messageMap, locale); 715 return ServiceUtil.returnError(errMsg); 716 } 717 if (parties.size() > 0) { 718 result.put("parties", parties); 719 } 720 return result; 721 } 722 723 729 public static Map getPartyFromPerson(DispatchContext dctx, Map context) { 730 Map result = new HashMap (); 731 GenericDelegator delegator = dctx.getDelegator(); 732 Collection parties = new LinkedList (); 733 String firstName = (String ) context.get("firstName"); 734 String lastName = (String ) context.get("lastName"); 735 Locale locale = (Locale ) context.get("locale"); 736 737 if (firstName == null) { 738 firstName = ""; 739 } 740 if (lastName == null) { 741 lastName = ""; 742 } 743 if (firstName.length() == 0 && lastName.length() == 0){ 744 String errMsg = UtilProperties.getMessage(resource,"partyservices.both_names_cannot_be_empty", locale); 745 return ServiceUtil.returnError(errMsg); 746 } 747 748 try { 749 List exprs = new LinkedList (); 750 751 exprs.add(new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue("firstName")), EntityOperator.LIKE, new EntityFunction.UPPER("%" + firstName.toUpperCase() + "%"))); 752 exprs.add(new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue("lastName")), EntityOperator.LIKE, new EntityFunction.UPPER("%" + lastName.toUpperCase() + "%"))); 753 Collection pc = delegator.findByAnd("Person", exprs, UtilMisc.toList("lastName", "firstName", "partyId")); 754 755 if (Debug.infoOn()) Debug.logInfo("PartyFromPerson number found: " + pc.size(), module); 756 if (pc != null) { 757 Iterator i = pc.iterator(); 758 759 while (i.hasNext()) { 760 GenericValue person = (GenericValue) i.next(); 761 GenericValue party = delegator.makeValue("Party", UtilMisc.toMap("partyId", person.get("partyId"), "partyTypeId", "PERSON")); 762 763 parties.add(UtilMisc.toMap("person", person, "party", party)); 764 } 765 } 766 } catch (GenericEntityException e) { 767 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 768 String errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_get_party_entities_read", messageMap, locale); 769 return ServiceUtil.returnError(errMsg); 770 } 771 if (parties.size() > 0) { 772 result.put("parties", parties); 773 } 774 return result; 775 } 776 777 783 public static Map getPartyFromPartyGroup(DispatchContext dctx, Map context) { 784 Map result = new HashMap (); 785 GenericDelegator delegator = dctx.getDelegator(); 786 Collection parties = new LinkedList (); 787 String groupName = (String ) context.get("groupName"); 788 Locale locale = (Locale ) context.get("locale"); 789 790 if (groupName.length() == 0) { 791 return ServiceUtil.returnError("Required parameter 'groupName' cannot be empty."); 792 } 793 794 try { 795 List exprs = new LinkedList (); 796 797 exprs.add(new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue("groupName")), EntityOperator.LIKE, new EntityFunction.UPPER("%" + groupName.toUpperCase() + "%"))); 798 Collection pc = delegator.findByAnd("PartyGroup", exprs, UtilMisc.toList("groupName", "partyId")); 799 800 if (Debug.infoOn()) Debug.logInfo("PartyFromGroup number found: " + pc.size(), module); 801 if (pc != null) { 802 Iterator i = pc.iterator(); 803 804 while (i.hasNext()) { 805 GenericValue group = (GenericValue) i.next(); 806 GenericValue party = delegator.makeValue("Party", UtilMisc.toMap("partyId", group.get("partyId"), "partyTypeId", "PARTY_GROUP")); 807 808 parties.add(UtilMisc.toMap("partyGroup", group, "party", party)); 809 } 810 } 811 } catch (GenericEntityException e) { 812 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 813 String errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_get_party_entities_read", messageMap, locale); 814 return ServiceUtil.returnError(errMsg); 815 } 816 if (parties.size() > 0) { 817 result.put("parties", parties); 818 } 819 return result; 820 } 821 822 public static Map getPerson(DispatchContext dctx, Map context) { 823 Map result = new HashMap (); 824 GenericDelegator delegator = dctx.getDelegator(); 825 String partyId = (String ) context.get("partyId"); 826 GenericValue person = null; 827 828 try { 829 person = delegator.findByPrimaryKeyCache("Person", UtilMisc.toMap("partyId", partyId)); 830 } catch (GenericEntityException e) { 831 return ServiceUtil.returnError("Cannot get person entity (read failure): " + e.getMessage()); 832 } 833 if (person != null) { 834 result.put("lookupPerson", person); 835 } 836 return result; 837 } 838 839 public static Map createRoleType(DispatchContext dctx, Map context) { 840 Map result = new HashMap (); 841 GenericDelegator delegator = dctx.getDelegator(); 842 GenericValue roleType = null; 843 844 try { 845 roleType = delegator.makeValue("RoleType", null); 846 roleType.setPKFields(context); 847 roleType.setNonPKFields(context); 848 roleType = delegator.create(roleType); 849 } catch (GenericEntityException e) { 850 Debug.logError(e, module); 851 return ServiceUtil.returnError("Cannot create role type entity (write failure): " + e.getMessage()); 852 } 853 if (roleType != null) { 854 result.put("roleType", roleType); 855 } 856 return result; 857 } 858 859 public static Map createPartyDataSource(DispatchContext ctx, Map context) { 860 GenericDelegator delegator = ctx.getDelegator(); 861 Security security = ctx.getSecurity(); 862 GenericValue userLogin = (GenericValue) context.get("userLogin"); 863 Locale locale = (Locale ) context.get("locale"); 864 865 String partyId = (String ) context.get("partyId"); 867 String dataSourceId = (String ) context.get("dataSourceId"); 868 Timestamp fromDate = (Timestamp ) context.get("fromDate"); 869 if (fromDate == null) fromDate = UtilDateTime.nowTimestamp(); 870 871 if (!security.hasEntityPermission("PARTYMGR", "_SRC_CREATE", userLogin)) { 873 String errorMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.no_permission_to_operation", locale) + "."; 874 return ServiceUtil.returnError(errorMsg); 875 } 876 try { 877 GenericValue party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId)); 879 GenericValue dataSource = delegator.findByPrimaryKey("DataSource", UtilMisc.toMap("dataSourceId", dataSourceId)); 880 if (party == null || dataSource == null) { 881 List errorList = UtilMisc.toList("Cannot create PartyDataSource"); 882 if (party == null) errorList.add("party with ID [" + partyId + "] was not found "); 883 if (dataSource == null) errorList.add("data source with ID [" + dataSourceId + "] was not found "); 884 return ServiceUtil.returnError(errorList); 885 } 886 887 GenericValue partyDataSource = delegator.makeValue("PartyDataSource", UtilMisc.toMap("partyId", partyId, "dataSourceId", dataSourceId, "fromDate", fromDate)); 889 partyDataSource.create(); 890 891 } catch (GenericEntityException e) { 892 Debug.logError(e, e.getMessage(), module); 893 return ServiceUtil.returnError(e.getMessage()); 894 } 895 return ServiceUtil.returnSuccess(); 896 } 897 898 public static Map findParty(DispatchContext dctx, Map context) { 899 Map result = ServiceUtil.returnSuccess(); 900 GenericDelegator delegator = dctx.getDelegator(); 901 902 String extInfo = (String ) context.get("extInfo"); 903 904 try { 906 List roleTypes = delegator.findAll("RoleType", UtilMisc.toList("description")); 907 result.put("roleTypes", roleTypes); 908 } catch (GenericEntityException e) { 909 String errMsg = "Error looking up RoleTypes: " + e.toString(); 910 Debug.logError(e, errMsg, module); 911 return ServiceUtil.returnError(errMsg); 912 } 913 914 String roleTypeId; 916 try { 917 roleTypeId = (String ) context.get("roleTypeId"); 918 if (roleTypeId != null && roleTypeId.length() > 0) { 919 GenericValue currentRole = delegator.findByPrimaryKeyCache("RoleType", UtilMisc.toMap("roleTypeId", roleTypeId)); 920 result.put("currentRole", currentRole); 921 } 922 } catch (GenericEntityException e) { 923 String errMsg = "Error looking up current RoleType: " + e.toString(); 924 Debug.logError(e, errMsg, module); 925 return ServiceUtil.returnError(errMsg); 926 } 927 928 String stateProvinceGeoId; 930 try { 931 stateProvinceGeoId = (String ) context.get("stateProvinceGeoId"); 932 if (stateProvinceGeoId != null && stateProvinceGeoId.length() > 0) { 933 GenericValue currentStateGeo = delegator.findByPrimaryKeyCache("Geo", UtilMisc.toMap("geoId", stateProvinceGeoId)); 934 result.put("currentStateGeo", currentStateGeo); 935 } 936 } catch (GenericEntityException e) { 937 String errMsg = "Error looking up current stateProvinceGeo: " + e.toString(); 938 Debug.logError(e, errMsg, module); 939 return ServiceUtil.returnError(errMsg); 940 } 941 942 int viewIndex = 1; 944 try { 945 viewIndex = Integer.parseInt((String ) context.get("VIEW_INDEX")); 946 } catch (Exception e) { 947 viewIndex = 1; 948 } 949 result.put("viewIndex", new Integer (viewIndex)); 950 951 int viewSize = 20; 952 try { 953 viewSize = Integer.parseInt((String ) context.get("VIEW_SIZE")); 954 } catch (Exception e) { 955 viewSize = 20; 956 } 957 result.put("viewSize", new Integer (viewSize)); 958 959 String lookupFlag = (String ) context.get("lookupFlag"); 961 962 String paramList = ""; 964 965 List partyList = null; 966 int partyListSize = 0; 967 int lowIndex = 0; 968 int highIndex = 0; 969 970 if ("Y".equals(lookupFlag)) { 971 String showAll = (context.get("showAll") != null ? (String ) context.get("showAll") : "N"); 972 paramList = paramList + "&lookupFlag=" + lookupFlag + "&showAll=" + showAll + "&extInfo=" + extInfo; 973 974 DynamicViewEntity dynamicView = new DynamicViewEntity(); 976 977 dynamicView.addMemberEntity("PT", "Party"); 979 dynamicView.addAlias("PT", "partyId"); 980 dynamicView.addAlias("PT", "partyTypeId"); 981 dynamicView.addRelation("one-nofk", "", "PartyType", ModelKeyMap.makeKeyMapList("partyTypeId")); 982 dynamicView.addRelation("many", "", "UserLogin", ModelKeyMap.makeKeyMapList("partyId")); 983 984 List andExprs = FastList.newInstance(); 986 EntityCondition mainCond = null; 987 988 List orderBy = FastList.newInstance(); 989 List fieldsToSelect = FastList.newInstance(); 990 fieldsToSelect.add("partyId"); 992 fieldsToSelect.add("partyTypeId"); 993 994 String partyId = (String ) context.get("partyId"); 996 String userLoginId = (String ) context.get("userLoginId"); 997 String firstName = (String ) context.get("firstName"); 998 String lastName = (String ) context.get("lastName"); 999 String groupName = (String ) context.get("groupName"); 1000 1001 if (!"Y".equals(showAll)) { 1002 if (partyId != null && partyId.length() > 0) { 1004 paramList = paramList + "&partyId=" + partyId; 1005 andExprs.add(new EntityExpr("partyId", true, EntityOperator.LIKE, "%"+partyId+"%", true)); 1006 } 1007 1008 1012 if (userLoginId != null && userLoginId.length() > 0) { 1014 paramList = paramList + "&userLoginId=" + userLoginId; 1015 1016 dynamicView.addMemberEntity("UL", "UserLogin"); 1018 dynamicView.addAlias("UL", "userLoginId"); 1019 dynamicView.addViewLink("PT", "UL", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId")); 1020 1021 andExprs.add(new EntityExpr("userLoginId", true, EntityOperator.LIKE, "%"+userLoginId+"%", true)); 1023 1024 fieldsToSelect.add("userLoginId"); 1025 } 1026 1027 1031 if (groupName != null && groupName.length() > 0) { 1033 paramList = paramList + "&groupName=" + groupName; 1034 1035 dynamicView.addMemberEntity("PG", "PartyGroup"); 1037 dynamicView.addAlias("PG", "groupName"); 1038 dynamicView.addViewLink("PT", "PG", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId")); 1039 1040 andExprs.add(new EntityExpr("groupName", true, EntityOperator.LIKE, "%"+groupName+"%", true)); 1042 } 1043 1044 1048 if ((firstName != null && firstName.length() > 0) || (lastName != null && lastName.length() > 0)) { 1050 dynamicView.addMemberEntity("PE", "Person"); 1051 dynamicView.addAlias("PE", "firstName"); 1052 dynamicView.addAlias("PE", "lastName"); 1053 dynamicView.addViewLink("PT", "PE", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId")); 1054 1055 fieldsToSelect.add("firstName"); 1056 fieldsToSelect.add("lastName"); 1057 orderBy.add("lastName"); 1058 orderBy.add("firstName"); 1059 } 1060 1061 if (firstName != null && firstName.length() > 0) { 1063 paramList = paramList + "&firstName=" + firstName; 1064 andExprs.add(new EntityExpr("firstName", true, EntityOperator.LIKE, "%"+firstName+"%", true)); 1065 } 1066 1067 if (lastName != null && lastName.length() > 0) { 1069 paramList = paramList + "&lastName=" + lastName; 1070 andExprs.add(new EntityExpr("lastName", true, EntityOperator.LIKE, "%"+lastName+"%", true)); 1071 } 1072 1073 1077 if (roleTypeId != null && !"ANY".equals(roleTypeId)) { 1079 paramList = paramList + "&roleTypeId=" + roleTypeId; 1080 1081 dynamicView.addMemberEntity("PR", "PartyRole"); 1083 dynamicView.addAlias("PR", "roleTypeId"); 1084 dynamicView.addViewLink("PT", "PR", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId")); 1085 1086 andExprs.add(new EntityExpr("roleTypeId", EntityOperator.EQUALS, roleTypeId)); 1088 1089 fieldsToSelect.add("roleTypeId"); 1090 } 1091 1092 if ("P".equals(extInfo)) { 1096 dynamicView.addMemberEntity("PC", "PartyContactMech"); 1098 dynamicView.addMemberEntity("PA", "PostalAddress"); 1099 dynamicView.addAlias("PC", "contactMechId"); 1100 dynamicView.addAlias("PA", "address1"); 1101 dynamicView.addAlias("PA", "address2"); 1102 dynamicView.addAlias("PA", "city"); 1103 dynamicView.addAlias("PA", "stateProvinceGeoId"); 1104 dynamicView.addAlias("PA", "countryGeoId"); 1105 dynamicView.addAlias("PA", "postalCode"); 1106 dynamicView.addViewLink("PT", "PC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId")); 1107 dynamicView.addViewLink("PC", "PA", Boolean.FALSE, ModelKeyMap.makeKeyMapList("contactMechId")); 1108 1109 String address1 = (String ) context.get("address1"); 1111 if (address1 != null && address1.length() > 0) { 1112 paramList = paramList + "&address1=" + address1; 1113 andExprs.add(new EntityExpr("address1", true, EntityOperator.LIKE, "%" + address1 + "%", true)); 1114 } 1115 1116 String address2 = (String ) context.get("address2"); 1118 if (address2 != null && address2.length() > 0) { 1119 paramList = paramList + "&address2=" + address2; 1120 andExprs.add(new EntityExpr("address2", true, EntityOperator.LIKE, "%" + address2 + "%", true)); 1121 } 1122 1123 String city = (String ) context.get("city"); 1125 if (city != null && city.length() > 0) { 1126 paramList = paramList + "&city=" + city; 1127 andExprs.add(new EntityExpr("city", true, EntityOperator.EQUALS, city, true)); 1128 } 1129 1130 if (stateProvinceGeoId != null && !"ANY".equals(stateProvinceGeoId)) { 1132 paramList = paramList + "&stateProvinceGeoId=" + stateProvinceGeoId; 1133 andExprs.add(new EntityExpr("stateProvinceGeoId", EntityOperator.EQUALS, stateProvinceGeoId)); 1134 } 1135 1136 String postalCode = (String ) context.get("postalCode"); 1138 if (postalCode != null && postalCode.length() > 0) { 1139 paramList = paramList + "&postalCode=" + postalCode; 1140 andExprs.add(new EntityExpr("postalCode", true, EntityOperator.LIKE, "%" + postalCode + "%", true)); 1141 } 1142 1143 fieldsToSelect.add("postalCode"); 1144 } 1145 1146 if ("O".equals(extInfo)) { 1150 dynamicView.addMemberEntity("PC", "PartyContactMech"); 1152 dynamicView.addMemberEntity("CM", "ContactMech"); 1153 dynamicView.addAlias("PC", "contactMechId"); 1154 dynamicView.addAlias("CM", "infoString"); 1155 dynamicView.addViewLink("PT", "PC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId")); 1156 dynamicView.addViewLink("PC", "CM", Boolean.FALSE, ModelKeyMap.makeKeyMapList("contactMechId")); 1157 1158 String infoString = (String ) context.get("infoString"); 1160 if (infoString != null && infoString.length() > 0) { 1161 paramList = paramList + "&infoString=" + infoString; 1162 andExprs.add(new EntityExpr("infoString", true, EntityOperator.LIKE, "%"+infoString+"%", true)); 1163 } 1164 1165 fieldsToSelect.add("infoString"); 1166 } 1167 1168 if ("T".equals(extInfo)) { 1172 dynamicView.addMemberEntity("PC", "PartyContactMech"); 1174 dynamicView.addMemberEntity("TM", "TelecomNumber"); 1175 dynamicView.addAlias("PC", "contactMechId"); 1176 dynamicView.addAlias("TM", "countryCode"); 1177 dynamicView.addAlias("TM", "areaCode"); 1178 dynamicView.addAlias("TM", "contactNumber"); 1179 dynamicView.addViewLink("PT", "PC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId")); 1180 dynamicView.addViewLink("PC", "TM", Boolean.FALSE, ModelKeyMap.makeKeyMapList("contactMechId")); 1181 1182 String countryCode = (String ) context.get("countryCode"); 1184 if (countryCode != null && countryCode.length() > 0) { 1185 paramList = paramList + "&countryCode=" + countryCode; 1186 andExprs.add(new EntityExpr("countryCode", true, EntityOperator.EQUALS, countryCode, true)); 1187 } 1188 1189 String areaCode = (String ) context.get("areaCode"); 1191 if (areaCode != null && areaCode.length() > 0) { 1192 paramList = paramList + "&areaCode=" + areaCode; 1193 andExprs.add(new EntityExpr("areaCode", true, EntityOperator.EQUALS, areaCode, true)); 1194 } 1195 1196 String contactNumber = (String ) context.get("contactNumber"); 1198 if (contactNumber != null && contactNumber.length() > 0) { 1199 paramList = paramList + "&contactNumber=" + contactNumber; 1200 andExprs.add(new EntityExpr("contactNumber", true, EntityOperator.EQUALS, contactNumber, true)); 1201 } 1202 1203 fieldsToSelect.add("contactNumber"); 1204 fieldsToSelect.add("areaCode"); 1205 } 1206 1207 1209 if (andExprs.size() > 0) mainCond = new EntityConditionList(andExprs, EntityOperator.AND); 1211 } 1212 1213 Debug.logInfo("In findParty mainCond=" + mainCond, module); 1214 1215 if (mainCond != null || "Y".equals(showAll)) { 1217 try { 1218 EntityFindOptions findOpts = new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, true); 1220 EntityListIterator pli = delegator.findListIteratorByCondition(dynamicView, mainCond, null, fieldsToSelect, orderBy, findOpts); 1222 1223 lowIndex = (((viewIndex - 1) * viewSize) + 1); 1225 highIndex = viewIndex * viewSize; 1226 1227 partyList = pli.getPartialList(lowIndex, viewSize); 1229 1230 pli.last(); 1232 partyListSize = pli.currentIndex(); 1233 if (highIndex > partyListSize) { 1234 highIndex = partyListSize; 1235 } 1236 1237 pli.close(); 1239 } catch (GenericEntityException e) { 1240 String errMsg = "Failure in party find operation, rolling back transaction: " + e.toString(); 1241 Debug.logError(e, errMsg, module); 1242 return ServiceUtil.returnError(errMsg); 1243 } 1244 } else { 1245 partyListSize = 0; 1246 } 1247 } 1248 1249 if (partyList == null) partyList = FastList.newInstance(); 1250 result.put("partyList", partyList); 1251 result.put("partyListSize", new Integer (partyListSize)); 1252 result.put("paramList", paramList); 1253 result.put("highIndex", new Integer (highIndex)); 1254 result.put("lowIndex", new Integer (lowIndex)); 1255 1256 return result; 1257 } 1258} 1259 | Popular Tags |