KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_process > SendMailText


1 /*
2  ******************************************************************************
3  * The contents of this file are subject to the Compiere License Version 1.1
4  * ("License"); You may not use this file except in compliance with the License
5  * You may obtain a copy of the License at http://www.compiere.org/license.html
6  * Software distributed under the License is distributed on an "AS IS" basis,
7  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
8  * the specific language governing rights and limitations under the License.
9  * The Original Code is Compiere ERP & CRM Business Solution
10  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
11  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
12  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
13  * Contributor(s): Openbravo SL
14  * Contributions are Copyright (C) 2001-2006 Openbravo S.L.
15  ******************************************************************************
16 */

17 package org.openbravo.erpCommon.ad_process;
18
19 import org.openbravo.erpCommon.utility.ToolBar;
20 import org.openbravo.erpCommon.utility.KeyMap;
21 import org.openbravo.erpCommon.utility.OBError;
22 import org.openbravo.erpCommon.utility.LeftTabsBar;
23 import org.openbravo.erpCommon.utility.NavigationBar;
24 import org.openbravo.erpCommon.businessUtility.WindowTabs;
25 import org.openbravo.erpCommon.ad_actionButton.*;
26 import org.openbravo.erpCommon.utility.Utility;
27 import org.openbravo.erpCommon.utility.ComboTableData;
28 import org.openbravo.erpCommon.businessUtility.*;
29 import org.openbravo.base.secureApp.HttpSecureAppServlet;
30 import org.openbravo.base.secureApp.VariablesSecureApp;
31 import org.openbravo.xmlEngine.XmlDocument;
32 import java.io.*;
33 import javax.servlet.*;
34 import javax.servlet.http.*;
35
36
37 // imports for transactions
38
import java.sql.Connection JavaDoc;
39
40 public class SendMailText extends HttpSecureAppServlet {
41   
42   protected String JavaDoc g_log = "";
43
44
45   public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
46     VariablesSecureApp vars = new VariablesSecureApp(request);
47
48     if (!Utility.hasProcessAccess(this, vars, "", "R_MailText Send")) {
49       bdError(response, "AccessTableNoView", vars.getLanguage());
50       return;
51     }
52
53     if (vars.commandIn("DEFAULT")) {
54       String JavaDoc strMailTemplate = vars.getGlobalVariable("inpMailTemplate", "SendMailText|mailTemplate", "");
55       String JavaDoc strInterestArea = vars.getGlobalVariable("inpInterestArea", "SendMailText|interestArea", "");
56       String JavaDoc strBPGroup = vars.getGlobalVariable("inpBPGroup", "SendMailText|bpGroup", "");
57       String JavaDoc strUser = vars.getGlobalVariable("inpUser", "SendMailText|user", "");
58       printPage(response, vars, strMailTemplate, strInterestArea, strBPGroup, strUser);
59     } else if (vars.commandIn("SEND")) {
60       String JavaDoc strMailTemplate = vars.getRequiredGlobalVariable("inpMailTemplate", "SendMailText|mailTemplate");
61       String JavaDoc strInterestArea = vars.getRequestGlobalVariable("inpInterestArea", "SendMailText|interestArea");
62       String JavaDoc strBPGroup = vars.getRequestGlobalVariable("inpBPGroup", "SendMailText|bpGroup");
63       String JavaDoc strUser = vars.getRequestGlobalVariable("inpUser", "SendMailText|user");
64       String JavaDoc strMessage = processSend(vars, strMailTemplate, strInterestArea, strBPGroup, strUser);
65       vars.setSessionValue("SendMailText|message", strMessage);
66       printPage(response, vars, strMailTemplate, strInterestArea, strBPGroup, strUser);
67     } else pageErrorPopUp(response);
68   }
69
70   void printPage(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc strMailTemplate, String JavaDoc strInterestArea, String JavaDoc strBPGroup, String JavaDoc strUser) throws IOException, ServletException {
71     if (log4j.isDebugEnabled()) log4j.debug("Output: SendMailText select page");
72
73     ActionButtonDefaultData[] data = null;
74     String JavaDoc strHelp="", strDescription="", strProcessId="209";
75     String JavaDoc[] discard = {""};
76     if (vars.getLanguage().equals("en_US")) data = ActionButtonDefaultData.select(this, strProcessId);
77     else data = ActionButtonDefaultData.selectLanguage(this, vars.getLanguage(), strProcessId);
78     if (data!=null && data.length!=0) {
79       strDescription = data[0].description;
80       strHelp = data[0].help;
81     }
82     if (strHelp.equals("")) discard[0] = new String JavaDoc("helpDiscard");
83
84     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_process/SendMailText", discard).createXmlDocument();
85
86     ToolBar toolbar = new ToolBar(this, vars.getLanguage(), "SendMailText", false, "", "", "",false, "ad_process", strReplaceWith, false, true);
87     toolbar.prepareSimpleToolBarTemplate();
88     xmlDocument.setParameter("toolbar", toolbar.toString());
89
90     xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
91     xmlDocument.setParameter("paramLanguage", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
92     xmlDocument.setParameter("question", Utility.messageBD(this, "StartProcess?", vars.getLanguage()));
93     xmlDocument.setParameter("description", strDescription);
94     xmlDocument.setParameter("help", strHelp);
95     xmlDocument.setParameter("mailTemplate", strMailTemplate);
96     xmlDocument.setParameter("interestArea", strInterestArea);
97     xmlDocument.setParameter("bpGroup", strBPGroup);
98     xmlDocument.setParameter("user", strUser);
99     try {
100       KeyMap key = new KeyMap(this, vars, "SendMailText.html");
101       xmlDocument.setParameter("keyMap", key.getActionButtonKeyMaps());
102     } catch (Exception JavaDoc ex) {
103       throw new ServletException(ex);
104     }
105     try {
106       WindowTabs tabs = new WindowTabs(this, vars, "org.openbravo.erpCommon.ad_process.SendMailText");
107       xmlDocument.setParameter("parentTabContainer", tabs.parentTabs());
108       xmlDocument.setParameter("mainTabContainer", tabs.mainTabs());
109       xmlDocument.setParameter("childTabContainer", tabs.childTabs());
110       xmlDocument.setParameter("theme", vars.getTheme());
111       NavigationBar nav = new NavigationBar(this, vars.getLanguage(), "SendMailText.html", classInfo.id, classInfo.type, strReplaceWith, tabs.breadcrumb());
112       xmlDocument.setParameter("navigationBar", nav.toString());
113       LeftTabsBar lBar = new LeftTabsBar(this, vars.getLanguage(), "SendMailText.html", strReplaceWith);
114       xmlDocument.setParameter("leftTabs", lBar.manualTemplate());
115     } catch (Exception JavaDoc ex) {
116       throw new ServletException(ex);
117     }
118     {
119       OBError myMessage = vars.getMessage("SendMailText");
120       vars.removeMessage("SendMailText");
121       if (myMessage!=null) {
122         xmlDocument.setParameter("messageType", myMessage.getType());
123         xmlDocument.setParameter("messageTitle", myMessage.getTitle());
124         xmlDocument.setParameter("messageMessage", myMessage.getMessage());
125       }
126     }
127
128     String JavaDoc strMessage = vars.getSessionValue("SendMailText|message");
129     vars.removeSessionValue("SendMailText|message");
130     if (!strMessage.equals("")) strMessage = "alert('" + strMessage + "');";
131     xmlDocument.setParameter("message", strMessage);
132
133
134     try {
135         ComboTableData comboTableData = new ComboTableData(vars, this, "TABLEDIR", "R_MailText_ID", "", "", Utility.getContext(this, vars, "#User_Org", "SendMailText"), Utility.getContext(this, vars, "#User_Client", "SendMailText"), 0);
136         Utility.fillSQLParameters(this, vars, null, comboTableData, "SendMailText", strMailTemplate);
137         xmlDocument.setData("reportMailTemplate","liststructure", comboTableData.select(false));
138         comboTableData = null;
139     } catch (Exception JavaDoc ex) {
140         throw new ServletException(ex);
141     }
142
143
144     try {
145         ComboTableData comboTableData = new ComboTableData(vars, this, "TABLEDIR", "R_InterestArea_ID", "", "", Utility.getContext(this, vars, "#User_Org", "SendMailText"), Utility.getContext(this, vars, "#User_Client", "SendMailText"), 0);
146         Utility.fillSQLParameters(this, vars, null, comboTableData, "SendMailText", strInterestArea);
147         xmlDocument.setData("reportInterestArea","liststructure", comboTableData.select(false));
148         comboTableData = null;
149     } catch (Exception JavaDoc ex) {
150         throw new ServletException(ex);
151     }
152
153
154     try {
155         ComboTableData comboTableData = new ComboTableData(vars, this, "TABLEDIR", "C_BP_Group_ID", "", "", Utility.getContext(this, vars, "#User_Org", "SendMailText"), Utility.getContext(this, vars, "#User_Client", "SendMailText"), 0);
156         Utility.fillSQLParameters(this, vars, null, comboTableData, "SendMailText", strBPGroup);
157         xmlDocument.setData("reportBPGroup","liststructure", comboTableData.select(false));
158         comboTableData = null;
159     } catch (Exception JavaDoc ex) {
160         throw new ServletException(ex);
161     }
162
163
164
165     try {
166         ComboTableData comboTableData = new ComboTableData(vars, this, "TABLE", "User", "AD_User - Internal", "", Utility.getContext(this, vars, "#User_Org", "SendMailText"), Utility.getContext(this, vars, "#User_Client", "SendMailText"), 0);
167         Utility.fillSQLParameters(this, vars, null, comboTableData, "SendMailText", strUser);
168         xmlDocument.setData("reportUser","liststructure", comboTableData.select(false));
169         comboTableData = null;
170     } catch (Exception JavaDoc ex) {
171         throw new ServletException(ex);
172     }
173
174
175     response.setContentType("text/html; charset=UTF-8");
176     PrintWriter out = response.getWriter();
177     out.println(xmlDocument.print());
178     out.close();
179   }
180
181   protected String JavaDoc processSend(VariablesSecureApp vars, String JavaDoc strMailTemplate, String JavaDoc strInterestArea, String JavaDoc strBPGroup, String JavaDoc strUser) throws IOException, ServletException {
182     String JavaDoc client = vars.getClient();
183     long start = 0;
184     int counter = 0;
185     int errors = 0;
186     String JavaDoc language = vars.getLanguage();
187     Connection JavaDoc conn = null;
188     try {
189       conn = getTransactionConnection();
190       if (log4j.isDebugEnabled()) log4j.debug("processSend - R_MailText_ID=" + strMailTemplate);
191       if (strMailTemplate.equals("")) return (Utility.messageBD(this, "NotFound", language)+ " " + Utility.messageBD(this, "R_MailText_ID", language));
192       if (log4j.isDebugEnabled()) log4j.debug("processSend - AD_Client_ID=" + client);
193       if (client.equals("0")) return (Utility.messageBD(this, "NotFound", language) + " - " + Utility.messageBD(this, "AD_Client_ID", language));
194       String JavaDoc smtpHost = RequestActionData.selectSMTPHost(conn, this, client);
195       if (log4j.isDebugEnabled()) log4j.debug("processSend - SMTP Host=" + smtpHost);
196       if(smtpHost.equals("")) return(Utility.messageBD(this, "NotFound", language) + " " + Utility.messageBD(this, "SMTPHost", language));
197       String JavaDoc from = "";
198       String JavaDoc fromID = "";
199       String JavaDoc fromPW = "";
200       SendMailTextData[] mailTextData = SendMailTextData.selectMailData(conn, this, strMailTemplate);
201       if (mailTextData==null || mailTextData.length==0) return (Utility.messageBD(this, "NotFound", language) + " " + Utility.messageBD(this, "R_MailText_ID", language) + ": " + strMailTemplate);
202       String JavaDoc subject = mailTextData[0].mailheader;
203       String JavaDoc message = mailTextData[0].mailtext;
204       RequestActionData[] mails= null;
205       if (!strUser.equals("")) {
206         mails= RequestActionData.selectEmail(conn, this, strUser);
207         if (mails==null || mails.length==0) return ("From EMail not complete - " + from + "(" + fromID + "/" + fromPW + ")");
208         from = mails[0].email;
209         from = from.trim().toLowerCase();
210         for(int pos = from.indexOf(" "); pos != -1; pos = from.indexOf(" "))
211           from = from.substring(0, pos) + from.substring(pos + 1);
212         fromID = mails[0].emailuser;
213         fromPW = mails[0].emailuserpw;
214       } else {
215         mails= RequestActionData.selectEmailRequest(conn, this, client);
216         if (mails==null || mails.length==0) return ("From EMail not complete - " + from + "(" + fromID + "/" + fromPW + ")");
217         from = mails[0].email;
218         from = from.trim().toLowerCase();
219         for(int pos = from.indexOf(" "); pos != -1; pos = from.indexOf(" "))
220           from = from.substring(0, pos) + from.substring(pos + 1);
221         fromID = mails[0].emailuser;
222         fromPW = mails[0].emailuserpw;
223       }
224       if (from.equals("") || fromID.equals("") || fromPW.equals("")) return ("From EMail not complete - " + from + "(" + fromID + "/" + fromPW + ")");
225       if (log4j.isDebugEnabled()) log4j.debug("processSend - from " + from + "(" + fromID + "/" + fromPW + ")");
226
227       start = System.currentTimeMillis();
228       log4j.info("processSend - Send to R_InterestArea_ID=" + strInterestArea);
229       SendMailTextData[] mailData = SendMailTextData.select(conn, this, strInterestArea);
230       if (mailData!=null && mailData.length>0) {
231         for(int i=0;i<mailData.length;i++)
232           if(sendIndividualMail(conn, vars, smtpHost, from, subject, message, mailData[i].name, mailData[i].email, mailData[i].adUserId, fromID, fromPW)) counter++;
233           else errors++;
234       }
235       log4j.info("processSend - Send to M_BP_Group_ID=" + strBPGroup);
236       mailData = SendMailTextData.selectBPGroup(conn, this, strBPGroup);
237       if (mailData!=null && mailData.length>0) {
238         for(int i=0;i<mailData.length;i++)
239           if(sendIndividualMail(conn, vars, smtpHost, from, subject, message, mailData[i].name, mailData[i].email, mailData[i].adUserId, fromID, fromPW)) counter++;
240           else errors++;
241       }
242
243       releaseCommitConnection(conn);
244     } catch (Exception JavaDoc e) {
245       try {
246         releaseRollbackConnection(conn);
247       } catch (Exception JavaDoc ignored) {}
248       e.printStackTrace();
249       log4j.warn("Rollback in transaction");
250       return(Utility.messageBD(this, "ProcessRunError", language));
251     }
252     return(g_log + "\\n" + Utility.messageBD(this, "Created", language) + "=" + counter + ", " + Utility.messageBD(this, "Errors", language) + "=" + errors + " - " + (System.currentTimeMillis() - start) + "ms");
253   }
254
255   private boolean sendIndividualMail(Connection JavaDoc conn, VariablesSecureApp vars, String JavaDoc smtpHost, String JavaDoc from, String JavaDoc subject, String JavaDoc message, String JavaDoc name, String JavaDoc EMailAddress, String JavaDoc AD_User_ID, String JavaDoc fromID, String JavaDoc fromPW) throws IOException, ServletException {
256     EMail email = new EMail(vars, smtpHost, from, EMailAddress, subject, message);
257     if(!email.isValid()) {
258       log4j.warn("sendIndividualMail NOT VALID - " + EMailAddress);
259       try {
260         SendMailTextData.update(conn, this, AD_User_ID);
261         g_log += Utility.messageBD(this, "AD_User_ID", vars.getLanguage()) + ": " + AD_User_ID + " " + Utility.messageBD(this, "Deactivated", vars.getLanguage()) + "\\n";
262       } catch (ServletException e) {
263         e.printStackTrace();
264         log4j.warn("Rollback in transaction");
265       }
266       return false;
267     }
268     email.setEMailUser(fromID, fromPW);
269     boolean OK = "OK".equals(email.send());
270     if(OK) if (log4j.isDebugEnabled()) log4j.debug("sendIndividualMail - " + EMailAddress);
271     else log4j.warn("sendIndividualMail FAILURE - " + EMailAddress);
272     g_log += Utility.messageBD(this, "EMailAddress", vars.getLanguage()) + ": " + EMailAddress + " " + Utility.messageBD(this, (OK ? "OK" : "ERROR"), vars.getLanguage()) + "\\n";
273     return OK;
274   }
275
276
277   public String JavaDoc getServletInfo() {
278     return "Servlet SendMailText";
279   } // end of getServletInfo() method
280
}
281
282
Popular Tags