1 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 import java.sql.Connection ; 39 40 public class SendMailText extends HttpSecureAppServlet { 41 42 protected String 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 strMailTemplate = vars.getGlobalVariable("inpMailTemplate", "SendMailText|mailTemplate", ""); 55 String strInterestArea = vars.getGlobalVariable("inpInterestArea", "SendMailText|interestArea", ""); 56 String strBPGroup = vars.getGlobalVariable("inpBPGroup", "SendMailText|bpGroup", ""); 57 String strUser = vars.getGlobalVariable("inpUser", "SendMailText|user", ""); 58 printPage(response, vars, strMailTemplate, strInterestArea, strBPGroup, strUser); 59 } else if (vars.commandIn("SEND")) { 60 String strMailTemplate = vars.getRequiredGlobalVariable("inpMailTemplate", "SendMailText|mailTemplate"); 61 String strInterestArea = vars.getRequestGlobalVariable("inpInterestArea", "SendMailText|interestArea"); 62 String strBPGroup = vars.getRequestGlobalVariable("inpBPGroup", "SendMailText|bpGroup"); 63 String strUser = vars.getRequestGlobalVariable("inpUser", "SendMailText|user"); 64 String 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 strMailTemplate, String strInterestArea, String strBPGroup, String strUser) throws IOException, ServletException { 71 if (log4j.isDebugEnabled()) log4j.debug("Output: SendMailText select page"); 72 73 ActionButtonDefaultData[] data = null; 74 String strHelp="", strDescription="", strProcessId="209"; 75 String [] 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 ("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 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 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 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 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 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 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 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 processSend(VariablesSecureApp vars, String strMailTemplate, String strInterestArea, String strBPGroup, String strUser) throws IOException, ServletException { 182 String client = vars.getClient(); 183 long start = 0; 184 int counter = 0; 185 int errors = 0; 186 String language = vars.getLanguage(); 187 Connection 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 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 from = ""; 198 String fromID = ""; 199 String 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 subject = mailTextData[0].mailheader; 203 String 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 e) { 245 try { 246 releaseRollbackConnection(conn); 247 } catch (Exception 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 conn, VariablesSecureApp vars, String smtpHost, String from, String subject, String message, String name, String EMailAddress, String AD_User_ID, String fromID, String 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 getServletInfo() { 278 return "Servlet SendMailText"; 279 } } 281 282
| Popular Tags
|