KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > www > WLocation


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

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 /**
29  * WLocation Servlet.
30  * <p>
31  * The servlet is invoked by a parent window via
32  * <code>
33  * WLocation?FormName=formName%ColumnName=columnName
34  * </code>
35  * and assumes that in the opening window/form there are two fields
36  * <code>
37  * opener.document.formName.columnName - The (hidden) field for the ID
38  * opener.document.formName.columnName_D - The display field for the value
39  * </code>
40  * When selecting an entry, the window is closed and the value of the two fields set.
41  *
42  * @author Jorg Janke
43  * @version $Id: WLocation.java,v 1.3 2003/09/29 01:34:27 jjanke Exp $
44  */

45 public class WLocation extends HttpServlet
46 {
47     /**
48      * Initialize global variables
49      *
50      * @param config config
51      * @throws ServletException
52      */

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     } // init
59

60     /** Parameter */
61     private static final String JavaDoc P_TARGET = "TARGET";
62     private static final String JavaDoc P_C_LOCATION_ID = "C_LOCATION_ID";
63     private static final String JavaDoc P_ADDRESS1 = "ADDRESS1";
64     private static final String JavaDoc P_ADDRESS2 = "ADDRESS2";
65     private static final String JavaDoc P_CITY = "CITY";
66     private static final String JavaDoc P_POSTAL = "POSTAL";
67     private static final String JavaDoc P_C_COUNTRY_ID = "C_COUNTRY_ID";
68     private static final String JavaDoc P_C_REGION_ID = "C_REGION_ID";
69
70     /**
71      * Process the HTTP Get request - initial Start
72      * Needs to have parameters FormName and ColumnName
73      *
74      * @param request request
75      * @param response response
76      * @throws ServletException
77      * @throws IOException
78      */

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         // Get Mandatory Parameters
92
String JavaDoc formName = request.getParameter("FormName");
93         String JavaDoc 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 JavaDoc value = ws.curTab.getValue(columnName);
103         String JavaDoc target = "opener.document." + formName + "." + columnName;
104
105         // Create Document
106
WDoc doc = WDoc.create (mField.getHeader());
107         body body = doc.getBody();
108         body.setOnLoad("self.focus();");
109         //
110
MLocation mLocation = new MLocation(ws.ctx, ws.mWindow.getWindowNo());
111         if (value instanceof Integer JavaDoc)
112             mLocation.load(((Integer JavaDoc)value).intValue());
113         body.addElement(createForm(request.getRequestURI(), ws, mLocation, target));
114         //
115
WUtil.createResponse (request, response, this, null, doc, false);
116     } // doGet
117

118
119     /**
120      * Process the HTTP Post request (update Address)
121      *
122      * @param request request
123      * @param response response
124      * @throws ServletException
125      * @throws IOException
126      */

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 JavaDoc target = request.getParameter(P_TARGET);
140         int C_Location_ID = WUtil.getParameterAsInt(request, P_C_LOCATION_ID);
141
142         // Create Location
143
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         // Update Location
149
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         // Document
157
WDoc doc = WDoc.create ("WLocation");
158         body b = doc.getBody();
159
160         // Save Location
161
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         // Update Target
166
script script = new script(new StringBuffer JavaDoc(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         //
173
WUtil.createResponse(request, response, this, null, doc, false);
174     } // doPost
175

176     /**
177      * Create Form
178      *
179      * @param action action
180      * @param ws window status
181      * @param mLocation location model
182      * @param target target
183      * @return Form with Address Info
184      */

185     private form createForm (String JavaDoc action, WWindowStatus ws, MLocation mLocation, String JavaDoc 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         // -- Line 1
196
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 2
204
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 3
211
line = new tr();
212         line.addElement(new td(Msg.getMsg(ws.ctx, "City")).setAlign(AlignType.right)); // 1
213
myInput = new input (input.text, P_CITY, mLocation.getCity());
214         myInput.setMaxlength(30).setSize(30);
215         line.addElement(new td(myInput).setAlign(AlignType.left)); // 2
216
//
217
if (mLocation.getCountry().isHasRegion())
218         {
219             line.addElement(new td(Msg.getMsg(ws.ctx, "Region")).setAlign(AlignType.right)); // 3
220
line.addElement(new td(getRegion(mLocation, ws)).setAlign(AlignType.left)); // 4
221
}
222         //
223
line.addElement(new td(Msg.getMsg(ws.ctx, "Postal")).setAlign(AlignType.right)); // 5
224
myInput = new input (input.text, P_POSTAL, mLocation.getPostal());
225         myInput.setMaxlength(10).setSize(6);
226         line.addElement(new td(myInput).setAlign(AlignType.left)); // 6
227
//
228
// input = new input (input.text, "PostalAdd", mLocation.PostalAdd );
229
// line.addElement(new td(input).setAlign(AlignType.left));
230
table.addElement(line);
231         // -- Line 4
232
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         //
237
myForm.addElement(table);
238
239         // Reset - <input type="reset" name="Reset" value="Reset">
240
input reset = new input(input.reset, "Reset", "Reset"); // translate
241
// Cancel
242
button cancel = new button();
243         cancel.addElement("Cancel"); // translate
244
cancel.setOnClick("window.close();");
245         // Submit
246
input submit = new input(input.submit, "Submit", "Submit"); // translate
247
//
248
myForm.addElement(new p(AlignType.left)
249             .addElement(reset)
250             .addElement("&nbsp")
251             .addElement(cancel)
252             .addElement("&nbsp")
253             .addElement(submit));
254         //
255
return myForm;
256     } // createForm
257

258     /**
259      * Get Country Pick-List
260      *
261      * @param mLocation location model
262      * @param ws window status
263      * @return Select Field with Countries
264      */

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     } // getRegion
283

284     /**
285      * Get Region Pick-List
286      *
287      * @param mLocation localion model
288      * @param ws window status
289      * @return Select field with Region
290      */

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     } // getRegion
309

310 } // WLocation
311
Popular Tags