1 14 package org.compiere.www; 15 16 import javax.servlet.*; 17 import javax.servlet.http.*; 18 import java.io.*; 19 import java.util.*; 20 import org.apache.ecs.*; 21 22 import org.apache.ecs.*; 23 import org.apache.ecs.xhtml.*; 24 25 import org.compiere.util.*; 26 import org.compiere.model.*; 27 28 45 public class WLocation extends HttpServlet 46 { 47 53 public void init(ServletConfig config) throws ServletException 54 { 55 super.init(config); 56 if (!WEnv.initWeb(config)) 57 throw new ServletException("WLocation.init"); 58 } 60 61 private static final String P_TARGET = "TARGET"; 62 private static final String P_C_LOCATION_ID = "C_LOCATION_ID"; 63 private static final String P_ADDRESS1 = "ADDRESS1"; 64 private static final String P_ADDRESS2 = "ADDRESS2"; 65 private static final String P_CITY = "CITY"; 66 private static final String P_POSTAL = "POSTAL"; 67 private static final String P_C_COUNTRY_ID = "C_COUNTRY_ID"; 68 private static final String P_C_REGION_ID = "C_REGION_ID"; 69 70 79 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 80 { 81 Log.trace(Log.l3_Util, "WLocation.doGet"); 82 HttpSession sess = request.getSession(); 83 WWindowStatus ws = null; 84 if (sess != null) 85 ws = (WWindowStatus)sess.getAttribute(WEnv.SA_WINDOW); 86 if (ws == null) 87 { 88 WUtil.createTimeoutPage(request, response, this, null, null); 89 return; 90 } 91 String formName = request.getParameter("FormName"); 93 String columnName = request.getParameter("ColumnName"); 94 MField mField = ws.curTab.getField(columnName); 95 96 Log.trace(Log.l4_Data, "WLocation.doGet", "FormName=" + formName + ", ColumnName=" + columnName); 97 if (mField == null || formName == null || columnName == null || formName.equals("") || columnName.equals("")) 98 { 99 WUtil.createTimeoutPage(request, response, this, ws.ctx, Msg.getMsg(ws.ctx, "ParameterMissing")); 100 return; 101 } 102 Object value = ws.curTab.getValue(columnName); 103 String target = "opener.document." + formName + "." + columnName; 104 105 WDoc doc = WDoc.create (mField.getHeader()); 107 body body = doc.getBody(); 108 body.setOnLoad("self.focus();"); 109 MLocation mLocation = new MLocation(ws.ctx, ws.mWindow.getWindowNo()); 111 if (value instanceof Integer ) 112 mLocation.load(((Integer )value).intValue()); 113 body.addElement(createForm(request.getRequestURI(), ws, mLocation, target)); 114 WUtil.createResponse (request, response, this, null, doc, false); 116 } 118 119 127 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 128 { 129 Log.trace(Log.l3_Util, "WLocation.doPost"); 130 HttpSession sess = request.getSession(); 131 WWindowStatus ws = null; 132 if (sess != null) 133 ws = (WWindowStatus)sess.getAttribute(WEnv.SA_WINDOW); 134 if (ws == null) 135 { 136 WUtil.createTimeoutPage(request, response, this, null, null); 137 return; 138 } 139 String target = request.getParameter(P_TARGET); 140 int C_Location_ID = WUtil.getParameterAsInt(request, P_C_LOCATION_ID); 141 142 MLocation mLocation = new MLocation(ws.ctx, ws.mWindow.getWindowNo()); 144 if (C_Location_ID != 0) 145 mLocation.load(C_Location_ID); 146 Log.trace(Log.l4_Data, "WLocation.doPost updating C_Location_ID=" + C_Location_ID, target); 147 148 mLocation.setAddress1 (request.getParameter(P_ADDRESS1)); 150 mLocation.setAddress2 (request.getParameter(P_ADDRESS2)); 151 mLocation.setCity (request.getParameter(P_CITY)); 152 mLocation.setPostal (request.getParameter(P_POSTAL)); 153 mLocation.setC_Country_ID (WUtil.getParameterAsInt(request, P_C_COUNTRY_ID)); 154 mLocation.setC_Region_ID (WUtil.getParameterAsInt(request, P_C_REGION_ID)); 155 156 WDoc doc = WDoc.create ("WLocation"); 158 body b = doc.getBody(); 159 160 C_Location_ID = mLocation.save(); 162 if (C_Location_ID == 0) 163 b.addElement(new p("ERROR - Location=0")); 164 b.addElement(new p().addElement(mLocation.toString())); 165 script script = new script(new StringBuffer (target) 167 .append(".value='").append(C_Location_ID).append("';") 168 .append(target).append("_D.value='").append(mLocation.toString()) 169 .append("';window.close();").toString()); 170 b.addElement(script); 171 Log.trace(Log.l6_Database, "WLocation.doPost", script.toString()); 172 WUtil.createResponse(request, response, this, null, doc, false); 174 } 176 185 private form createForm (String action, WWindowStatus ws, MLocation mLocation, String target) 186 { 187 Log.trace(Log.l4_Data, "WLocation.createForm", "Location=" + mLocation.toStringX()); 188 189 form myForm = null; 190 myForm = new form (action, form.post, form.ENC_DEFAULT); 191 myForm.addElement(new input(input.hidden, P_TARGET, target)); 192 myForm.addElement(new input(input.hidden, P_C_LOCATION_ID, mLocation.getC_Location_ID())); 193 194 table table = new table(); 195 tr line = new tr(); 197 line.addElement(new td(Msg.getMsg(ws.ctx, "Address")+ " 1").setAlign(AlignType.right)); 198 input myInput = null; 199 myInput = new input (input.text, P_ADDRESS1, mLocation.getAddress1()); 200 myInput.setMaxlength(50).setSize(50); 201 line.addElement(new td(myInput).setAlign(AlignType.left).setColSpan(5)); 202 table.addElement(line); 203 line = new tr(); 205 line.addElement(new td(Msg.getMsg(ws.ctx, "Address")+ " 2").setAlign(AlignType.right)); 206 myInput = new input (input.text, P_ADDRESS2, mLocation.getAddress2()); 207 myInput.setMaxlength(50).setSize(50); 208 line.addElement(new td(myInput).setAlign(AlignType.left).setColSpan(5)); 209 table.addElement(line); 210 line = new tr(); 212 line.addElement(new td(Msg.getMsg(ws.ctx, "City")).setAlign(AlignType.right)); myInput = new input (input.text, P_CITY, mLocation.getCity()); 214 myInput.setMaxlength(30).setSize(30); 215 line.addElement(new td(myInput).setAlign(AlignType.left)); if (mLocation.getCountry().isHasRegion()) 218 { 219 line.addElement(new td(Msg.getMsg(ws.ctx, "Region")).setAlign(AlignType.right)); line.addElement(new td(getRegion(mLocation, ws)).setAlign(AlignType.left)); } 222 line.addElement(new td(Msg.getMsg(ws.ctx, "Postal")).setAlign(AlignType.right)); myInput = new input (input.text, P_POSTAL, mLocation.getPostal()); 225 myInput.setMaxlength(10).setSize(6); 226 line.addElement(new td(myInput).setAlign(AlignType.left)); table.addElement(line); 231 line = new tr(); 233 line.addElement(new td(Msg.getMsg(ws.ctx, "Country")).setAlign(AlignType.right)); 234 line.addElement(new td(this.getCountry(mLocation, ws)).setAlign(AlignType.left).setColSpan(5)); 235 table.addElement(line); 236 myForm.addElement(table); 238 239 input reset = new input(input.reset, "Reset", "Reset"); button cancel = new button(); 243 cancel.addElement("Cancel"); cancel.setOnClick("window.close();"); 245 input submit = new input(input.submit, "Submit", "Submit"); myForm.addElement(new p(AlignType.left) 249 .addElement(reset) 250 .addElement(" ") 251 .addElement(cancel) 252 .addElement(" ") 253 .addElement(submit)); 254 return myForm; 256 } 258 265 private select getCountry (MLocation mLocation, WWindowStatus ws) 266 { 267 MCountry[] countries = MCountry.getCountries (mLocation.getCtx()); 268 int comp = mLocation.getC_Country_ID(); 269 if (comp == 0) 270 comp = Env.getContextAsInt(ws.ctx, "C_Country_ID"); 271 option[] options = new option[countries.length]; 272 for (int i = 0; i < countries.length; i++) 273 { 274 options[i] = new option (String.valueOf(countries[i].getC_Country_ID())); 275 options[i].addElement(countries[i].getName()); 276 if (comp == countries[i].getC_Country_ID()) 277 options[i].setSelected(true); 278 } 279 280 select select = new select (P_C_COUNTRY_ID, options); 281 return select; 282 } 284 291 private select getRegion (MLocation mLocation, WWindowStatus ws) 292 { 293 MRegion[] regions = MRegion.getRegions (mLocation.getCtx(), mLocation.getC_Country_ID()); 294 int comp = mLocation.getC_Region_ID(); 295 if (comp == 0) 296 comp = Env.getContextAsInt(ws.ctx, "C_Region_ID"); 297 option[] options = new option[regions.length]; 298 for (int i = 0; i < regions.length; i++) 299 { 300 options[i] = new option (String.valueOf(regions[i].getC_Region_ID())); 301 options[i].addElement(regions[i].getName()); 302 if (comp == regions[i].getC_Region_ID()) 303 options[i].setSelected(true); 304 } 305 306 select select = new select (P_C_REGION_ID, options); 307 return select; 308 } 310 } | Popular Tags |