Sample Java Code: LqtLocalAPICaller.java
package com.loqate.ws.rest.core;
import com.loqate.*;
import com.loqate.ws.rest.service.LqtWebQueryRecord;
import java.io.File;
import java.util.LinkedList;
import java.util.Map;
/**
* LqtLocalAPICaller
* This class provides a wrapper to the Loqate Local API.
* REST annotations are not used in this class.
*
* Method runQuery() interacts with the Loqate server, passing the input
* parameters and returning the results.
*
* For this sample code it is assumed that the Loqate data directory is
* installed in C:\Data1\data directory. A different directory can be used.
*
*/
public class LqtLocalAPICaller {
// Define constants for Loqate Java library and Loqate data directory
public static final String LOQATE_LQTLIBRARY_FILE = "C:\\Data1\\lqtjava.dll";
public static final String LOQATE_DATA_DIRECTORY = "C:\\Data1\\data";
static {
// Load the Loqate Java library (lqtjava.dll)
System.load( LOQATE_LQTLIBRARY_FILE );
}
// Create an instance of the Loqate server
static lqtServer srv = lqtServer.create();
// Default class constructor
public LqtLocalAPICaller() {
// Set Loqate data directory
File file = new File( LOQATE_DATA_DIRECTORY );
file.setExecutable(true, true);
// Set Loqate data directory read permission
file.setReadable(true, true);
// Initialize the Loqate server
srv.init(LOQATE_DATA_DIRECTORY);
}
/**
* This method runs the address query against the Loqate Local API engine
* and returns a LinkedList with the including the result(s). This method
* sets the Loqate input record, result, list and options.
*
* @param organization String Query field used to supply country
* name or code
* @param address String Query field used to supply full or
* partial address
* @param locality String Query field used to supply locality
* (i.e. US city)
* @param country String Query field used to supply country
* name or code
* @param administrativeArea String Query field used to supply common
* geographic element (i.e. US State)
* @param postalCode String Query field used to supply postal
* code
* @return LinkedList
* of Loqate Query records that include
* the results returned by the Loqate
* Local API
*/
public LinkedList
String address,
String locality,
String country,
String administrativeArea,
String postalCode)
{
// String used to store Loqate's Address Verification Code (AVC) result
String avc = "";
// Create Loqate objects
lqtInputRecord rec = lqtInputRecord.create();
lqtProcessResult res = lqtProcessResult.create();
// Create Loqate process list and options
lqtProcessList lst = lqtProcessList.create();
lqtProcessOptions opts = lqtProcessOptions.create();
lst.add("Verify", opts);
// Open session
int session = srv.open();
// Set only those Loqate input record parameters for
// which a value was provided
if (organization != null && ! (organization.equals(""))) {
rec.set("Organization", organization);
}
if (address != null && ! (address.equals(""))) {
rec.set("Address1", address);
}
if (locality != null && ! (locality.equals(""))) {
rec.set("Locality", locality);
}
if (country != null && ! (country.equals(""))) {
rec.set("Country", country);
}
if (administrativeArea != null && ! (administrativeArea.equals(""))) {
rec.set("AdministrativeArea", administrativeArea);
}
if (postalCode != null && ! (postalCode.equals(""))) {
rec.set("PostalCode", postalCode);
}
// Process the record
srv.process(rec, lst, res);
// Use linked list to store results
LinkedList
// Store result in the location record
for (int i = 0; i < res.getCount(); i++)
{
// Create instance of a web query record (LqtWebQueryRecord)
LqtWebQueryRecord locationRecord = new LqtWebQueryRecord();
// Get and store result information into the record
locationRecord.setOrganization(res.getField(i, "Organization"));
locationRecord.setAddress1(res.getField(i, "Address1"));
locationRecord.setAddress2(res.getField(i, "Address2"));
locationRecord.setLocality(res.getField(i, "Locality"));
locationRecord.setAdministrativeArea(res.getField(i,
"AdministrativeArea"));
locationRecord.setPostalCode(res.getField(i, "PostalCode"));
locationRecord.setCountry(res.getField(i, "CountryName"));
// Get an store AVC into the record
avc = res.getAccuracyCode();
locationRecord.setAVC(avc);
// Add the record to the end of the result linked list
result.add(locationRecord);
}
// Close session
srv.close(session);
// Destroy the process list
lqtProcessList.destroy(lst);
lqtProcessOptions.destroy(opts);
// Tidy up
lqtInputRecord.destroy(rec);
lqtProcessResult.destroy(res);
//return linked list result
return result;
}
}