1 19 package org.openbravo.erpCommon.businessUtility; 20 21 import org.openbravo.erpCommon.ad_combos.DataTypeComboData; 22 import org.openbravo.erpCommon.utility.SequenceIdData; 23 import org.openbravo.utils.*; 24 import org.openbravo.base.secureApp.*; 25 import org.openbravo.erpCommon.utility.*; 26 import org.openbravo.xmlEngine.XmlDocument; 27 import org.openbravo.erpCommon.utility.Utility; 28 import java.io.*; 29 import java.sql.*; 30 import javax.servlet.*; 31 import javax.servlet.http.*; 32 33 import org.apache.commons.fileupload.*; 34 35 36 public class TabAttachments extends HttpSecureAppServlet { 37 38 39 public void init (ServletConfig config) { 40 super.init(config); 41 boolHist = false; 42 } 43 44 public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException { 45 VariablesSecureApp vars = new VariablesSecureApp(request); 46 47 if (vars.getCommand().startsWith("SAVE_NEW")) { 48 String strTab = vars.getStringParameter("inpTabId"); 49 vars.setSessionValue("TabAttachments.tabId", strTab); 50 String strWindow = vars.getStringParameter("inpwindowId"); 51 vars.setSessionValue("TabAttachments.windowId", strWindow); 52 String key = vars.getStringParameter("inpKey"); 53 vars.setSessionValue("TabAttachments.key", key); 54 String strText = vars.getStringParameter("inptext"); 55 String strDataType = vars.getStringParameter("inpadDatatypeId"); 56 TabAttachmentsData[] data = TabAttachmentsData.selectTabInfo(this, strTab); 57 String tableId = ""; 58 if (data==null || data.length==0) throw new ServletException("Tab not found: " + strTab); 59 else tableId = data[0].adTableId; 60 61 String strFileReference = SequenceIdData.getSequence(this, "C_File", vars.getClient()); 62 String strInsert = insert(vars, strFileReference, tableId, key, strDataType, strText); 63 if (!strInsert.equals("")) { 64 vars.setSessionValue("TabAttachments.message", Utility.messageBD(this, "Error", vars.getLanguage())); 65 response.sendRedirect(strDireccion + request.getServletPath() + "?Command=NEW"); 66 } else { 67 if (vars.commandIn("SAVE_NEW_RELATION")) { 68 response.sendRedirect(strDireccion + request.getServletPath() + "?Command=DEFAULT&inpcFileId=" + strFileReference); 69 } else if (vars.commandIn("SAVE_NEW_EDIT")) { 70 response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT&inpcFileId=" + strFileReference); 71 } else if (vars.commandIn("SAVE_NEW_NEW")) { 72 response.sendRedirect(strDireccion + request.getServletPath() + "?Command=NEW"); 73 } 74 } 75 } else if (vars.getCommand().startsWith("SAVE_EDIT")) { 76 String strTab = vars.getStringParameter("inpTabId"); 77 vars.setSessionValue("TabAttachments.tabId", strTab); 78 String strWindow = vars.getStringParameter("inpwindowId"); 79 vars.setSessionValue("TabAttachments.windowId", strWindow); 80 String key = vars.getStringParameter("inpKey"); 81 vars.setSessionValue("TabAttachments.key", key); 82 String strFileReference = vars.getStringParameter("inpcFileId"); 83 String strText = vars.getStringParameter("inptext"); 84 if (TabAttachmentsData.update(this, vars.getUser(), strText, strFileReference)==0) { 85 vars.setSessionValue("TabAttachments.message", Utility.messageBD(this, "Error", vars.getLanguage())); 86 response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT&inpcFileId=" + strFileReference); 87 } else { 88 if (vars.commandIn("SAVE_EDIT_RELATION")) { 89 response.sendRedirect(strDireccion + request.getServletPath() + "?Command=DEFAULT&inpcFileId=" + strFileReference); 90 } else if (vars.commandIn("SAVE_EDIT_EDIT")) { 91 response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT&inpcFileId=" + strFileReference); 92 } else if (vars.commandIn("SAVE_EDIT_NEW")) { 93 response.sendRedirect(strDireccion + request.getServletPath() + "?Command=NEW&inpKey=" + key); 94 } else if (vars.commandIn("SAVE_EDIT_NEXT")) { 95 TabAttachmentsData[] data = TabAttachmentsData.selectTabInfo(this, strTab); 96 String tableId = ""; 97 if (data==null || data.length==0) throw new ServletException("Tab not found: " + strTab); 98 else { 99 tableId = data[0].adTableId; 100 if (data[0].isreadonly.equals("Y")) throw new ServletException("This tab is read only"); 101 } 102 String strNewFile = TabAttachmentsData.selectNext(this, Utility.getContext(this, vars, "#User_Client", strWindow), Utility.getContext(this, vars, "#User_Org", strWindow), strFileReference, tableId, key); 103 if (!strNewFile.equals("")) strFileReference = strNewFile; 104 response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT&inpcFileId=" + strFileReference); 105 } 106 } 107 } else if (vars.getCommand().startsWith("DELETE")) { 108 String strTab = vars.getStringParameter("inpTabId"); 109 vars.setSessionValue("TabAttachments.tabId", strTab); 110 String strWindow = vars.getStringParameter("inpwindowId"); 111 vars.setSessionValue("TabAttachments.windowId", strWindow); 112 String key = vars.getStringParameter("inpKey"); 113 vars.setSessionValue("TabAttachments.key", key); 114 String strFileReference = vars.getStringParameter("inpcFileId"); 115 String strDelete = delete(vars, strFileReference); 116 if (!strDelete.equals("")) { 117 vars.setSessionValue("TabAttachments.message", Utility.messageBD(this, "Error", vars.getLanguage())); 118 response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT&inpcFileId=" + strFileReference); 119 } else response.sendRedirect(strDireccion + request.getServletPath()); 120 } else if (vars.commandIn("DISPLAY_DATA")) { 121 String strFileReference = vars.getStringParameter("inpcFileId"); 122 printPageFile(response, vars, strFileReference); 123 } else if (vars.commandIn("DEFAULT")) { 124 String strTab = vars.getGlobalVariable("inpTabId", "TabAttachments.tabId"); 125 String strWindow = vars.getGlobalVariable("inpwindowId", "TabAttachments.windowId"); 126 String key = vars.getGlobalVariable("inpKey", "TabAttachments.key"); 127 printPageFS(response, vars); 128 } else if (vars.commandIn("FRAME1", "RELATION")) { 129 String strTab = vars.getGlobalVariable("inpTabId", "TabAttachments.tabId"); 130 String strWindow = vars.getGlobalVariable("inpwindowId", "TabAttachments.windowId"); 131 String key = vars.getGlobalVariable("inpKey", "TabAttachments.key"); 132 printPage(response, vars, strTab, strWindow, key); 133 } else if (vars.commandIn("FRAME2")) { 134 whitePage(response); 135 } else if (vars.commandIn("EDIT")) { 136 String strTab = vars.getGlobalVariable("inpTabId", "TabAttachments.tabId"); 137 String strWindow = vars.getGlobalVariable("inpwindowId", "TabAttachments.windowId"); 138 String key = vars.getGlobalVariable("inpKey", "TabAttachments.key"); 139 String strFileReference = vars.getRequiredStringParameter("inpcFileId"); 140 printPageEdit(response, vars, strTab, strWindow, key, strFileReference); 141 } else if (vars.commandIn("NEW")) { 142 String strTab = vars.getGlobalVariable("inpTabId", "TabAttachments.tabId"); 143 String strWindow = vars.getGlobalVariable("inpwindowId", "TabAttachments.windowId"); 144 String key = vars.getRequestGlobalVariable("inpKey", "TabAttachments.key"); 145 printPageEdit(response, vars, strTab, strWindow, key, ""); 146 } else if (vars.commandIn("DELETE")) { 147 String strTab = vars.getRequestGlobalVariable("inpTabId", "TabAttachments.tabId"); 148 String strWindow = vars.getRequestGlobalVariable("inpwindowId", "TabAttachments.windowId"); 149 String key = vars.getRequestGlobalVariable("inpKey", "TabAttachments.key"); 150 String strFileReference = vars.getRequiredStringParameter("inpcFileId"); 151 String strDelete = delete(vars, strFileReference); 152 if (!strDelete.equals("")) { 153 vars.setSessionValue("TabAttachments.message", Utility.messageBD(this, strDelete, vars.getLanguage())); 154 response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT&inpcFileId=" + strFileReference); 155 } else response.sendRedirect(strDireccion + request.getServletPath()); 156 } else if (vars.commandIn("DISPLAY_DATA")) { 157 String strFileReference = vars.getRequiredStringParameter("inpcFileId"); 158 printPageFile(response, vars, strFileReference); 159 } else pageError(response); 160 } 161 162 String insert(VariablesSecureApp vars, String strFileReference, String tableId, String key, String strDataType, String strText) throws IOException, ServletException { 163 if (log4j.isDebugEnabled()) log4j.debug("Deleting records"); 164 Connection conn = null; 165 try { 166 conn = this.getTransactionConnection(); 167 String inpName="inpname", strName = ""; 168 FileItem file = vars.getMultiFile(inpName); 169 if (file==null) throw new ServletException("Empty file"); 170 strName = file.getName(); 171 int i = strName.lastIndexOf("\\"); 172 if (i!=-1) { 173 strName = strName.substring(i+1); 174 } else if ((i=strName.lastIndexOf("/"))!=-1) { 175 strName = strName.substring(i+1); 176 } 177 TabAttachmentsData.insert(conn, this, strFileReference, vars.getClient(), vars.getOrg(), vars.getUser(), tableId, key, strDataType, strText, strName); 178 try { 179 File uploadedDir = new File(strFTPDirectory+"/"+tableId+"-"+key); 180 if (!uploadedDir.exists()) uploadedDir.mkdirs(); 181 File uploadedFile = new File(uploadedDir, strName); 182 file.write(uploadedFile); 183 } catch (Exception ex) { 184 throw new ServletException(ex); 185 } 186 releaseCommitConnection(conn); 187 } catch (Exception e) { 188 try { 189 releaseRollbackConnection(conn); 190 } catch (Exception ignored) {} 191 e.printStackTrace(); 192 log4j.error("Rollback in transaction"); 193 return "ProcessRunError"; 194 } 195 return ""; 196 } 197 198 String delete(VariablesSecureApp vars, String strFileReference) throws IOException, ServletException { 199 if (log4j.isDebugEnabled()) log4j.debug("Deleting records"); 200 Connection conn = null; 201 try { 202 conn = this.getTransactionConnection(); 203 TabAttachmentsData[] data = TabAttachmentsData.selectReference(this, strFileReference); 204 TabAttachmentsData.delete(conn, this, strFileReference); 205 try { 206 FileUtility f = new FileUtility(strFTPDirectory+"/"+data[0].adTableId+"-"+data[0].adRecordId, data[0].name, false); 207 f.deleteFile(); 208 } catch (Exception ex) { 209 throw new ServletException(ex); 210 } 211 releaseCommitConnection(conn); 212 } catch (Exception e) { 213 try { 214 releaseRollbackConnection(conn); 215 } catch (Exception ignored) {} 216 e.printStackTrace(); 217 log4j.error("Rollback in transaction"); 218 return "ProcessRunError"; 219 } 220 return ""; 221 } 222 223 void printPageFS(HttpServletResponse response, VariablesSecureApp vars) throws IOException, ServletException { 224 if (log4j.isDebugEnabled()) log4j.debug("Output: Attachments relations frame set"); 225 XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/businessUtility/TabAttachments_FS").createXmlDocument(); 226 227 response.setContentType("text/html; charset=UTF-8"); 228 PrintWriter out = response.getWriter(); 229 out.println(xmlDocument.print()); 230 out.close(); 231 } 232 233 void printPage(HttpServletResponse response, VariablesSecureApp vars, String strTab, String strWindow, String key) throws IOException, ServletException { 234 if (log4j.isDebugEnabled()) log4j.debug("Output: Frame 1 of the attachments relations"); 235 String [] discard = {"noData"}; 236 TabAttachmentsData[] data = TabAttachmentsData.selectTabInfo(this, strTab); 237 String tableId = ""; 238 if (data==null || data.length==0) throw new ServletException("Tab not found: " + strTab); 239 else { 240 tableId = data[0].adTableId; 241 if (data[0].isreadonly.equals("Y")) discard[0] = new String ("selReadOnly"); 242 } 243 244 TabAttachmentsData[] files = TabAttachmentsData.select(this, Utility.getContext(this, vars, "#User_Client", strWindow), Utility.getContext(this, vars, "#User_Org", strWindow), tableId, key); 245 246 if ((files==null)||(files.length==0)) discard[0] ="widthData"; 247 XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/businessUtility/TabAttachments_F1", discard).createXmlDocument(); 248 249 xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n"); 250 xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";"); 251 xmlDocument.setParameter("tab", strTab); 252 xmlDocument.setParameter("window", strWindow); 253 xmlDocument.setParameter("key", key); 254 xmlDocument.setParameter("recordIdentifier",TabAttachmentsData.selectRecordIdentifier(this,key,vars.getLanguage(),strTab)); 255 256 { 257 OBError myMessage = vars.getMessage("TabAttachments"); 258 vars.removeMessage("TabAttachments"); 259 if (myMessage!=null) { 260 xmlDocument.setParameter("messageType", myMessage.getType()); 261 xmlDocument.setParameter("messageTitle", myMessage.getTitle()); 262 xmlDocument.setParameter("messageMessage", myMessage.getMessage()); 263 } 264 } 265 266 xmlDocument.setData("structure1", files); 267 268 response.setContentType("text/html; charset=UTF-8"); 269 PrintWriter out = response.getWriter(); 270 out.println(xmlDocument.print()); 271 out.close(); 272 } 273 274 void printPageEdit(HttpServletResponse response, VariablesSecureApp vars, String strTab, String strWindow, String key, String strFileReference) throws IOException, ServletException { 275 if (log4j.isDebugEnabled()) log4j.debug("Output: Frame 1 of the attachments edition"); 276 String [] discard = {"editDiscard"}; 277 TabAttachmentsData[] data = TabAttachmentsData.selectTabInfo(this, strTab); 278 String tableId = ""; 279 if (data==null || data.length==0) throw new ServletException("Tab not found: " + strTab); 280 else { 281 tableId = data[0].adTableId; 282 if (data[0].isreadonly.equals("Y")) throw new ServletException("This tab is read only"); 283 } 284 if (strFileReference.equals("")) discard[0] = new String ("newDiscard"); 285 XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/businessUtility/TabAttachments_Edition", discard).createXmlDocument(); 286 287 xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n"); 288 xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";"); 289 xmlDocument.setParameter("tab", strTab); 290 xmlDocument.setParameter("window", strWindow); 291 xmlDocument.setParameter("key", key); 292 xmlDocument.setParameter("save", (strFileReference.equals("")?"NEW":"EDIT")); 293 xmlDocument.setParameter("recordIdentifier",TabAttachmentsData.selectRecordIdentifier(this,key,vars.getLanguage(),strTab)); 294 295 String message = vars.getSessionValue("TabAttachments.message"); 296 if (!message.equals("")) message = "alert('" + message + "');"; 297 xmlDocument.setParameter("body", message); 298 299 TabAttachmentsData [] files = TabAttachmentsData.selectEdit(this, strFileReference); 300 307 308 xmlDocument.setData("structure1", (strFileReference.equals("")?TabAttachmentsData.set():files)); 309 xmlDocument.setData("reportAD_Datatype_ID_D", "liststructure", DataTypeComboData.select(this, Utility.getContext(this, vars, "#User_Client", "TabAttachments"), Utility.getContext(this, vars, "#User_Org", "TabAttachments"))); 310 311 response.setContentType("text/html; charset=UTF-8"); 312 PrintWriter out = response.getWriter(); 313 out.println(xmlDocument.print()); 314 out.close(); 315 } 316 317 void printPageFile(HttpServletResponse response, VariablesSecureApp vars, String strFileReference) throws IOException, ServletException { 318 TabAttachmentsData[] data = TabAttachmentsData.selectEdit(this, strFileReference); 319 if (data==null || data.length==0) throw new ServletException("Missing file"); 320 FileUtility f = new FileUtility(strFTPDirectory+"/"+data[0].adTableId+"-"+data[0].adRecordId, data[0].name, false); 321 if (data[0].datatypeContent.equals("")) response.setContentType("application/txt"); 322 else response.setContentType(data[0].datatypeContent); 323 response.setHeader("Content-Disposition","attachment; filename=" + data[0].name ); 324 325 f.dumpFile(response.getOutputStream()); 326 response.getOutputStream().flush(); 327 response.getOutputStream().close(); 328 } 329 330 public String getServletInfo() { 331 return "Servlet that presents the attachments"; 332 } } 334 | Popular Tags |