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;
29
30 import java.io.File;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.sql.Connection;
34 import java.util.Map;
35
36 import net.sf.jasperreports.engine.fill.JRFiller;
37 import net.sf.jasperreports.engine.util.JRLoader;
38 import net.sf.jasperreports.engine.util.JRSaver;
39
40
41 /**
42 * Fa?ade class for filling compiled report designs with data from report data sources,
43 * in order to produce page-oriented documents, ready-to-print.
44 * <p>
45 * All methods receive a Map object that should contain the values for the report parameters.
46 * These value are retrieved by the engine using the corresponding report parameter name as the key.
47 * <p>
48 * There are two types of method signatures with regards to the data source
49 * provided for filling the report:
50 * <ul>
51 * <li>Methods that receive an instance of the {@link net.sf.jasperreports.engine.JRDataSource} interface
52 * and use it directly for retrieving report data;
53 * <li>Methods that receive an instance of the {@link java.sql.Connection} interface and retrieve
54 * the report data by executing the report internal SQL query through this JDBC connection and wrapping
55 * the returned {@link java.sql.ResultSet} object inside a {@link net.sf.jasperreports.engine.JRResultSetDataSource}
56 * instance.
57 * </ul>
58 *
59 * @see net.sf.jasperreports.engine.JasperReport
60 * @see net.sf.jasperreports.engine.JRDataSource
61 * @see net.sf.jasperreports.engine.fill.JRFiller
62 * @see net.sf.jasperreports.engine.JasperPrint
63 * @author Teodor Danciu (teodord@users.sourceforge.net)
64 * @version $Id: JasperFillManager.java 1229 2006-04-19 10:27:35Z teodord $
65 */
66 public class JasperFillManager
67 {
68
69
70 /**
71 * Fills the compiled report design loaded from the specified file.
72 * The result of this operation is another file that will contain the serialized
73 * {@link JasperPrint} object representing the generated document,
74 * having the same name as the report design as declared in the source file,
75 * plus the <code>*.jrprint</code> extension, located in the same directory as the source file.
76 *
77 * @param sourceFileName source file containing the compile report design
78 * @param parameters report parameters map
79 * @param connection JDBC connection object to use for executing the report internal SQL query
80 */
81 public static String fillReportToFile(
82 String sourceFileName,
83 Map parameters,
84 Connection connection
85 ) throws JRException
86 {
87 File sourceFile = new File(sourceFileName);
88
89 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);
90
91 File destFile = new File(sourceFile.getParent(), jasperReport.getName() + ".jrprint");
92 String destFileName = destFile.toString();
93
94 fillReportToFile(jasperReport, destFileName, parameters, connection);
95
96 return destFileName;
97 }
98
99
100 /**
101 * Fills the compiled report design loaded from the specified file.
102 * The result of this operation is another file that will contain the serialized
103 * {@link JasperPrint} object representing the generated document,
104 * having the same name as the report design as declared in the source file,
105 * plus the <code>*.jrprint</code> extension, located in the same directory as the source file.
106 *
107 * @param sourceFileName source file containing the compile report design
108 * @param parameters report parameters map
109 * @see JRFiller#fillReport(JasperReport, Map)
110 */
111 public static String fillReportToFile(
112 String sourceFileName,
113 Map parameters
114 ) throws JRException
115 {
116 File sourceFile = new File(sourceFileName);
117
118 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);
119
120 File destFile = new File(sourceFile.getParent(), jasperReport.getName() + ".jrprint");
121 String destFileName = destFile.toString();
122
123 fillReportToFile(jasperReport, destFileName, parameters);
124
125 return destFileName;
126 }
127
128
129 /**
130 * Fills the compiled report design loaded from the file received as the first parameter
131 * and places the result in the file specified by the second parameter.
132 *
133 * @param sourceFileName source file containing the compile report design
134 * @param destFileName file name to place the generated report into
135 * @param parameters report parameters map
136 * @param connection JDBC connection object to use for executing the report internal SQL query
137 */
138 public static void fillReportToFile(
139 String sourceFileName,
140 String destFileName,
141 Map parameters,
142 Connection connection
143 ) throws JRException
144 {
145 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFileName);
146
147 fillReportToFile(jasperReport, destFileName, parameters, connection);
148 }
149
150
151 /**
152 * Fills the compiled report design loaded from the file received as the first parameter
153 * and places the result in the file specified by the second parameter.
154 *
155 * @param sourceFileName source file containing the compile report design
156 * @param destFileName file name to place the generated report into
157 * @param parameters report parameters map
158 * @see JRFiller#fillReport(JasperReport, Map)
159 */
160 public static void fillReportToFile(
161 String sourceFileName,
162 String destFileName,
163 Map parameters
164 ) throws JRException
165 {
166 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFileName);
167
168 fillReportToFile(jasperReport, destFileName, parameters);
169 }
170
171
172 /**
173 * Fills the compiled report design received as the first parameter
174 * and places the result in the file specified by the second parameter.
175 *
176 * @param jasperReport compiled report design object to use for filling
177 * @param destFileName file name to place the generated report into
178 * @param parameters report parameters map
179 * @param connection JDBC connection object to use for executing the report internal SQL query
180 */
181 public static void fillReportToFile(
182 JasperReport jasperReport,
183 String destFileName,
184 Map parameters,
185 Connection connection
186 ) throws JRException
187 {
188 JasperPrint jasperPrint = fillReport(jasperReport, parameters, connection);
189
190 JRSaver.saveObject(jasperPrint, destFileName);
191 }
192
193
194 /**
195 * Fills the compiled report design received as the first parameter
196 * and places the result in the file specified by the second parameter.
197 *
198 * @param jasperReport compiled report design object to use for filling
199 * @param destFileName file name to place the generated report into
200 * @param parameters report parameters map
201 * @see JRFiller#fillReport(JasperReport, Map)
202 */
203 public static void fillReportToFile(
204 JasperReport jasperReport,
205 String destFileName,
206 Map parameters
207 ) throws JRException
208 {
209 JasperPrint jasperPrint = fillReport(jasperReport, parameters);
210
211 JRSaver.saveObject(jasperPrint, destFileName);
212 }
213
214
215 /**
216 * Fills the compiled report design loaded from the specified file and returns
217 * the generated report object.
218 *
219 * @param sourceFileName source file containing the compile report design
220 * @param parameters report parameters map
221 * @param connection JDBC connection object to use for executing the report internal SQL query
222 * @return generated report object
223 */
224 public static JasperPrint fillReport(
225 String sourceFileName,
226 Map parameters,
227 Connection connection
228 ) throws JRException
229 {
230 File sourceFile = new File(sourceFileName);
231
232 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);
233
234 return fillReport(jasperReport, parameters, connection);
235 }
236
237
238 /**
239 * Fills the compiled report design loaded from the specified file and returns
240 * the generated report object.
241 *
242 * @param sourceFileName source file containing the compile report design
243 * @param parameters report parameters map
244 * @return generated report object
245 * @see JRFiller#fillReport(JasperReport, Map)
246 */
247 public static JasperPrint fillReport(
248 String sourceFileName,
249 Map parameters
250 ) throws JRException
251 {
252 File sourceFile = new File(sourceFileName);
253
254 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);
255
256 return fillReport(jasperReport, parameters);
257 }
258
259
260 /**
261 * Fills the compiled report design loaded from the supplied input stream and writes
262 * the generated report object to the output stream specified by the second parameter.
263 *
264 * @param inputStream input stream to read the compiled report design object from
265 * @param outputStream output stream to write the generated report object to
266 * @param parameters report parameters map
267 * @param connection JDBC connection object to use for executing the report internal SQL query
268 */
269 public static void fillReportToStream(
270 InputStream inputStream,
271 OutputStream outputStream,
272 Map parameters,
273 Connection connection
274 ) throws JRException
275 {
276 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(inputStream);
277
278 fillReportToStream(jasperReport, outputStream, parameters, connection);
279 }
280
281
282 /**
283 * Fills the compiled report design loaded from the supplied input stream and writes
284 * the generated report object to the output stream specified by the second parameter.
285 *
286 * @param inputStream input stream to read the compiled report design object from
287 * @param outputStream output stream to write the generated report object to
288 * @param parameters report parameters map
289 * @see JRFiller#fillReport(JasperReport, Map)
290 */
291 public static void fillReportToStream(
292 InputStream inputStream,
293 OutputStream outputStream,
294 Map parameters
295 ) throws JRException
296 {
297 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(inputStream);
298
299 fillReportToStream(jasperReport, outputStream, parameters);
300 }
301
302
303 /**
304 * Fills the compiled report design supplied as the first parameter and writes
305 * the generated report object to the output stream specified by the second parameter.
306 *
307 * @param jasperReport compiled report design object to use for filling
308 * @param outputStream output stream to write the generated report object to
309 * @param parameters report parameters map
310 * @param connection JDBC connection object to use for executing the report internal SQL query
311 */
312 public static void fillReportToStream(
313 JasperReport jasperReport,
314 OutputStream outputStream,
315 Map parameters,
316 Connection connection
317 ) throws JRException
318 {
319 JasperPrint jasperPrint = fillReport(jasperReport, parameters, connection);
320
321 JRSaver.saveObject(jasperPrint, outputStream);
322 }
323
324
325 /**
326 * Fills the compiled report design supplied as the first parameter and writes
327 * the generated report object to the output stream specified by the second parameter.
328 *
329 * @param jasperReport compiled report design object to use for filling
330 * @param outputStream output stream to write the generated report object to
331 * @param parameters report parameters map
332 * @see JRFiller#fillReport(JasperReport, Map)
333 */
334 public static void fillReportToStream(
335 JasperReport jasperReport,
336 OutputStream outputStream,
337 Map parameters
338 ) throws JRException
339 {
340 JasperPrint jasperPrint = fillReport(jasperReport, parameters);
341
342 JRSaver.saveObject(jasperPrint, outputStream);
343 }
344
345
346 /**
347 * Fills the compiled report design loaded from the supplied input stream and returns
348 * the generated report object.
349 *
350 * @param inputStream input stream to read the compiled report design object from
351 * @param parameters report parameters map
352 * @param connection JDBC connection object to use for executing the report internal SQL query
353 * @return generated report object
354 */
355 public static JasperPrint fillReport(
356 InputStream inputStream,
357 Map parameters,
358 Connection connection
359 ) throws JRException
360 {
361 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(inputStream);
362
363 return fillReport(jasperReport, parameters, connection);
364 }
365
366
367 /**
368 * Fills the compiled report design loaded from the supplied input stream and returns
369 * the generated report object.
370 *
371 * @param inputStream input stream to read the compiled report design object from
372 * @param parameters report parameters map
373 * @return generated report object
374 * @see JRFiller#fillReport(JasperReport, Map)
375 */
376 public static JasperPrint fillReport(
377 InputStream inputStream,
378 Map parameters
379 ) throws JRException
380 {
381 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(inputStream);
382
383 return fillReport(jasperReport, parameters);
384 }
385
386
387 /**
388 * Fills the compiled report design supplied as the first parameter and returns
389 * the generated report object.
390 *
391 * @param jasperReport compiled report design object to use for filling
392 * @param parameters report parameters map
393 * @param connection JDBC connection object to use for executing the report internal SQL query
394 * @return generated report object
395 */
396 public static JasperPrint fillReport(
397 JasperReport jasperReport,
398 Map parameters,
399 Connection connection
400 ) throws JRException
401 {
402 return JRFiller.fillReport(jasperReport, parameters, connection);
403 }
404
405
406 /**
407 * Fills the compiled report design supplied as the first parameter and returns
408 * the generated report object.
409 *
410 * @param jasperReport compiled report design object to use for filling
411 * @param parameters report parameters map
412 * @return generated report object
413 * @see JRFiller#fillReport(JasperReport, Map)
414 */
415 public static JasperPrint fillReport(
416 JasperReport jasperReport,
417 Map parameters
418 ) throws JRException
419 {
420 return JRFiller.fillReport(jasperReport, parameters);
421 }
422
423
424 /**
425 * Fills the compiled report design loaded from the specified file.
426 * The result of this operation is another file that will contain the serialized
427 * {@link JasperPrint} object representing the generated document,
428 * having the same name as the report design as declared in the source file,
429 * plus the <code>*.jrprint</code> extension, located in the same directory as the source file.
430 *
431 * @param sourceFileName source file containing the compile report design
432 * @param parameters report parameters map
433 * @param dataSource data source object
434 */
435 public static String fillReportToFile(
436 String sourceFileName,
437 Map parameters,
438 JRDataSource dataSource
439 ) throws JRException
440 {
441 File sourceFile = new File(sourceFileName);
442
443 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);
444
445 File destFile = new File(sourceFile.getParent(), jasperReport.getName() + ".jrprint");
446 String destFileName = destFile.toString();
447
448 fillReportToFile(jasperReport, destFileName, parameters, dataSource);
449
450 return destFileName;
451 }
452
453
454 /**
455 * Fills the compiled report design loaded from the file received as the first parameter
456 * and places the result in the file specified by the second parameter.
457 *
458 * @param sourceFileName source file containing the compile report design
459 * @param destFileName file name to place the generated report into
460 * @param parameters report parameters map
461 * @param dataSource data source object
462 */
463 public static void fillReportToFile(
464 String sourceFileName,
465 String destFileName,
466 Map parameters,
467 JRDataSource dataSource
468 ) throws JRException
469 {
470 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFileName);
471
472 fillReportToFile(jasperReport, destFileName, parameters, dataSource);
473 }
474
475
476 /**
477 * Fills the compiled report design received as the first parameter
478 * and places the result in the file specified by the second parameter.
479 *
480 * @param jasperReport compiled report design object to use for filling
481 * @param destFileName file name to place the generated report into
482 * @param parameters report parameters map
483 * @param dataSource data source object
484 */
485 public static void fillReportToFile(
486 JasperReport jasperReport,
487 String destFileName,
488 Map parameters,
489 JRDataSource dataSource
490 ) throws JRException
491 {
492 JasperPrint jasperPrint = fillReport(jasperReport, parameters, dataSource);
493
494 JRSaver.saveObject(jasperPrint, destFileName);
495 }
496
497
498 /**
499 * Fills the compiled report design loaded from the specified file and returns
500 * the generated report object.
501 *
502 * @param sourceFileName source file containing the compile report design
503 * @param parameters report parameters map
504 * @param dataSource data source object
505 * @return generated report object
506 */
507 public static JasperPrint fillReport(
508 String sourceFileName,
509 Map parameters,
510 JRDataSource dataSource
511 ) throws JRException
512 {
513 File sourceFile = new File(sourceFileName);
514
515 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);
516
517 return fillReport(jasperReport, parameters, dataSource);
518 }
519
520
521 /**
522 * Fills the compiled report design loaded from the supplied input stream and writes
523 * the generated report object to the output stream specified by the second parameter.
524 *
525 * @param inputStream input stream to read the compiled report design object from
526 * @param outputStream output stream to write the generated report object to
527 * @param parameters report parameters map
528 * @param dataSource data source object
529 */
530 public static void fillReportToStream(
531 InputStream inputStream,
532 OutputStream outputStream,
533 Map parameters,
534 JRDataSource dataSource
535 ) throws JRException
536 {
537 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(inputStream);
538
539 fillReportToStream(jasperReport, outputStream, parameters, dataSource);
540 }
541
542
543 /**
544 * Fills the compiled report design supplied as the first parameter and writes
545 * the generated report object to the output stream specified by the second parameter.
546 *
547 * @param jasperReport compiled report design object to use for filling
548 * @param outputStream output stream to write the generated report object to
549 * @param parameters report parameters map
550 * @param dataSource data source object
551 */
552 public static void fillReportToStream(
553 JasperReport jasperReport,
554 OutputStream outputStream,
555 Map parameters,
556 JRDataSource dataSource
557 ) throws JRException
558 {
559 JasperPrint jasperPrint = fillReport(jasperReport, parameters, dataSource);
560
561 JRSaver.saveObject(jasperPrint, outputStream);
562 }
563
564
565 /**
566 * Fills the compiled report design loaded from the supplied input stream and returns
567 * the generated report object.
568 *
569 * @param inputStream input stream to read the compiled report design object from
570 * @param parameters report parameters map
571 * @param dataSource data source object
572 * @return generated report object
573 */
574 public static JasperPrint fillReport(
575 InputStream inputStream,
576 Map parameters,
577 JRDataSource dataSource
578 ) throws JRException
579 {
580 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(inputStream);
581
582 return fillReport(jasperReport, parameters, dataSource);
583 }
584
585
586 /**
587 * Fills the compiled report design supplied as the first parameter and returns
588 * the generated report object.
589 *
590 * @param jasperReport compiled report design object to use for filling
591 * @param parameters report parameters map
592 * @param dataSource data source object
593 * @return generated report object
594 */
595 public static JasperPrint fillReport(
596 JasperReport jasperReport,
597 Map parameters,
598 JRDataSource dataSource
599 ) throws JRException
600 {
601 return JRFiller.fillReport(jasperReport, parameters, dataSource);
602 }
603
604
605 }