use of org.compiere.model.MRegion 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());
}
}
}
}
use of org.compiere.model.MRegion in project adempiere by adempiere.
the class ADServiceImpl method getRegion.
private LookupValues getRegion(MLocation location) {
MRegion[] regions = MRegion.getRegions(location.getCtx(), location.getC_Country_ID());
int comp = location.getC_Region_ID();
if (comp == 0)
comp = Env.getContextAsInt(m_cs.getM_ctx(), "C_Region_ID");
LookupValues lvs = LookupValues.Factory.newInstance();
for (int i = 0; i < regions.length; i++) {
LookupValue lv = lvs.addNewLv();
lv.setKey(String.valueOf(regions[i].getC_Region_ID()));
lv.setVal(regions[i].getName());
}
return lvs;
}
use of org.compiere.model.MRegion in project adempiere by adempiere.
the class LocationServlet method doPost.
// doGet
/**
* Process the HTTP Post request.
*
* @param request request
* @param response response
* @throws ServletException
* @throws IOException
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.info("From " + request.getRemoteHost() + " - " + request.getRemoteAddr());
HttpSession session = request.getSession(true);
Properties ctx = JSPEnv.getCtx(request);
MLocation loc = new MLocation(ctx, 0, null);
response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/xml; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String cmd = request.getParameter("cmd");
cmd = Util.maskHTML(cmd, true);
if (cmd == null) {
out.println("<error>Unknown Request: NULL</error>");
} else {
String selected = request.getParameter("selected");
int selectedID = 0;
try {
selectedID = Integer.parseInt(selected);
} catch (Exception e) {
selectedID = 0;
}
if (// should probably put these in enum
cmd.equalsIgnoreCase("countries")) {
out.println("<countries>");
MCountry[] countries = MCountry.getCountries(loc.getCtx());
for (MCountry country : countries) {
int id = country.getC_Country_ID();
out.print("<country id='" + id + "'");
if (id == selectedID)
out.print(" selected='true'");
out.println(">" + country.getName() + "</country>");
}
out.println("</countries>");
} else if (cmd.equalsIgnoreCase("regions")) {
String country = Util.maskHTML(request.getParameter("country"), true);
try {
int countryId = Integer.parseInt(country);
out.println("<regions country='" + countryId + "'>");
MRegion[] regions = MRegion.getRegions(loc.getCtx(), countryId);
if ((regions.length > 0) && (selectedID == 0))
selectedID = regions[0].getC_Region_ID();
for (MRegion region : regions) {
int id = region.getC_Region_ID();
out.print("<region id='" + id + "'");
if (id == selectedID)
out.print(" selected='true'");
out.println(">" + region.getName() + "</region>");
}
out.println("</regions>");
} catch (Exception e) {
out.println("<error>Unknown Country: " + country + "</error>");
}
} else {
out.println("<error>Unknown Request: " + cmd + "</error>");
}
}
out.flush();
out.close();
}
use of org.compiere.model.MRegion in project adempiere by adempiere.
the class LocationTag method getRegions.
// getCountries
/**
* Get Region Options for Country
* @param loc location
* @param C_Country_ID country
* @param C_Region_ID default region
* @return region array
*/
private option[] getRegions(MLocation loc, int C_Country_ID, int C_Region_ID) {
MRegion[] regions = MRegion.getRegions(loc.getCtx(), C_Country_ID);
option[] options = new option[regions.length + 1];
//
options[0] = new option("0");
options[0].addElement(" ");
//
for (int i = 0; i < regions.length; i++) {
options[i + 1] = new option(String.valueOf(regions[i].getC_Region_ID()));
options[i + 1].addElement(regions[i].getName());
if (regions[i].getC_Region_ID() == C_Region_ID)
options[i + 1].setSelected(true);
}
return options;
}
use of org.compiere.model.MRegion in project adempiere by adempiere.
the class WLocation method getRegion.
// getRegion
/**
* Get Region Pick-List
*
* @param location localion model
* @param ws window status
* @return Select field with Region
*/
private select getRegion(MLocation location, WWindowStatus ws) {
MRegion[] regions = MRegion.getRegions(location.getCtx(), location.getC_Country_ID());
int comp = location.getC_Region_ID();
if (comp == 0)
comp = Env.getContextAsInt(ws.ctx, "C_Region_ID");
option[] options = new option[regions.length];
for (int i = 0; i < regions.length; i++) {
options[i] = new option(String.valueOf(regions[i].getC_Region_ID()));
options[i].addElement(regions[i].getName());
if (comp == regions[i].getC_Region_ID())
options[i].setSelected(true);
}
select select = new select(P_C_REGION_ID, options);
return select;
}
Aggregations