1 19 package org.openbravo.erpCommon.utility; 20 21 import org.openbravo.base.secureApp.VariablesSecureApp; 22 import org.openbravo.base.secureApp.HttpSecureAppServlet; 23 24 import java.util.LinkedList ; 25 import java.util.Vector ; 26 import javax.servlet.http.*; 27 import java.io.*; 28 import javax.servlet.*; 29 import java.sql.*; 30 31 import net.sf.jasperreports.engine.JRException; 32 33 public class ExportGrid extends HttpSecureAppServlet { 34 private GridBO gridBO; 35 private PreparedStatement st = null; 36 37 public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 38 VariablesSecureApp vars = new VariablesSecureApp(request); 39 String strTabId = vars.getRequiredStringParameter("inpTabId"); 40 if (log4j.isDebugEnabled()) log4j.debug("Export grid, tabID: " + strTabId); 41 ServletOutputStream os=null; 42 InputStream is = null; 43 44 String strLanguage = vars.getLanguage(); 45 if (strBaseDesignPath.endsWith("/")) strDefaultDesignPath = strDefaultDesignPath.substring(0, strDefaultDesignPath.length()-1); 46 if (log4j.isDebugEnabled()) log4j.debug("*********************Base path: " + strBaseDesignPath); 47 String strNewAddBase = strDefaultDesignPath; 48 String strFinal = strBaseDesignPath; 49 if (!strLanguage.equals("") && !strLanguage.equals("en_US")) strNewAddBase = strLanguage; 50 if (!strFinal.endsWith("/" + strNewAddBase)) strFinal += "/" + strNewAddBase; 51 if (log4j.isDebugEnabled()) log4j.debug("*********************Base path: " + strFinal); 52 String strBaseDesign = prefix + strFinal; 53 if (log4j.isDebugEnabled()) log4j.debug("*********************Base design path: " + strBaseDesign); 54 55 try { 56 GridReportVO gridReportVO = createGridReport(vars, strTabId); 57 os = response.getOutputStream(); 58 is = getInputStream(strBaseDesign+"/org/openbravo/erpCommon/utility/"+gridReportVO.getJrxmlTemplate()); 59 gridBO = new GridBO(); 60 61 if(log4j.isDebugEnabled()) log4j.debug("Create report, type: " + vars.getCommand()); 62 63 if (vars.commandIn("HTML")) gridBO.createHTMLReport(is, gridReportVO, os); 64 else if (vars.commandIn("PDF")) { 65 response.setContentType("application/pdf"); 66 gridBO.createPDFReport(is, gridReportVO, os); 67 } else if (vars.commandIn("EXCEL")) { 68 response.setContentType("application/vnd.ms-excel"); 69 gridBO.createXLSReport(is, gridReportVO, os); 70 } else if (vars.commandIn("CSV")) { 71 response.setContentType("text/csv"); 72 gridBO.createCSVReport(is, gridReportVO, os); 73 } 74 } catch (JRException e){ 75 throw new ServletException(e.getMessage()); 76 } finally { 77 try { 78 this.releasePreparedStatement(st); 79 } catch (SQLException ex){ } 80 is.close(); 81 os.close(); 82 } 83 } 84 GridReportVO createGridReport(VariablesSecureApp vars, String strTabId) throws ServletException{ 85 if (log4j.isDebugEnabled()) log4j.debug("Create Grid Report, tabID: " + strTabId); 86 LinkedList <GridColumnVO> columns = new LinkedList <GridColumnVO>(); 87 ResultSet data = null; 88 TableSQLData tableSQL = null; 89 try { 90 tableSQL = new TableSQLData(vars, this, strTabId, Utility.getContext(this, vars, "#User_Org", "ExportGrid"), Utility.getContext(this, vars, "#User_Client", "ExportGrid")); 91 } catch (Exception ex) { 92 ex.printStackTrace(); 93 log4j.error(ex.getMessage()); 94 throw new ServletException(ex.getMessage()); 95 } 96 SQLReturnObject[] headers = tableSQL.getHeaders(true); 97 98 if (tableSQL!=null && headers!=null) { 99 try{ 100 if (log4j.isDebugEnabled()) log4j.debug("Geting the grid data."); 101 String strSQL = ModelSQLGeneration.generateSQL(this, vars, tableSQL, "", new Vector <String >(), new Vector <String >()); 102 ExecuteQuery execquery = new ExecuteQuery(this, strSQL, tableSQL.getParameterValues()); 104 st = this.getPreparedStatement(strSQL); 105 data = execquery.selectResultset(st); 106 } catch (Exception e) { 107 if (log4j.isDebugEnabled()) log4j.debug("Error obtaining rows data"); 108 e.printStackTrace(); 109 throw new ServletException(e.getMessage()); 110 } 111 } 112 int totalWidth = 0; 113 for (int i=0; i<headers.length; i++){ 114 if (headers[i].getField("isvisible").equals("true")){ 115 String columnname = headers[i].getField("columnname"); 116 if (!tableSQL.getSelectField(columnname + "_R").equals("")) columnname += "_R"; 117 if (log4j.isDebugEnabled()) log4j.debug("Add column: " + columnname + " width: " + headers[i].getField("width") + " reference: " + headers[i].getField("adReferenceId")); 118 totalWidth += Integer.valueOf(headers[i].getField("width")); 119 Class fieldClass = String .class; 120 if (headers[i].getField("adReferenceId").equals("11")) fieldClass = int.class; 121 else if (headers[i].getField("adReferenceId").equals("12") || headers[i].getField("adReferenceId").equals("800008") || headers[i].getField("adReferenceId").equals("800019")) fieldClass = Double .class; 122 123 columns.add(new GridColumnVO(headers[i].getField("name"), columnname, Integer.valueOf(headers[i].getField("width")), fieldClass)); 124 } 125 } 126 String strTitle = ExportGridData.getTitle(this, strTabId, vars.getLanguage()); 127 GridReportVO gridReportVO = new GridReportVO("plantilla.jrxml", data, strTitle, columns, strReplaceWith, totalWidth); 128 return gridReportVO; 129 } 130 private InputStream getInputStream(String reportFile) throws IOException { 131 if (log4j.isDebugEnabled()) log4j.debug("Get input stream file: " + reportFile); 132 return (new FileInputStream(reportFile)); 133 } 134 } 135 | Popular Tags |