KickJava   Java API By Example, From Geeks To Geeks.

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


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  * WLookup Servlet.
30  * <p>
31  * The servlet is invoked by a parent window via
32  * <code>
33  * WLookup?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: WLookup.java,v 1.1.1.1 2002/10/12 01:06:55 jjanke Exp $
44  */

45 public class WLookup extends HttpServlet
46 {
47     /**
48      * Initialize global variables
49      *
50      * @param 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("WLookup.init");
58     } // init
59

60     /**
61      * Process the HTTP Get request - initial Start
62      * Needs to have parameters FormName and ColumnName
63      *
64      * @param request
65      * @param response
66      * @throws ServletException
67      * @throws IOException
68      */

69     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
70     {
71         Log.trace(Log.l3_Util, "WLookup.doGet");
72         HttpSession sess = request.getSession(false);
73         WWindowStatus ws = null;
74         if (sess != null)
75             ws = (WWindowStatus)sess.getAttribute(WEnv.SA_WINDOW);
76         if (ws == null)
77         {
78             WUtil.createTimeoutPage(request, response, this, null, null);
79             return;
80         }
81         // Get Mandatory Parameters
82
String JavaDoc formName = request.getParameter("FormName");
83         String JavaDoc columnName = request.getParameter("ColumnName");
84         //
85
MField mField = ws.curTab.getField(columnName);
86         Log.trace(Log.l4_Data, "FormName=" + formName, "ColumnName=" + columnName + ", MField=" + mField.toString());
87         if (mField == null || formName == null || columnName == null || formName.equals("") || columnName.equals(""))
88         {
89             WUtil.createTimeoutPage(request, response, this, ws.ctx, Msg.getMsg(ws.ctx, "ParameterMissing"));
90             return;
91         }
92     // Object value = ws.curTab.getValue(columnName);
93
String JavaDoc target = "opener.document." + formName + "." + columnName;
94
95         // Create Document
96
WDoc doc = WDoc.create (mField.getHeader());
97         body body = doc.getBody();
98         body.setOnBlur("self.focus();");
99         body.addElement(fillTable(ws, mField, target));
100
101         // Reset, Cancel
102
button reset = new button();
103         reset.addElement("Reset"); // translate
104
reset.setOnClick(target + ".value='';" + target + "_D.value='';window.close();");
105         button cancel = new button();
106         cancel.addElement("Cancel"); // translate
107
cancel.setOnClick("window.close();");
108         body.addElement(new p(AlignType.right)
109             .addElement(reset)
110             .addElement("&nbsp")
111             .addElement(cancel));
112         //
113
// Log.trace(Log.l6_Database, doc.toString());
114
WUtil.createResponse (request, response, this, null, doc, false);
115     } // doGet
116

117
118     /**
119      * Process the HTTP Post request - perform Get
120      * @param request
121      * @param response
122      * @throws ServletException
123      * @throws IOException
124      */

125     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
126     {
127         Log.trace(Log.l3_Util, "WLookup.doPost");
128         doGet(request, response);
129     } // doPost
130

131
132     /*************************************************************************/
133
134     /**
135      * Fill Table (Generic)
136      *
137      * @param ws WindowStatus
138      * @param mField the Field
139      * @param target target field string
140      * @return Table with selection
141      */

142     private table fillTable (WWindowStatus ws, MField mField, String JavaDoc target)
143     {
144         if (mField.getColumnName().equals("C_BPartner_ID"))
145             return fillTable_BPartner (ws, mField, target);
146         else if (mField.getColumnName().equals("M_Product_ID"))
147             return fillTable_Product (ws, mField, target);
148         //
149
table table = new table("1");
150         tr line = new tr();
151         line.addElement(new th("&nbsp")).addElement(new th(Msg.translate(ws.ctx, "Name")));
152         table.addElement(line);
153
154         // Fill & list options
155
Lookup lookup = mField.getLookup();
156         lookup.fillComboBox(mField.isMandatory(false), true, true, true); // no context check
157
int size = lookup.getSize();
158         for (int i = 0; i < size; i++)
159         {
160             Object JavaDoc lValue = lookup.getElementAt(i);
161             if (!(lValue != null && lValue instanceof KeyNamePair))
162                 continue;
163             //
164
// Log.trace(Log.l6_Database, lValue.toString());
165
KeyNamePair np = (KeyNamePair)lValue;
166             button button = new button();
167             button.addElement("&gt;");
168             StringBuffer JavaDoc script = new StringBuffer JavaDoc(target);
169             script.append(".value='").append(np.getKey()).append("';")
170                 .append(target).append("_D.value='").append(np.getName()).append("';window.close();");
171             button.setOnClick(script.toString());
172             //
173
line = new tr();
174             line.addElement(new td(button));
175             String JavaDoc name = np.getName();
176             if (name == null || name.length() == 0)
177                 name = "&nbsp";
178             line.addElement(new td(name));
179             table.addElement(line);
180         }
181         // Restore
182
lookup.fillComboBox(true);
183         return table;
184     } // fillTable
185

186     /**
187      * Fill Table BPartner
188      *
189      * @param ws WindowStatus
190      * @param mField the Field
191      * @param target target field string
192      * @return Table with selection
193      */

194     private table fillTable_BPartner (WWindowStatus ws, MField mField, String JavaDoc target)
195     {
196         table table = new table("1");
197         tr line = new tr();
198         line.addElement(new th("&nbsp")).addElement(new th(Msg.translate(ws.ctx, "Name")));
199         table.addElement(line);
200
201         // Fill & list options
202
Lookup lookup = mField.getLookup();
203         lookup.fillComboBox(mField.isMandatory(false), true, true, true); // no context check
204
int size = lookup.getSize();
205         for (int i = 0; i < size; i++)
206         {
207             Object JavaDoc lValue = lookup.getElementAt(i);
208             if (!(lValue != null && lValue instanceof KeyNamePair))
209                 continue;
210             //
211
// Log.trace(Log.l6_Database, lValue.toString());
212
KeyNamePair np = (KeyNamePair)lValue;
213             button button = new button();
214             button.addElement("&gt;");
215             StringBuffer JavaDoc script = new StringBuffer JavaDoc(target);
216             script.append(".value='").append(np.getKey()).append("';")
217                 .append(target).append("_D.value='").append(np.getName()).append("';window.close();");
218             button.setOnClick(script.toString());
219             //
220
line = new tr();
221             line.addElement(new td(button));
222             String JavaDoc name = np.getName();
223             if (name == null || name.length() == 0)
224                 name = "&nbsp";
225             line.addElement(new td(name));
226             table.addElement(line);
227         }
228         // Restore
229
lookup.fillComboBox(true);
230         return table;
231     } // fillTable_BPartner
232

233     /**
234      * Fill Table Product
235      *
236      * @param ws WindowStatus
237      * @param mField the Field
238      * @param target target field string
239      * @return Table with selection
240      */

241     private table fillTable_Product (WWindowStatus ws, MField mField, String JavaDoc target)
242     {
243         table table = new table("1");
244         tr line = new tr();
245         line.addElement(new th("&nbsp")).addElement(new th(Msg.translate(ws.ctx, "Name")));
246         table.addElement(line);
247
248         // Fill & list options
249
Lookup lookup = mField.getLookup();
250         lookup.fillComboBox(mField.isMandatory(false), true, true, true); // no context check
251
int size = lookup.getSize();
252         for (int i = 0; i < size; i++)
253         {
254             Object JavaDoc lValue = lookup.getElementAt(i);
255             if (!(lValue != null && lValue instanceof KeyNamePair))
256                 continue;
257             //
258
// Log.trace(Log.l6_Database, lValue.toString());
259
KeyNamePair np = (KeyNamePair)lValue;
260             button button = new button();
261             button.addElement("&gt;");
262             StringBuffer JavaDoc script = new StringBuffer JavaDoc(target);
263             script.append(".value='").append(np.getKey()).append("';")
264                 .append(target).append("_D.value='").append(np.getName()).append("';window.close();");
265             button.setOnClick(script.toString());
266             //
267
line = new tr();
268             line.addElement(new td(button));
269             String JavaDoc name = np.getName();
270             if (name == null || name.length() == 0)
271                 name = "&nbsp";
272             line.addElement(new td(name));
273             table.addElement(line);
274         }
275         // Restore
276
lookup.fillComboBox(true);
277         return table;
278     } // fillTable_Product
279

280 } // WLookup
281
Popular Tags