1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package ar.com.fdvs.dj.test.subreport;
31
32 import java.util.Date;
33 import java.util.HashMap;
34
35 import net.sf.jasperreports.engine.JasperFillManager;
36 import net.sf.jasperreports.engine.JasperReport;
37 import net.sf.jasperreports.view.JasperDesignViewer;
38 import net.sf.jasperreports.view.JasperViewer;
39 import ar.com.fdvs.dj.core.DJConstants;
40 import ar.com.fdvs.dj.core.DynamicJasperHelper;
41 import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
42 import ar.com.fdvs.dj.domain.AutoText;
43 import ar.com.fdvs.dj.domain.DynamicReport;
44 import ar.com.fdvs.dj.domain.Style;
45 import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
46 import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
47 import ar.com.fdvs.dj.domain.constants.Font;
48 import ar.com.fdvs.dj.test.BaseDjReportTest;
49 import ar.com.fdvs.dj.test.ChartReportTest;
50 import ar.com.fdvs.dj.test.ReportExporter;
51 import ar.com.fdvs.dj.test.TestRepositoryProducts;
52 import ar.com.fdvs.dj.test.domain.Product;
53
54 public class ConcatenatedReportTest extends BaseDjReportTest {
55
56 public DynamicReport buildReport() throws Exception {
57
58 Style titleStyle = new Style();
59 titleStyle.setFont(new Font(24, Font._FONT_VERDANA, true));
60
61
62
63
64 DynamicReportBuilder drb = new DynamicReportBuilder();
65 Integer margin = new Integer(20);
66 drb
67 .setTitleStyle(titleStyle)
68 .setTitle("Concatenated reports")
69 .setSubtitle("All the reports shown here are concatenated as sub reports")
70 .setDetailHeight(new Integer(15)).setLeftMargin(margin)
71 .setRightMargin(margin).setTopMargin(margin).setBottomMargin(margin)
72 .setUseFullPageWidth(true)
73 .setWhenNoDataAllSectionNoDetail()
74 .addAutoText(AutoText.AUTOTEXT_PAGE_X_OF_Y, AutoText.POSITION_FOOTER,AutoText.ALIGNMENT_CENTER)
75 .addConcatenatedReport(createSubreport1("Sub report 1"), new ClassicLayoutManager(), "statistics",
76 DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION,false)
77 .addConcatenatedReport(createSubreport2("Sub report 2"), "statistics",
78 DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION,true)
79 .addConcatenatedReport(createSubreport2("Sub report 3"), "statistics",
80 DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION,true)
81 .addConcatenatedReport(createSubreport2("Sub report 4"), "statistics",
82 DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION,true);
83
84
85
86 params.put("statistics", Product.statistics_ );
87
88
89
90 params.put("statisticsArray", Product.statistics_.toArray() );
91
92
93
94
95
96 ChartReportTest crt = new ChartReportTest();
97 JasperReport chartJr = DynamicJasperHelper.generateJasperReport(crt.buildReport(), new ClassicLayoutManager(),new HashMap());
98
99
100
101 params.put("subreportsDataSource", TestRepositoryProducts.getDummyCollection() );
102
103
104
105 dr = drb.build();
106
107 return dr;
108 }
109
110 public void testReport() throws Exception {
111 dr = buildReport();
112
113 jr = DynamicJasperHelper.generateJasperReport(dr, getLayoutManager(), params);
114
115
116
117
118
119 jp = JasperFillManager.fillReport(jr, params);
120 ReportExporter.exportReport(jp, System.getProperty("user.dir") + "/target/ConcatenatedReportTest.pdf");
121
122 }
123
124
125
126
127
128
129
130 private DynamicReport createSubreport1(String title) throws Exception {
131 FastReportBuilder rb = new FastReportBuilder();
132 DynamicReport dr = rb
133 .addColumn("Date", "date", Date.class.getName(), 100)
134 .addColumn("Average", "average", Float.class.getName(), 50)
135 .addColumn("%", "percentage", Float.class.getName(), 50)
136 .addColumn("Amount", "amount", Float.class.getName(), 50)
137 .setMargins(5, 5, 20, 20)
138 .setUseFullPageWidth(true)
139 .setTitle(title)
140 .build();
141 return dr;
142 }
143
144
145
146
147
148
149
150 private JasperReport createSubreport2(String title) throws Exception {
151 FastReportBuilder rb = new FastReportBuilder();
152 DynamicReport dr = rb
153 .addColumn("Area", "name", String.class.getName(), 100)
154 .addColumn("Average", "average", Float.class.getName(), 50)
155 .addColumn("%", "percentage", Float.class.getName(), 50)
156 .addColumn("Amount", "amount", Float.class.getName(), 50)
157 .setMargins(5, 5, 20, 20)
158 .setUseFullPageWidth(true)
159 .setTitle(title)
160 .build();
161 return DynamicJasperHelper.generateJasperReport(dr, new ClassicLayoutManager(),null);
162 }
163
164 public static void main(String[] args) throws Exception {
165 ConcatenatedReportTest test = new ConcatenatedReportTest();
166 test.testReport();
167 JasperViewer.viewReport(test.jp);
168 JasperDesignViewer.viewReportDesign(test.jr);
169 test.exportToJRXML();
170 }
171
172 }
173