package gw.address uses gw.api.locale.DisplayKey uses java.util.Set uses java.util.List /** * Helper class for LinkedAddress UI */ @Export class LinkedAddressUIHelper { private construct() { } static function getPrimaryAddressForPolicyContact( aContact : PolicyContactRole) : Address { return aContact.AccountContactRole.AccountContact.Contact.PrimaryAddress } static function getAllNonPrimaryAddressesForPolicyContact( aContact : PolicyContactRole) : Address[] { return aContact.AccountContactRole.AccountContact.Contact.SecondaryAddresses } /** * Retrieves all contact/address candidates the user can select to link any address to. * * @param currentContact The current contact, used to filter the results * @param account The current account (if available, can be null) * @param period The current PolicyPeriod (if available, can be null) */ static function getContactsAvailableAsLinks(currentContact : Contact, account : Account, period : PolicyPeriod) : List { var contacts : Set = {} if (period != null) { contacts.addAll(getPeriodContacts(currentContact, period)) } if (account != null) { contacts.addAll(getAccountContacts(currentContact, account)) } return contacts.toList().sortBy(\ c -> c.SortOrder) } private static function getPeriodContacts(currentContact : Contact, period : PolicyPeriod) : List { var primaryNamedInsured = new ContactTypePair(period.PrimaryNamedInsured.AccountContactRole.AccountContact.Contact, DisplayKey.get("entity.PolicyPriNamedInsured"), 1) var periodContacts : List = {primaryNamedInsured} return periodContacts.where(\ a -> a.Contact != currentContact) } private static function getAccountContacts(currentContact : Contact, account : Account) : List { var accountHolder = new ContactTypePair(account.AccountHolder.AccountContact.Contact, DisplayKey.get("entity.AccountHolder"), 2) var accountContacts : List = {accountHolder} var namedInsureds = account.AccountContacts .where(\ a -> a.Roles.countWhere(\ role -> role typeis entity.NamedInsured) > 0) .orderBy(\ ni -> ni.DisplayName) var namedInsuredSortIndex = 300 namedInsureds.each(\ a -> { accountContacts.add(new ContactTypePair(a.Contact, DisplayKey.get("entity.NamedInsured"), namedInsuredSortIndex) ) namedInsuredSortIndex++ } ) return accountContacts.where(\ a -> a.Contact != currentContact) } static class ContactTypePair { var _contact : Contact as readonly Contact var _contactType : String as readonly ContactType var _sortingOrder : int as readonly SortOrder construct(newContact : Contact, type : String, sortingOrder : int) { _contact = newContact _contactType = type _sortingOrder = sortingOrder } override function hashCode() : int { return _contact.ID.hashCode() } override function equals(o : Object) : boolean { if (o === this) return true if (not (o typeis ContactTypePair)) return false return _contact == (o as ContactTypePair).Contact } } }