KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.sql.*;
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  * Dynamic Field Updates
30  *
31  * @author Jorg Janke
32  * @version $Id: WFieldUpdate.java,v 1.2 2002/11/11 07:03:37 jjanke Exp $
33  */

34 public class WFieldUpdate extends HttpServlet
35 {
36     /**
37      * Initialize global variables
38      * @param config
39      * @throws ServletException
40      */

41     public void init(ServletConfig config) throws ServletException
42     {
43         super.init(config);
44         if (!WEnv.initWeb(config))
45             throw new ServletException("WFieldUpdate.init");
46     } // init
47

48     /**
49      * Clean up resources
50      */

51     public void destroy()
52     {
53     } // destroy
54

55     private static final String JavaDoc FORM_NAME = "fieldUpdate";
56     //
57
private static final String JavaDoc FIELD_FORM = "formName";
58     private static final String JavaDoc FIELD_NAME = "fieldName";
59     private static final String JavaDoc FIELD_VALUE = "fieldValue";
60
61     /**
62      * Process the HTTP Get request
63      * @param request
64      * @param response
65      * @throws ServletException
66      * @throws IOException
67      */

68     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
69     {
70         doPost(request, response);
71     } // doPost
72

73
74     /**
75      * Process the HTTP Post request
76      * @param request
77      * @param response
78      * @throws ServletException
79      * @throws IOException
80      */

81     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
82     {
83         Log.trace(Log.l1_User, "WFieldUpdate.doPost");
84         // Get Session Info
85
HttpSession sess = request.getSession();
86         Properties ctx = (Properties)sess.getAttribute(WEnv.SA_CONTEXT);
87         WWindowStatus ws = (WWindowStatus)sess.getAttribute(WEnv.SA_WINDOW);
88
89         if (sess == null || ctx == null) // ws can be null for Login
90
;
91
92         // Get Parameter
93
String JavaDoc formName = request.getParameter(FIELD_FORM);
94         String JavaDoc fieldName = request.getParameter(FIELD_NAME);
95         String JavaDoc fieldValue = request.getParameter(FIELD_VALUE);
96
97         // Document
98
WDoc doc = WDoc.create (true);
99         body body = doc.getBody();
100
101         // The Reply
102
createReply (body, ctx, ws, formName, fieldName, fieldValue);
103
104         // The Form
105
form fu = new form(request.getRequestURI(), form.post);
106         fu.setName(FORM_NAME);
107         fu.addElement(new input(input.hidden, FIELD_FORM, "y"));
108         fu.addElement(new input(input.hidden, FIELD_NAME, "y"));
109         fu.addElement(new input(input.hidden, FIELD_VALUE, "y"));
110         body.addElement(fu);
111
112     // Log.trace(Log.l1_User, "WFieldUpdate=" + doc.toString());
113

114         // Answer
115
WUtil.createResponse (request, response, this, null, doc, false);
116     } // doPost
117

118     /**
119      * Create Reply.
120      * <p>
121      * Including special handling of login pages
122      * @param body
123      * @param ctx
124      * @param ws
125      * @param formName
126      * @param fieldName
127      * @param fieldValue
128      */

129     private static void createReply (body body, Properties ctx, WWindowStatus ws,
130         String JavaDoc formName, String JavaDoc fieldName, String JavaDoc fieldValue)
131     {
132         StringBuffer JavaDoc sb = new StringBuffer JavaDoc (FIELD_FORM);
133         sb.append("=").append(formName).append(", ")
134             .append(FIELD_NAME).append("=").append(fieldName).append(", ")
135             .append(FIELD_VALUE).append("=").append(fieldValue);
136         Log.trace(Log.l3_Util, "WFieldUpdate.createReply", sb.toString());
137
138         // Info
139
body.addElement(new p().addElement(sb.toString()));
140
141         // Called manually - do nothing
142
if (formName == null || fieldName == null)
143             ;
144         //
145
else if (formName.equals("Login2") && fieldName.equals(WLogin.P_ROLE))
146             reply_Login2_Role (body, ctx, formName, fieldValue);
147         //
148
else if (formName.equals("Login2") && fieldName.equals(WLogin.P_CLIENT))
149             reply_Login2_Client (body, ctx, formName, fieldValue);
150     } // getReply
151

152     /**
153      * Login 2nd page Response - Field Role.
154      * <p>
155      * fill Client, Org, Warehouse
156      * @param body
157      * @param ctx
158      * @param formName
159      * @param fieldValue
160      */

161     private static void reply_Login2_Role (body body, Properties ctx, String JavaDoc formName, String JavaDoc fieldValue)
162     {
163         // Formname
164
String JavaDoc form = "top." + WEnv.TARGET_WINDOW + ".document." + formName + ".";
165         // Get Data
166
KeyNamePair[] clients = DB.loadClients(ctx, new KeyNamePair(Integer.parseInt(fieldValue) , fieldValue));
167
168         // Set Client ----
169
StringBuffer JavaDoc script = new StringBuffer JavaDoc ();
170         // var A=top.WWindow.document.formName.selectName.options;
171
script.append("var A=").append(form).append(WLogin.P_CLIENT).append(".options; ");
172         // A.length=0; // resets options
173
script.append("A.length=0; ");
174         // A[0]=new Option('text','value'); // add new oprtion
175

176         for (int i = 0; i < clients.length; i++)
177         {
178             KeyNamePair p = clients[i];
179             script.append("A[").append(i).append("]=new Option('");
180             script.append(p.getName()); // text
181
script.append("','");
182             script.append(p.getKey()); // value
183
script.append("'); ");
184         }
185         script.append("\n");
186
187         // Set Organization ----
188

189         if (clients.length > 0)
190         {
191             // var A=top.WWindow.document.formName.selectName.options;
192
script.append("var B=").append(form).append(WLogin.P_ORG).append(".options; ");
193             // A.length=0; // resets options
194
script.append("B.length=0; ");
195             // A[0]=new Option('text','value'); // add new oprtion
196

197             KeyNamePair[] orgs = DB.loadOrgs(ctx, clients[0]);
198             for (int i = 0; i < orgs.length; i++)
199             {
200                 KeyNamePair p = orgs[i];
201                 script.append("B[").append(i).append("]=new Option('");
202                 script.append(p.getName()); // text
203
script.append("','");
204                 script.append(p.getKey()); // value
205
script.append("'); ");
206             }
207             script.append("\n");
208         }
209
210         // Set Warehouse ----
211

212         if (clients.length > 0)
213         {
214             // var A=top.WWindow.document.formName.selectName.options;
215
script.append("var C=").append(form).append(WLogin.P_WAREHOUSE).append(".options; ");
216             // A.length=0; // resets options
217
script.append("C.length=0; ");
218             // A[0]=new Option('text','value'); // add new oprtion
219

220             KeyNamePair[] whs = DB.loadWarehouses(ctx, clients[0]);
221             if (whs != null)
222             {
223                 for (int i = 0; i < whs.length; i++)
224                 {
225                     KeyNamePair p = whs[i];
226                     script.append("C[").append(i).append("]=new Option('");
227                     script.append(p.getName()); // text
228
script.append("','");
229                     script.append(p.getKey()); // value
230
script.append("'); ");
231                 }
232             }
233         }
234
235         // add script
236
body.addElement(new p().addElement(WLogin.P_CLIENT + "="));
237         body.addElement(new script(script.toString()));
238     // Log.trace(Log.l6_Database, "reply_Login2_Role - Script=" + script.toString());
239
} // reply_Login2_Role
240

241     /**
242      * Login 2nd page Response - Field Client.
243      * <p>
244      * fill Org & Warehouse -
245      * @param body
246      * @param ctx
247      * @param formName
248      * @param fieldValue
249      */

250     private static void reply_Login2_Client (body body, Properties ctx, String JavaDoc formName, String JavaDoc fieldValue)
251     {
252         // Formname
253
String JavaDoc form = "top." + WEnv.TARGET_WINDOW + ".document." + formName + ".";
254         StringBuffer JavaDoc script = new StringBuffer JavaDoc ();
255
256         // Set Organization ----
257

258         // var A=top.WWindow.document.formName.selectName.options;
259
script.append("var B=").append(form).append(WLogin.P_ORG).append(".options; ");
260         // A.length=0; // resets options
261
script.append("B.length=0; ");
262         // A[0]=new Option('text','value'); // add new oprtion
263

264         KeyNamePair client = new KeyNamePair(Integer.parseInt(fieldValue), fieldValue);
265         KeyNamePair[] orgs = DB.loadOrgs(ctx, client);
266         for (int i = 0; i < orgs.length; i++)
267         {
268             KeyNamePair p = orgs[i];
269             script.append("B[").append(i).append("]=new Option('");
270             script.append(p.getName()); // text
271
script.append("','");
272             script.append(p.getKey()); // value
273
script.append("'); ");
274         }
275         script.append("\n");
276
277         // Set Warehouse ----
278

279         // var A=top.WWindow.document.formName.selectName.options;
280
script.append("var C=").append(form).append(WLogin.P_WAREHOUSE).append(".options; ");
281         // A.length=0; // resets options
282
script.append("C.length=0; ");
283         // A[0]=new Option('text','value'); // add new oprtion
284

285         KeyNamePair[] whs = DB.loadWarehouses(ctx, client);
286         if (whs != null)
287         {
288             for (int i = 0; i < whs.length; i++)
289             {
290                 KeyNamePair p = whs[i];
291                 script.append("C[").append(i).append("]=new Option('");
292                 script.append(p.getName()); // text
293
script.append("','");
294                 script.append(p.getKey()); // value
295
script.append("'); ");
296             }
297         }
298
299         // add script
300
body.addElement(new p().addElement(WLogin.P_WAREHOUSE + "="));
301         body.addElement(new script(script.toString()));
302     // Log.trace(Log.l6_Database, "Login2-Client - Script=" + script.toString());
303
} // reply_Login2_Client
304

305 } // WFieldUpdate
306
Popular Tags