You are here:
ValidateAddressInterface
Validates street addresses.
Type
Loosely typed
Triggered When
The ValidateAddressInterface is triggered when the AddressValidationComponent loads. The AddressValidationComponent is a Visualforce component.
Called In
The ValidationAddressInterface is called in
ValidationAddressComponentController.
Signature
Access |
Signature |
|---|---|
global |
|
Default Implementation
The DefaultValidateAddressImplementationDefaultValidateAddressImplementation is an empty implementation that returns the value TRUE.
Other Implementations
ValidateAddressImplementationValidateAddressImplementation uses the Google Maps API to validate the passed address.
Input Parameters
methodName
Required
Type: String
The name of the method to execute, for example, validateAddress
input
Required
Type: Map<String, Object>
The input map includes addressToValidate, a comma-delimited address string.
Output Parameters
options
Required
Type: Map<String, Object>
Null
output
Type: Map<String, Object>
The output map includes validAddresses, of type List<Map<String, String>>. Each List Item Map must contain the following:
-
streetNumber -
streetName -
city -
state -
country -
postalCode
Sample Implementation
global abstract class SampleValidateAddressImplementation implements VlocityOpenInterface
{
private static final string APIAddress = 'https://maps.googleapis.com/maps/api/geocode/';
private static final string APIResponseType = 'json';
global Boolean invokeMethod(string methodName, Map<string, object> input, Map<string, object> output, Map<string, object> options)
{
if(methodName.equals('validateAddress'))
{
return validateAddress(input, output, options);
}
return false;
}
public Boolean validateAddress(Map<string, object> inputMap, Map<string, object> outputMap, Map<string, object> optionsMap)
{
string response = null;
if(inputMap.get('addressToValidate') != null)
{
response = getCalloutResponseContents(buildCalloutURL(string.valueOf(inputMap.get('addressToValidate'))));
parseResponse(response);
}
outputMap.put('validAddresses', parseResponse(response));
return true;
}
private List<Map<string, string>> parseResponse(string response)
{
List<Map<string, string>> validAddresses = new List<Map<string, string>>();
if(APIResponseType == 'json')
{
Map<string, object> jsonResponse = (Map<string, object>)JSON.deserializeUntyped(response);
if((string)jsonResponse.get('status') == 'OK')
{
for(object entry: (List<object>)jsonResponse.get('results'))
{
Map<string, object> addressEntry = (Map<string, object>)entry;
Map<string, string> addressEntries = new Map<string, string>();
addressEntries.put('formattedAddress', string.valueOf(addressEntry.get('formatted_address')));
addressEntries.put('streetNumber', '');
addressEntries.put('streetName', '');
addressEntries.put('city', '');
addressEntries.put('state', '');
addressEntries.put('country', '');
addressEntries.put('postalCode', '');
for(object addressComponent: (List<object>)addressEntry.get('address_components'))
{
Map<string, object> component = (Map<string, object>)addressComponent;
Set<string> types = new Set<string>();
List<object> componentType = (List<object>)component.get('types');
for(object typeObject: (List<object>)component.get('types'))
{
types.add(string.valueOf(typeObject));
}
if(types.contains('street_number'))
{
addressEntries.put('streetNumber', string.valueOf(component.get('long_name')));
}
else if(types.contains('route'))
{
addressEntries.put('streetName', string.valueOf(component.get('long_name')));
}
else if(types.contains('locality'))
{
addressEntries.put('city', string.valueOf(component.get('long_name')));
}
else if(types.contains('administrative_area_level_1'))
{
addressEntries.put('state', string.valueOf(component.get('long_name')));
}
else if(types.contains('country'))
{
addressEntries.put('country', string.valueOf(component.get('long_name')));
}
else if(types.contains('postal_code'))
{
addressEntries.put('postalCode', string.valueOf(component.get('long_name')));
}
}
validAddresses.add(addressEntries);
}
return validAddresses;
}
else
{
return null;
}
}
else
{
return null;
}
}
private string buildCalloutURL(string address)
{
return APIAddress + APIResponseType + '?address=' + address.replaceAll(' ', '+');
}
public string getCalloutResponseContents(string url)
{
// Instantiate a new http object
Http h = new Http();
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
// Send the request, and return a response
HttpResponse res = h.send(req);
return res.getBody();
}
}
