package gw.api.phone uses gw.api.locale.DisplayKey uses gw.api.locale.DisplayKey uses java.util.Set uses gw.api.util.phone.GWPhoneNumberBuilder uses gw.api.util.PhoneUtil abstract class PhoneOwnerBase implements PhoneOwner { var _contactPhoneDelegate : PhoneFields as PhoneFields var _label : String as WidgetLabel construct(fields : PhoneFields){ PhoneFields = fields WidgetLabel = PhoneWidgetLabelDefaults.getDefaultLabel(PhoneFields.Type) } construct(fields : PhoneFields, label : String) { PhoneFields = fields WidgetLabel = label } override property get RequiredFields() : Set { return PhoneOwnerFieldId.REQUIRED_FIELDS } override function isAvailable(fieldId : PhoneOwnerFieldId) : boolean { return true } override function isEditable(fieldId : PhoneOwnerFieldId) : boolean { return true } override function isRequired(fieldId : PhoneOwnerFieldId) : boolean { return PhoneOwnerFieldId.REQUIRED_FIELDS.contains(fieldId) } override function isRegionCodeRequired() : boolean { if (PhoneFields.CountryCode == null && PhoneFields.NationalSubscriberNumber != null) { return false } return true } override function format() : String { var phoneVal = "" if (PhoneFields.NationalSubscriberNumber == null){ return null } if (PhoneFields.CountryCode == null || PhoneFields.CountryCode == PhoneCountryCode.TC_ZZ) { return PhoneFields.NationalSubscriberNumber } var phone = new GWPhoneNumberBuilder() .withCountryCode(PhoneFields.CountryCode) .withNationalNumber(PhoneFields.NationalSubscriberNumber) .withExtension(PhoneFields.Extension) .build() if (phone == null) { phoneVal = PhoneFields.NationalSubscriberNumber } else { phoneVal = phone.formatWithLocalizedExtension(PhoneUtil.UserDefaultPhoneCountry) } return phoneVal } override function autoFormatAndCorrect(value : String, region : PhoneCountryCode){ //Unknown country set on purpose if (region == PhoneCountryCode.TC_ZZ){ return } if (value != null && PhoneUtil.isViableNumberWithExtension(value)){ var parsingRegion = region if (region == null || region == PhoneCountryCode.TC_UNPARSEABLE){ parsingRegion = PhoneUtil.UserDefaultPhoneCountry } var gwPhone = PhoneUtil.parse(value, parsingRegion) if (gwPhone != null){ if (!region.equals(gwPhone.CountryCode)){ if (region == null || region == PhoneCountryCode.TC_UNPARSEABLE){ if (gwPhone.CountryCode == parsingRegion && gwPhone.isPossible()){ PhoneFields.CountryCode = gwPhone.CountryCode }else if (gwPhone.CountryCode != parsingRegion){ PhoneFields.CountryCode = gwPhone.CountryCode } }else{ PhoneFields.CountryCode = gwPhone.CountryCode } } if (gwPhone.CountryCode != PhoneCountryCode.TC_ZZ){ PhoneFields.NationalSubscriberNumber = gwPhone.NationalNumberFormatted if (gwPhone.Extension != null && !gwPhone.Extension.Empty){ PhoneFields.Extension = gwPhone.Extension } } } } } override function validatePhone() : String { if (PhoneFields.CountryCode == null || PhoneFields.CountryCode == PhoneCountryCode.TC_ZZ){ return null } if (PhoneFields.Extension != null && PhoneFields.NationalSubscriberNumber == null) { return DisplayKey.get("Validator.Phone.EmptyNsn") } var viableNumber = PhoneUtil.isViableNumber(PhoneFields.NationalSubscriberNumber) var possibleNumber = PhoneUtil.isPossibleNumber(PhoneFields.NationalSubscriberNumber, PhoneFields.CountryCode) return (viableNumber && possibleNumber) ? null : DisplayKey.get("Validator.Phone.Invalid", PhoneFields.NationalSubscriberNumber, PhoneFields.CountryCode) } override function getPhoneLabel(singleInput : boolean) : String { var label = WidgetLabel.HasContent ? WidgetLabel : PhoneFields.Type.DisplayName return singleInput ? label : DisplayKey.get("Web.ContactDetail.Phone.MultiField.PhoneNumber", label) } }