KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > info > Product


1 /*
2  *************************************************************************
3  * The contents of this file are subject to the Openbravo Public License
4  * Version 1.0 (the "License"), being the Mozilla Public License
5  * Version 1.1 with a permitted attribution clause; you may not use this
6  * file except in compliance with the License. You may obtain a copy of
7  * the License at http://www.openbravo.com/legal/license.html
8  * Software distributed under the License is distributed on an "AS IS"
9  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
10  * License for the specific language governing rights and limitations
11  * under the License.
12  * The Original Code is Openbravo ERP.
13  * The Initial Developer of the Original Code is Openbravo SL
14  * All portions are Copyright (C) 2001-2006 Openbravo SL
15  * All Rights Reserved.
16  * Contributor(s): ______________________________________.
17  ************************************************************************
18 */

19 package org.openbravo.erpCommon.info;
20
21 import org.openbravo.base.secureApp.*;
22 import org.openbravo.xmlEngine.XmlDocument;
23 import java.io.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26 import org.openbravo.utils.Replace;
27
28 import org.openbravo.erpCommon.utility.DateTimeData;
29 import org.openbravo.erpCommon.utility.Utility;
30 import org.openbravo.erpCommon.utility.ToolBar;
31
32 import org.openbravo.erpCommon.ad_combos.WarehouseComboData;
33 import org.openbravo.erpCommon.ad_combos.PriceListVersionComboData;
34
35
36 public class Product extends HttpSecureAppServlet {
37   
38   
39   public void init (ServletConfig config) {
40     super.init(config);
41     boolHist = false;
42   }
43
44   public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
45     VariablesSecureApp vars = new VariablesSecureApp(request);
46
47     if (vars.commandIn("DEFAULT")) {
48       String JavaDoc strNameValue = vars.getRequestGlobalVariable("inpNameValue", "Product.name");
49       String JavaDoc strIDValue = vars.getStringParameter("inpIDValue");
50       if (!strIDValue.equals("")) {
51         String JavaDoc strNameAux = ProductData.existsActual(this, vars.getLanguage(), strNameValue, strIDValue);
52         if (!strNameAux.equals("")) strNameValue = strNameAux;
53       }
54       String JavaDoc strPriceList = vars.getRequestGlobalVariable("inpPriceList", "Product.priceList");
55       String JavaDoc strDate = vars.getRequestGlobalVariable("inpDate", "Product.date");
56       String JavaDoc windowId = vars.getRequestGlobalVariable("WindowID", "Product.windowId");
57       String JavaDoc strWarehouse = vars.getGlobalVariable("inpWarehouse", "Product.warehouse", Utility.getContext(this, vars, "M_Warehouse_ID", windowId));
58       vars.removeSessionValue("Product.key");
59       if (!strNameValue.equals("")) vars.setSessionValue("Product.name", strNameValue + "%");
60       if (strPriceList.equals("")) {
61         strPriceList = Utility.getContext(this, vars, "M_Pricelist_ID", windowId);
62         if (strPriceList.equals("")) strPriceList = ProductData.priceListDefault(this, Utility.getContext(this, vars, "#User_Client", "Product"), Utility.getContext(this, vars, "#User_Org", "Product"));
63         vars.setSessionValue("Product.priceList", strPriceList);
64       }
65       if (strDate.equals("")) {
66         strDate = Utility.getContext(this, vars, "DateOrdered", windowId);
67         if (log4j.isDebugEnabled()) log4j.debug("DateOrdered:"+strDate);
68       }
69       if (strDate.equals("")) {
70         strDate = Utility.getContext(this, vars, "DateInvoiced", windowId);
71         if (log4j.isDebugEnabled()) log4j.debug("DateInvoiced:"+strDate);
72       }
73       if (strDate.equals("")) strDate = DateTimeData.today(this);
74       vars.setSessionValue("Product.date", strDate);
75
76       vars.setSessionValue("Product.priceListVersion", getPriceListVersion(vars, strPriceList, strDate));
77
78       printPageFS(response, vars);
79     } else if (vars.commandIn("KEY")) {
80       String JavaDoc strKeyValue = vars.getRequestGlobalVariable("inpNameValue", "Product.key");
81       String JavaDoc strIDValue = vars.getStringParameter("inpIDValue");
82       if (!strIDValue.equals("")) {
83         String JavaDoc strNameAux = ProductData.existsActualValue(this, vars.getLanguage(), strKeyValue, strIDValue);
84         if (!strNameAux.equals("")) strKeyValue = strNameAux;
85       }
86       String JavaDoc strWarehouse = vars.getRequestGlobalVariable("inpWarehouse", "Product.warehouse");
87       String JavaDoc strPriceList = vars.getRequestGlobalVariable("inpPriceList", "Product.priceList");
88       String JavaDoc strDate = vars.getRequestGlobalVariable("inpDate", "Product.date");
89       String JavaDoc windowId = vars.getRequestGlobalVariable("WindowID", "Product.windowId");
90       vars.removeSessionValue("Product.name");
91       if (!strKeyValue.equals("")) vars.setSessionValue("Product.key", strKeyValue + "%");
92       if (strWarehouse.equals("")) strWarehouse = Utility.getContext(this, vars, "M_Warehouse_ID", windowId);
93       vars.setSessionValue("Product.warehouse", strWarehouse);
94       if (strPriceList.equals("")) {
95         strPriceList = Utility.getContext(this, vars, "M_Pricelist_ID", windowId);
96         if (strPriceList.equals("")) strPriceList = ProductData.priceListDefault(this, Utility.getContext(this, vars, "#User_Client", "Product"), Utility.getContext(this, vars, "#User_Org", "Product"));
97         vars.setSessionValue("Product.priceList", strPriceList);
98       }
99       
100       if (strDate.equals("")) {
101         strDate = Utility.getContext(this, vars, "DateOrdered", windowId);
102         if (log4j.isDebugEnabled()) log4j.debug("DateOrdered:"+strDate);
103       }
104       if (strDate.equals("")) {
105         strDate = Utility.getContext(this, vars, "DateInvoiced", windowId);
106         if (log4j.isDebugEnabled()) log4j.debug("DateInvoiced:"+strDate);
107       }
108       vars.setSessionValue("Product.date", strDate);
109
110       String JavaDoc strPriceListVersion = getPriceListVersion(vars, strPriceList, strDate);
111       vars.setSessionValue("Product.priceListVersion", strPriceListVersion);
112       ProductData[] data = ProductData.select(this, vars.getLanguage(), strWarehouse, strKeyValue + "%", "", strPriceListVersion, Utility.getContext(this, vars, "#User_Client", "Product"), Utility.getContext(this, vars, "#User_Org", "Product"));
113       if (data!=null && data.length==1) printPageKey(response, vars, data, strWarehouse, strPriceListVersion);
114       else printPageFS(response, vars);
115     } else if (vars.commandIn("FRAME1")) {
116       String JavaDoc strKeyValue = vars.getGlobalVariable("inpKey", "Product.key", "");
117       String JavaDoc strNameValue = vars.getGlobalVariable("inpName", "Product.name", "");
118       String JavaDoc strWarehouse = vars.getGlobalVariable("inpWarehouse", "Product.warehouse", "");
119       String JavaDoc strPriceList = vars.getGlobalVariable("inpPriceList", "Product.priceList", "");
120       String JavaDoc strPriceListVersion = vars.getGlobalVariable("inpPriceListVersion", "Product.priceListVersion", "");
121       String JavaDoc windowId = vars.getGlobalVariable("WindowID", "Product.windowId", "");
122       printPageFrame1(response, vars, strKeyValue, strNameValue, strWarehouse, strPriceList, strPriceListVersion, windowId);
123     } else if (vars.commandIn("FRAME2")) {
124       String JavaDoc strKey = vars.getGlobalVariable("inpKey", "Product.key", "");
125       String JavaDoc strName = vars.getGlobalVariable("inpName", "Product.name", "");
126       String JavaDoc strWarehouse = vars.getGlobalVariable("inpWarehouse", "Product.warehouse", "");
127       String JavaDoc strPriceListVersion = vars.getGlobalVariable("inpPriceListVersion", "Product.priceListVersion", "");
128       printPageFrame2(response, vars, strKey, strName, strWarehouse, strPriceListVersion);
129     } else if (vars.commandIn("FIND")) {
130       String JavaDoc strKey = vars.getRequestGlobalVariable("inpKey", "Product.key");
131       String JavaDoc strName = vars.getRequestGlobalVariable("inpName", "Product.name");
132       String JavaDoc strWarehouse = vars.getRequiredGlobalVariable("inpWarehouse", "Product.warehouse");
133       String JavaDoc strPriceListVersion = vars.getRequiredGlobalVariable("inpPriceListVersion", "Product.priceListVersion");
134
135       vars.setSessionValue("Product.initRecordNumber", "0");
136
137       printPageFrame2(response, vars, strKey, strName, strWarehouse, strPriceListVersion);
138     } else if (vars.commandIn("FRAME3")) {
139       printPageFrame3(response, vars);
140     } else if (vars.commandIn("PREVIOUS_RELATION")) {
141       String JavaDoc strInitRecord = vars.getSessionValue("Product.initRecordNumber");
142       String JavaDoc strRecordRange = Utility.getContext(this, vars, "#RecordRangeInfo", "Product");
143       int intRecordRange = strRecordRange.equals("")?0:Integer.parseInt(strRecordRange);
144       if (strInitRecord.equals("") || strInitRecord.equals("0")) vars.setSessionValue("Product.initRecordNumber", "0");
145       else {
146         int initRecord = (strInitRecord.equals("")?0:Integer.parseInt(strInitRecord));
147         initRecord -= intRecordRange;
148         strInitRecord = ((initRecord<0)?"0":Integer.toString(initRecord));
149         vars.setSessionValue("Product.initRecordNumber", strInitRecord);
150       }
151
152       response.sendRedirect(strDireccion + request.getServletPath() + "?Command=FRAME2");
153     } else if (vars.commandIn("NEXT_RELATION")) {
154       String JavaDoc strInitRecord = vars.getSessionValue("Product.initRecordNumber");
155       String JavaDoc strRecordRange = Utility.getContext(this, vars, "#RecordRangeInfo", "Product");
156       int intRecordRange = strRecordRange.equals("")?0:Integer.parseInt(strRecordRange);
157       int initRecord = (strInitRecord.equals("")?0:Integer.parseInt(strInitRecord));
158       if (initRecord==0) initRecord=1;
159       initRecord += intRecordRange;
160       strInitRecord = ((initRecord<0)?"0":Integer.toString(initRecord));
161       vars.setSessionValue("Product.initRecordNumber", strInitRecord);
162
163       response.sendRedirect(strDireccion + request.getServletPath() + "?Command=FRAME2");
164     } else pageError(response);
165   }
166
167
168   String JavaDoc getPriceListVersion(VariablesSecureApp vars, String JavaDoc strPriceList, String JavaDoc strDate) throws IOException,ServletException {
169     PriceListVersionComboData[] data = PriceListVersionComboData.selectActual(this, strPriceList, strDate, Utility.getContext(this, vars, "#User_Client", "Product"));
170     if (log4j.isDebugEnabled()) log4j.debug("Selecting pricelistversion date:"+strDate+" - pricelist:"+strPriceList);
171     if (data==null || data.length==0) return "";
172     return data[0].mPricelistVersionId;
173   }
174
175   void printPageFS(HttpServletResponse response, VariablesSecureApp vars) throws IOException, ServletException {
176     if (log4j.isDebugEnabled()) log4j.debug("Output: product seeker Frame Set");
177     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/info/Product_FS").createXmlDocument();
178
179     response.setContentType("text/html; charset=UTF-8");
180     PrintWriter out = response.getWriter();
181     out.println(xmlDocument.print());
182     out.close();
183   }
184
185   void printPageKey(HttpServletResponse response, VariablesSecureApp vars, ProductData[] data, String JavaDoc strWarehouse, String JavaDoc strPriceListVersion) throws IOException, ServletException {
186     if (log4j.isDebugEnabled()) log4j.debug("Output: product seeker Frame Set");
187     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/info/SearchUniqueKeyResponse").createXmlDocument();
188
189     xmlDocument.setParameter("script", generateResult(data, strWarehouse, strPriceListVersion));
190     response.setContentType("text/html; charset=UTF-8");
191     PrintWriter out = response.getWriter();
192     out.println(xmlDocument.print());
193     out.close();
194   }
195
196   String JavaDoc generateResult(ProductData[] data, String JavaDoc strWarehouse, String JavaDoc strPriceListVersion) throws IOException, ServletException {
197     StringBuffer JavaDoc html = new StringBuffer JavaDoc();
198     
199     html.append("\nfunction depurarSelector() {\n");
200     html.append("var clave = \"" + data[0].mProductId + "\";\n");
201     html.append("var texto = \"" + Replace.replace(data[0].name, "\"", "\\\"") + "\";\n");
202     html.append("var parameter = new Array(\n");
203     html.append("new SearchElements(\"_UOM\", true, \"" + data[0].cUomId + "\"),\n");
204     html.append("new SearchElements(\"_PSTD\", true, \"" + data[0].pricestd + "\"),\n");
205     html.append("new SearchElements(\"_PLIM\", true, \"" + data[0].pricelimit + "\"),\n");
206     html.append("new SearchElements(\"_CURR\", true, \"" + data[0].cCurrencyId + "\"),\n");
207     html.append("new SearchElements(\"_PLIST\", true, \"" + data[0].pricelist + "\")\n");
208     html.append(");\n");
209     html.append("parent.opener.closeSearch(\"SAVE\", clave, texto, parameter);\n");
210     html.append("}\n");
211     return html.toString();
212   }
213
214   void printPageFrame1(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc strKeyValue, String JavaDoc strNameValue, String JavaDoc strWarehouse, String JavaDoc strPriceList, String JavaDoc strPriceListVersion, String JavaDoc windowId) throws IOException, ServletException {
215     if (log4j.isDebugEnabled()) log4j.debug("Output: Frame 1 of the products seeker");
216     String JavaDoc[] discard=new String JavaDoc[1];
217     if (windowId.equals("800004")) {
218       discard[0] = new String JavaDoc("NotReducedSearch");
219     } else {
220       discard[0] = new String JavaDoc("ReducedSearch");
221     }
222     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/info/Product_F1", discard).createXmlDocument();
223
224     if (strKeyValue.equals("") && strNameValue.equals("")) {
225     xmlDocument.setParameter("key", "%");
226     } else {
227     xmlDocument.setParameter("key", strKeyValue);
228     }
229     xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
230     xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
231     xmlDocument.setParameter("name", strNameValue);
232     xmlDocument.setParameter("warehouse", strWarehouse);
233     xmlDocument.setParameter("priceListVersion", strPriceListVersion);
234     xmlDocument.setData("structure1", WarehouseComboData.selectFilter(this, Utility.getContext(this, vars, "#User_Client", "Product")));
235     xmlDocument.setData("structure2", PriceListVersionComboData.select(this, strPriceList, Utility.getContext(this, vars, "#User_Client", "Product")));
236     response.setContentType("text/html; charset=UTF-8");
237     PrintWriter out = response.getWriter();
238     out.println(xmlDocument.print());
239     out.close();
240   }
241
242   void printPageFrame2(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc strKey, String JavaDoc strName, String JavaDoc strWarehouse, String JavaDoc strPriceListVersion) throws IOException, ServletException {
243     if (log4j.isDebugEnabled()) log4j.debug("Output: Frame 2 of the products seeker");
244     XmlDocument xmlDocument;
245
246     String JavaDoc strRecordRange = Utility.getContext(this, vars, "#RecordRangeInfo", "Product");
247     int intRecordRange = (strRecordRange.equals("")?0:Integer.parseInt(strRecordRange));
248     String JavaDoc strInitRecord = vars.getSessionValue("Product.initRecordNumber");
249     int initRecordNumber = (strInitRecord.equals("")?0:Integer.parseInt(strInitRecord));
250     boolean hasPrevious = false, hasNext = false;
251
252     if (strKey.equals("") && strName.equals("")) {
253       String JavaDoc[] discard = {"sectionDetail"};
254       xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/info/Product_F2", discard).createXmlDocument();
255       xmlDocument.setData("structure1", ProductData.set());
256     } else {
257       hasPrevious = hasNext = true;
258       ProductData[] data = ProductData.select(this, vars.getLanguage(), strWarehouse, strKey, strName, strPriceListVersion, Utility.getContext(this, vars, "#User_Client", "Product"), Utility.getContext(this, vars, "#User_Org", "Product"), initRecordNumber, intRecordRange);
259       if (data==null || data.length==0 || initRecordNumber<=1) hasPrevious = false;
260       if (data==null || data.length==0 || data.length<intRecordRange) hasNext = false;
261       xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/info/Product_F2").createXmlDocument();
262       xmlDocument.setData("structure1", data);
263     }
264
265     ToolBar toolbar = new ToolBar(this, vars.getLanguage(), "Product_F2", false, "document.frmMain.inpClave", "", "", false, "info", strReplaceWith, false, true);
266     toolbar.prepareInfoTemplate(hasPrevious, hasNext, vars.getSessionValue("#ShowTest", "N").equals("Y"));
267     xmlDocument.setParameter("toolbar", toolbar.toString());
268     xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
269     response.setContentType("text/html; charset=UTF-8");
270     PrintWriter out = response.getWriter();
271     out.println(xmlDocument.print());
272     out.close();
273   }
274
275   void printPageFrame3(HttpServletResponse response, VariablesSecureApp vars) throws IOException, ServletException {
276     if (log4j.isDebugEnabled()) log4j.debug("Output: Frame 3 of the products seeker");
277     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/info/Product_F3").createXmlDocument();
278
279     response.setContentType("text/html; charset=UTF-8");
280     PrintWriter out = response.getWriter();
281     out.println(xmlDocument.print());
282     out.close();
283   }
284
285   public String JavaDoc getServletInfo() {
286     return "Servlet that presents the products seeker";
287   } // end of getServletInfo() method
288
}
289
Popular Tags