1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.engine.fill;
25
26 import java.sql.Connection;
27 import java.util.Locale;
28 import java.util.Map;
29 import java.util.TimeZone;
30
31 import net.sf.jasperreports.annotations.properties.Property;
32 import net.sf.jasperreports.annotations.properties.PropertyScope;
33 import net.sf.jasperreports.engine.JRDataSource;
34 import net.sf.jasperreports.engine.JRException;
35 import net.sf.jasperreports.engine.JRParameter;
36 import net.sf.jasperreports.engine.JRPropertiesUtil;
37 import net.sf.jasperreports.engine.JRRuntimeException;
38 import net.sf.jasperreports.engine.JasperPrint;
39 import net.sf.jasperreports.engine.JasperReport;
40 import net.sf.jasperreports.engine.JasperReportsContext;
41 import net.sf.jasperreports.engine.type.SectionTypeEnum;
42 import net.sf.jasperreports.properties.PropertyConstants;
43
44
45 /**
46  * @author Teodor Danciu (teodord@users.sourceforge.net)
47  */

48 public final class JRFiller
49 {
50
51     public static final String EXCEPTION_MESSAGE_KEY_THREAD_INTERRUPTED = "fill.common.filler.thread.interrupted";
52     public static final String EXCEPTION_MESSAGE_KEY_UNKNOWN_REPORT_SECTION_TYPE = "fill.common.filler.unknown.report.section.type";
53     
54     /**
55      * The default locale used to fill reports.
56      * 
57      * <p>
58      * The property is overridden by the value of the {@link JRParameter#REPORT_LOCALE} parameter.
59      * By default, the system/JVM locale is used.
60      * </p>
61      * 
62      * @see JRParameter#REPORT_LOCALE
63      */

64     @Property(
65             category = PropertyConstants.CATEGORY_FILL,
66             valueType = Locale.class,
67             defaultValue = "system locale",
68             scopes = {PropertyScope.CONTEXT, PropertyScope.REPORT},
69             sinceVersion = PropertyConstants.VERSION_5_2_0
70             )
71     public static final String PROPERTY_DEFAULT_LOCALE = JRPropertiesUtil.PROPERTY_PREFIX + "default.locale";
72     
73
74     /**
75      * The default time zone used to fill reports.
76      * 
77      * <p>
78      * The property is overridden by the value of the {@link JRParameter#REPORT_TIME_ZONE} parameter.
79      * By default, the system/JVM time zone is used.
80      * </p>
81      * 
82      * @see JRParameter#REPORT_TIME_ZONE
83      */

84     @Property(
85             category = PropertyConstants.CATEGORY_FILL,
86             valueType = TimeZone.class,
87             defaultValue = "system time zone",
88             scopes = {PropertyScope.CONTEXT, PropertyScope.REPORT},
89             sinceVersion = PropertyConstants.VERSION_5_2_0
90             )
91     public static final String PROPERTY_DEFAULT_TIMEZONE = JRPropertiesUtil.PROPERTY_PREFIX + "default.timezone";
92
93     /**
94      *
95      */

96     public static JasperPrint fill(
97         JasperReportsContext jasperReportsContext,
98         JasperReport jasperReport,
99         Map<String,Object> parameters,
100         Connection conn
101         ) throws JRException
102     {
103         return fill(jasperReportsContext, SimpleJasperReportSource.from(jasperReport),
104                 parameters, conn);
105     }
106
107     public static JasperPrint fill(
108         JasperReportsContext jasperReportsContext,
109         JasperReportSource reportSource,
110         Map<String,Object> parameters,
111         Connection conn
112         ) throws JRException
113     {
114         ReportFiller filler = createReportFiller(jasperReportsContext, reportSource);
115         
116         JasperPrint jasperPrint = null;
117         
118         try
119         {
120             jasperPrint = filler.fill(parameters, conn);
121         }
122         catch(JRFillInterruptedException e)
123         {
124             throw 
125                 new JRException(
126                     EXCEPTION_MESSAGE_KEY_THREAD_INTERRUPTED,
127                     null,
128                     e);
129         }
130         
131         return jasperPrint;
132     }
133
134
135     /**
136      *
137      */

138     public static JasperPrint fill(
139         JasperReportsContext jasperReportsContext,
140         JasperReport jasperReport,
141         Map<String,Object> parameters,
142         JRDataSource dataSource
143         ) throws JRException
144     {
145         return fill(jasperReportsContext, SimpleJasperReportSource.from(jasperReport),
146                 parameters, dataSource);
147     }
148     
149     public static JasperPrint fill(
150         JasperReportsContext jasperReportsContext,
151         JasperReportSource reportSource,
152         Map<String,Object> parameters,
153         JRDataSource dataSource
154         ) throws JRException
155     {
156         ReportFiller filler = createReportFiller(jasperReportsContext, reportSource);
157         
158         JasperPrint jasperPrint = null;
159         
160         try
161         {
162             jasperPrint = filler.fill(parameters, dataSource);
163         }
164         catch(JRFillInterruptedException e)
165         {
166             throw 
167                 new JRException(
168                     EXCEPTION_MESSAGE_KEY_THREAD_INTERRUPTED,
169                     null,
170                     e);
171         }
172         
173         return jasperPrint;
174     }
175     
176
177     /**
178      * Fills a report.
179      * <p/>
180      * The data source used to fill the report is determined in the following way:
181      * <ul>
182      *     <li>If a non-null value of the {@link net.sf.jasperreports.engine.JRParameter#REPORT_DATA_SOURCE REPORT_DATA_SOURCE}
183      * has been specified, it will be used as data source.</li>
184      *     <li>Otherwise, if the report has a query then a data source will be created based on the query and connection
185      * parameter values.</li>
186      *     <li>Otherwise, the report will be filled without a data source.</li>
187      * </ul>
188      * 
189      * @param jasperReport the report
190      * @param parameters the fill parameters
191      * @return the filled report
192      * @throws JRException
193      */

194     public static JasperPrint fill(
195         JasperReportsContext jasperReportsContext,
196         JasperReport jasperReport, 
197         Map<String,Object> parameters
198         ) throws JRException
199     {
200         return fill(jasperReportsContext, SimpleJasperReportSource.from(jasperReport),
201                 parameters);
202     }
203     
204     public static JasperPrint fill(
205             JasperReportsContext jasperReportsContext,
206             JasperReportSource reportSource, 
207             Map<String,Object> parameters
208             ) throws JRException
209     {
210         ReportFiller filler = createReportFiller(jasperReportsContext, reportSource);
211
212         try
213         {
214             JasperPrint jasperPrint = filler.fill(parameters);
215
216             return jasperPrint;
217         }
218         catch (JRFillInterruptedException e)
219         {
220             throw 
221                 new JRException(
222                     EXCEPTION_MESSAGE_KEY_THREAD_INTERRUPTED,
223                     null,
224                     e);
225         }
226     }
227
228
229     /**
230      *
231      */

232     //FIXMEBOOK deprecate?
233     public static JRBaseFiller createFiller(JasperReportsContext jasperReportsContext, JasperReport jasperReport) throws JRException
234     {
235         return createBandReportFiller(jasperReportsContext, SimpleJasperReportSource.from(jasperReport));
236     }
237
238     protected static JRBaseFiller createBandReportFiller(JasperReportsContext jasperReportsContext, JasperReportSource reportSource) throws JRException
239     {
240         JRBaseFiller filler = null;
241
242         switch (reportSource.getReport().getPrintOrderValue())
243         {
244             case HORIZONTAL :
245             {
246                 filler = new JRHorizontalFiller(jasperReportsContext, reportSource, null);
247                 break;
248             }
249             case VERTICAL :
250             {
251                 filler = new JRVerticalFiller(jasperReportsContext, reportSource, null);
252                 break;
253             }
254         }
255         return filler;
256     }
257     
258     public static ReportFiller createReportFiller(JasperReportsContext jasperReportsContext, JasperReport jasperReport) throws JRException
259     {
260         return createReportFiller(jasperReportsContext, SimpleJasperReportSource.from(jasperReport));
261     }
262     
263     public static ReportFiller createReportFiller(JasperReportsContext jasperReportsContext, 
264             JasperReportSource reportSource) throws JRException
265     {
266         ReportFiller filler;
267         SectionTypeEnum sectionType = reportSource.getReport().getSectionType();
268         sectionType = sectionType == null ? SectionTypeEnum.BAND : sectionType;
269         switch (sectionType)
270         {
271         case BAND:
272             filler = createBandReportFiller(jasperReportsContext, reportSource);
273             break;
274         case PART:
275         {
276             filler = new PartReportFiller(jasperReportsContext, reportSource, null);
277             break;
278         }
279         default:
280             throw 
281                 new JRRuntimeException(
282                     EXCEPTION_MESSAGE_KEY_UNKNOWN_REPORT_SECTION_TYPE,  
283                     new Object[]{reportSource.getReport().getSectionType()} 
284                     );
285         }
286         return filler;
287     }
288     
289     
290     private JRFiller()
291     {
292     }
293 }
294