ar.com.fdvs.dj.core.DJServletHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of DynamicJasper Show documentation
Show all versions of DynamicJasper Show documentation
DynamicJasper (DJ) is an API that hides the complexity of Jasper
Reports, it helps developers to save time when designing
simple/medium complexity reports generating the layout of the
report elements automatically. It creates reports dynamically,
defining at runtime the columns, column width (auto width), groups,
variables, fonts, charts, crosstabs, sub reports (that can also be dynamic), page size
and everything else that you can define at design time.
DJ keeps full compatibility with Jasper Reports since
it's a tool that helps create reports programmatically in
a easy way (it only interferes with the creation of the report
design doing the layout of the elements).
You can use the classic .jrxml files as templates while the
content and layout of the report elements are handled by the DJ
API.
http://dynamicjasper.com
package ar.com.fdvs.dj.core;
import ar.com.fdvs.dj.core.layout.LayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.output.ReportWriter;
import ar.com.fdvs.dj.output.ReportWriterFactory;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class DJServletHelper {
private static final ThreadLocal pageTreshold = new ThreadLocal();
static {
pageTreshold.set(5);
}
/**
* Sets the number of pages to keep the report in memory, if the report surpases such limit, a file will
* be used.
* @param treshold
*/
public static void setPageTreshold(int treshold){
if (treshold >= 0)
pageTreshold.set(treshold);
}
/**
* Generates the report as HTML and setups everything for a clean response (serving images as well).
* You have to declare JasperReport servlet in web.xml (net.sf.jasperreports.j2ee.servlets.ImageServlet)
*
* Web XML must be configured somehow like this:
*
* <servlet>
* <servlet-name>image</servlet-name>
* <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
* </servlet>
*
* <servlet-mapping>
* <servlet-name>image</servlet-name>
* <url-pattern>/reports/image</url-pattern>
* </servlet-mapping>
*
*
* @param request
* @param response
* @param imageServletUrl the URI to reach net.sf.jasperreports.j2ee.servlets.ImageServlet servlet (in example it would be "reports/image")
* @param dynamicReport
* @param layoutManager
* @param ds
* @param parameters Parameters for the DynamicReport
* @param exporterParams Extra parameters for JasperReport's HTML exporter (HTMLJRHtmlExporter)
* @throws JRException
* @throws IOException
*/
public static void exportToHtml(HttpServletRequest request,
HttpServletResponse response,
String imageServletUrl,
DynamicReport dynamicReport,
LayoutManager layoutManager,
JRDataSource ds,
Map parameters,
Map exporterParams) throws JRException, IOException {
if (parameters == null)
parameters = new HashMap();
if (exporterParams == null)
exporterParams = new HashMap();
JasperPrint _jasperPrint = DynamicJasperHelper.generateJasperPrint(dynamicReport, layoutManager, ds, parameters);
exportToHtml(request,response,imageServletUrl,_jasperPrint,exporterParams);
}
public static void exportToHtml(HttpServletRequest request,
HttpServletResponse response,
String imageServletUrl,
JasperPrint jasperPrint,
Map exporterParams) throws JRException, IOException {
if (exporterParams == null)
exporterParams = new HashMap ();
exporterParams.put(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imageServletUrl);
final ReportWriter reportWriter = ReportWriterFactory.build(pageTreshold.get()).getReportWriter(jasperPrint, DJConstants.FORMAT_HTML, exporterParams);
Map imagesMap = new HashMap();
JRExporter exporter = reportWriter.getExporter();
exporter.setParameters(exporterParams);
setupParameters(request, imageServletUrl, jasperPrint, imagesMap, exporter);
//write generated HTML to the http-response (the one you got from the helper)
reportWriter.writeTo(response);
}
public static InputStream exportToHtml(HttpServletRequest request,
String imageServletUrl,
DynamicReport dynamicReport,
LayoutManager layoutManager,
JRDataSource ds,
Map parameters,
Map exporterParams) throws JRException, IOException {
if (parameters == null)
parameters = new HashMap();
if (exporterParams == null)
exporterParams = new HashMap ();
JasperPrint _jasperPrint = DynamicJasperHelper.generateJasperPrint(dynamicReport, layoutManager, ds, parameters);
return exportToHtml(request, imageServletUrl,_jasperPrint,exporterParams);
}
public static InputStream exportToHtml(HttpServletRequest request,
String imageServletUrl,
JasperPrint jasperPrint,
Map exporterParams) throws JRException, IOException {
if (exporterParams == null)
exporterParams = new HashMap();
exporterParams.put(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imageServletUrl);
final ReportWriter reportWriter = ReportWriterFactory.build(pageTreshold.get()).getReportWriter(jasperPrint, DJConstants.FORMAT_HTML, exporterParams);
Map imagesMap = new HashMap();
JRExporter exporter = reportWriter.getExporter();
exporter.setParameters(exporterParams);
setupParameters(request, imageServletUrl, jasperPrint, imagesMap, exporter);
//write generated HTML to the http-response (the one you got from the helper)
return reportWriter.write();
}
private static void setupParameters(HttpServletRequest request, String imageServletUrl, JasperPrint jasperPrint, Map imagesMap, JRExporter exporter) {
exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + "/" + imageServletUrl + "?image=");
// Needed to support chart images:
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
HttpSession session = request.getSession();
session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
session.setAttribute("net.sf.jasperreports.j2ee.jasper_print", jasperPrint);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy