1 19 package org.openbravo.erpCommon.ad_actionButton; 20 21 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 import java.sql.Connection ; 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 strProcessId = vars.getStringParameter("inpProcessId"); 53 String strWindow = vars.getStringParameter("inpwindowId"); 54 String strTab = vars.getStringParameter("inpTabId"); 55 String strKey = vars.getRequiredGlobalVariable("inprRequestId", strWindow + "|R_Request_ID"); 56 printPage(response, vars, strKey, strWindow, strTab, strProcessId); 57 } else if (vars.commandIn("SAVE")) { 58 String strWindow = vars.getStringParameter("inpwindowId"); 59 String strKey = vars.getRequestGlobalVariable("inprRequestId", strWindow + "|R_Request_ID"); 60 String strTab = vars.getStringParameter("inpTabId"); 61 ActionButtonDefaultData[] tab = ActionButtonDefaultData.windowName(this, strTab); 62 String 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 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 messageResult=""; 77 if (pinstanceData!=null && pinstanceData.length>0) { 78 if (!pinstanceData[0].errormsg.equals("")) { 79 String 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 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 processButton(VariablesSecureApp vars, String strKey, String windowId) { 105 int i=0; 106 Connection 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 smtpHost = RequestActionData.selectSMTPHost(conn, this, data[0].adClientId); 124 String 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 to = RequestActionData.selectEmailTo(conn, this, (data[0].adUserId.equals("0")?"":data[0].adUserId), data[0].cBpartnerId); 154 String 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 subject = Utility.messageBD(this, "RequestActionTransfer", vars.getLanguage()); 167 168 RequestActionData.update(conn, this, subject, strKey); 169 String smtpHost = RequestActionData.selectSMTPHost(conn, this, data[0].adClientId); 170 String 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 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 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 msg = email.send(); 220 } 221 222 releaseCommitConnection(conn); 223 } catch (Exception e) { 224 try { 225 releaseRollbackConnection(conn); 226 } catch (Exception 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 strKey, String windowId, String strTab, String strProcessId) 236 throws IOException, ServletException { 237 if (log4j.isDebugEnabled()) log4j.debug("Output: Button process Copy from Invoice"); 238 239 ActionButtonDefaultData[] data = null; 240 String 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 [] discard = {""}; 249 if (strHelp.equals("")) discard[0] = new String ("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 getServletInfo() { 268 return "Servlet Copy from invoice"; 269 } } 271 272 | Popular Tags |