KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > businessUtility > TabAttachments


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.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 JavaDoc strTab = vars.getStringParameter("inpTabId");
49       vars.setSessionValue("TabAttachments.tabId", strTab);
50       String JavaDoc strWindow = vars.getStringParameter("inpwindowId");
51       vars.setSessionValue("TabAttachments.windowId", strWindow);
52       String JavaDoc key = vars.getStringParameter("inpKey");
53       vars.setSessionValue("TabAttachments.key", key);
54       String JavaDoc strText = vars.getStringParameter("inptext");
55       String JavaDoc strDataType = vars.getStringParameter("inpadDatatypeId");
56       TabAttachmentsData[] data = TabAttachmentsData.selectTabInfo(this, strTab);
57       String JavaDoc tableId = "";
58       if (data==null || data.length==0) throw new ServletException("Tab not found: " + strTab);
59       else tableId = data[0].adTableId;
60
61       String JavaDoc strFileReference = SequenceIdData.getSequence(this, "C_File", vars.getClient());
62       String JavaDoc 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 JavaDoc strTab = vars.getStringParameter("inpTabId");
77       vars.setSessionValue("TabAttachments.tabId", strTab);
78       String JavaDoc strWindow = vars.getStringParameter("inpwindowId");
79       vars.setSessionValue("TabAttachments.windowId", strWindow);
80       String JavaDoc key = vars.getStringParameter("inpKey");
81       vars.setSessionValue("TabAttachments.key", key);
82       String JavaDoc strFileReference = vars.getStringParameter("inpcFileId");
83       String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc strTab = vars.getStringParameter("inpTabId");
109       vars.setSessionValue("TabAttachments.tabId", strTab);
110       String JavaDoc strWindow = vars.getStringParameter("inpwindowId");
111       vars.setSessionValue("TabAttachments.windowId", strWindow);
112       String JavaDoc key = vars.getStringParameter("inpKey");
113       vars.setSessionValue("TabAttachments.key", key);
114       String JavaDoc strFileReference = vars.getStringParameter("inpcFileId");
115       String JavaDoc 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 JavaDoc strFileReference = vars.getStringParameter("inpcFileId");
122       printPageFile(response, vars, strFileReference);
123     } else if (vars.commandIn("DEFAULT")) {
124       String JavaDoc strTab = vars.getGlobalVariable("inpTabId", "TabAttachments.tabId");
125       String JavaDoc strWindow = vars.getGlobalVariable("inpwindowId", "TabAttachments.windowId");
126       String JavaDoc key = vars.getGlobalVariable("inpKey", "TabAttachments.key");
127       printPageFS(response, vars);
128     } else if (vars.commandIn("FRAME1", "RELATION")) {
129       String JavaDoc strTab = vars.getGlobalVariable("inpTabId", "TabAttachments.tabId");
130       String JavaDoc strWindow = vars.getGlobalVariable("inpwindowId", "TabAttachments.windowId");
131       String JavaDoc 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 JavaDoc strTab = vars.getGlobalVariable("inpTabId", "TabAttachments.tabId");
137       String JavaDoc strWindow = vars.getGlobalVariable("inpwindowId", "TabAttachments.windowId");
138       String JavaDoc key = vars.getGlobalVariable("inpKey", "TabAttachments.key");
139       String JavaDoc strFileReference = vars.getRequiredStringParameter("inpcFileId");
140       printPageEdit(response, vars, strTab, strWindow, key, strFileReference);
141     } else if (vars.commandIn("NEW")) {
142       String JavaDoc strTab = vars.getGlobalVariable("inpTabId", "TabAttachments.tabId");
143       String JavaDoc strWindow = vars.getGlobalVariable("inpwindowId", "TabAttachments.windowId");
144       String JavaDoc key = vars.getRequestGlobalVariable("inpKey", "TabAttachments.key");
145       printPageEdit(response, vars, strTab, strWindow, key, "");
146     } else if (vars.commandIn("DELETE")) {
147       String JavaDoc strTab = vars.getRequestGlobalVariable("inpTabId", "TabAttachments.tabId");
148       String JavaDoc strWindow = vars.getRequestGlobalVariable("inpwindowId", "TabAttachments.windowId");
149       String JavaDoc key = vars.getRequestGlobalVariable("inpKey", "TabAttachments.key");
150       String JavaDoc strFileReference = vars.getRequiredStringParameter("inpcFileId");
151       String JavaDoc 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 JavaDoc strFileReference = vars.getRequiredStringParameter("inpcFileId");
158       printPageFile(response, vars, strFileReference);
159     } else pageError(response);
160   }
161
162   String JavaDoc insert(VariablesSecureApp vars, String JavaDoc strFileReference, String JavaDoc tableId, String JavaDoc key, String JavaDoc strDataType, String JavaDoc strText) throws IOException, ServletException {
163     if (log4j.isDebugEnabled()) log4j.debug("Deleting records");
164     Connection conn = null;
165     try {
166       conn = this.getTransactionConnection();
167       String JavaDoc 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 JavaDoc ex) {
184         throw new ServletException(ex);
185       }
186       releaseCommitConnection(conn);
187     } catch (Exception JavaDoc e) {
188       try {
189         releaseRollbackConnection(conn);
190       } catch (Exception JavaDoc ignored) {}
191       e.printStackTrace();
192       log4j.error("Rollback in transaction");
193       return "ProcessRunError";
194     }
195     return "";
196   }
197
198   String JavaDoc delete(VariablesSecureApp vars, String JavaDoc 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 JavaDoc ex) {
209         throw new ServletException(ex);
210       }
211       releaseCommitConnection(conn);
212     } catch (Exception JavaDoc e) {
213       try {
214         releaseRollbackConnection(conn);
215       } catch (Exception JavaDoc 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 JavaDoc strTab, String JavaDoc strWindow, String JavaDoc key) throws IOException, ServletException {
234     if (log4j.isDebugEnabled()) log4j.debug("Output: Frame 1 of the attachments relations");
235     String JavaDoc[] discard = {"noData"};
236     TabAttachmentsData[] data = TabAttachmentsData.selectTabInfo(this, strTab);
237     String JavaDoc 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 JavaDoc("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 JavaDoc strTab, String JavaDoc strWindow, String JavaDoc key, String JavaDoc strFileReference) throws IOException, ServletException {
275     if (log4j.isDebugEnabled()) log4j.debug("Output: Frame 1 of the attachments edition");
276     String JavaDoc[] discard = {"editDiscard"};
277     TabAttachmentsData[] data = TabAttachmentsData.selectTabInfo(this, strTab);
278     String JavaDoc 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 JavaDoc("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 JavaDoc 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     /*
301     String viewButtons = "yes";
302     if (strFileReference.equals("")||files==null||files.length==0) viewButtons="none";
303       
304     xmlDocument.setParameter("butEdit", viewButtons);
305     xmlDocument.setParameter("butDownload", viewButtons);
306     xmlDocument.setParameter("butDel", viewButtons);*/

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 JavaDoc 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 JavaDoc getServletInfo() {
331     return "Servlet that presents the attachments";
332   } // end of getServletInfo() method
333
}
334
Popular Tags