KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > utility > ExportGrid


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) 2007 Openbravo SL
15  * All Rights Reserved.
16  * Contributor(s): ______________________________________.
17  ************************************************************************
18 */

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 JavaDoc;
25 import java.util.Vector JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc strNewAddBase = strDefaultDesignPath;
48     String JavaDoc 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 JavaDoc 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 JavaDoc strTabId) throws ServletException{
85     if (log4j.isDebugEnabled()) log4j.debug("Create Grid Report, tabID: " + strTabId);
86     LinkedList JavaDoc<GridColumnVO> columns = new LinkedList JavaDoc<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 JavaDoc 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 JavaDoc strSQL = ModelSQLGeneration.generateSQL(this, vars, tableSQL, "", new Vector JavaDoc<String JavaDoc>(), new Vector JavaDoc<String JavaDoc>());
102         //if (log4j.isDebugEnabled()) log4j.debug("SQL: " + strSQL);
103
ExecuteQuery execquery = new ExecuteQuery(this, strSQL, tableSQL.getParameterValues());
104         st = this.getPreparedStatement(strSQL);
105         data = execquery.selectResultset(st);
106       } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc fieldClass = String JavaDoc.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 JavaDoc.class;
122         
123         columns.add(new GridColumnVO(headers[i].getField("name"), columnname, Integer.valueOf(headers[i].getField("width")), fieldClass));
124       }
125     }
126     String JavaDoc 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 JavaDoc reportFile) throws IOException {
131     if (log4j.isDebugEnabled()) log4j.debug("Get input stream file: " + reportFile);
132     return (new FileInputStream(reportFile));
133   }
134 }
135
Popular Tags