1 24 package org.ofbiz.party.contact; 25 26 import java.util.Collection ; 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 33 import javax.servlet.ServletRequest ; 34 import javax.servlet.jsp.PageContext ; 35 36 import org.ofbiz.base.util.Debug; 37 import org.ofbiz.base.util.UtilMisc; 38 import org.ofbiz.base.util.UtilProperties; 39 import org.ofbiz.base.util.UtilValidate; 40 import org.ofbiz.entity.GenericDelegator; 41 import org.ofbiz.entity.GenericEntityException; 42 import org.ofbiz.entity.GenericValue; 43 import org.ofbiz.entity.util.EntityUtil; 44 45 52 public class ContactMechWorker { 53 54 public static final String module = ContactMechWorker.class.getName(); 55 56 public static void getPartyContactMechValueMaps(PageContext pageContext, String partyId, boolean showOld, String partyContactMechValueMapsAttr) { 57 GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator"); 58 List partyContactMechValueMaps = getPartyContactMechValueMaps(delegator, partyId, showOld); 59 if (partyContactMechValueMaps.size() > 0) { 60 pageContext.setAttribute(partyContactMechValueMapsAttr, partyContactMechValueMaps); 61 } 62 } 63 64 public static List getPartyContactMechValueMaps(GenericDelegator delegator, String partyId, boolean showOld) { 65 return getPartyContactMechValueMaps(delegator, partyId, showOld, null); 66 } 67 68 public static List getPartyContactMechValueMaps(GenericDelegator delegator, String partyId, boolean showOld, String contactMechTypeId) { 69 List partyContactMechValueMaps = new LinkedList (); 70 71 Iterator allPartyContactMechs = null; 72 73 try { 74 List tempCol = delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId)); 75 if(contactMechTypeId != null) { 76 List tempColTemp = new LinkedList (); 77 for(Iterator iterator = tempCol.iterator(); iterator.hasNext();) { 78 GenericValue partyContactMech = (GenericValue) iterator.next(); 79 GenericValue contactMech = delegator.getRelatedOne("ContactMech", partyContactMech); 80 if(contactMech != null && contactMechTypeId.equals(contactMech.getString("contactMechTypeId"))) { 81 tempColTemp.add(partyContactMech); 82 } 83 84 } 85 tempCol = tempColTemp; 86 } 87 if (!showOld) tempCol = EntityUtil.filterByDate(tempCol, true); 88 allPartyContactMechs = UtilMisc.toIterator(tempCol); 89 } catch (GenericEntityException e) { 90 Debug.logWarning(e, module); 91 } 92 93 while (allPartyContactMechs != null && allPartyContactMechs.hasNext()) { 94 GenericValue partyContactMech = (GenericValue) allPartyContactMechs.next(); 95 GenericValue contactMech = null; 96 97 try { 98 contactMech = partyContactMech.getRelatedOne("ContactMech"); 99 } catch (GenericEntityException e) { 100 Debug.logWarning(e, module); 101 } 102 if (contactMech != null) { 103 Map partyContactMechValueMap = new HashMap (); 104 105 partyContactMechValueMaps.add(partyContactMechValueMap); 106 partyContactMechValueMap.put("contactMech", contactMech); 107 partyContactMechValueMap.put("partyContactMech", partyContactMech); 108 109 try { 110 partyContactMechValueMap.put("contactMechType", contactMech.getRelatedOneCache("ContactMechType")); 111 } catch (GenericEntityException e) { 112 Debug.logWarning(e, module); 113 } 114 115 try { 116 List partyContactMechPurposes = partyContactMech.getRelated("PartyContactMechPurpose"); 117 118 if (!showOld) partyContactMechPurposes = EntityUtil.filterByDate(partyContactMechPurposes, true); 119 partyContactMechValueMap.put("partyContactMechPurposes", partyContactMechPurposes); 120 } catch (GenericEntityException e) { 121 Debug.logWarning(e, module); 122 } 123 124 try { 125 if ("POSTAL_ADDRESS".equals(contactMech.getString("contactMechTypeId"))) { 126 partyContactMechValueMap.put("postalAddress", contactMech.getRelatedOne("PostalAddress")); 127 } else if ("TELECOM_NUMBER".equals(contactMech.getString("contactMechTypeId"))) { 128 partyContactMechValueMap.put("telecomNumber", contactMech.getRelatedOne("TelecomNumber")); 129 } 130 } catch (GenericEntityException e) { 131 Debug.logWarning(e, module); 132 } 133 } 134 } 135 136 return partyContactMechValueMaps; 137 } 138 139 public static List getFacilityContactMechValueMaps(GenericDelegator delegator, String facilityId, boolean showOld, String contactMechTypeId) { 140 List facilityContactMechValueMaps = new LinkedList (); 141 142 Iterator allFacilityContactMechs = null; 143 144 try { 145 List tempCol = delegator.findByAnd("FacilityContactMech", UtilMisc.toMap("facilityId", facilityId)); 146 if(contactMechTypeId != null) { 147 List tempColTemp = new LinkedList (); 148 for(Iterator iterator = tempCol.iterator(); iterator.hasNext();) { 149 GenericValue partyContactMech = (GenericValue) iterator.next(); 150 GenericValue contactMech = delegator.getRelatedOne("ContactMech", partyContactMech); 151 if(contactMech != null && contactMechTypeId.equals(contactMech.getString("contactMechTypeId"))) { 152 tempColTemp.add(partyContactMech); 153 } 154 155 } 156 tempCol = tempColTemp; 157 } 158 if (!showOld) tempCol = EntityUtil.filterByDate(tempCol, true); 159 allFacilityContactMechs = UtilMisc.toIterator(tempCol); 160 } catch (GenericEntityException e) { 161 Debug.logWarning(e, module); 162 } 163 164 while (allFacilityContactMechs != null && allFacilityContactMechs.hasNext()) { 165 GenericValue facilityContactMech = (GenericValue) allFacilityContactMechs.next(); 166 GenericValue contactMech = null; 167 168 try { 169 contactMech = facilityContactMech.getRelatedOne("ContactMech"); 170 } catch (GenericEntityException e) { 171 Debug.logWarning(e, module); 172 } 173 if (contactMech != null) { 174 Map facilityContactMechValueMap = new HashMap (); 175 176 facilityContactMechValueMaps.add(facilityContactMechValueMap); 177 facilityContactMechValueMap.put("contactMech", contactMech); 178 facilityContactMechValueMap.put("facilityContactMech", facilityContactMech); 179 180 try { 181 facilityContactMechValueMap.put("contactMechType", contactMech.getRelatedOneCache("ContactMechType")); 182 } catch (GenericEntityException e) { 183 Debug.logWarning(e, module); 184 } 185 186 try { 187 List facilityContactMechPurposes = facilityContactMech.getRelated("FacilityContactMechPurpose"); 188 189 if (!showOld) facilityContactMechPurposes = EntityUtil.filterByDate(facilityContactMechPurposes, true); 190 facilityContactMechValueMap.put("facilityContactMechPurposes", facilityContactMechPurposes); 191 } catch (GenericEntityException e) { 192 Debug.logWarning(e, module); 193 } 194 195 try { 196 if ("POSTAL_ADDRESS".equals(contactMech.getString("contactMechTypeId"))) { 197 facilityContactMechValueMap.put("postalAddress", contactMech.getRelatedOne("PostalAddress")); 198 } else if ("TELECOM_NUMBER".equals(contactMech.getString("contactMechTypeId"))) { 199 facilityContactMechValueMap.put("telecomNumber", contactMech.getRelatedOne("TelecomNumber")); 200 } 201 } catch (GenericEntityException e) { 202 Debug.logWarning(e, module); 203 } 204 } 205 } 206 207 return facilityContactMechValueMaps; 208 } 209 210 211 public static void getOrderContactMechValueMaps(PageContext pageContext, String orderId, String orderContactMechValueMapsAttr) { 212 GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator"); 213 List maps = getOrderContactMechValueMaps(delegator, orderId); 214 if (maps != null && maps.size() > 0) { 215 pageContext.setAttribute(orderContactMechValueMapsAttr, maps); 216 } 217 } 218 public static List getOrderContactMechValueMaps(GenericDelegator delegator, String orderId) { 219 List orderContactMechValueMaps = new LinkedList (); 220 221 Iterator allOrderContactMechs = null; 222 223 try { 224 Collection tempCol = delegator.findByAnd("OrderContactMech", UtilMisc.toMap("orderId", orderId), UtilMisc.toList("contactMechPurposeTypeId")); 225 226 allOrderContactMechs = UtilMisc.toIterator(tempCol); 227 } catch (GenericEntityException e) { 228 Debug.logWarning(e, module); 229 } 230 231 while (allOrderContactMechs != null && allOrderContactMechs.hasNext()) { 232 GenericValue orderContactMech = (GenericValue) allOrderContactMechs.next(); 233 GenericValue contactMech = null; 234 235 try { 236 contactMech = orderContactMech.getRelatedOne("ContactMech"); 237 } catch (GenericEntityException e) { 238 Debug.logWarning(e, module); 239 } 240 if (contactMech != null) { 241 Map orderContactMechValueMap = new HashMap (); 242 243 orderContactMechValueMaps.add(orderContactMechValueMap); 244 orderContactMechValueMap.put("contactMech", contactMech); 245 orderContactMechValueMap.put("orderContactMech", orderContactMech); 246 247 try { 248 orderContactMechValueMap.put("contactMechType", contactMech.getRelatedOneCache("ContactMechType")); 249 } catch (GenericEntityException e) { 250 Debug.logWarning(e, module); 251 } 252 253 try { 254 GenericValue contactMechPurposeType = orderContactMech.getRelatedOne("ContactMechPurposeType"); 255 256 orderContactMechValueMap.put("contactMechPurposeType", contactMechPurposeType); 257 } catch (GenericEntityException e) { 258 Debug.logWarning(e, module); 259 } 260 261 try { 262 if ("POSTAL_ADDRESS".equals(contactMech.getString("contactMechTypeId"))) { 263 orderContactMechValueMap.put("postalAddress", contactMech.getRelatedOne("PostalAddress")); 264 } else if ("TELECOM_NUMBER".equals(contactMech.getString("contactMechTypeId"))) { 265 orderContactMechValueMap.put("telecomNumber", contactMech.getRelatedOne("TelecomNumber")); 266 } 267 } catch (GenericEntityException e) { 268 Debug.logWarning(e, module); 269 } 270 } 271 } 272 273 return orderContactMechValueMaps; 274 } 275 276 public static Collection getWorkEffortContactMechValueMaps(GenericDelegator delegator, String workEffortId) { 277 Collection workEffortContactMechValueMaps = new LinkedList (); 278 279 Iterator allWorkEffortContactMechs = null; 280 281 try { 282 Collection tempCol = delegator.findByAnd("WorkEffortContactMech", UtilMisc.toMap("workEffortId", workEffortId)); 283 allWorkEffortContactMechs = UtilMisc.toIterator(tempCol); 284 } catch (GenericEntityException e) { 285 Debug.logWarning(e, module); 286 } 287 288 while (allWorkEffortContactMechs != null && allWorkEffortContactMechs.hasNext()) { 289 GenericValue workEffortContactMech = (GenericValue) allWorkEffortContactMechs.next(); 290 GenericValue contactMech = null; 291 292 try { 293 contactMech = workEffortContactMech.getRelatedOne("ContactMech"); 294 } catch (GenericEntityException e) { 295 Debug.logWarning(e, module); 296 } 297 if (contactMech != null) { 298 Map workEffortContactMechValueMap = new HashMap (); 299 300 workEffortContactMechValueMaps.add(workEffortContactMechValueMap); 301 workEffortContactMechValueMap.put("contactMech", contactMech); 302 workEffortContactMechValueMap.put("workEffortContactMech", workEffortContactMech); 303 304 try { 305 workEffortContactMechValueMap.put("contactMechType", contactMech.getRelatedOneCache("ContactMechType")); 306 } catch (GenericEntityException e) { 307 Debug.logWarning(e, module); 308 } 309 310 try { 311 if ("POSTAL_ADDRESS".equals(contactMech.getString("contactMechTypeId"))) { 312 workEffortContactMechValueMap.put("postalAddress", contactMech.getRelatedOne("PostalAddress")); 313 } else if ("TELECOM_NUMBER".equals(contactMech.getString("contactMechTypeId"))) { 314 workEffortContactMechValueMap.put("telecomNumber", contactMech.getRelatedOne("TelecomNumber")); 315 } 316 } catch (GenericEntityException e) { 317 Debug.logWarning(e, module); 318 } 319 } 320 } 321 322 return workEffortContactMechValueMaps.size() > 0 ? workEffortContactMechValueMaps : null; 323 } 324 325 326 public static void getContactMechAndRelated(PageContext pageContext, String partyId, String contactMechAttr, String contactMechIdAttr, 327 String partyContactMechAttr, String partyContactMechPurposesAttr, String contactMechTypeIdAttr, String contactMechTypeAttr, String purposeTypesAttr, 328 String postalAddressAttr, String telecomNumberAttr, String requestNameAttr, String donePageAttr, String tryEntityAttr, String contactMechTypesAttr) { 329 330 ServletRequest request = pageContext.getRequest(); 331 GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator"); 332 333 boolean tryEntity = true; 334 335 if (request.getAttribute("_ERROR_MESSAGE_") != null) tryEntity = false; 336 if ("true".equals(request.getParameter("tryEntity"))) tryEntity = true; 337 338 String donePage = request.getParameter("DONE_PAGE"); 339 340 if (donePage == null) donePage = (String ) request.getAttribute("DONE_PAGE"); 341 if (donePage == null || donePage.length() <= 0) donePage = "viewprofile"; 342 pageContext.setAttribute(donePageAttr, donePage); 343 344 String contactMechTypeId = request.getParameter("preContactMechTypeId"); 345 346 if (contactMechTypeId == null) contactMechTypeId = (String ) request.getAttribute("preContactMechTypeId"); 347 if (contactMechTypeId != null) 348 tryEntity = false; 349 350 String contactMechId = request.getParameter("contactMechId"); 351 352 if (request.getAttribute("contactMechId") != null) 353 contactMechId = (String ) request.getAttribute("contactMechId"); 354 355 GenericValue contactMech = null; 356 357 if (contactMechId != null) { 358 pageContext.setAttribute(contactMechIdAttr, contactMechId); 359 360 List partyContactMechs = null; 362 363 try { 364 partyContactMechs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId)), true); 365 } catch (GenericEntityException e) { 366 Debug.logWarning(e, module); 367 } 368 369 GenericValue partyContactMech = EntityUtil.getFirst(partyContactMechs); 370 371 if (partyContactMech != null) { 372 pageContext.setAttribute(partyContactMechAttr, partyContactMech); 373 374 Collection partyContactMechPurposes = null; 375 376 try { 377 partyContactMechPurposes = EntityUtil.filterByDate(partyContactMech.getRelated("PartyContactMechPurpose"), true); 378 } catch (GenericEntityException e) { 379 Debug.logWarning(e, module); 380 } 381 if (partyContactMechPurposes != null && partyContactMechPurposes.size() > 0) 382 pageContext.setAttribute(partyContactMechPurposesAttr, partyContactMechPurposes); 383 } 384 385 try { 386 contactMech = delegator.findByPrimaryKey("ContactMech", UtilMisc.toMap("contactMechId", contactMechId)); 387 } catch (GenericEntityException e) { 388 Debug.logWarning(e, module); 389 } 390 391 if (contactMech != null) { 392 pageContext.setAttribute(contactMechAttr, contactMech); 393 contactMechTypeId = contactMech.getString("contactMechTypeId"); 394 } 395 } 396 397 if (contactMechTypeId != null) { 398 pageContext.setAttribute(contactMechTypeIdAttr, contactMechTypeId); 399 400 try { 401 GenericValue contactMechType = delegator.findByPrimaryKey("ContactMechType", UtilMisc.toMap("contactMechTypeId", contactMechTypeId)); 402 403 if (contactMechType != null) 404 pageContext.setAttribute(contactMechTypeAttr, contactMechType); 405 } catch (GenericEntityException e) { 406 Debug.logWarning(e, module); 407 } 408 409 Collection purposeTypes = new LinkedList (); 410 Iterator typePurposes = null; 411 412 try { 413 typePurposes = UtilMisc.toIterator(delegator.findByAnd("ContactMechTypePurpose", UtilMisc.toMap("contactMechTypeId", contactMechTypeId))); 414 } catch (GenericEntityException e) { 415 Debug.logWarning(e, module); 416 } 417 while (typePurposes != null && typePurposes.hasNext()) { 418 GenericValue contactMechTypePurpose = (GenericValue) typePurposes.next(); 419 GenericValue contactMechPurposeType = null; 420 421 try { 422 contactMechPurposeType = contactMechTypePurpose.getRelatedOne("ContactMechPurposeType"); 423 } catch (GenericEntityException e) { 424 Debug.logWarning(e, module); 425 } 426 if (contactMechPurposeType != null) { 427 purposeTypes.add(contactMechPurposeType); 428 } 429 } 430 if (purposeTypes.size() > 0) 431 pageContext.setAttribute(purposeTypesAttr, purposeTypes); 432 } 433 434 String requestName; 435 436 if (contactMech == null) { 437 if ("POSTAL_ADDRESS".equals(contactMechTypeId)) { 439 if (request.getParameter("contactMechPurposeTypeId") != null || request.getAttribute("contactMechPurposeTypeId") != null) { 440 requestName = "createPostalAddressAndPurpose"; 441 } else { 442 requestName = "createPostalAddress"; 443 } 444 } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) { 445 requestName = "createTelecomNumber"; 446 } else if ("EMAIL_ADDRESS".equals(contactMechTypeId)) { 447 requestName = "createEmailAddress"; 448 } else { 449 requestName = "createContactMech"; 450 } 451 } else { 452 if ("POSTAL_ADDRESS".equals(contactMechTypeId)) { 454 requestName = "updatePostalAddress"; 455 } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) { 456 requestName = "updateTelecomNumber"; 457 } else if ("EMAIL_ADDRESS".equals(contactMechTypeId)) { 458 requestName = "updateEmailAddress"; 459 } else { 460 requestName = "updateContactMech"; 461 } 462 } 463 pageContext.setAttribute(requestNameAttr, requestName); 464 465 if ("POSTAL_ADDRESS".equals(contactMechTypeId)) { 466 GenericValue postalAddress = null; 467 468 try { 469 if (contactMech != null) postalAddress = contactMech.getRelatedOne("PostalAddress"); 470 } catch (GenericEntityException e) { 471 Debug.logWarning(e, module); 472 } 473 if (postalAddress != null) pageContext.setAttribute(postalAddressAttr, postalAddress); 474 } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) { 475 GenericValue telecomNumber = null; 476 477 try { 478 if (contactMech != null) telecomNumber = contactMech.getRelatedOne("TelecomNumber"); 479 } catch (GenericEntityException e) { 480 Debug.logWarning(e, module); 481 } 482 if (telecomNumber != null) pageContext.setAttribute(telecomNumberAttr, telecomNumber); 483 } 484 485 if ("true".equals(request.getParameter("useValues"))) tryEntity = true; 486 pageContext.setAttribute(tryEntityAttr, new Boolean (tryEntity)); 487 488 try { 489 Collection contactMechTypes = delegator.findAllCache("ContactMechType", null); 490 491 if (contactMechTypes != null) { 492 pageContext.setAttribute(contactMechTypesAttr, contactMechTypes); 493 } 494 } catch (GenericEntityException e) { 495 Debug.logWarning(e, module); 496 } 497 } 498 499 public static void getContactMechAndRelated(ServletRequest request, String partyId, Map target) { 500 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 501 502 boolean tryEntity = true; 503 if (request.getAttribute("_ERROR_MESSAGE_") != null) tryEntity = false; 504 if ("true".equals(request.getParameter("tryEntity"))) tryEntity = true; 505 506 String donePage = request.getParameter("DONE_PAGE"); 507 if (donePage == null) donePage = (String ) request.getAttribute("DONE_PAGE"); 508 if (donePage == null || donePage.length() <= 0) donePage = "viewprofile"; 509 target.put("donePage", donePage); 510 511 String contactMechTypeId = request.getParameter("preContactMechTypeId"); 512 513 if (contactMechTypeId == null) contactMechTypeId = (String ) request.getAttribute("preContactMechTypeId"); 514 if (contactMechTypeId != null) 515 tryEntity = false; 516 517 String contactMechId = request.getParameter("contactMechId"); 518 519 if (request.getAttribute("contactMechId") != null) 520 contactMechId = (String ) request.getAttribute("contactMechId"); 521 522 GenericValue contactMech = null; 523 524 if (contactMechId != null) { 525 target.put("contactMechId", contactMechId); 526 527 List partyContactMechs = null; 529 530 try { 531 partyContactMechs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId)), true); 532 } catch (GenericEntityException e) { 533 Debug.logWarning(e, module); 534 } 535 536 GenericValue partyContactMech = EntityUtil.getFirst(partyContactMechs); 537 538 if (partyContactMech != null) { 539 target.put("partyContactMech", partyContactMech); 540 541 Collection partyContactMechPurposes = null; 542 543 try { 544 partyContactMechPurposes = EntityUtil.filterByDate(partyContactMech.getRelated("PartyContactMechPurpose"), true); 545 } catch (GenericEntityException e) { 546 Debug.logWarning(e, module); 547 } 548 if (partyContactMechPurposes != null && partyContactMechPurposes.size() > 0) 549 target.put("partyContactMechPurposes", partyContactMechPurposes); 550 } 551 552 try { 553 contactMech = delegator.findByPrimaryKey("ContactMech", UtilMisc.toMap("contactMechId", contactMechId)); 554 } catch (GenericEntityException e) { 555 Debug.logWarning(e, module); 556 } 557 558 if (contactMech != null) { 559 target.put("contactMech", contactMech); 560 contactMechTypeId = contactMech.getString("contactMechTypeId"); 561 } 562 } 563 564 if (contactMechTypeId != null) { 565 target.put("contactMechTypeId", contactMechTypeId); 566 567 try { 568 GenericValue contactMechType = delegator.findByPrimaryKey("ContactMechType", UtilMisc.toMap("contactMechTypeId", contactMechTypeId)); 569 570 if (contactMechType != null) 571 target.put("contactMechType", contactMechType); 572 } catch (GenericEntityException e) { 573 Debug.logWarning(e, module); 574 } 575 576 Collection purposeTypes = new LinkedList (); 577 Iterator typePurposes = null; 578 579 try { 580 typePurposes = UtilMisc.toIterator(delegator.findByAnd("ContactMechTypePurpose", UtilMisc.toMap("contactMechTypeId", contactMechTypeId))); 581 } catch (GenericEntityException e) { 582 Debug.logWarning(e, module); 583 } 584 while (typePurposes != null && typePurposes.hasNext()) { 585 GenericValue contactMechTypePurpose = (GenericValue) typePurposes.next(); 586 GenericValue contactMechPurposeType = null; 587 588 try { 589 contactMechPurposeType = contactMechTypePurpose.getRelatedOne("ContactMechPurposeType"); 590 } catch (GenericEntityException e) { 591 Debug.logWarning(e, module); 592 } 593 if (contactMechPurposeType != null) { 594 purposeTypes.add(contactMechPurposeType); 595 } 596 } 597 if (purposeTypes.size() > 0) 598 target.put("purposeTypes", purposeTypes); 599 } 600 601 String requestName; 602 603 if (contactMech == null) { 604 if ("POSTAL_ADDRESS".equals(contactMechTypeId)) { 606 if (request.getParameter("contactMechPurposeTypeId") != null || request.getAttribute("contactMechPurposeTypeId") != null) { 607 requestName = "createPostalAddressAndPurpose"; 608 } else { 609 requestName = "createPostalAddress"; 610 } 611 } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) { 612 requestName = "createTelecomNumber"; 613 } else if ("EMAIL_ADDRESS".equals(contactMechTypeId)) { 614 requestName = "createEmailAddress"; 615 } else { 616 requestName = "createContactMech"; 617 } 618 } else { 619 if ("POSTAL_ADDRESS".equals(contactMechTypeId)) { 621 requestName = "updatePostalAddress"; 622 } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) { 623 requestName = "updateTelecomNumber"; 624 } else if ("EMAIL_ADDRESS".equals(contactMechTypeId)) { 625 requestName = "updateEmailAddress"; 626 } else { 627 requestName = "updateContactMech"; 628 } 629 } 630 target.put("requestName", requestName); 631 632 if ("POSTAL_ADDRESS".equals(contactMechTypeId)) { 633 GenericValue postalAddress = null; 634 635 try { 636 if (contactMech != null) postalAddress = contactMech.getRelatedOne("PostalAddress"); 637 } catch (GenericEntityException e) { 638 Debug.logWarning(e, module); 639 } 640 if (postalAddress != null) target.put("postalAddress", postalAddress); 641 } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) { 642 GenericValue telecomNumber = null; 643 644 try { 645 if (contactMech != null) telecomNumber = contactMech.getRelatedOne("TelecomNumber"); 646 } catch (GenericEntityException e) { 647 Debug.logWarning(e, module); 648 } 649 if (telecomNumber != null) target.put("telecomNumber", telecomNumber); 650 } 651 652 if ("true".equals(request.getParameter("useValues"))) tryEntity = true; 653 target.put("tryEntity", new Boolean (tryEntity)); 654 655 try { 656 Collection contactMechTypes = delegator.findAllCache("ContactMechType", null); 657 658 if (contactMechTypes != null) { 659 target.put("contactMechTypes", contactMechTypes); 660 } 661 } catch (GenericEntityException e) { 662 Debug.logWarning(e, module); 663 } 664 } 665 666 public static void getFacilityContactMechAndRelated(ServletRequest request, String facilityId, Map target) { 667 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 668 669 boolean tryEntity = true; 670 if (request.getAttribute("_ERROR_MESSAGE") != null) tryEntity = false; 671 if ("true".equals(request.getParameter("tryEntity"))) tryEntity = true; 672 673 String donePage = request.getParameter("DONE_PAGE"); 674 if (donePage == null) donePage = (String ) request.getAttribute("DONE_PAGE"); 675 if (donePage == null || donePage.length() <= 0) donePage = "viewprofile"; 676 target.put("donePage", donePage); 677 678 String contactMechTypeId = request.getParameter("preContactMechTypeId"); 679 680 if (contactMechTypeId == null) contactMechTypeId = (String ) request.getAttribute("preContactMechTypeId"); 681 if (contactMechTypeId != null) 682 tryEntity = false; 683 684 String contactMechId = request.getParameter("contactMechId"); 685 686 if (request.getAttribute("contactMechId") != null) 687 contactMechId = (String ) request.getAttribute("contactMechId"); 688 689 GenericValue contactMech = null; 690 691 if (contactMechId != null) { 692 target.put("contactMechId", contactMechId); 693 694 List facilityContactMechs = null; 696 697 try { 698 facilityContactMechs = EntityUtil.filterByDate(delegator.findByAnd("FacilityContactMech", UtilMisc.toMap("facilityId", facilityId, "contactMechId", contactMechId)), true); 699 } catch (GenericEntityException e) { 700 Debug.logWarning(e, module); 701 } 702 703 GenericValue facilityContactMech = EntityUtil.getFirst(facilityContactMechs); 704 705 if (facilityContactMech != null) { 706 target.put("facilityContactMech", facilityContactMech); 707 708 Collection facilityContactMechPurposes = null; 709 710 try { 711 facilityContactMechPurposes = EntityUtil.filterByDate(facilityContactMech.getRelated("FacilityContactMechPurpose"), true); 712 } catch (GenericEntityException e) { 713 Debug.logWarning(e, module); 714 } 715 if (facilityContactMechPurposes != null && facilityContactMechPurposes.size() > 0) 716 target.put("facilityContactMechPurposes", facilityContactMechPurposes); 717 } 718 719 try { 720 contactMech = delegator.findByPrimaryKey("ContactMech", UtilMisc.toMap("contactMechId", contactMechId)); 721 } catch (GenericEntityException e) { 722 Debug.logWarning(e, module); 723 } 724 725 if (contactMech != null) { 726 target.put("contactMech", contactMech); 727 contactMechTypeId = contactMech.getString("contactMechTypeId"); 728 } 729 } 730 731 if (contactMechTypeId != null) { 732 target.put("contactMechTypeId", contactMechTypeId); 733 734 try { 735 GenericValue contactMechType = delegator.findByPrimaryKey("ContactMechType", UtilMisc.toMap("contactMechTypeId", contactMechTypeId)); 736 737 if (contactMechType != null) 738 target.put("contactMechType", contactMechType); 739 } catch (GenericEntityException e) { 740 Debug.logWarning(e, module); 741 } 742 743 Collection purposeTypes = new LinkedList (); 744 Iterator typePurposes = null; 745 746 try { 747 typePurposes = UtilMisc.toIterator(delegator.findByAnd("ContactMechTypePurpose", UtilMisc.toMap("contactMechTypeId", contactMechTypeId))); 748 } catch (GenericEntityException e) { 749 Debug.logWarning(e, module); 750 } 751 while (typePurposes != null && typePurposes.hasNext()) { 752 GenericValue contactMechTypePurpose = (GenericValue) typePurposes.next(); 753 GenericValue contactMechPurposeType = null; 754 755 try { 756 contactMechPurposeType = contactMechTypePurpose.getRelatedOne("ContactMechPurposeType"); 757 } catch (GenericEntityException e) { 758 Debug.logWarning(e, module); 759 } 760 if (contactMechPurposeType != null) { 761 purposeTypes.add(contactMechPurposeType); 762 } 763 } 764 if (purposeTypes.size() > 0) 765 target.put("purposeTypes", purposeTypes); 766 } 767 768 String requestName; 769 770 if (contactMech == null) { 771 if ("POSTAL_ADDRESS".equals(contactMechTypeId)) { 773 if (request.getParameter("contactMechPurposeTypeId") != null || request.getAttribute("contactMechPurposeTypeId") != null) { 774 requestName = "createPostalAddressAndPurpose"; 775 } else { 776 requestName = "createPostalAddress"; 777 } 778 } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) { 779 requestName = "createTelecomNumber"; 780 } else if ("EMAIL_ADDRESS".equals(contactMechTypeId)) { 781 requestName = "createEmailAddress"; 782 } else { 783 requestName = "createContactMech"; 784 } 785 } else { 786 if ("POSTAL_ADDRESS".equals(contactMechTypeId)) { 788 requestName = "updatePostalAddress"; 789 } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) { 790 requestName = "updateTelecomNumber"; 791 } else if ("EMAIL_ADDRESS".equals(contactMechTypeId)) { 792 requestName = "updateEmailAddress"; 793 } else { 794 requestName = "updateContactMech"; 795 } 796 } 797 target.put("requestName", requestName); 798 799 if ("POSTAL_ADDRESS".equals(contactMechTypeId)) { 800 GenericValue postalAddress = null; 801 802 try { 803 if (contactMech != null) postalAddress = contactMech.getRelatedOne("PostalAddress"); 804 } catch (GenericEntityException e) { 805 Debug.logWarning(e, module); 806 } 807 if (postalAddress != null) target.put("postalAddress", postalAddress); 808 } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) { 809 GenericValue telecomNumber = null; 810 811 try { 812 if (contactMech != null) telecomNumber = contactMech.getRelatedOne("TelecomNumber"); 813 } catch (GenericEntityException e) { 814 Debug.logWarning(e, module); 815 } 816 if (telecomNumber != null) target.put("telecomNumber", telecomNumber); 817 } 818 819 if ("true".equals(request.getParameter("useValues"))) tryEntity = true; 820 target.put("tryEntity", new Boolean (tryEntity)); 821 822 try { 823 Collection contactMechTypes = delegator.findAllCache("ContactMechType", null); 824 825 if (contactMechTypes != null) { 826 target.put("contactMechTypes", contactMechTypes); 827 } 828 } catch (GenericEntityException e) { 829 Debug.logWarning(e, module); 830 } 831 } 832 833 834 public static void getPartyPostalAddresses(PageContext pageContext, String partyId, String curContactMechId, String postalAddressInfosAttr) { 835 ServletRequest request = pageContext.getRequest(); 836 List postalAddressInfos = getPartyPostalAddresses(request, partyId, curContactMechId); 837 if (postalAddressInfos.size() > 0) { 838 pageContext.setAttribute(postalAddressInfosAttr, postalAddressInfos); 839 } 840 } 841 842 public static List getPartyPostalAddresses(ServletRequest request, String partyId, String curContactMechId) { 843 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 844 List postalAddressInfos = new LinkedList (); 845 846 Iterator allPartyContactMechs = null; 847 848 try { 849 allPartyContactMechs = UtilMisc.toIterator(EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId)), true)); 850 } catch (GenericEntityException e) { 851 Debug.logWarning(e, module); 852 } 853 while (allPartyContactMechs != null && allPartyContactMechs.hasNext()) { 854 GenericValue partyContactMech = (GenericValue) allPartyContactMechs.next(); 855 GenericValue contactMech = null; 856 857 try { 858 contactMech = partyContactMech.getRelatedOne("ContactMech"); 859 } catch (GenericEntityException e) { 860 Debug.logWarning(e, module); 861 } 862 if (contactMech != null && "POSTAL_ADDRESS".equals(contactMech.getString("contactMechTypeId")) && !contactMech.getString("contactMechId").equals(curContactMechId)) { 863 Map postalAddressInfo = new HashMap (); 864 865 postalAddressInfos.add(postalAddressInfo); 866 postalAddressInfo.put("contactMech", contactMech); 867 postalAddressInfo.put("partyContactMech", partyContactMech); 868 869 try { 870 GenericValue postalAddress = contactMech.getRelatedOne("PostalAddress"); 871 postalAddressInfo.put("postalAddress", postalAddress); 872 } catch (GenericEntityException e) { 873 Debug.logWarning(e, module); 874 } 875 876 try { 877 List partyContactMechPurposes = EntityUtil.filterByDate(partyContactMech.getRelated("PartyContactMechPurpose"), true); 878 postalAddressInfo.put("partyContactMechPurposes", partyContactMechPurposes); 879 } catch (GenericEntityException e) { 880 Debug.logWarning(e, module); 881 } 882 } 883 } 884 885 return postalAddressInfos; 886 } 887 888 889 public static void getCurrentPostalAddress(PageContext pageContext, String partyId, String curContactMechId, 890 String curPartyContactMechAttr, String curContactMechAttr, String curPostalAddressAttr, String curPartyContactMechPurposesAttr) { 891 ServletRequest request = pageContext.getRequest(); 892 Map results = getCurrentPostalAddress(request, partyId, curContactMechId); 893 if (results.get("curPartyContactMech") != null) pageContext.setAttribute(curPartyContactMechAttr, results.get("curPartyContactMech")); 894 if (results.get("curContactMech") != null) pageContext.setAttribute(curContactMechAttr, results.get("curContactMech")); 895 if (results.get("curPostalAddress") != null) pageContext.setAttribute(curPostalAddressAttr, results.get("curPostalAddress")); 896 if (results.get("curPartyContactMechPurposes") != null) pageContext.setAttribute(curPartyContactMechPurposesAttr, results.get("curPartyContactMechPurposes")); 897 } 898 public static Map getCurrentPostalAddress(ServletRequest request, String partyId, String curContactMechId) { 899 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 900 Map results = new HashMap (); 901 902 if (curContactMechId != null) { 903 List partyContactMechs = null; 904 905 try { 906 partyContactMechs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId, "contactMechId", curContactMechId)), true); 907 } catch (GenericEntityException e) { 908 Debug.logWarning(e, module); 909 } 910 GenericValue curPartyContactMech = EntityUtil.getFirst(partyContactMechs); 911 results.put("curPartyContactMech", curPartyContactMech); 912 913 GenericValue curContactMech = null; 914 if (curPartyContactMech != null) { 915 try { 916 curContactMech = curPartyContactMech.getRelatedOne("ContactMech"); 917 } catch (GenericEntityException e) { 918 Debug.logWarning(e, module); 919 } 920 921 Collection curPartyContactMechPurposes = null; 922 try { 923 curPartyContactMechPurposes = EntityUtil.filterByDate(curPartyContactMech.getRelated("PartyContactMechPurpose"), true); 924 } catch (GenericEntityException e) { 925 Debug.logWarning(e, module); 926 } 927 results.put("curPartyContactMechPurposes", curPartyContactMechPurposes); 928 } 929 results.put("curContactMech", curContactMech); 930 931 GenericValue curPostalAddress = null; 932 if (curContactMech != null) { 933 try { 934 curPostalAddress = curContactMech.getRelatedOne("PostalAddress"); 935 } catch (GenericEntityException e) { 936 Debug.logWarning(e, module); 937 } 938 } 939 940 results.put("curPostalAddress", curPostalAddress); 941 } 942 return results; 943 } 944 945 public static boolean isUspsAddress(GenericValue postalAddress) { 946 if (postalAddress == null) { 947 return false; 949 } 950 if (!"PostalAddress".equals(postalAddress.getEntityName())) { 951 return false; 953 } 954 955 String addr1 = postalAddress.getString("address1"); 957 String addr2 = postalAddress.getString("address2"); 958 959 String matcher = UtilProperties.getPropertyValue("general.properties", "usps.address.match"); 961 if (UtilValidate.isNotEmpty(matcher)) { 962 if (addr1 != null && addr1.toLowerCase().matches(matcher)) { 963 return true; 964 } 965 if (addr2 != null && addr2.toLowerCase().matches(matcher)) { 966 return true; 967 } 968 } 969 970 return false; 971 } 972 973 public static boolean isCompanyAddress(GenericValue postalAddress, String companyPartyId) { 974 if (postalAddress == null) { 975 return false; 977 } 978 if (!"PostalAddress".equals(postalAddress.getEntityName())) { 979 return false; 981 } 982 if (companyPartyId == null) { 983 return false; 985 } 986 987 String state = postalAddress.getString("stateProvinceGeoId"); 988 String addr1 = postalAddress.getString("address1"); 989 String addr2 = postalAddress.getString("address2"); 990 if (state != null) { 991 state = state.replaceAll("\\W", "").toLowerCase(); 992 } else { 993 state = ""; 994 } 995 if (addr1 != null) { 996 addr1 = addr1.replaceAll("\\W", "").toLowerCase(); 997 } else { 998 addr1 = ""; 999 } 1000 if (addr2 != null) { 1001 addr2 = addr2.replaceAll("\\W", "").toLowerCase(); 1002 } else { 1003 addr2 = ""; 1004 } 1005 1006 GenericDelegator delegator = postalAddress.getDelegator(); 1008 List postalAddresses = new LinkedList (); 1009 try { 1010 List partyContactMechs = delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", companyPartyId)); 1011 partyContactMechs = EntityUtil.filterByDate(partyContactMechs); 1012 if (partyContactMechs != null) { 1013 Iterator pci = partyContactMechs.iterator(); 1014 while (pci.hasNext()) { 1015 GenericValue pcm = (GenericValue) pci.next(); 1016 GenericValue addr = pcm.getRelatedOne("PostalAddress"); 1017 if (addr != null) { 1018 postalAddresses.add(addr); 1019 } 1020 } 1021 } 1022 } catch (GenericEntityException e) { 1023 Debug.logError(e, "Unable to get party postal addresses", module); 1024 } 1025 1026 if (postalAddresses != null) { 1027 Iterator pai = postalAddresses.iterator(); 1028 while (pai.hasNext()) { 1029 GenericValue addr = (GenericValue) pai.next(); 1030 String thisAddr1 = addr.getString("address1"); 1031 String thisAddr2 = addr.getString("address2"); 1032 String thisState = addr.getString("stateProvinceGeoId"); 1033 if (thisState != null) { 1034 thisState = thisState.replaceAll("\\W", "").toLowerCase(); 1035 } else { 1036 thisState = ""; 1037 } 1038 if (thisAddr1 != null) { 1039 thisAddr1 = thisAddr1.replaceAll("\\W", "").toLowerCase(); 1040 } else { 1041 thisAddr1 = ""; 1042 } 1043 if (thisAddr2 != null) { 1044 thisAddr2 = thisAddr2.replaceAll("\\W", "").toLowerCase(); 1045 } else { 1046 thisAddr2 = ""; 1047 } 1048 if (thisAddr1.equals(addr1) && thisAddr2.equals(addr2) && thisState.equals(state)) { 1049 return true; 1050 } 1051 } 1052 } 1053 1054 return false; 1055 } 1056 1057 public static String getContactMechAttribute(GenericDelegator delegator, String contactMechId, String attrName) { 1058 GenericValue attr = null; 1059 try { 1060 attr = delegator.findByPrimaryKey("ContactMechAttribute", UtilMisc.toMap("contactMechId", contactMechId, "attrName", attrName)); 1061 } catch (GenericEntityException e) { 1062 Debug.logError(e, module); 1063 } 1064 if (attr == null) { 1065 return null; 1066 } else { 1067 return attr.getString("attrValue"); 1068 } 1069 } 1070} 1071 | Popular Tags |