1 /*
2 * ============================================================================
3 * GNU Lesser General Public License
4 * ============================================================================
5 *
6 * JasperReports - Free Java report-generating library.
7 * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 *
23 * JasperSoft Corporation
24 * 303 Second Street, Suite 450 North
25 * San Francisco, CA 94107
26 * http://www.jaspersoft.com
27 */
28 package net.sf.jasperreports.engine.fill;
29
30 import java.sql.Connection;
31 import java.util.Map;
32
33 import net.sf.jasperreports.engine.JRDataSource;
34 import net.sf.jasperreports.engine.JRException;
35 import net.sf.jasperreports.engine.JRReport;
36 import net.sf.jasperreports.engine.JasperPrint;
37 import net.sf.jasperreports.engine.JasperReport;
38
39
40 /**
41 * @author Teodor Danciu (teodord@users.sourceforge.net)
42 * @version $Id: JRFiller.java 1229 2006-04-19 10:27:35Z teodord $
43 */
44 public abstract class JRFiller
45 {
46
47
48 /**
49 *
50 */
51 public static JasperPrint fillReport(
52 JasperReport jasperReport,
53 Map parameters,
54 Connection conn
55 ) throws JRException
56 {
57 JRBaseFiller filler = createFiller(jasperReport);
58
59 JasperPrint jasperPrint = null;
60
61 try
62 {
63 jasperPrint = filler.fill(parameters, conn);
64 }
65 catch(JRFillInterruptedException e)
66 {
67 throw new JRException("The report filling thread was interrupted.");
68 }
69
70 return jasperPrint;
71 }
72
73
74 /**
75 *
76 */
77 public static JasperPrint fillReport(
78 JasperReport jasperReport,
79 Map parameters,
80 JRDataSource dataSource
81 ) throws JRException
82 {
83 JRBaseFiller filler = createFiller(jasperReport);
84
85 JasperPrint jasperPrint = null;
86
87 try
88 {
89 jasperPrint = filler.fill(parameters, dataSource);
90 }
91 catch(JRFillInterruptedException e)
92 {
93 throw new JRException("The report filling thread was interrupted.");
94 }
95
96 return jasperPrint;
97 }
98
99
100 /**
101 * Fills a report.
102 * <p/>
103 * The data source used to fill the report is determined in the following way:
104 * <ul>
105 * <li>If a non-null value of the {@link net.sf.jasperreports.engine.JRParameter#REPORT_DATA_SOURCE REPORT_DATA_SOURCE}
106 * has been specified, it will be used as data source.</li>
107 * <li>Otherwise, if the report has a query then a data source will be created based on the query and connection
108 * parameter values.</li>
109 * <li>Otherwise, the report will be filled without a data source.</li>
110 * </ul>
111 *
112 * @param jasperReport the report
113 * @param parameters the fill parameters
114 * @return the filled report
115 * @throws JRException
116 */
117 public static JasperPrint fillReport(JasperReport jasperReport, Map parameters) throws JRException
118 {
119 JRBaseFiller filler = createFiller(jasperReport);
120
121 try
122 {
123 JasperPrint jasperPrint = filler.fill(parameters);
124
125 return jasperPrint;
126 }
127 catch (JRFillInterruptedException e)
128 {
129 throw new JRException("The report filling thread was interrupted.");
130 }
131 }
132
133
134 public static JRBaseFiller createFiller(JasperReport jasperReport) throws JRException
135 {
136 JRBaseFiller filler = null;
137
138 switch (jasperReport.getPrintOrder())
139 {
140 case JRReport.PRINT_ORDER_HORIZONTAL :
141 {
142 filler = new JRHorizontalFiller(jasperReport);
143 break;
144 }
145 case JRReport.PRINT_ORDER_VERTICAL :
146 {
147 filler = new JRVerticalFiller(jasperReport);
148 break;
149 }
150 }
151 return filler;
152 }
153 }