KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.*;
23 import java.util.*;
24 import java.text.*;
25 import javax.servlet.*;
26 import javax.servlet.http.*;
27 import net.sf.jasperreports.engine.export.JRHtmlExporter;
28 import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
29 import org.openbravo.base.secureApp.HttpSecureAppServlet;
30 import java.sql.Connection JavaDoc;
31 import org.openbravo.data.Sqlc;
32 import org.openbravo.utils.Replace;
33
34 import net.sf.jasperreports.engine.export.JExcelApiExporter;
35 import net.sf.jasperreports.engine.*;
36 import net.sf.jasperreports.engine.design.JasperDesign;
37 import net.sf.jasperreports.engine.xml.JRXmlLoader;
38 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
39
40 public class PrintJR extends HttpSecureAppServlet {
41   private JasperReport jasperReport;
42   public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
43     VariablesSecureApp vars = new VariablesSecureApp(request);
44     
45     String JavaDoc strProcessId = vars.getRequiredStringParameter("inpadProcessId");
46     String JavaDoc strOutputType = vars.getStringParameter("inpoutputtype", "HTML");
47     
48     if (!Utility.hasProcessAccess(this, vars, strProcessId)) {
49       bdError(response, "AccessTableNoView", vars.getLanguage());
50       return;
51     }
52  
53     createJasperPrint(response, vars, strProcessId, strOutputType);
54   }
55
56   void createJasperPrint(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc strProcessId, String JavaDoc strOutputType) throws IOException, ServletException {
57     if (log4j.isDebugEnabled()) log4j.debug("JR: Create JasperPrint");
58     JasperPrint jasperPrint;
59     String JavaDoc strReportName = PrintJRData.getReportName(this, strProcessId);
60     String JavaDoc strAttach = strFTPDirectory + "/284-" +strProcessId;
61     
62     String JavaDoc strLanguage = vars.getLanguage();
63     if (strBaseDesignPath.endsWith("/")) strDefaultDesignPath = strDefaultDesignPath.substring(0, strDefaultDesignPath.length()-1);
64     log4j.info("*********************Base path: " + strBaseDesignPath);
65     String JavaDoc strNewAddBase = strDefaultDesignPath;
66     String JavaDoc strFinal = strBaseDesignPath;
67     if (!strLanguage.equals("") && !strLanguage.equals("en_US")) strNewAddBase = strLanguage;
68     if (!strFinal.endsWith("/" + strNewAddBase)) strFinal += "/" + strNewAddBase;
69     log4j.info("*********************Base path: " + strFinal);
70     String JavaDoc strBaseDesign = prefix + "/" + strFinal;
71     
72     strReportName = Replace.replace(Replace.replace(strReportName,"@basedesign@",strBaseDesign),"@attach@",strAttach);
73     response.setHeader( "Content-disposition","inline; filename=" + strReportName + "." +strOutputType);
74
75
76     Connection JavaDoc con = null;
77     try {
78       con = this.getTransactionConnection();
79
80       JasperDesign jasperDesign= JRXmlLoader.load(strReportName);
81       jasperReport= JasperCompileManager.compileReport(jasperDesign);
82       HashMap<String JavaDoc, Object JavaDoc> parameters = createParameters(vars, strProcessId);
83       JRParameter[] jrparams = jasperReport.getParameters();
84       if (log4j.isDebugEnabled())
85         for (int j=0; j<jrparams.length;j++)
86           log4j.debug("JR: params: " +jrparams[j].getName());
87         
88
89       Boolean JavaDoc pagination = true;
90       if (strOutputType.equals("PDF")) pagination = false;
91
92       parameters.put("IS_IGNORE_PAGINATION", pagination );
93       parameters.put("BASE_WEB", strReplaceWith );
94       parameters.put("BASE_DESIGN", strBaseDesign);
95       parameters.put("ATTACH", strAttach);
96       parameters.put("USER_CLIENT", Utility.getContext(this, vars, "#User_Client", ""));
97       parameters.put("USER_ORG", Utility.getContext(this, vars, "#User_Org", ""));
98       parameters.put("LANGUAGE", vars.getLanguage());
99
100       if (log4j.isDebugEnabled()) log4j.debug("creating the format factory: " + vars.getJavaDateFormat());
101       JRFormatFactory jrFormatFactory = new JRFormatFactory();
102       jrFormatFactory.setDatePattern(vars.getJavaDateFormat());
103       parameters.put(JRParameter.REPORT_FORMAT_FACTORY, jrFormatFactory);
104
105       if (log4j.isDebugEnabled()) log4j.debug("JR: try the fill");
106       jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, con);
107       this.releaseCommitConnection(con);
108       if (log4j.isDebugEnabled()) log4j.debug("JR: report filled");
109
110       ServletOutputStream os = response.getOutputStream();
111       if (strOutputType.equals("HTML")){
112         if (log4j.isDebugEnabled()) log4j.debug("JR: Print HTML");
113         JRHtmlExporter exporter = new JRHtmlExporter();
114         Map<Object JavaDoc, Object JavaDoc> p = new HashMap<Object JavaDoc, Object JavaDoc>();
115         p.put(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);
116         p.put(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
117         p.put(JRHtmlExporterParameter.OUTPUT_STREAM, os);
118         exporter.setParameters(p);
119         exporter.exportReport();
120       } else if (strOutputType.equals("PDF")){
121         response.setContentType("application/pdf");
122         JasperExportManager.exportReportToPdfStream(jasperPrint, os);
123       } else if (strOutputType.equals("XLS")){
124         response.setContentType("application/vnd.ms-excel");
125         JExcelApiExporter exporter = new JExcelApiExporter();
126         Map<Object JavaDoc, Object JavaDoc> p = new HashMap<Object JavaDoc, Object JavaDoc>();
127         p.put(JRExporterParameter.JASPER_PRINT, jasperPrint);
128         p.put(JRExporterParameter.OUTPUT_STREAM, os);
129         p.put(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
130         p.put(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
131     
132         exporter.setParameters(p);
133         exporter.exportReport();
134       
135       } else {
136         throw new ServletException("Output format no supported");
137       }
138
139     } catch (JRException e) {
140       try {
141         this.releaseRollbackConnection(con);
142       } catch (Exception JavaDoc ignored) {}
143       if (log4j.isDebugEnabled()) log4j.debug("JR: Error: " + e);
144       System.out.println("SO: Error: " + e);
145       e.printStackTrace();
146       throw new ServletException(e.getMessage());
147     } catch (Exception JavaDoc e) {
148       try {
149         releaseRollbackConnection(con);
150       } catch (Exception JavaDoc ignored) {}
151       throw new ServletException(e.getMessage());
152     }
153
154   }
155   HashMap<String JavaDoc, Object JavaDoc> createParameters(VariablesSecureApp vars, String JavaDoc strProcessId) throws ServletException{
156     if (log4j.isDebugEnabled()) log4j.debug("JR: Get Parameters");
157     String JavaDoc strParamname;
158     String JavaDoc strParamValue;
159     String JavaDoc strSqlFormatDate = vars.getSqlDateFormat();
160     String JavaDoc strJavaFormatDate = vars.getJavaDateFormat();
161     HashMap<String JavaDoc, Object JavaDoc> parameters = new HashMap<String JavaDoc, Object JavaDoc>();
162     PrintJRData[] processparams = PrintJRData.getProcessParams(this, strProcessId);
163     for (int i=0; i<processparams.length;i++) {
164       strParamname = Sqlc.TransformaNombreColumna(processparams[i].paramname);
165       if (log4j.isDebugEnabled()) log4j.debug("JR: -----parameter: " + strParamname + " " + vars.getStringParameter("inp"+strParamname));
166       if (!vars.getStringParameter("inp"+strParamname).equals(""))
167         parameters.put(processparams[i].paramname, formatParameter(vars, processparams[i].paramname, vars.getStringParameter("inp"+strParamname), processparams[i].reference));
168     }
169     return parameters;
170   }
171   Object JavaDoc formatParameter(VariablesSecureApp vars, String JavaDoc strParamName, String JavaDoc strParamValue, String JavaDoc reference) throws ServletException{
172     String JavaDoc strObjectClass = "";
173     Object JavaDoc object ;
174     JRParameter[] jrparams = jasperReport.getParameters();
175     for (int i=0; i<jrparams.length;i++){
176       if ( jrparams[i].getName().equals(strParamName)) strObjectClass = jrparams[i].getValueClassName();
177     }
178     if (log4j.isDebugEnabled()) log4j.debug("ClassType: " + strObjectClass);
179     if (strObjectClass.equals("java.lang.String")) {
180       object = new String JavaDoc(strParamValue);
181     } else if (strObjectClass.equals("java.util.Date")){
182       String JavaDoc strDateFormat;
183       strDateFormat = vars.getJavaDateFormat();
184       SimpleDateFormat dateFormat = new SimpleDateFormat(strDateFormat);
185       try {
186         object = dateFormat.parse(strParamValue);
187       } catch (Exception JavaDoc e) {
188         throw new ServletException(e.getMessage());
189       }
190     } else {
191       object = new String JavaDoc(strParamValue);
192     }
193     return object;
194   }
195   public String JavaDoc getServletInfo() {
196     return "Servlet that generates the output of a JasperReports report.";
197   } // end of getServletInfo() method
198
}
199
Popular Tags