KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.mail.smtp.SMTPMessage;
22
import org.openbravo.erpCommon.utility.SequenceIdData;
23 import org.openbravo.erpCommon.reference.*;
24 import org.openbravo.erpCommon.utility.Utility;
25 import org.openbravo.utils.Replace;
26 import org.openbravo.utils.FormatUtilities;
27 import org.openbravo.erpCommon.businessUtility.*;
28 import org.openbravo.base.secureApp.HttpSecureAppServlet;
29 import org.openbravo.base.secureApp.VariablesSecureApp;
30 import org.openbravo.xmlEngine.XmlDocument;
31 import java.io.*;
32 import javax.servlet.*;
33 import javax.servlet.http.*;
34
35
36 // imports for transactions
37
import java.sql.Connection JavaDoc;
38
39
40 public class RequestAction extends HttpSecureAppServlet {
41   
42
43   public void init (ServletConfig config) {
44     super.init(config);
45     boolHist = false;
46   }
47
48   public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
49     VariablesSecureApp vars = new VariablesSecureApp(request);
50
51     if (vars.commandIn("DEFAULT")) {
52       String JavaDoc strProcessId = vars.getStringParameter("inpProcessId");
53       String JavaDoc strWindow = vars.getStringParameter("inpwindowId");
54       String JavaDoc strTab = vars.getStringParameter("inpTabId");
55       String JavaDoc strKey = vars.getRequiredGlobalVariable("inprRequestId", strWindow + "|R_Request_ID");
56       printPage(response, vars, strKey, strWindow, strTab, strProcessId);
57     } else if (vars.commandIn("SAVE")) {
58       String JavaDoc strWindow = vars.getStringParameter("inpwindowId");
59       String JavaDoc strKey = vars.getRequestGlobalVariable("inprRequestId", strWindow + "|R_Request_ID");
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       processButton(vars, strKey, strWindow);
69
70       String JavaDoc pinstance = SequenceIdData.getSequence(this, "AD_PInstance", vars.getClient());
71       PInstanceProcessData.insertPInstance(this, pinstance, "158", strKey, "N", vars.getUser(), vars.getClient(), vars.getOrg());
72
73       ActionButtonData.process158(this, pinstance);
74
75       PInstanceProcessData[] pinstanceData = PInstanceProcessData.select(this, pinstance);
76       String JavaDoc messageResult="";
77       if (pinstanceData!=null && pinstanceData.length>0) {
78         if (!pinstanceData[0].errormsg.equals("")) {
79           String JavaDoc message = pinstanceData[0].errormsg;
80           if (message.startsWith("@") && message.endsWith("@")) {
81             message = message.substring(1, message.length()-1);
82             if (message.indexOf("@")==-1) messageResult = Utility.messageBD(this, message, vars.getLanguage());
83             else messageResult = Utility.parseTranslation(this, vars, vars.getLanguage(), "@" + message + "@");
84           } else {
85             messageResult = Utility.parseTranslation(this, vars, vars.getLanguage(), message);
86           }
87         } else if (!pinstanceData[0].pMsg.equals("")) {
88           String JavaDoc message = pinstanceData[0].pMsg;
89           messageResult = Utility.parseTranslation(this, vars, vars.getLanguage(), message);
90         } else if (pinstanceData[0].result.equals("1")) {
91           messageResult = Utility.messageBD(this, "Success", vars.getLanguage());
92         } else {
93           messageResult = Utility.messageBD(this, "Error", vars.getLanguage());
94         }
95       }
96       messageResult = Replace.replace(messageResult, "'", "\\'");
97       if (log4j.isDebugEnabled()) log4j.debug(messageResult);
98       vars.setSessionValue(strWindow + "|" + strTabName + ".message", messageResult);
99       printPageClosePopUp(response, vars, strWindowPath);
100     } else pageErrorPopUp(response);
101   }
102
103
104   String JavaDoc processButton(VariablesSecureApp vars, String JavaDoc strKey, String JavaDoc windowId) {
105     int i=0;
106     Connection JavaDoc conn = null;
107     try {
108       conn = getTransactionConnection();
109       RequestActionData[] data = RequestActionData.select(conn, this, strKey);
110       if (data==null || data.length==0) {
111         releaseRollbackConnection(conn);
112         log4j.warn("Rollback in transaction");
113         return Utility.messageBD(this, "ProcessRunError", vars.getLanguage());
114       }
115       vars.removeSessionValue("#User_EMail");
116       vars.removeSessionValue("#User_EMailUser");
117       vars.removeSessionValue("#User_EMailUserPw");
118       vars.removeSessionValue("#Request_EMail");
119       vars.removeSessionValue("#Request_EMailUser");
120       vars.removeSessionValue("#Request_EMailUserPw");
121
122       if(data[0].actiontype.equals("E")) {
123         String JavaDoc smtpHost = RequestActionData.selectSMTPHost(conn, this, data[0].adClientId);
124         String JavaDoc from = "";
125         RequestActionData[] mails= RequestActionData.selectEmail(conn, this, data[0].salesrepId);
126         if (mails==null || mails.length==0) {
127           mails= RequestActionData.selectEmailRequest(conn, this, data[0].adClientId);
128           if (mails!=null && mails.length>0) {
129             from = mails[0].email;
130             from = from.trim().toLowerCase();
131             for(int pos = from.indexOf(" "); pos != -1; pos = from.indexOf(" "))
132               from = from.substring(0, pos) + from.substring(pos + 1);
133             vars.setSessionValue("#Request_EMail", from);
134             vars.setSessionValue("#Request_EMailUser", mails[0].emailuser);
135             vars.setSessionValue("#Request_EMailUserPw", mails[0].emailuserpw);
136           }
137         } else {
138           from = mails[0].email;
139           from = from.trim().toLowerCase();
140           for(int pos = from.indexOf(" "); pos != -1; pos = from.indexOf(" "))
141             from = from.substring(0, pos) + from.substring(pos + 1);
142           vars.setSessionValue("#User_EMail", from);
143           vars.setSessionValue("#User_EMailUser", mails[0].emailuser);
144           vars.setSessionValue("#User_EMailUserPw", mails[0].emailuserpw);
145         }
146
147         if (from.equals("")) {
148           from = System.getProperty("user.name") + "@" + Utility.getContext(this, vars, "#AD_Client_Name", windowId) + ".com";
149           from = from.trim().toLowerCase();
150           for(int pos = from.indexOf(" "); pos != -1; pos = from.indexOf(" "))
151             from = from.substring(0, pos) + from.substring(pos + 1);
152         }
153         String JavaDoc to = RequestActionData.selectEmailTo(conn, this, (data[0].adUserId.equals("0")?"":data[0].adUserId), data[0].cBpartnerId);
154         String JavaDoc msg = "OK";
155         if (log4j.isDebugEnabled()) log4j.debug("*************************************************************************************************");
156         if (log4j.isDebugEnabled()) log4j.debug(smtpHost + "\n" + from + "\n" + to + "\n" + data[0].mailsubject + "\n" + data[0].mailtext);
157         if (log4j.isDebugEnabled()) log4j.debug("*************************************************************************************************");
158         EMail email = new EMail(vars, smtpHost, from, to, data[0].mailsubject, data[0].mailtext);
159         msg = email.send();
160         if("OK".equals(msg)) {
161           RequestActionData.update(conn, this, Utility.messageBD(this, "RequestActionEMailOK", vars.getLanguage()), strKey);
162         } else {
163           RequestActionData.update(conn, this, Utility.messageBD(this, "RequestActionEMailError", vars.getLanguage()) + " - " + msg, strKey);
164         }
165       } else if(data[0].actiontype.equals("T")) {
166         String JavaDoc subject = Utility.messageBD(this, "RequestActionTransfer", vars.getLanguage());
167         
168         RequestActionData.update(conn, this, subject, strKey);
169         String JavaDoc smtpHost = RequestActionData.selectSMTPHost(conn, this, data[0].adClientId);
170         String JavaDoc to = "";
171         RequestActionData[] dataTo = RequestActionData.selectEmail(conn, this, data[0].adUserId);
172         if (dataTo!=null && dataTo.length>0) {
173           to = dataTo[0].email;
174         }
175         to = to.trim().toLowerCase();
176         for(int pos = to.indexOf(" "); pos != -1; pos = to.indexOf(" "))
177           to = to.substring(0, pos) + to.substring(pos + 1);
178
179         if (to.equals("")) {
180           to = System.getProperty("user.name") + "@" + Utility.getContext(this, vars, "#AD_Client_Name", windowId) + ".com";
181           to = to.trim().toLowerCase();
182           for(int pos = to.indexOf(" "); pos != -1; pos = to.indexOf(" "))
183             to = to.substring(0, pos) + to.substring(pos + 1);
184         }
185         String JavaDoc from = "";
186         RequestActionData[] mails= RequestActionData.selectEmail(conn, this, data[0].updatedby);
187         if (mails==null || mails.length==0) {
188           mails= RequestActionData.selectEmailRequest(conn, this, data[0].adClientId);
189           if (mails!=null && mails.length>0) {
190             from = mails[0].email;
191             from = from.trim().toLowerCase();
192             for(int pos = from.indexOf(" "); pos != -1; pos = from.indexOf(" "))
193               from = from.substring(0, pos) + from.substring(pos + 1);
194             vars.setSessionValue("#Request_EMail", from);
195             vars.setSessionValue("#Request_EMailUser", mails[0].emailuser);
196             vars.setSessionValue("#Request_EMailUserPw", mails[0].emailuserpw);
197           }
198         } else {
199           from = mails[0].email;
200           from = from.trim().toLowerCase();
201           for(int pos = from.indexOf(" "); pos != -1; pos = from.indexOf(" "))
202             from = from.substring(0, pos) + from.substring(pos + 1);
203           vars.setSessionValue("#User_EMail", from);
204           vars.setSessionValue("#User_EMailUser", mails[0].emailuser);
205           vars.setSessionValue("#User_EMailUserPw", mails[0].emailuserpw);
206         }
207
208         if (from.equals("")) {
209           from = System.getProperty("user.name") + "@" + Utility.getContext(this, vars, "#AD_Client_Name", windowId) + ".com";
210           from = from.trim().toLowerCase();
211           for(int pos = from.indexOf(" "); pos != -1; pos = from.indexOf(" "))
212             from = from.substring(0, pos) + from.substring(pos + 1);
213         }
214         String JavaDoc message = subject + "\n" + data[0].summary;
215         if (log4j.isDebugEnabled()) log4j.debug("*************************************************************************************************");
216         if (log4j.isDebugEnabled()) log4j.debug(smtpHost + "\n" + from + "\n" + to + "\n" + subject + "\n" + message);
217         if (log4j.isDebugEnabled()) log4j.debug("*************************************************************************************************");
218         EMail email = new EMail(vars, smtpHost, from, to, subject, message);
219         String JavaDoc msg = email.send();
220       }
221
222       releaseCommitConnection(conn);
223     } catch (Exception JavaDoc e) {
224       try {
225         releaseRollbackConnection(conn);
226       } catch (Exception JavaDoc ignored) {}
227       e.printStackTrace();
228       log4j.warn("Rollback in transaction");
229       return Utility.messageBD(this, "ProcessRunError", vars.getLanguage());
230     }
231     return (Utility.messageBD(this, "RecordsCopied", vars.getLanguage()) + i);
232   }
233
234
235   void printPage(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc strKey, String JavaDoc windowId, String JavaDoc strTab, String JavaDoc strProcessId)
236     throws IOException, ServletException {
237       if (log4j.isDebugEnabled()) log4j.debug("Output: Button process Copy from Invoice");
238
239       ActionButtonDefaultData[] data = null;
240       String JavaDoc strHelp="", strDescription="";
241       if (vars.getLanguage().equals("en_US")) data = ActionButtonDefaultData.select(this, strProcessId);
242       else data = ActionButtonDefaultData.selectLanguage(this, vars.getLanguage(), strProcessId);
243
244       if (data!=null && data.length!=0) {
245         strDescription = data[0].description;
246         strHelp = data[0].help;
247       }
248       String JavaDoc[] discard = {""};
249       if (strHelp.equals("")) discard[0] = new String JavaDoc("helpDiscard");
250       XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/RequestAction", discard).createXmlDocument();
251       xmlDocument.setParameter("key", strKey);
252       xmlDocument.setParameter("window", windowId);
253       xmlDocument.setParameter("tab", strTab);
254       xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
255       xmlDocument.setParameter("question", Utility.messageBD(this, "StartProcess?", vars.getLanguage()));
256       xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
257       xmlDocument.setParameter("description", strDescription);
258       xmlDocument.setParameter("help", strHelp);
259
260       
261       response.setContentType("text/html; charset=UTF-8");
262       PrintWriter out = response.getWriter();
263       out.println(xmlDocument.print());
264       out.close();
265     }
266
267   public String JavaDoc getServletInfo() {
268     return "Servlet Copy from invoice";
269   } // end of getServletInfo() method
270
}
271
272
Popular Tags