KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_actionButton > CreateFromMultiple


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.ad_actionButton;
20
21 import org.openbravo.erpCommon.utility.SequenceIdData;
22 import org.openbravo.erpCommon.utility.Utility;
23 import org.openbravo.erpCommon.utility.ComboTableData;
24 import org.openbravo.utils.FormatUtilities;
25 import org.openbravo.base.secureApp.*;
26 import org.openbravo.xmlEngine.XmlDocument;
27 import java.io.*;
28 import java.util.*;
29 import javax.servlet.*;
30 import javax.servlet.http.*;
31 import org.openbravo.utils.Replace;
32
33 import java.math.BigDecimal JavaDoc;
34 import org.openbravo.erpCommon.ad_combos.WarehouseComboData;
35
36 // imports for transactions
37
import java.sql.Connection JavaDoc;
38
39
40 public class CreateFromMultiple extends HttpSecureAppServlet {
41   
42   static final BigDecimal JavaDoc ZERO = new BigDecimal JavaDoc(0.0);
43   
44
45   public void init (ServletConfig config) {
46     super.init(config);
47     boolHist = false;
48   }
49
50   public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
51     VariablesSecureApp vars = new VariablesSecureApp(request);
52     if (vars.commandIn("DEFAULT")) {
53       String JavaDoc strKey = vars.getGlobalVariable("inpmInoutId", "CreateFromMultiple|mInoutId");
54       String JavaDoc strWindowId = vars.getGlobalVariable("inpwindowId", "CreateFromMultiple|windowId", "");
55       String JavaDoc strTabName = vars.getGlobalVariable("inpTabId", "CreateFromMultiple|adTabId", "");
56       String JavaDoc strBpartner = vars.getGlobalVariable("inpcBpartnerId", "CreateFromMultiple|bpartner", "");
57       String JavaDoc strmWarehouseId = vars.getGlobalVariable("inpmWarehouseId", "CreateFromMultiple|mWarehouseId", "");
58       vars.setSessionValue("CreateFromMultiple|adProcessId", "800062");
59
60       printPage_FS(response, vars);
61     } else if (vars.commandIn("FRAME2")) {
62       printPage_F2(response, vars);
63     } else if (vars.commandIn("FRAME1")) {
64       String JavaDoc strWindowId = vars.getGlobalVariable("inpWindowId", "CreateFromMultiple|windowId");
65       String JavaDoc strSOTrx = Utility.getContext(this, vars, "isSOTrx", strWindowId);
66       String JavaDoc strKey = vars.getGlobalVariable("inpmInoutId", "CreateFromMultiple|mInoutId");
67       String JavaDoc strTabId = vars.getGlobalVariable("inpTabId", "CreateFromMultiple|adTabId");
68       String JavaDoc strProcessId = vars.getGlobalVariable("inpadProcessId", "CreateFromMultiple|adProcessId");
69       String JavaDoc strBpartner = vars.getGlobalVariable("inpcBpartnerId", "CreateFromMultiple|bpartner", "");
70       String JavaDoc strmWarehouseId = vars.getGlobalVariable("inpmWarehouseId", "CreateFromMultiple|mWarehouseId", "");
71       vars.removeSessionValue("CreateFromMultiple|mInoutId");
72       vars.removeSessionValue("CreateFromMultiple|windowId");
73       vars.removeSessionValue("CreateFromMultiple|adTabId");
74       vars.removeSessionValue("CreateFromMultiple|adProcessId");
75       vars.removeSessionValue("CreateFromMultiple|bpartner");
76
77       callPrintPage(response, vars, strKey, strWindowId, strSOTrx, strTabId, strProcessId, strBpartner, strmWarehouseId);
78     } else if (vars.commandIn("FIND")) {
79       String JavaDoc strKey = vars.getRequiredStringParameter("inpmInoutId");
80       String JavaDoc strWindowId = vars.getStringParameter("inpWindowId");
81       String JavaDoc strSOTrx = vars.getStringParameter("inpissotrx");
82       String JavaDoc strTabId = vars.getStringParameter("inpTabId");
83       String JavaDoc strBpartner = vars.getRequestGlobalVariable("inpcBpartnerId", "CreateFromMultiple|bpartner");
84       String JavaDoc strmWarehouseId = vars.getRequestGlobalVariable("inpmWarehouseId", "CreateFromMultiple|mWarehouseId");
85       callPrintPage(response, vars, strKey, strWindowId, strSOTrx, strTabId, "", strBpartner, strmWarehouseId);
86     } else if (vars.commandIn("SAVE")) {
87       String JavaDoc strKey = vars.getRequiredStringParameter("inpmInoutId");
88       String JavaDoc strWindowId = vars.getStringParameter("inpWindowId");
89       String JavaDoc strSOTrx = vars.getStringParameter("inpissotrx");
90       String JavaDoc strTabId = vars.getStringParameter("inpTabId");
91       String JavaDoc strMessage = saveMethod(vars, strKey, strWindowId, strSOTrx);
92       ActionButtonDefaultData[] tab = ActionButtonDefaultData.windowName(this, strTabId);
93       String JavaDoc strWindowPath="", strTabName="";
94       if (tab!=null && tab.length!=0) {
95         strTabName = FormatUtilities.replace(tab[0].name);
96         if (tab[0].help.equals("Y")) strWindowPath="../utility/WindowTree_FS.html?inpTabId=" + strTabId;
97         else strWindowPath = "../" + FormatUtilities.replace(tab[0].description) + "/" + strTabName + "_Relation.html";
98       } else strWindowPath = strDefaultServlet;
99       if (!strMessage.equals("")) vars.setSessionValue(strWindowId + "|" + strTabName + ".message", strMessage);
100       printPageClosePopUp(response, vars, strWindowPath);
101     } else pageErrorPopUp(response);
102   }
103
104
105   void printPage_FS(HttpServletResponse response, VariablesSecureApp vars) throws IOException, ServletException {
106     if (log4j.isDebugEnabled()) log4j.debug("Output: FrameSet");
107     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/CreateFromMultiple_FS").createXmlDocument();
108     response.setContentType("text/html; charset=UTF-8");
109     PrintWriter out = response.getWriter();
110     out.println(xmlDocument.print());
111     out.close();
112   }
113
114   void printPage_F2(HttpServletResponse response, VariablesSecureApp vars) throws IOException, ServletException {
115     if (log4j.isDebugEnabled()) log4j.debug("Output: Frame2");
116     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/CreateFromMultiple_F2").createXmlDocument();
117     xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
118     xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
119     response.setContentType("text/html; charset=UTF-8");
120     PrintWriter out = response.getWriter();
121     out.println(xmlDocument.print());
122     out.close();
123   }
124
125
126   void callPrintPage(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc strKey, String JavaDoc strWindowId, String JavaDoc strSOTrx, String JavaDoc strTabId, String JavaDoc strProcessId, String JavaDoc strBpartner, String JavaDoc strmWarehouseId) throws IOException, ServletException {
127     if (strSOTrx.equals("Y")) { //Shipment
128
printPageShipment(response, vars, strKey, strWindowId, strTabId, strSOTrx, strProcessId, strBpartner, strmWarehouseId);
129     } else { //Receipt
130
printPageReceipt(response, vars, strKey, strWindowId, strTabId, strSOTrx, strProcessId, strBpartner, strmWarehouseId);
131     }
132   }
133
134   void printPageReceipt(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc strKey, String JavaDoc strWindowId, String JavaDoc strTabId, String JavaDoc strSOTrx, String JavaDoc strProcessId, String JavaDoc strBpartner, String JavaDoc strmWarehouseId) throws IOException, ServletException {
135     if (log4j.isDebugEnabled()) log4j.debug("Output: Receipt");
136     ActionButtonDefaultData[] data = null;
137     String JavaDoc strHelp="", strDescription="";
138     if (vars.getLanguage().equals("en_US")) data = ActionButtonDefaultData.select(this, strProcessId);
139     else data = ActionButtonDefaultData.selectLanguage(this, vars.getLanguage(), strProcessId);
140     if (data!=null && data.length!=0) {
141       strDescription = data[0].description;
142       strHelp = data[0].help;
143     }
144     String JavaDoc[] discard = {""};
145     if (strHelp.equals("")) discard[0] = new String JavaDoc("helpDiscard");
146     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/CreateFromMultiple_Receipt", discard).createXmlDocument();
147     
148     xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
149     xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
150     xmlDocument.setParameter("help", strHelp);
151     xmlDocument.setParameter("description", strDescription);
152     xmlDocument.setParameter("key", strKey);
153     xmlDocument.setParameter("windowId", strWindowId);
154     xmlDocument.setParameter("tabId", strTabId);
155     xmlDocument.setParameter("sotrx", strSOTrx);
156     xmlDocument.setParameter("bpartner", strBpartner);
157     xmlDocument.setParameter("mWarehouseId", strmWarehouseId);
158
159
160     try {
161         ComboTableData comboTableData = new ComboTableData(this, "TABLEDIR", "C_UOM_ID", "", "", Utility.getContext(this, vars, "#User_Org", strWindowId), Utility.getContext(this, vars, "#User_Client", strWindowId), 0);
162         Utility.fillSQLParameters(this, vars, null, comboTableData, strWindowId, "");
163         xmlDocument.setData("reportC_UOM_ID","liststructure", comboTableData.select(false));
164         comboTableData = null;
165     } catch (Exception JavaDoc ex) {
166         throw new ServletException(ex);
167     }
168
169
170
171     try {
172         ComboTableData comboTableData = new ComboTableData(this, "TABLEDIR", "M_Warehouse_ID", "", "", Utility.getContext(this, vars, "#User_Org", strWindowId), Utility.getContext(this, vars, "#User_Client", strWindowId), 0);
173         Utility.fillSQLParameters(this, vars, null, comboTableData, strWindowId, strmWarehouseId);
174         xmlDocument.setData("reportM_WAREHOUSE_ID","liststructure", comboTableData.select(false));
175         comboTableData = null;
176     } catch (Exception JavaDoc ex) {
177         throw new ServletException(ex);
178     }
179
180     WarehouseComboData[] dataW = WarehouseComboData.select(this,vars.getRole(), vars.getClient());
181     if (strmWarehouseId.equals("") && dataW!=null && dataW.length>0) strmWarehouseId = dataW[0].mWarehouseId;
182     xmlDocument.setData("reportM_LOCATOR_X", "liststructure", CreateFromMultipleReceiptData.selectM_Locator_X(this,strmWarehouseId));
183
184
185 if (vars.getLanguage().equals("en_US")) {
186
187     try {
188         ComboTableData comboTableData = new ComboTableData(this, "TABLEDIR", "M_Inoutline_Type_ID", "", "", Utility.getContext(this, vars, "#User_Org", strWindowId), Utility.getContext(this, vars, "#User_Client", strWindowId), 0);
189         Utility.fillSQLParameters(this, vars, null, comboTableData, strWindowId, "");
190         xmlDocument.setData("reportM_INOUTLINETYPE_ID","liststructure", comboTableData.select(false));
191         comboTableData = null;
192     } catch (Exception JavaDoc ex) {
193         throw new ServletException(ex);
194     }
195
196     } else {
197
198     try {
199         ComboTableData comboTableData = new ComboTableData(this, "TABLEDIR", "M_Inoutline_Type_ID", "", "", Utility.getContext(this, vars, "#User_Org", strWindowId), Utility.getContext(this, vars, "#User_Client", strWindowId), 0);
200         Utility.fillSQLParameters(this, vars, null, comboTableData, strWindowId, "");
201         xmlDocument.setData("reportM_INOUTLINETYPE_ID","liststructure", comboTableData.select(false));
202         comboTableData = null;
203     } catch (Exception JavaDoc ex) {
204         throw new ServletException(ex);
205     }
206
207     }
208
209     try {
210         ComboTableData comboTableData = new ComboTableData(this, "TABLEDIR", "M_Locator_Type_ID", "", "", Utility.getContext(this, vars, "#User_Org", strWindowId), Utility.getContext(this, vars, "#User_Client", strWindowId), 0);
211         Utility.fillSQLParameters(this, vars, null, comboTableData, strWindowId, "");
212         xmlDocument.setData("reportM_LOCATOR_TYPE","liststructure", comboTableData.select(false));
213         comboTableData = null;
214     } catch (Exception JavaDoc ex) {
215         throw new ServletException(ex);
216     }
217
218
219     response.setContentType("text/html; charset=UTF-8");
220     PrintWriter out = response.getWriter();
221     out.println(xmlDocument.print());
222     out.close();
223   }
224
225   void printPageShipment(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc strKey, String JavaDoc strWindowId, String JavaDoc strTabId, String JavaDoc strSOTrx, String JavaDoc strProcessId, String JavaDoc strBpartner, String JavaDoc strmWarehouseId) throws IOException, ServletException {
226     if (log4j.isDebugEnabled()) log4j.debug("Output: Shipment");
227     String JavaDoc[] discard = {""};
228     String JavaDoc strProduct = vars.getStringParameter("inpmProductId");
229     //String strWarehouse = vars.getStringParameter("inpmWarehouseId");
230
String JavaDoc strX = vars.getStringParameter("inpx");
231     String JavaDoc strY = vars.getStringParameter("inpy");
232     String JavaDoc strZ = vars.getStringParameter("inpz");
233     CreateFromMultipleShipmentData[] data = null;
234     if (strProduct.equals("") && strmWarehouseId.equals("") && strX.equals("") && strY.equals("") && strZ.equals("")) {
235       discard[0] = new String JavaDoc("sectionDetail");
236       data = new CreateFromMultipleShipmentData[0];
237     } else {
238       data = CreateFromMultipleShipmentData.select(this, strBpartner, strProduct, strmWarehouseId, strX, strY, strZ, Utility.getContext(this, vars, "#User_Client", strWindowId));
239     }
240     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/CreateFromMultiple_Shipment", discard).createXmlDocument();
241     
242     xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
243     xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
244     xmlDocument.setParameter("key", strKey);
245     xmlDocument.setParameter("windowId", strWindowId);
246     xmlDocument.setParameter("tabId", strTabId);
247     xmlDocument.setParameter("sotrx", strSOTrx);
248     xmlDocument.setParameter("mWarehouseId", strmWarehouseId);//
249
xmlDocument.setParameter("bpartnerId", strBpartner);
250     xmlDocument.setParameter("bpartnerId_DES", CreateFromMultipleShipmentData.bpartnerDescription(this, strBpartner));
251     xmlDocument.setParameter("productId", strProduct);
252     xmlDocument.setParameter("productId_DES", CreateFromMultipleShipmentData.productDescription(this, strProduct));
253     xmlDocument.setParameter("x", strX);
254     xmlDocument.setParameter("y", strY);
255     xmlDocument.setParameter("z", strZ);
256
257
258     try {
259         ComboTableData comboTableData = new ComboTableData(this, "TABLEDIR", "M_Warehouse_ID", "", "", Utility.getContext(this, vars, "#User_Org", strWindowId), Utility.getContext(this, vars, "#User_Client", strWindowId), 0);
260         Utility.fillSQLParameters(this, vars, null, comboTableData, strWindowId, strmWarehouseId);
261         xmlDocument.setData("reportM_WAREHOUSE_ID","liststructure", comboTableData.select(false));
262         comboTableData = null;
263     } catch (Exception JavaDoc ex) {
264         throw new ServletException(ex);
265     }
266
267
268
269     try {
270         ComboTableData comboTableData = new ComboTableData(this, "TABLEDIR", "M_Inoutline_Type_ID", "", "", Utility.getContext(this, vars, "#User_Org", strWindowId), Utility.getContext(this, vars, "#User_Client", strWindowId), 0);
271         Utility.fillSQLParameters(this, vars, null, comboTableData, strWindowId, "");
272         xmlDocument.setData("reportM_INOUTLINETYPE_ID","liststructure", comboTableData.select(false));
273         comboTableData = null;
274     } catch (Exception JavaDoc ex) {
275         throw new ServletException(ex);
276     }
277
278     xmlDocument.setData("structure1", data);
279     response.setContentType("text/html; charset=UTF-8");
280     PrintWriter out = response.getWriter();
281     out.println(xmlDocument.print());
282     out.close();
283     
284   }
285
286   String JavaDoc saveMethod(VariablesSecureApp vars, String JavaDoc strKey, String JavaDoc strWindowId, String JavaDoc strSOTrx) throws IOException, ServletException {
287     if (strSOTrx.equals("Y")) return saveShipment(vars, strKey, strWindowId);
288     else return saveReceipt(vars, strKey, strWindowId);
289   }
290
291   String JavaDoc saveReceipt(VariablesSecureApp vars, String JavaDoc strKey, String JavaDoc strWindowId) throws IOException, ServletException {
292     if (log4j.isDebugEnabled()) log4j.debug("Save: Receipt");
293     String JavaDoc strProduct = vars.getRequiredStringParameter("inpmProductId");
294     String JavaDoc strAtributo = vars.getStringParameter("inpmAttributesetinstanceId");
295     String JavaDoc strQty = vars.getStringParameter("inpmovementqty");
296     String JavaDoc strUOM = vars.getStringParameter("inpcUomId");
297     String JavaDoc strQuantityOrder = vars.getStringParameter("inpquantityorder");
298     String JavaDoc strProductUOM = vars.getStringParameter("inpmProductUomId");
299     String JavaDoc strWarehouse = vars.getRequiredStringParameter("inpmWarehouseId");
300     String JavaDoc strInoutlineType = vars.getStringParameter("inpmInoutlineTypeId");
301     String JavaDoc strLocator = vars.getStringParameter("inpmLocatorX");
302     String JavaDoc strNumero = vars.getRequiredStringParameter("inpnumerolineas");
303     String JavaDoc strLocatorType = vars.getStringParameter("inpmLocatorType");
304     String JavaDoc strMessage = "";
305
306     Connection JavaDoc conn = null;
307     try {
308       conn = this.getTransactionConnection();
309       int total = Integer.valueOf(strNumero).intValue();
310       CreateFromMultipleReceiptData[] locators = CreateFromMultipleReceiptData.select(conn, this, Utility.getContext(this, vars, "#User_Client", strWindowId), Utility.getContext(this, vars, "#User_Org", strWindowId), strWarehouse, strLocator, strLocatorType);
311       int count = 0;
312       if (locators!=null && locators.length>0) {
313         for (count=0;count<total;count++) {
314           String JavaDoc strM_Locator_ID = (count>locators.length-1)?"":locators[count].mLocatorId;
315           if (strM_Locator_ID.equals("")) break;
316           String JavaDoc strSequence = SequenceIdData.getSequence(this, "M_InOutLine", vars.getClient());
317           CreateFromMultipleReceiptData.insert(conn, this, strSequence, vars.getClient(), vars.getOrg(), vars.getUser(), strKey, strM_Locator_ID, strProduct, strUOM, strQty, strAtributo, strQuantityOrder, strProductUOM, strInoutlineType);
318         }
319       }
320       strMessage = Utility.messageBD(this, "Success", vars.getLanguage()) + " - " + Utility.messageBD(this, "Created", vars.getLanguage()) + ": " + count;
321
322       releaseCommitConnection(conn);
323     } catch (Exception JavaDoc e) {
324       try {
325         releaseRollbackConnection(conn);
326       } catch (Exception JavaDoc ignored) {}
327       e.printStackTrace();
328       log4j.warn("Rollback in transaction");
329       return Utility.messageBD(this, "ProcessRunError", vars.getLanguage());
330     }
331     return strMessage;
332   }
333
334   String JavaDoc saveShipment(VariablesSecureApp vars, String JavaDoc strKey, String JavaDoc strWindowId) throws IOException, ServletException {
335     if (log4j.isDebugEnabled()) log4j.debug("Save: Shipment");
336     String JavaDoc strStorageDetail = vars.getInStringParameter("inpmStorageDetailId");
337     String JavaDoc strInoutlineType = vars.getStringParameter("inpmInoutlineTypeId");
338     if (strStorageDetail.equals("")) return "";
339     Connection JavaDoc conn = null;
340     try {
341       conn = this.getTransactionConnection();
342       if (strStorageDetail.startsWith("(")) strStorageDetail = strStorageDetail.substring(1, strStorageDetail.length()-1);
343       if (!strStorageDetail.equals("")) {
344         strStorageDetail = Replace.replace(strStorageDetail, "'", "");
345         StringTokenizer st = new StringTokenizer(strStorageDetail, ",", false);
346         while (st.hasMoreTokens()) {
347           String JavaDoc strStorageDetailId = st.nextToken().trim();
348           String JavaDoc strQty = vars.getStringParameter("inpmovementqty" + strStorageDetailId);
349           String JavaDoc strQtyOrder = vars.getStringParameter("inpquantityorder" + strStorageDetailId);
350           
351           String JavaDoc strSequence = SequenceIdData.getSequence(this, "M_InOutLine", vars.getClient());
352           CreateFromMultipleShipmentData.insert(conn, this, strSequence, vars.getClient(), vars.getOrg(), vars.getUser(), strKey, strQty, strQtyOrder, strInoutlineType, strStorageDetailId);
353         }
354       }
355       releaseCommitConnection(conn);
356     } catch (Exception JavaDoc e) {
357       try {
358         releaseRollbackConnection(conn);
359       } catch (Exception JavaDoc ignored) {}
360       e.printStackTrace();
361       log4j.warn("Rollback in transaction");
362       return Utility.messageBD(this, "ProcessRunError", vars.getLanguage());
363     }
364     return Utility.messageBD(this, "Success", vars.getLanguage());
365   }
366
367
368   public String JavaDoc getServletInfo() {
369     return "Servlet that presents the button of Create From Multiple";
370   } // end of getServletInfo() method
371
}
372
Popular Tags