Author: jleroux
Date: Sun Dec 16 14:38:02 2007 New Revision: 604728 URL: http://svn.apache.org/viewvc?rev=604728&view=rev Log: A patch from Fabien Carrion "To permit to have a view with jasperreports to export to excel using the poi api, or the jxls api." (https://issues.apache.org/jira/browse/OFBIZ-1250) - OFBIZ-1250 JasperReports is any longer used in OFBIz but this might be helpful in some occasions. As it changes anything (I reviewed but no tested) I prefer to commit than ignore. Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsJXlsViewHandler.java (with props) ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsPoiXlsViewHandler.java (with props) Modified: ofbiz/trunk/applications/content/src/org/ofbiz/content/survey/PdfSurveyServices.java ofbiz/trunk/framework/webapp/build.xml Modified: ofbiz/trunk/applications/content/src/org/ofbiz/content/survey/PdfSurveyServices.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/content/src/org/ofbiz/content/survey/PdfSurveyServices.java?rev=604728&r1=604727&r2=604728&view=diff ============================================================================== --- ofbiz/trunk/applications/content/src/org/ofbiz/content/survey/PdfSurveyServices.java (original) +++ ofbiz/trunk/applications/content/src/org/ofbiz/content/survey/PdfSurveyServices.java Sun Dec 16 14:38:02 2007 @@ -127,9 +127,9 @@ } else if (type == AcroFields.FIELD_TYPE_LIST || type == AcroFields.FIELD_TYPE_COMBO) { surveyQuestion.set("surveyQuestionTypeId", "OPTION"); // TODO: handle these specially with the acroFields.getListOptionDisplay (and getListOptionExport?) - String[] listOptionDisplayArray = acroFields.getListOptionDisplay(fieldName); + /*String[] listOptionDisplayArray = acroFields.getListOptionDisplay(fieldName); String[] listOptionExportArray = acroFields.getListOptionExport(fieldName); - Debug.logInfo("listOptionDisplayArray: " + listOptionDisplayArray + "; listOptionExportArray: " + listOptionExportArray, module); + Debug.logInfo("listOptionDisplayArray: " + listOptionDisplayArray + "; listOptionExportArray: " + listOptionExportArray, module);*/ } else { surveyQuestion.set("surveyQuestionTypeId", "TEXT_SHORT"); Debug.logWarning("Building Survey from PDF, fieldName=[" + fieldName + "]: don't know how to handle field type: " + type + "; defaulting to short text", module); Modified: ofbiz/trunk/framework/webapp/build.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/build.xml?rev=604728&r1=604727&r2=604728&view=diff ============================================================================== --- ofbiz/trunk/framework/webapp/build.xml (original) +++ ofbiz/trunk/framework/webapp/build.xml Sun Dec 16 14:38:02 2007 @@ -91,6 +91,8 @@ <!-- JasperReports dependent files --> <exclude name="org/ofbiz/webapp/view/JasperReportsPdfViewHandler.java"/> <exclude name="org/ofbiz/webapp/view/JasperReportsXmlViewHandler.java"/> + <exclude name="org/ofbiz/webapp/view/JasperReportsJXlsViewHandler.java"/> + <exclude name="org/ofbiz/webapp/view/JasperReportsPoiXlsViewHandler.java"/> <!-- JPublish/EdenLib dependent files --> <exclude name="org/ofbiz/webapp/view/GenericViewRenderer.java"/> <exclude name="org/ofbiz/webapp/view/JPublishViewHandler.java"/> Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsJXlsViewHandler.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsJXlsViewHandler.java?rev=604728&view=auto ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsJXlsViewHandler.java (added) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsJXlsViewHandler.java Sun Dec 16 14:38:02 2007 @@ -0,0 +1,137 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.webapp.view; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import net.sf.jasperreports.engine.JRDataSource; +import net.sf.jasperreports.engine.JREmptyDataSource; +import net.sf.jasperreports.engine.JasperCompileManager; +import net.sf.jasperreports.engine.JasperFillManager; +import net.sf.jasperreports.engine.JasperPrint; +import net.sf.jasperreports.engine.JasperReport; +import net.sf.jasperreports.engine.export.JExcelApiExporter; +import net.sf.jasperreports.engine.export.JExcelApiExporterParameter; + +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilHttp; +import org.ofbiz.base.util.cache.UtilCache; +import org.ofbiz.webapp.control.ContextFilter; +import org.ofbiz.entity.GenericDelegator; +import org.ofbiz.entity.jdbc.ConnectionFactory; + + +/** + * Handles JasperReports JXls view rendering + */ +public class JasperReportsJXlsViewHandler implements ViewHandler { + + public static final String module = JasperReportsJXlsViewHandler.class.getName(); + + protected ServletContext context; + public static UtilCache jasperReportsCompiledCache = new UtilCache("webapp.JasperReportsCompiled"); + + public void init(ServletContext context) throws ViewHandlerException { + this.context = context; + } + + public void render(String name, String page, String info, String contentType, String encoding, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException { + // some containers call filters on EVERY request, even forwarded ones, + // so let it know that it came from the control servlet + + if (request == null) { + throw new ViewHandlerException("The HttpServletRequest object was null, how did that happen?"); + } + if (page == null || page.length() == 0) { + throw new ViewHandlerException("View page was null or empty, but must be specified"); + } + if (info == null || info.length() == 0) { + Debug.logInfo("View info string was null or empty, (optionally used to specify an Entity that is mapped to the Entity Engine datasource that the report will use).", module); + } + + // tell the ContextFilter we are forwarding + request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, new Boolean(true)); + GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); + if (delegator == null) { + throw new ViewHandlerException("The delegator object was null, how did that happen?"); + } + + try { + JasperReport report = (JasperReport) jasperReportsCompiledCache.get(page); + if (report == null) { + synchronized (this) { + report = (JasperReport) jasperReportsCompiledCache.get(page); + if (report == null) { + InputStream is = context.getResourceAsStream(page); + report = JasperCompileManager.compileReport(is); + jasperReportsCompiledCache.put(page, report); + } + } + } + + response.setContentType("application/xls"); + + Map parameters = (Map) request.getAttribute("jrParameters"); + if (parameters == null) { + parameters = UtilHttp.getParameterMap(request); + } + + JRDataSource jrDataSource = (JRDataSource) request.getAttribute("jrDataSource"); + JasperPrint jp = null; + if (jrDataSource == null) { + String datasourceName = delegator.getEntityHelperName(info); + if (datasourceName != null && datasourceName.length() > 0) { + Debug.logInfo("Filling report with connection from datasource: " + datasourceName, module); + jp = JasperFillManager.fillReport(report, parameters, ConnectionFactory.getConnection(datasourceName)); + } else { + Debug.logInfo("Filling report with an empty JR datasource", module); + jp = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource()); + } + } else { + Debug.logInfo("Filling report with a passed in jrDataSource", module); + jp = JasperFillManager.fillReport(report, parameters, jrDataSource); + } + + if (jp.getPages().size() < 1) { + throw new ViewHandlerException("Report is Empty (no results?)"); + } else { + Debug.logInfo("Got report, there are " + jp.getPages().size() + " pages.", module); + } + JExcelApiExporter exporter = new JExcelApiExporter(); + exporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, jp); + exporter.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, response.getOutputStream()); + exporter.exportReport(); + + } catch (IOException ie) { + throw new ViewHandlerException("IO Error in report", ie); + } catch (java.sql.SQLException e) { + throw new ViewHandlerException("Database error while running report", e); + } catch (Exception e) { + throw new ViewHandlerException("Error in report", e); + // } catch (ServletException se) { + // throw new ViewHandlerException("Error in region", se.getRootCause()); + } + } +} Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsJXlsViewHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsJXlsViewHandler.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsJXlsViewHandler.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsPoiXlsViewHandler.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsPoiXlsViewHandler.java?rev=604728&view=auto ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsPoiXlsViewHandler.java (added) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsPoiXlsViewHandler.java Sun Dec 16 14:38:02 2007 @@ -0,0 +1,138 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.webapp.view; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import net.sf.jasperreports.engine.JRDataSource; +import net.sf.jasperreports.engine.JREmptyDataSource; +import net.sf.jasperreports.engine.JasperCompileManager; +import net.sf.jasperreports.engine.JasperFillManager; +import net.sf.jasperreports.engine.JasperPrint; +import net.sf.jasperreports.engine.JasperReport; +import net.sf.jasperreports.engine.export.JRXlsExporter; +import net.sf.jasperreports.engine.export.JRXlsExporterParameter; + +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilHttp; +import org.ofbiz.base.util.cache.UtilCache; +import org.ofbiz.webapp.control.ContextFilter; +import org.ofbiz.entity.GenericDelegator; +import org.ofbiz.entity.jdbc.ConnectionFactory; + + +/** + * Handles JasperReports PoiXls view rendering + */ +public class JasperReportsPoiXlsViewHandler implements ViewHandler { + + public static final String module = JasperReportsPoiXlsViewHandler.class.getName(); + + protected ServletContext context; + public static UtilCache jasperReportsCompiledCache = new UtilCache("webapp.JasperReportsCompiled"); + + public void init(ServletContext context) throws ViewHandlerException { + this.context = context; + } + + public void render(String name, String page, String info, String contentType, String encoding, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException { + // some containers call filters on EVERY request, even forwarded ones, + // so let it know that it came from the control servlet + + if (request == null) { + throw new ViewHandlerException("The HttpServletRequest object was null, how did that happen?"); + } + if (page == null || page.length() == 0) { + throw new ViewHandlerException("View page was null or empty, but must be specified"); + } + if (info == null || info.length() == 0) { + Debug.logInfo("View info string was null or empty, (optionally used to specify an Entity that is mapped to the Entity Engine datasource that the report will use).", module); + } + + // tell the ContextFilter we are forwarding + request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, new Boolean(true)); + GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); + if (delegator == null) { + throw new ViewHandlerException("The delegator object was null, how did that happen?"); + } + + try { + JasperReport report = (JasperReport) jasperReportsCompiledCache.get(page); + if (report == null) { + synchronized (this) { + report = (JasperReport) jasperReportsCompiledCache.get(page); + if (report == null) { + InputStream is = context.getResourceAsStream(page); + report = JasperCompileManager.compileReport(is); + jasperReportsCompiledCache.put(page, report); + } + } + } + + response.setContentType("application/xls"); + + Map parameters = (Map) request.getAttribute("jrParameters"); + if (parameters == null) { + parameters = UtilHttp.getParameterMap(request); + } + + JRDataSource jrDataSource = (JRDataSource) request.getAttribute("jrDataSource"); + JasperPrint jp = null; + if (jrDataSource == null) { + String datasourceName = delegator.getEntityHelperName(info); + if (datasourceName != null && datasourceName.length() > 0) { + Debug.logInfo("Filling report with connection from datasource: " + datasourceName, module); + jp = JasperFillManager.fillReport(report, parameters, ConnectionFactory.getConnection(datasourceName)); + } else { + Debug.logInfo("Filling report with an empty JR datasource", module); + jp = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource()); + } + } else { + Debug.logInfo("Filling report with a passed in jrDataSource", module); + jp = JasperFillManager.fillReport(report, parameters, jrDataSource); + } + + if (jp.getPages().size() < 1) { + throw new ViewHandlerException("Report is Empty (no results?)"); + } else { + Debug.logInfo("Got report, there are " + jp.getPages().size() + " pages.", module); + } + JRXlsExporter exporter = new JRXlsExporter(); + exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jp); + exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, response.getOutputStream()); + exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); + exporter.exportReport(); + + } catch (IOException ie) { + throw new ViewHandlerException("IO Error in report", ie); + } catch (java.sql.SQLException e) { + throw new ViewHandlerException("Database error while running report", e); + } catch (Exception e) { + throw new ViewHandlerException("Error in report", e); + // } catch (ServletException se) { + // throw new ViewHandlerException("Error in region", se.getRootCause()); + } + } +} Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsPoiXlsViewHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsPoiXlsViewHandler.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/JasperReportsPoiXlsViewHandler.java ------------------------------------------------------------------------------ svn:mime-type = text/plain |
Free forum by Nabble | Edit this page |