Author: arunpatidar
Date: Sat Jul 22 09:10:50 2017 New Revision: 1802661 URL: http://svn.apache.org/viewvc?rev=1802661&view=rev Log: Integrating google phone number library for validating telecom numbers. (OFBIZ-9358) Applied slightly modified patch. Thanks Suraj Khurana and Renuka Srishti for your contribution. Modified: ofbiz/ofbiz-framework/trunk/build.gradle ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilValidate.java Modified: ofbiz/ofbiz-framework/trunk/build.gradle URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/build.gradle?rev=1802661&r1=1802660&r2=1802661&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/build.gradle (original) +++ ofbiz/ofbiz-framework/trunk/build.gradle Sat Jul 22 09:10:50 2017 @@ -98,6 +98,7 @@ dependencies { compile 'com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.0' compile 'com.googlecode.ez-vcard:ez-vcard:0.9.10' compile 'com.googlecode.owasp-java-html-sanitizer:owasp-java-html-sanitizer:20160628.1' + compile 'com.googlecode.libphonenumber:libphonenumber:8.6.0' compile 'com.ibm.icu:icu4j:57.1' compile 'com.lowagie:itext:2.1.7' compile 'com.sun.mail:javax.mail:1.5.1' Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java?rev=1802661&r1=1802660&r2=1802661&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java Sat Jul 22 09:10:50 2017 @@ -39,6 +39,15 @@ import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; +import com.google.i18n.phonenumbers.NumberParseException; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; +import org.apache.ofbiz.entity.Delegator; +import org.apache.ofbiz.entity.GenericEntityException; +import org.apache.ofbiz.entity.GenericValue; +import org.apache.ofbiz.entity.util.EntityUtilProperties; +import org.apache.ofbiz.entity.util.EntityQuery; +import org.apache.ofbiz.service.ModelService; import org.apache.ofbiz.base.util.collections.MapComparator; @@ -577,5 +586,35 @@ public final class UtilMisc { public static int getViewLastIndex(int listSize, int viewSize) { return (int)Math.ceil(listSize / (float) viewSize) - 1; } + + public static Map<String, String> splitPhoneNumber(String phoneNumber, Delegator delegator) { + Map<String, String> result = new HashMap<>(); + try { + PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); + String defaultCountry = EntityUtilProperties.getPropertyValue("general", "country.geo.id.default", delegator); + GenericValue defaultGeo = EntityQuery.use(delegator).from("Geo").where("geoId", defaultCountry).cache().queryOne(); + String defaultGeoCode = defaultGeo != null ? defaultGeo.getString("geoCode") : "US"; + PhoneNumber phNumber = phoneUtil.parse(phoneNumber, defaultGeoCode); + if (phoneUtil.isValidNumber(phNumber) || phoneUtil.isPossibleNumber(phNumber)) { + String nationalSignificantNumber = phoneUtil.getNationalSignificantNumber(phNumber); + int areaCodeLength = phoneUtil.getLengthOfGeographicalAreaCode(phNumber); + result.put("countryCode", Integer.toString(phNumber.getCountryCode())); + if (areaCodeLength > 0) { + result.put("areaCode", nationalSignificantNumber.substring(0, areaCodeLength)); + result.put("contactNumber", nationalSignificantNumber.substring(areaCodeLength)); + } else { + result.put("areaCode", ""); + result.put("contactNumber", nationalSignificantNumber); + } + } else { + Debug.logError("Invalid phone number " + phoneNumber, module); + result.put(ModelService.ERROR_MESSAGE, "Invalid phone number"); + } + } catch (GenericEntityException | NumberParseException ex) { + Debug.logError(ex, module); + result.put(ModelService.ERROR_MESSAGE, ex.getMessage()); + } + return result; + } } Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilValidate.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilValidate.java?rev=1802661&r1=1802660&r2=1802661&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilValidate.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilValidate.java Sat Jul 22 09:10:50 2017 @@ -27,6 +27,15 @@ import org.apache.ofbiz.base.lang.IsEmpt import com.ibm.icu.util.Calendar; +import com.google.i18n.phonenumbers.NumberParseException; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; +import org.apache.ofbiz.entity.Delegator; +import org.apache.ofbiz.entity.GenericEntityException; +import org.apache.ofbiz.entity.GenericValue; +import org.apache.ofbiz.entity.util.EntityUtilProperties; +import org.apache.ofbiz.entity.util.EntityQuery; + /** * General input/data validation methods * Utility methods for validating data, especially input. @@ -596,7 +605,10 @@ public final class UtilValidate { return (isInteger(normalizedSSN) && normalizedSSN.length() == digitsInSocialSecurityNumber); } - /** isUSPhoneNumber returns true if string s is a valid U.S. Phone Number. Must be 10 digits. */ + /** isUSPhoneNumber returns true if string s is a valid U.S. Phone Number. Must be 10 digits. + * @deprecated Use {@link #isValidPhoneNumber(String,Delegator)} instead + **/ + @Deprecated public static boolean isUSPhoneNumber(String s) { if (isEmpty(s)) return defaultEmptyOK; String normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters); @@ -604,7 +616,10 @@ public final class UtilValidate { return (isInteger(normalizedPhone) && normalizedPhone.length() == digitsInUSPhoneNumber); } - /** isUSPhoneAreaCode returns true if string s is a valid U.S. Phone Area Code. Must be 3 digits. */ + /** isUSPhoneAreaCode returns true if string s is a valid U.S. Phone Area Code. Must be 3 digits. + * @deprecated Use {@link #isValidPhoneNumber(String,Delegator)} instead + * */ + @Deprecated public static boolean isUSPhoneAreaCode(String s) { if (isEmpty(s)) return defaultEmptyOK; String normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters); @@ -612,7 +627,10 @@ public final class UtilValidate { return (isInteger(normalizedPhone) && normalizedPhone.length() == digitsInUSPhoneAreaCode); } - /** isUSPhoneMainNumber returns true if string s is a valid U.S. Phone Main Number. Must be 7 digits. */ + /** isUSPhoneMainNumber returns true if string s is a valid U.S. Phone Main Number. Must be 7 digits. + * @deprecated Use {@link #isValidPhoneNumber(String,Delegator)} instead + * */ + @Deprecated public static boolean isUSPhoneMainNumber(String s) { if (isEmpty(s)) return defaultEmptyOK; String normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters); @@ -623,7 +641,9 @@ public final class UtilValidate { /** isInternationalPhoneNumber returns true if string s is a valid * international phone number. Must be digits only; any length OK. * May be prefixed by + character. + * @deprecated Use {@link #isValidPhoneNumber(String,Delegator)} instead */ + @Deprecated public static boolean isInternationalPhoneNumber(String s) { if (isEmpty(s)) return defaultEmptyOK; @@ -1356,4 +1376,21 @@ public final class UtilValidate { } return isValid; } + + public static boolean isValidPhoneNumber(String phoneNumber, Delegator delegator) { + boolean isValid = false; + try { + PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); + String defaultCountry = EntityUtilProperties.getPropertyValue("general", "country.geo.id.default", delegator); + GenericValue defaultGeo = EntityQuery.use(delegator).from("Geo").where("geoId", defaultCountry).cache().queryOne(); + String defaultGeoCode = defaultGeo != null ? defaultGeo.getString("geoCode") : "US"; + PhoneNumber phNumber = phoneUtil.parse(phoneNumber, defaultGeoCode); + if (phoneUtil.isValidNumber(phNumber) || phoneUtil.isPossibleNumber(phNumber)) { + isValid = true; + } + } catch (GenericEntityException | NumberParseException ex) { + Debug.logError(ex, module); + } + return isValid; + } } |
Free forum by Nabble | Edit this page |