use of com.akunagroup.uk.postcode.Postcode in project adempiere by adempiere.
the class VLocationDialog method fillLocation.
/**
* Fills the location field using the information retrieved from postcode
* servers.
*
* @param ctx
* Context
* @param pkeyData
* Lookup results
* @param windowNo
* Window No.
* @param tab
* Tab
* @param field
* Field
*/
private void fillLocation(HashMap<String, Object> postcodeData, MCountry country) {
// If it's not empty warn the user.
if (fAddress1 != null || fAddress2 != null || fAddress3 != null || fAddress4 != null || fCity != null) {
String warningMsg = "Existing address information will be overwritten. Proceed?";
String warningTitle = "Warning";
int response = JOptionPane.showConfirmDialog(null, warningMsg, warningTitle, JOptionPane.YES_NO_OPTION);
if (response == JOptionPane.NO_OPTION)
return;
}
Set<String> pcodeKeys = postcodeData.keySet();
Iterator<String> iterator = pcodeKeys.iterator();
header = null;
// Allocate the header array
header = new Object[pcodeKeys.size()];
String headerStr = null;
// TODO - check number of records returns - size() method is incorrect
if (pcodeKeys.size() > 2) {
// TODO: Implement ResultData Grid and get return (for premises level data)
System.out.println("Too many postcodes returned from Postcode Lookup - need to Implement ResultData");
} else {
for (int i = 0; (headerStr = (iterator.hasNext() ? iterator.next() : null)) != null || iterator.hasNext(); i++) {
header[i] = headerStr;
Postcode values = (Postcode) postcodeData.get(headerStr);
// Overwrite the values in location field.
fAddress1.setText(values.getStreet1());
fAddress2.setText(values.getStreet2());
fAddress3.setText(values.getStreet3());
fAddress4.setText(values.getStreet4());
fCity.setText(values.getCity());
fPostal.setText(values.getPostcode());
// Do region lookup
if (country.isHasRegion()) {
// get all regions for this country
MRegion[] regions = MRegion.getRegions(country.getCtx(), country.getC_Country_ID());
// If regions were loaded
if (regions.length > 0) {
// loop through regions array to attempt a region match - don't finish loop if region found
boolean found = false;
for (i = 0; i < regions.length && !found; i++) {
if (regions[i].getName().equals(values.getRegion())) {
// found Region
fRegion.setSelectedItem(regions[i]);
log.fine("Found region: " + regions[i].getName());
found = true;
}
}
if (!found) {
// add new region
MRegion region = new MRegion(country, values.getRegion());
if (region.save()) {
log.fine("Added new region from web service: " + values.getRegion());
// clears cache
Env.reset(false);
//reload regions to combo box
fRegion = new CComboBoxEditable(MRegion.getRegions(Env.getCtx(), country.getC_Country_ID()));
// select region
fRegion.setSelectedItem(values);
} else
log.severe("Error saving new region: " + region.getName());
}
} else
log.severe("Region lookup failed for Country: " + country.getName());
}
}
}
}
Aggregations