Integrating google phone number library for validating telecom numbers. (OFBIZ-9358)

Applied slightly modified patch. Thanks Suraj Khurana and Renuka Srishti for your contribution.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1802661 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Arun Patidar
2017-07-22 09:10:50 +00:00
parent 3080813151
commit 001bbe9a52
3 changed files with 80 additions and 3 deletions
@@ -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;
}
}
@@ -27,6 +27,15 @@ import org.apache.ofbiz.base.lang.IsEmpty;
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;
}
}