KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
23 import org.openbravo.utils.FormatUtilities;
24 import org.openbravo.utils.Replace;
25 import org.openbravo.base.secureApp.HttpSecureAppServlet;
26 import org.openbravo.base.secureApp.VariablesSecureApp;
27 import org.openbravo.xmlEngine.XmlDocument;
28 import org.openbravo.exception.*;
29 import java.io.*;
30 import javax.servlet.*;
31 import javax.servlet.http.*;
32 import java.sql.*;
33
34
35 // imports for transactions
36
import java.sql.Connection JavaDoc;
37
38
39 public class CopyFromInvoice extends HttpSecureAppServlet {
40
41
42   public void init (ServletConfig config) {
43     super.init(config);
44     boolHist = false;
45   }
46
47   public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
48     VariablesSecureApp vars = new VariablesSecureApp(request);
49
50     if (vars.commandIn("DEFAULT")) {
51       String JavaDoc strProcessId = vars.getStringParameter("inpProcessId");
52       String JavaDoc strWindow = vars.getStringParameter("inpwindowId");
53       String JavaDoc strTab = vars.getStringParameter("inpTabId");
54       String JavaDoc strKey = vars.getStringParameter("inpcInvoiceId");
55       printPage(response, vars, strKey, strWindow, strTab, strProcessId);
56     } else if (vars.commandIn("SAVE")) {
57       String JavaDoc strKey = vars.getStringParameter("inpcInvoiceId");
58       String JavaDoc strInvoice = vars.getStringParameter("inpNewcInvoiceId");
59       String JavaDoc strWindow = vars.getStringParameter("inpwindowId");
60       String JavaDoc strTab = vars.getStringParameter("inpTabId");
61       ActionButtonDefaultData[] tab = ActionButtonDefaultData.windowName(this, strTab);
62       String JavaDoc strWindowPath="", strTabName="";
63       if (tab!=null && tab.length!=0) {
64         strTabName = FormatUtilities.replace(tab[0].name);
65         strWindowPath = "../" + FormatUtilities.replace(tab[0].description) + "/" + strTabName + "_Relation.html";
66       } else strWindowPath = strDefaultServlet;
67
68       try {
69         Connection JavaDoc conn = getTransactionConnection();
70         String JavaDoc strMessage = processButton(conn, vars, strKey, strInvoice, strWindow);
71         if (!strMessage.equals("") && !strTabName.equals("")) {
72           vars.setSessionValue(strWindow + "|" + strTabName + ".message", Replace.replace(strMessage, "'", "\\'"));
73         }
74       } catch (NoConnectionAvailableException ex) {
75         bdErrorConnection(response);
76         return;
77       } catch (SQLException ex2) {
78         OBError myError = Utility.translateError(this, vars, vars.getLanguage(), ex2.toString());
79         if (!myError.isConnectionAvailable()) {
80           bdErrorConnection(response);
81           return;
82         } else vars.setMessage(strTab, myError);
83       }
84       printPageClosePopUp(response, vars, strWindowPath);
85       //response.sendRedirect(strDireccion + strWindowPath);
86
} else pageErrorPopUp(response);
87   }
88
89   String JavaDoc processButton(Connection JavaDoc conn, VariablesSecureApp vars, String JavaDoc strKey, String JavaDoc strInvoice, String JavaDoc windowId) {
90     int i=0;
91     try {
92       CopyFromInvoiceData[] data = CopyFromInvoiceData.select(conn, this, strInvoice, Utility.getContext(this, vars, "#User_Client", windowId), Utility.getContext(this, vars, "#User_Org", windowId));
93       if (data!=null && data.length!=0) {
94         for (i=0;i<data.length;i++) {
95           String JavaDoc strSequence = SequenceIdData.getSequence(this, "C_InvoiceLine", vars.getClient());
96           CopyFromInvoiceData.insert(conn, this, strSequence, strKey, vars.getClient(), vars.getOrg(), vars.getUser(), data[i].cInvoicelineId);
97         }
98       }
99       releaseCommitConnection(conn);
100     } catch (Exception JavaDoc e) {
101       try {
102         releaseRollbackConnection(conn);
103       } catch (Exception JavaDoc ignored) {}
104       e.printStackTrace();
105       log4j.warn("Rollback in transaction");
106       return Utility.messageBD(this, "ProcessRunError", vars.getLanguage());
107     }
108     return (Utility.messageBD(this, "RecordsCopied", vars.getLanguage()) + i);
109   }
110
111
112   void printPage(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc strKey, String JavaDoc windowId, String JavaDoc strTab, String JavaDoc strProcessId)
113     throws IOException, ServletException {
114       if (log4j.isDebugEnabled()) log4j.debug("Output: Button process Copy from Invoice");
115
116       ActionButtonDefaultData[] data = null;
117       String JavaDoc strHelp="", strDescription="";
118       if (vars.getLanguage().equals("en_US")) data = ActionButtonDefaultData.select(this, strProcessId);
119       else data = ActionButtonDefaultData.selectLanguage(this, vars.getLanguage(), strProcessId);
120
121       if (data!=null && data.length!=0) {
122         strDescription = data[0].description;
123         strHelp = data[0].help;
124       }
125       String JavaDoc[] discard = {""};
126       if (strHelp.equals("")) discard[0] = new String JavaDoc("helpDiscard");
127       XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/CopyFromInvoice", discard).createXmlDocument();
128       xmlDocument.setParameter("key", strKey);
129       xmlDocument.setParameter("window", windowId);
130       xmlDocument.setParameter("tab", strTab);
131       xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
132       xmlDocument.setParameter("question", Utility.messageBD(this, "StartProcess?", vars.getLanguage()));
133       xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
134       xmlDocument.setParameter("description", strDescription);
135       xmlDocument.setParameter("help", strHelp);
136
137       
138       response.setContentType("text/html; charset=UTF-8");
139       PrintWriter out = response.getWriter();
140       out.println(xmlDocument.print());
141       out.close();
142     }
143
144   public String JavaDoc getServletInfo() {
145     return "Servlet Copy from invoice";
146   } // end of getServletInfo() method
147
}
148
149
Popular Tags