1 24 package org.ofbiz.party.contact; 25 26 import java.sql.Timestamp ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Locale ; 33 34 import org.ofbiz.base.util.Debug; 35 import org.ofbiz.base.util.UtilDateTime; 36 import org.ofbiz.base.util.UtilMisc; 37 import org.ofbiz.base.util.UtilProperties; 38 import org.ofbiz.base.util.UtilValidate; 39 import org.ofbiz.entity.GenericDelegator; 40 import org.ofbiz.entity.GenericEntityException; 41 import org.ofbiz.entity.GenericValue; 42 import org.ofbiz.entity.util.EntityUtil; 43 import org.ofbiz.security.Security; 44 import org.ofbiz.service.DispatchContext; 45 import org.ofbiz.service.ModelService; 46 import org.ofbiz.service.ServiceUtil; 47 import org.ofbiz.service.LocalDispatcher; 48 import org.ofbiz.service.GenericServiceException; 49 50 51 58 public class ContactMechServices { 59 60 public static final String module = ContactMechServices.class.getName(); 61 public static final String resource = "PartyUiLabels"; 62 63 70 public static Map createContactMech(DispatchContext ctx, Map context) { 71 Map result = new HashMap (); 72 GenericDelegator delegator = ctx.getDelegator(); 73 Security security = ctx.getSecurity(); 74 GenericValue userLogin = (GenericValue) context.get("userLogin"); 75 Timestamp now = UtilDateTime.nowTimestamp(); 76 List toBeStored = new LinkedList (); 77 78 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_CREATE"); 79 String errMsg = null; 80 Locale locale = (Locale ) context.get("locale"); 81 82 83 if (result.size() > 0) 84 return result; 85 86 String contactMechTypeId = (String ) context.get("contactMechTypeId"); 87 88 String newCmId = null; 89 try { 90 newCmId = delegator.getNextSeqId("ContactMech"); 91 } catch (IllegalArgumentException e) { 92 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_create_contact_info_id_generation_failure", locale); 93 return ServiceUtil.returnError(errMsg); 94 } 95 96 GenericValue tempContactMech = delegator.makeValue("ContactMech", UtilMisc.toMap("contactMechId", newCmId.toString(), "contactMechTypeId", contactMechTypeId)); 97 toBeStored.add(tempContactMech); 98 99 if (!partyId.equals("_NA_")) { 100 toBeStored.add(delegator.makeValue("PartyContactMech", UtilMisc.toMap("partyId", partyId, "contactMechId", newCmId.toString(), 101 "fromDate", now, "roleTypeId", context.get("roleTypeId"), "allowSolicitation", context.get("allowSolicitation"), "extension", context.get("extension")))); 102 } 103 104 if ("POSTAL_ADDRESS".equals(contactMechTypeId)) { 105 errMsg = UtilProperties.getMessage(resource,"contactmechservices.service_createContactMech_not_be_used_for_POSTAL_ADDRESS", locale); 106 return ServiceUtil.returnError(errMsg); 107 } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) { 108 errMsg = UtilProperties.getMessage(resource,"contactmechservices.service_createContactMech_not_be_used_for_TELECOM_NUMBER", locale); 109 return ServiceUtil.returnError(errMsg); 110 } else { 111 tempContactMech.set("infoString", context.get("infoString")); 112 } 113 114 try { 115 delegator.storeAll(toBeStored); 116 } catch (GenericEntityException e) { 117 Debug.logWarning(e.toString(), module); 118 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 119 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_create_contact_info_write", messageMap, locale); 120 return ServiceUtil.returnError(errMsg); 121 } 122 123 result.put("contactMechId", newCmId.toString()); 124 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 125 return result; 126 } 127 128 135 public static Map updateContactMech(DispatchContext ctx, Map context) { 136 Map result = new HashMap (); 137 GenericDelegator delegator = ctx.getDelegator(); 138 Security security = ctx.getSecurity(); 139 GenericValue userLogin = (GenericValue) context.get("userLogin"); 140 Timestamp now = UtilDateTime.nowTimestamp(); 141 List toBeStored = new LinkedList (); 142 boolean isModified = false; 143 144 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_UPDATE"); 145 String errMsg = null; 146 Locale locale = (Locale ) context.get("locale"); 147 148 if (result.size() > 0) 149 return result; 150 151 String newCmId = null; 152 try { 153 newCmId = delegator.getNextSeqId("ContactMech"); 154 } catch (IllegalArgumentException e) { 155 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_change_contact_info_id_generation_failure", locale); 156 return ServiceUtil.returnError(errMsg); 157 } 158 159 String contactMechId = (String ) context.get("contactMechId"); 160 GenericValue contactMech = null; 161 GenericValue partyContactMech = null; 162 163 try { 164 contactMech = delegator.findByPrimaryKey("ContactMech", UtilMisc.toMap("contactMechId", contactMechId)); 165 } catch (GenericEntityException e) { 166 Debug.logWarning(e.getMessage(), module); 167 contactMech = null; 168 } 169 170 if (!partyId.equals("_NA_")) { 171 try { 173 List partyContactMechs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId), UtilMisc.toList("fromDate")), true); 174 partyContactMech = EntityUtil.getFirst(partyContactMechs); 175 if (partyContactMech == null) { 176 errMsg = UtilProperties.getMessage(resource,"contactmechservices.cannot_update_specified_contact_info_not_corresponds", locale); 177 return ServiceUtil.returnError(errMsg); 178 } else { 179 toBeStored.add(partyContactMech); 180 } 181 } catch (GenericEntityException e) { 182 Debug.logWarning(e.getMessage(), module); 183 contactMech = null; 184 } 185 } 186 if (contactMech == null) { 187 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_find_specified_contact_info_read", locale); 188 return ServiceUtil.returnError(errMsg); 189 } 190 191 String contactMechTypeId = contactMech.getString("contactMechTypeId"); 192 193 GenericValue newContactMech = GenericValue.create(contactMech); 195 GenericValue newPartyContactMech = GenericValue.create(partyContactMech); 196 GenericValue relatedEntityToSet = null; 197 198 if ("POSTAL_ADDRESS".equals(contactMechTypeId)) { 199 errMsg = UtilProperties.getMessage(resource,"contactmechservices.service_updateContactMech_not_be_used_for_POSTAL_ADDRESS", locale); 200 return ServiceUtil.returnError(errMsg); 201 } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) { 202 errMsg = UtilProperties.getMessage(resource,"contactmechservices.service_updateContactMech_not_be_used_for_TELECOM_NUMBER", locale); 203 return ServiceUtil.returnError(errMsg); 204 } else { 205 newContactMech.set("infoString", context.get("infoString")); 206 } 207 208 newPartyContactMech.set("roleTypeId", context.get("roleTypeId")); 209 newPartyContactMech.set("allowSolicitation", context.get("allowSolicitation")); 210 211 if (!newContactMech.equals(contactMech)) isModified = true; 212 if (!newPartyContactMech.equals(partyContactMech)) isModified = true; 213 214 toBeStored.add(newContactMech); 215 toBeStored.add(newPartyContactMech); 216 217 if (isModified) { 218 if (relatedEntityToSet != null) toBeStored.add(relatedEntityToSet); 219 220 newContactMech.set("contactMechId", newCmId.toString()); 221 newPartyContactMech.set("contactMechId", newCmId.toString()); 222 newPartyContactMech.set("fromDate", now); 223 newPartyContactMech.set("thruDate", null); 224 225 try { 226 Iterator partyContactMechPurposes = UtilMisc.toIterator(partyContactMech.getRelated("PartyContactMechPurpose")); 227 228 while (partyContactMechPurposes != null && partyContactMechPurposes.hasNext()) { 229 GenericValue tempVal = GenericValue.create((GenericValue) partyContactMechPurposes.next()); 230 231 tempVal.set("contactMechId", newCmId.toString()); 232 toBeStored.add(tempVal); 233 } 234 } catch (GenericEntityException e) { 235 Debug.logWarning(e.toString(), module); 236 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 237 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_change_contact_info_read", messageMap, locale); 238 return ServiceUtil.returnError(errMsg); 239 } 240 241 partyContactMech.set("thruDate", now); 242 try { 243 delegator.storeAll(toBeStored); 244 } catch (GenericEntityException e) { 245 Debug.logWarning(e.toString(), module); 246 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 247 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_change_contact_info_write", messageMap, locale); 248 return ServiceUtil.returnError(errMsg); 249 } 250 } else { 251 String sucMsg = UtilProperties.getMessage(resource,"contactmechservices.no_changes_made_not_updating", locale); 252 result.put("newContactMechId", contactMechId); 253 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 254 result.put(ModelService.SUCCESS_MESSAGE, sucMsg); 255 return result; 256 } 257 258 result.put("newContactMechId", newCmId.toString()); 259 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 260 return result; 261 } 262 263 270 public static Map deleteContactMech(DispatchContext ctx, Map context) { 271 Map result = new HashMap (); 272 GenericDelegator delegator = ctx.getDelegator(); 273 Security security = ctx.getSecurity(); 274 GenericValue userLogin = (GenericValue) context.get("userLogin"); 275 Timestamp now = UtilDateTime.nowTimestamp(); 276 277 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_DELETE"); 278 String errMsg = null; 279 Locale locale = (Locale ) context.get("locale"); 280 281 if (result.size() > 0) 282 return result; 283 284 String contactMechId = (String ) context.get("contactMechId"); 286 GenericValue partyContactMech = null; 287 288 try { 289 List partyContactMechs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId), UtilMisc.toList("fromDate")), true); 291 292 partyContactMech = EntityUtil.getFirst(partyContactMechs); 293 } catch (GenericEntityException e) { 294 Debug.logWarning(e.toString(), module); 295 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 296 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_delete_contact_info_read", messageMap, locale); 297 return ServiceUtil.returnError(errMsg); 298 } 299 300 if (partyContactMech == null) { 301 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_delete_contact_info_no_contact_found", locale); 302 return ServiceUtil.returnError(errMsg); 303 } 304 305 partyContactMech.set("thruDate", UtilDateTime.nowTimestamp()); 306 try { 307 partyContactMech.store(); 308 } catch (GenericEntityException e) { 309 Debug.logWarning(e.toString(), module); 310 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_delete_contact_info_write", locale); 311 return ServiceUtil.returnError(errMsg); 312 } 313 314 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 315 return result; 316 } 317 318 321 328 public static Map createPostalAddress(DispatchContext ctx, Map context) { 329 Map result = new HashMap (); 330 GenericDelegator delegator = ctx.getDelegator(); 331 Security security = ctx.getSecurity(); 332 GenericValue userLogin = (GenericValue) context.get("userLogin"); 333 Timestamp now = UtilDateTime.nowTimestamp(); 334 List toBeStored = new LinkedList (); 335 336 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_CREATE"); 337 String errMsg = null; 338 Locale locale = (Locale ) context.get("locale"); 339 340 if (result.size() > 0) 341 return result; 342 343 String contactMechTypeId = "POSTAL_ADDRESS"; 344 345 String newCmId = null; 346 try { 347 newCmId = delegator.getNextSeqId("ContactMech"); 348 } catch (IllegalArgumentException e) { 349 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_create_contact_info_id_generation_failure", locale); 350 return ServiceUtil.returnError(errMsg); 351 } 352 353 GenericValue tempContactMech = delegator.makeValue("ContactMech", UtilMisc.toMap("contactMechId", newCmId.toString(), "contactMechTypeId", contactMechTypeId)); 354 toBeStored.add(tempContactMech); 355 356 if (!partyId.equals("_NA_")) { 358 toBeStored.add(delegator.makeValue("PartyContactMech", 359 UtilMisc.toMap("partyId", partyId, "contactMechId", newCmId.toString(), 360 "fromDate", now, "roleTypeId", context.get("roleTypeId"), "allowSolicitation", 361 context.get("allowSolicitation"), "extension", context.get("extension")))); 362 } 363 364 GenericValue newAddr = delegator.makeValue("PostalAddress", null); 365 366 newAddr.set("contactMechId", newCmId.toString()); 367 newAddr.set("toName", context.get("toName")); 368 newAddr.set("attnName", context.get("attnName")); 369 newAddr.set("address1", context.get("address1")); 370 newAddr.set("address2", context.get("address2")); 371 newAddr.set("directions", context.get("directions")); 372 newAddr.set("city", context.get("city")); 373 newAddr.set("postalCode", context.get("postalCode")); 374 newAddr.set("postalCodeExt", context.get("postalCodeExt")); 375 newAddr.set("stateProvinceGeoId", context.get("stateProvinceGeoId")); 376 newAddr.set("countryGeoId", context.get("countryGeoId")); 377 newAddr.set("postalCodeGeoId", context.get("postalCodeGeoId")); 378 toBeStored.add(newAddr); 379 380 try { 381 delegator.storeAll(toBeStored); 382 } catch (GenericEntityException e) { 383 Debug.logWarning(e.toString(), module); 384 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 385 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_create_contact_info_write", messageMap, locale); 386 return ServiceUtil.returnError(errMsg); 387 } 388 389 result.put("contactMechId", newCmId.toString()); 390 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 391 return result; 392 } 393 394 401 public static Map updatePostalAddress(DispatchContext ctx, Map context) { 402 Map result = new HashMap (); 403 GenericDelegator delegator = ctx.getDelegator(); 404 Security security = ctx.getSecurity(); 405 GenericValue userLogin = (GenericValue) context.get("userLogin"); 406 Timestamp now = UtilDateTime.nowTimestamp(); 407 List toBeStored = new LinkedList (); 408 boolean isModified = false; 409 410 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_UPDATE"); 411 String errMsg = null; 412 Locale locale = (Locale ) context.get("locale"); 413 414 415 if (result.size() > 0) { 416 return result; 417 } 418 419 String newCmId = null; 420 try { 421 newCmId = delegator.getNextSeqId("ContactMech"); 422 } catch (IllegalArgumentException e) { 423 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_change_contact_info_id_generation_failure", locale); 424 return ServiceUtil.returnError(errMsg); 425 } 426 427 String contactMechId = (String ) context.get("contactMechId"); 428 GenericValue contactMech = null; 429 GenericValue partyContactMech = null; 430 431 try { 432 contactMech = delegator.findByPrimaryKey("ContactMech", UtilMisc.toMap("contactMechId", contactMechId)); 433 } catch (GenericEntityException e) { 434 Debug.logWarning(e.getMessage(), module); 435 contactMech = null; 436 } 437 438 if (!partyId.equals("_NA_")) { 439 try { 441 List partyContactMechs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId), UtilMisc.toList("fromDate")), true); 442 partyContactMech = EntityUtil.getFirst(partyContactMechs); 443 if (partyContactMech == null) { 444 errMsg = UtilProperties.getMessage(resource,"contactmechservices.cannot_update_specified_contact_info_not_corresponds", locale); 445 return ServiceUtil.returnError(errMsg); 446 } else { 447 toBeStored.add(partyContactMech); 448 } 449 } catch (GenericEntityException e) { 450 Debug.logWarning(e.getMessage(), module); 451 contactMech = null; 452 } 453 } 454 if (contactMech == null) { 455 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_find_specified_contact_info_read", locale); 456 return ServiceUtil.returnError(errMsg); 457 } 458 459 GenericValue newContactMech = GenericValue.create(contactMech); 461 GenericValue newPartyContactMech = null; 462 if (partyContactMech != null) 463 newPartyContactMech = GenericValue.create(partyContactMech); 464 GenericValue relatedEntityToSet = null; 465 466 if ("POSTAL_ADDRESS".equals(contactMech.getString("contactMechTypeId"))) { 467 GenericValue addr = null; 468 469 try { 470 addr = delegator.findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", contactMechId)); 471 } catch (GenericEntityException e) { 472 Debug.logWarning(e.toString(), module); 473 addr = null; 474 } 475 relatedEntityToSet = GenericValue.create(addr); 476 relatedEntityToSet.set("toName", context.get("toName")); 477 relatedEntityToSet.set("attnName", context.get("attnName")); 478 relatedEntityToSet.set("address1", context.get("address1")); 479 relatedEntityToSet.set("address2", context.get("address2")); 480 relatedEntityToSet.set("directions", context.get("directions")); 481 relatedEntityToSet.set("city", context.get("city")); 482 relatedEntityToSet.set("postalCode", context.get("postalCode")); 483 relatedEntityToSet.set("postalCodeExt", context.get("postalCodeExt")); 484 relatedEntityToSet.set("stateProvinceGeoId", context.get("stateProvinceGeoId")); 485 relatedEntityToSet.set("countryGeoId", context.get("countryGeoId")); 486 relatedEntityToSet.set("postalCodeGeoId", context.get("postalCodeGeoId")); 487 if (addr == null || !relatedEntityToSet.equals(addr)) { 488 isModified = true; 489 } 490 relatedEntityToSet.set("contactMechId", newCmId.toString()); 491 } else { 492 Map messageMap = UtilMisc.toMap("contactMechTypeId", contactMech.getString("contactMechTypeId")); 493 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_update_contact_as_POSTAL_ADDRESS_specified", messageMap, locale); 494 return ServiceUtil.returnError(errMsg); 495 } 496 497 if (newPartyContactMech != null) { 498 newPartyContactMech.set("roleTypeId", context.get("roleTypeId")); 499 newPartyContactMech.set("allowSolicitation", context.get("allowSolicitation")); 500 } 501 502 if (!newContactMech.equals(contactMech)) isModified = true; 503 if (newPartyContactMech != null && !newPartyContactMech.equals(partyContactMech)) isModified = true; 504 505 toBeStored.add(newContactMech); 506 if (newPartyContactMech != null) 507 toBeStored.add(newPartyContactMech); 508 509 if (isModified) { 510 if (relatedEntityToSet != null) toBeStored.add(relatedEntityToSet); 511 512 newContactMech.set("contactMechId", newCmId.toString()); 513 if (newPartyContactMech != null) { 514 newPartyContactMech.set("contactMechId", newCmId.toString()); 515 newPartyContactMech.set("fromDate", now); 516 newPartyContactMech.set("thruDate", null); 517 518 try { 519 Iterator partyContactMechPurposes = UtilMisc.toIterator(partyContactMech.getRelated("PartyContactMechPurpose")); 520 521 while (partyContactMechPurposes != null && partyContactMechPurposes.hasNext()) { 522 GenericValue tempVal = GenericValue.create((GenericValue) partyContactMechPurposes.next()); 523 524 tempVal.set("contactMechId", newCmId.toString()); 525 toBeStored.add(tempVal); 526 } 527 } catch (GenericEntityException e) { 528 Debug.logWarning(e.toString(), module); 529 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 530 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_change_contact_info_read", messageMap, locale); 531 return ServiceUtil.returnError(errMsg); 532 } 533 534 partyContactMech.set("thruDate", now); 535 } 536 537 try { 538 delegator.storeAll(toBeStored); 539 } catch (GenericEntityException e) { 540 Debug.logWarning(e.toString(), module); 541 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 542 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_change_contact_info_write", messageMap, locale); 543 return ServiceUtil.returnError(errMsg); 544 } 545 } else { 546 String sucMsg = UtilProperties.getMessage(resource,"contactmechservices.no_changes_made_not_updating", locale); 547 result.put("newContactMechId", contactMechId); 548 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 549 result.put(ModelService.SUCCESS_MESSAGE, sucMsg); 550 return result; 551 } 552 553 result.put("newContactMechId", newCmId.toString()); 554 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 555 return result; 556 } 557 558 561 568 public static Map createTelecomNumber(DispatchContext ctx, Map context) { 569 Map result = new HashMap (); 570 GenericDelegator delegator = ctx.getDelegator(); 571 Security security = ctx.getSecurity(); 572 GenericValue userLogin = (GenericValue) context.get("userLogin"); 573 Timestamp now = UtilDateTime.nowTimestamp(); 574 List toBeStored = new LinkedList (); 575 576 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_CREATE"); 577 String errMsg = null; 578 Locale locale = (Locale ) context.get("locale"); 579 580 if (result.size() > 0) 581 return result; 582 583 String contactMechTypeId = "TELECOM_NUMBER"; 584 585 String newCmId = null; 586 try { 587 newCmId = delegator.getNextSeqId("ContactMech"); 588 } catch (IllegalArgumentException e) { 589 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_create_contact_info_id_generation_failure", locale); 590 return ServiceUtil.returnError(errMsg); 591 } 592 593 GenericValue tempContactMech = delegator.makeValue("ContactMech", UtilMisc.toMap("contactMechId", newCmId.toString(), "contactMechTypeId", contactMechTypeId)); 594 toBeStored.add(tempContactMech); 595 596 toBeStored.add(delegator.makeValue("PartyContactMech", UtilMisc.toMap("partyId", partyId, "contactMechId", newCmId.toString(), 597 "fromDate", now, "roleTypeId", context.get("roleTypeId"), "allowSolicitation", context.get("allowSolicitation"), "extension", context.get("extension")))); 598 599 toBeStored.add(delegator.makeValue("TelecomNumber", UtilMisc.toMap("contactMechId", newCmId.toString(), 600 "countryCode", context.get("countryCode"), "areaCode", context.get("areaCode"), "contactNumber", context.get("contactNumber")))); 601 602 try { 603 delegator.storeAll(toBeStored); 604 } catch (GenericEntityException e) { 605 Debug.logWarning(e.toString(), module); 606 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 607 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_create_contact_info_write", messageMap, locale); 608 return ServiceUtil.returnError(errMsg); 609 } 610 611 result.put("contactMechId", newCmId.toString()); 612 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 613 return result; 614 } 615 616 623 public static Map updateTelecomNumber(DispatchContext ctx, Map context) { 624 Map result = new HashMap (); 625 GenericDelegator delegator = ctx.getDelegator(); 626 Security security = ctx.getSecurity(); 627 GenericValue userLogin = (GenericValue) context.get("userLogin"); 628 Timestamp now = UtilDateTime.nowTimestamp(); 629 List toBeStored = new LinkedList (); 630 boolean isModified = false; 631 632 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_UPDATE"); 633 String errMsg = null; 634 Locale locale = (Locale ) context.get("locale"); 635 636 if (result.size() > 0) 637 return result; 638 639 String newCmId = null; 640 try { 641 newCmId = delegator.getNextSeqId("ContactMech"); 642 } catch (IllegalArgumentException e) { 643 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_change_contact_info_id_generation_failure", locale); 644 return ServiceUtil.returnError(errMsg); 645 } 646 647 String contactMechId = (String ) context.get("contactMechId"); 648 GenericValue contactMech = null; 649 GenericValue partyContactMech = null; 650 651 try { 652 contactMech = delegator.findByPrimaryKey("ContactMech", UtilMisc.toMap("contactMechId", contactMechId)); 653 List partyContactMechs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId), UtilMisc.toList("fromDate")), true); 655 656 partyContactMech = EntityUtil.getFirst(partyContactMechs); 657 } catch (GenericEntityException e) { 658 Debug.logWarning(e.getMessage(), module); 659 contactMech = null; 660 partyContactMech = null; 661 } 662 if (contactMech == null) { 663 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_find_specified_contact_info_read", locale); 664 return ServiceUtil.returnError(errMsg); 665 } 666 if (partyContactMech == null) { 667 errMsg = UtilProperties.getMessage(resource,"contactmechservices.cannot_update_specified_contact_info_not_corresponds", locale); 668 return ServiceUtil.returnError(errMsg); 669 } 670 toBeStored.add(partyContactMech); 671 672 GenericValue newContactMech = GenericValue.create(contactMech); 674 GenericValue newPartyContactMech = GenericValue.create(partyContactMech); 675 GenericValue relatedEntityToSet = null; 676 677 if ("TELECOM_NUMBER".equals(contactMech.getString("contactMechTypeId"))) { 678 GenericValue telNum = null; 679 680 try { 681 telNum = delegator.findByPrimaryKey("TelecomNumber", UtilMisc.toMap("contactMechId", contactMechId)); 682 } catch (GenericEntityException e) { 683 Debug.logWarning(e.toString(), module); 684 telNum = null; 685 } 686 relatedEntityToSet = GenericValue.create(telNum); 687 relatedEntityToSet.set("countryCode", context.get("countryCode")); 688 relatedEntityToSet.set("areaCode", context.get("areaCode")); 689 relatedEntityToSet.set("contactNumber", context.get("contactNumber")); 690 691 if (telNum == null || !relatedEntityToSet.equals(telNum)) { 692 isModified = true; 693 } 694 relatedEntityToSet.set("contactMechId", newCmId.toString()); 695 newPartyContactMech.set("extension", context.get("extension")); 696 } else { 697 Map messageMap = UtilMisc.toMap("contactMechTypeId", contactMech.getString("contactMechTypeId")); 698 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_update_contact_as_TELECOM_NUMBER_specified", messageMap, locale); 699 return ServiceUtil.returnError(errMsg); 700 } 701 702 newPartyContactMech.set("roleTypeId", context.get("roleTypeId")); 703 newPartyContactMech.set("allowSolicitation", context.get("allowSolicitation")); 704 705 if (!newContactMech.equals(contactMech)) isModified = true; 706 if (!newPartyContactMech.equals(partyContactMech)) isModified = true; 707 708 toBeStored.add(newContactMech); 709 toBeStored.add(newPartyContactMech); 710 711 if (isModified) { 712 if (relatedEntityToSet != null) toBeStored.add(relatedEntityToSet); 713 714 newContactMech.set("contactMechId", newCmId.toString()); 715 newPartyContactMech.set("contactMechId", newCmId.toString()); 716 newPartyContactMech.set("fromDate", now); 717 newPartyContactMech.set("thruDate", null); 718 719 try { 720 Iterator partyContactMechPurposes = UtilMisc.toIterator(partyContactMech.getRelated("PartyContactMechPurpose")); 721 722 while (partyContactMechPurposes != null && partyContactMechPurposes.hasNext()) { 723 GenericValue tempVal = GenericValue.create((GenericValue) partyContactMechPurposes.next()); 724 725 tempVal.set("contactMechId", newCmId.toString()); 726 toBeStored.add(tempVal); 727 } 728 } catch (GenericEntityException e) { 729 Debug.logWarning(e.toString(), module); 730 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 731 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_change_contact_info_read", messageMap, locale); 732 return ServiceUtil.returnError(errMsg); 733 } 734 735 partyContactMech.set("thruDate", now); 736 try { 737 delegator.storeAll(toBeStored); 738 } catch (GenericEntityException e) { 739 Debug.logWarning(e.toString(), module); 740 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 741 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_change_contact_info_write", messageMap, locale); 742 return ServiceUtil.returnError(errMsg); 743 } 744 } else { 745 String sucMsg = UtilProperties.getMessage(resource,"contactmechservices.no_changes_made_not_updating", locale); 746 result.put("newContactMechId", contactMechId); 747 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 748 result.put(ModelService.SUCCESS_MESSAGE, sucMsg); 749 return result; 750 } 751 752 result.put("newContactMechId", newCmId.toString()); 753 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 754 return result; 755 } 756 757 760 767 public static Map createEmailAddress(DispatchContext ctx, Map context) { 768 Map newContext = new HashMap (context); 769 770 newContext.put("infoString", newContext.get("emailAddress")); 771 newContext.remove("emailAddress"); 772 newContext.put("contactMechTypeId", "EMAIL_ADDRESS"); 773 774 return createContactMech(ctx, newContext); 775 } 776 777 784 public static Map updateEmailAddress(DispatchContext ctx, Map context) { 785 Map newContext = new HashMap (context); 786 787 newContext.put("infoString", newContext.get("emailAddress")); 788 newContext.remove("emailAddress"); 789 return updateContactMech(ctx, newContext); 790 } 791 792 795 802 public static Map createPartyContactMechPurpose(DispatchContext ctx, Map context) { 803 Map result = new HashMap (); 804 GenericDelegator delegator = ctx.getDelegator(); 805 Security security = ctx.getSecurity(); 806 GenericValue userLogin = (GenericValue) context.get("userLogin"); 807 808 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_CREATE"); 809 String errMsg = null; 810 Locale locale = (Locale ) context.get("locale"); 811 812 if (result.size() > 0) 813 return result; 814 815 String contactMechId = (String ) context.get("contactMechId"); 817 String contactMechPurposeTypeId = (String ) context.get("contactMechPurposeTypeId"); 818 819 GenericValue tempVal = null; 820 821 try { 822 List allPCMPs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMechPurpose", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId), null), true); 823 824 tempVal = EntityUtil.getFirst(allPCMPs); 825 } catch (GenericEntityException e) { 826 Debug.logWarning(e.getMessage(), module); 827 tempVal = null; 828 } 829 830 Timestamp fromDate = UtilDateTime.nowTimestamp(); 831 832 if (tempVal != null) { 833 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_create_new_purpose_already_exists", locale); 835 return ServiceUtil.returnError(errMsg); 836 } else { 837 GenericValue newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose", 839 UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, 840 "fromDate", fromDate)); 841 842 try { 843 delegator.create(newPartyContactMechPurpose); 844 } catch (GenericEntityException e) { 845 Debug.logWarning(e.getMessage(), module); 846 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 847 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_add_purpose_write", messageMap, locale); 848 return ServiceUtil.returnError(errMsg); 849 } 850 } 851 852 result.put("fromDate", fromDate); 853 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 854 return result; 855 } 856 857 864 public static Map deletePartyContactMechPurpose(DispatchContext ctx, Map context) { 865 Map result = new HashMap (); 866 GenericDelegator delegator = ctx.getDelegator(); 867 Security security = ctx.getSecurity(); 868 GenericValue userLogin = (GenericValue) context.get("userLogin"); 869 870 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_PCM_DELETE"); 871 String errMsg = null; 872 Locale locale = (Locale ) context.get("locale"); 873 874 if (result.size() > 0) 875 return result; 876 877 String contactMechId = (String ) context.get("contactMechId"); 879 String contactMechPurposeTypeId = (String ) context.get("contactMechPurposeTypeId"); 880 Timestamp fromDate = (Timestamp ) context.get("fromDate"); 881 882 GenericValue pcmp = null; 883 884 try { 885 pcmp = delegator.findByPrimaryKey("PartyContactMechPurpose", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", fromDate)); 886 if (pcmp == null) { 887 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_delete_purpose_from_contact_mechanism_not_found", locale); 888 return ServiceUtil.returnError(errMsg); 889 } 890 } catch (GenericEntityException e) { 891 Debug.logWarning(e.getMessage(), module); 892 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 893 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_delete_purpose_from_contact_mechanism_read", messageMap, locale); 894 return ServiceUtil.returnError(errMsg); 895 } 896 897 pcmp.set("thruDate", UtilDateTime.nowTimestamp()); 898 try { 899 pcmp.store(); 900 } catch (GenericEntityException e) { 901 Debug.logWarning(e.getMessage(), module); 902 Map messageMap = UtilMisc.toMap("errMessage", e.getMessage()); 903 errMsg = UtilProperties.getMessage(resource,"contactmechservices.could_not_delete_purpose_from_contact_mechanism_write", messageMap, locale); 904 return ServiceUtil.returnError(errMsg); 905 } 906 907 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 908 return result; 909 } 910 911 918 public static Map getPartyContactMechValueMaps(DispatchContext ctx, Map context) { 919 Map result = ServiceUtil.returnSuccess(); 920 GenericDelegator delegator = ctx.getDelegator(); 921 GenericValue userLogin = (GenericValue) context.get("userLogin"); 922 String partyId = (String )context.get("partyId"); 923 if (UtilValidate.isEmpty(partyId) ) { 924 if (userLogin != null) { 925 partyId = userLogin.getString("partyId"); 926 } else { 927 return ServiceUtil.returnError("Both 'partyId' and 'userLogin' are empty."); 928 } 929 } 930 Boolean bShowOld = (Boolean )context.get("showOld"); 931 boolean showOld = (bShowOld != null && bShowOld.booleanValue()) ? true : false; 932 String contactMechTypeId = (String )context.get("contactMechTypeId"); 933 List valueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, partyId, showOld, contactMechTypeId); 934 result.put("valueMaps", valueMaps ); 935 return result; 936 } 937 938 941 public static Map copyPartyContactMechs(DispatchContext dctx, Map context) { 942 GenericDelegator delegator = dctx.getDelegator(); 943 LocalDispatcher dispatcher = dctx.getDispatcher(); 944 Security security = dctx.getSecurity(); 945 GenericValue userLogin = (GenericValue) context.get("userLogin"); 946 Locale locale = (Locale ) context.get("locale"); 947 948 String partyIdFrom = (String ) context.get("partyIdFrom"); 949 String partyIdTo = (String ) context.get("partyIdTo"); 950 951 try { 952 List valueMaps = ContactMechWorker.getPartyContactMechValueMaps(delegator, partyIdFrom, false); 954 955 for (Iterator iter = valueMaps.iterator(); iter.hasNext(); ) { 957 Map thisMap = (Map ) iter.next(); 958 GenericValue contactMech = (GenericValue) thisMap.get("contactMech"); 959 GenericValue partyContactMech = (GenericValue) thisMap.get("partyContactMech"); 960 List partyContactMechPurposes = (List ) thisMap.get("partyContactMechPurposes"); 961 962 String contactMechId = contactMech.getString("contactMechId"); 964 965 Map serviceResults = dispatcher.runSync("createPartyContactMech", UtilMisc.toMap("partyId", partyIdTo, "userLogin", userLogin, 967 "contactMechId", contactMechId, "fromDate", UtilDateTime.nowTimestamp(), 968 "allowSolicitation", partyContactMech.getString("allowSolicitation"), "extension", partyContactMech.getString("extension"))); 969 if (ServiceUtil.isError(serviceResults)) { 970 return serviceResults; 971 } 972 973 for (Iterator piter = partyContactMechPurposes.iterator(); piter.hasNext(); ) { 975 GenericValue purpose = (GenericValue) piter.next(); 976 Map input = UtilMisc.toMap("partyId", partyIdTo, "contactMechId", contactMechId, "userLogin", userLogin); 977 input.put("contactMechPurposeTypeId", purpose.getString("contactMechPurposeTypeId")); 978 serviceResults = dispatcher.runSync("createPartyContactMechPurpose", input); 979 if (ServiceUtil.isError(serviceResults)) { 980 return serviceResults; 981 } 982 } 983 } 984 } catch (GenericServiceException e) { 985 Debug.logError(e, e.getMessage(), module); 986 return ServiceUtil.returnError("Failed to copy contact mechs. Error: " + e.getMessage()); 987 } 988 return ServiceUtil.returnSuccess(); 989 } 990 } 991 | Popular Tags |