All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ar.com.fdvs.dj.domain.builders.SubReportBuilder Maven / Gradle / Ivy

Go to download

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

There is a newer version: 5.3.9
Show newest version
/*
 * Dynamic Jasper: A library for creating reports dynamically by specifying
 * columns, groups, styles, etc. at runtime. It also saves a lot of development
 * time in many cases! (http://sourceforge.net/projects/dynamicjasper)
 *
 * Copyright (C) 2008  FDV Solutions (http://www.fdvsolutions.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 *
 * License as published by the Free Software Foundation; either
 *
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 *
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 */

package ar.com.fdvs.dj.domain.builders;

import ar.com.fdvs.dj.core.DJConstants;
import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.LayoutManager;
import ar.com.fdvs.dj.domain.DJDataSource;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.entities.Subreport;
import ar.com.fdvs.dj.domain.entities.SubreportParameter;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class SubReportBuilder {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(SubReportBuilder.class);


	private Subreport subreport = new Subreport();

	public Subreport build() throws DJBuilderException {
		if (subreport.getPath() == null && subreport.getDynamicReport() == null && subreport.getReport() == null)
			throw new DJBuilderException("No subreport origin defined (path, dynamicReport, jasperReport)");

		//If the subreport comes from a file, then we load it now
		if (subreport.getPath() != null) {
			JasperReport jr = null;
			File file = new File(subreport.getPath());
			if (file.exists()){
				logger.debug("Loading subreport from file path");
				try {
					jr = (JasperReport) JRLoader.loadObject(file);
				} catch (JRException e) {
					throw new DJBuilderException("Could not load subreport.",e);
				}
			} else {
				logger.debug("Loading subreport from classpath");
				URL url = DynamicJasperHelper.class.getClassLoader().getResource(subreport.getPath());
				try {
					jr = (JasperReport) JRLoader.loadObject(url.openStream());
				} catch (IOException e) {
					throw new DJBuilderException("Could not open subreport as an input stream",e);
				} catch (JRException e) {
					throw new DJBuilderException("Could not load subreport.",e);
				}
			}

			subreport.setReport(jr);
		}

		return subreport;
	}

	/**
	 * Indicates from where to get the data source.
* * @param origin Must be one of these constans located in DJConstants inteface
* - SUBREPORT_DATAS_OURCE_ORIGIN_PARAMETER
* - SUBREPORT_DATAS_OURCE_ORIGIN_FIELD
* - SUBREPORT_DATAS_OURCE_ORIGIN_INTERNAL
* @param type tell if the datasource is a Collection, an Array, a ResultSet or whatever.
* Its value must be a constant from {@link DJConstants} of the like DATA_SOURCE_TYPE_... * @param expression is -depending on the origin- te path to the datasource
* ie: if origin is SUBREPORT_DATAS_OURCE_ORIGIN_PARAMETER, then expression can be "subreport_datasource".
* You must in the parameters map an object using "subreport_datasource" as the key.
* The object must be an instance of {@link JRDataSource} or any of the following
* Collection, Array, ResultSet, or any of the data source types provided by Jasper Reports * @return */ public SubReportBuilder setDataSource(int origin, int type, String expression) { DJDataSource ds = new DJDataSource(expression, origin, type); subreport.setDatasource(ds); return this; } /** * like addDataSource(int origin, int type, String expression) but the type will be of the {@link JRDataSource} * @param origin * @param expression * @return */ public SubReportBuilder setDataSource(int origin, String expression) { return setDataSource(origin, DJConstants.DATA_SOURCE_TYPE_JRDATASOURCE, expression); } /** * like addDataSource(int origin, String expression) but the origin will be from a Parameter * @param origin * @param expression * @return */ public SubReportBuilder setDataSource(String expression) { return setDataSource(DJConstants.SUBREPORT_DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_JRDATASOURCE, expression); } public SubReportBuilder setReport(JasperReport jasperReport) { subreport.setReport(jasperReport); return this; } public SubReportBuilder setDynamicReport(DynamicReport dynamicReport, LayoutManager layoutManager) { subreport.setDynamicReport(dynamicReport); subreport.setLayoutManager(layoutManager); return this; } public SubReportBuilder setPathToReport(String path) { subreport.setPath(path); return this; } public SubReportBuilder setStartInNewPage(boolean startInNewPage) { subreport.setStartInNewPage(startInNewPage); return this; } public SubReportBuilder addParameter(SubreportParameter sp) { subreport.getParameters().add(sp); return this; } public SubReportBuilder addParameterFieldType(String propertyName, String paramName) { SubreportParameter sp = new SubreportParameter(paramName,propertyName,null,DJConstants.SUBREPORT_PARAM_ORIGIN_FIELD); subreport.getParameters().add(sp); return this; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy