1 | |
package org.kuali.student.common.ui.server.screenreport.jasper; |
2 | |
|
3 | |
import java.io.ByteArrayOutputStream; |
4 | |
import java.io.FileNotFoundException; |
5 | |
import java.io.IOException; |
6 | |
import java.io.InputStream; |
7 | |
import java.util.HashMap; |
8 | |
import java.util.List; |
9 | |
import java.util.Map; |
10 | |
import java.util.Properties; |
11 | |
|
12 | |
import net.sf.jasperreports.engine.JRException; |
13 | |
import net.sf.jasperreports.engine.JRExporterParameter; |
14 | |
import net.sf.jasperreports.engine.JasperCompileManager; |
15 | |
import net.sf.jasperreports.engine.JasperExportManager; |
16 | |
import net.sf.jasperreports.engine.JasperFillManager; |
17 | |
import net.sf.jasperreports.engine.JasperPrint; |
18 | |
import net.sf.jasperreports.engine.JasperReport; |
19 | |
import net.sf.jasperreports.engine.export.JRRtfExporter; |
20 | |
import net.sf.jasperreports.engine.export.JRTextExporter; |
21 | |
import net.sf.jasperreports.engine.export.JRXlsExporter; |
22 | |
import net.sf.jasperreports.engine.export.JRXlsExporterParameter; |
23 | |
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter; |
24 | |
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporterParameter; |
25 | |
|
26 | |
import org.apache.log4j.Logger; |
27 | |
import org.kuali.student.common.assembly.data.Data; |
28 | |
import org.kuali.student.common.ui.client.util.ExportElement; |
29 | |
import org.kuali.student.common.ui.server.screenreport.ScreenReportProcessor; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class JasperScreenReportProcessorImpl implements ScreenReportProcessor { |
38 | |
|
39 | 2 | final Logger LOG = Logger.getLogger(JasperScreenReportProcessorImpl.class); |
40 | |
|
41 | |
private static final String PROPERTIES_FILE = "jasper.properties"; |
42 | |
|
43 | 1 | private static Properties jasperProperties = new Properties(); |
44 | |
|
45 | |
public JasperScreenReportProcessorImpl() { |
46 | 2 | super(); |
47 | |
try { |
48 | 2 | InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE); |
49 | 2 | jasperProperties.load(inputStream); |
50 | 0 | } catch (FileNotFoundException e) { |
51 | 0 | LOG.error(e); |
52 | 0 | } catch (IOException e) { |
53 | 0 | LOG.error(e); |
54 | 2 | } |
55 | 2 | } |
56 | |
|
57 | |
@Override |
58 | |
public byte[] createPdf(Data source, String template, String reportTitle) { |
59 | |
try { |
60 | 1 | JasperPrint jprint = this.prepare(template, reportTitle, source, null); |
61 | 1 | return exportPdf(jprint); |
62 | |
|
63 | 0 | } catch (JRException e) { |
64 | 0 | LOG.error(e); |
65 | 0 | return null; |
66 | |
} |
67 | |
} |
68 | |
|
69 | |
@Override |
70 | |
public byte[] createPdf(List<ExportElement> source, String template, String reportTitle) { |
71 | |
try { |
72 | 3 | JasperPrint jprint = this.prepare(template, reportTitle, null, source); |
73 | 3 | return exportPdf(jprint); |
74 | |
|
75 | 0 | } catch (JRException e) { |
76 | 0 | LOG.error(e); |
77 | 0 | return null; |
78 | |
} |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
private byte[] exportPdf(JasperPrint jprint) throws JRException { |
89 | 4 | return JasperExportManager.exportReportToPdf(jprint); |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
public byte[] createXls(Data source, String template, String reportTitle) { |
94 | |
try { |
95 | 1 | JasperPrint jprint = this.prepare(template, reportTitle, source, null); |
96 | 1 | return exportXls(jprint); |
97 | |
|
98 | 0 | } catch (JRException e) { |
99 | 0 | LOG.error(e); |
100 | 0 | return null; |
101 | |
} |
102 | |
} |
103 | |
|
104 | |
@Override |
105 | |
public byte[] createXls(List<ExportElement> source, String template, String reportTitle) { |
106 | |
try { |
107 | 1 | JasperPrint jprint = this.prepare(template, reportTitle, null, source); |
108 | 1 | return exportXls(jprint); |
109 | |
|
110 | 0 | } catch (JRException e) { |
111 | 0 | LOG.error(e); |
112 | 0 | return null; |
113 | |
} |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
private byte[] exportXls(JasperPrint jprint) throws JRException { |
126 | 2 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
127 | |
|
128 | 2 | JRXlsExporter exporter = new JRXlsExporter(); |
129 | |
|
130 | 2 | exporter.setParameter(JRExporterParameter.JASPER_PRINT, jprint); |
131 | 2 | exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); |
132 | 2 | exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); |
133 | |
|
134 | 2 | exporter.exportReport(); |
135 | |
|
136 | 2 | return baos.toByteArray(); |
137 | |
} |
138 | |
|
139 | |
@Override |
140 | |
public String createXml(Data source, String template, String reportTitle) { |
141 | |
try { |
142 | 0 | JasperPrint jprint = prepare(template, reportTitle, source, null); |
143 | 0 | return JasperExportManager.exportReportToXml(jprint); |
144 | |
|
145 | 0 | } catch (JRException e) { |
146 | 0 | LOG.error(e); |
147 | 0 | return null; |
148 | |
} |
149 | |
} |
150 | |
|
151 | |
@Override |
152 | |
public byte[] createDoc(Data source, String template, String reportTitle) { |
153 | |
try { |
154 | 1 | JasperPrint jprint = prepare(template, reportTitle, source, null); |
155 | |
|
156 | 1 | return exportDoc(jprint, template); |
157 | 0 | } catch (JRException e) { |
158 | 0 | LOG.error(e); |
159 | 0 | return null; |
160 | |
} |
161 | |
} |
162 | |
|
163 | |
@Override |
164 | |
public byte[] createDoc(List<ExportElement> source, String template, String reportTitle) { |
165 | |
try { |
166 | 1 | JasperPrint jprint = prepare(template, reportTitle, null, source); |
167 | |
|
168 | 1 | return exportDoc(jprint, template); |
169 | 0 | } catch (JRException e) { |
170 | 0 | LOG.error(e); |
171 | 0 | return null; |
172 | |
} |
173 | |
} |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
private byte[] exportDoc(JasperPrint jprint, String template) { |
183 | 2 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
184 | |
try { |
185 | 2 | JRDocxExporter exporter = new JRDocxExporter(); |
186 | |
|
187 | 2 | exporter.setParameter(JRExporterParameter.JASPER_PRINT, jprint); |
188 | 2 | exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); |
189 | 2 | exporter.setParameter(JRDocxExporterParameter.FRAMES_AS_NESTED_TABLES, false); |
190 | |
|
191 | 2 | exporter.exportReport(); |
192 | 0 | } catch (JRException e) { |
193 | 0 | LOG.error(e); |
194 | 2 | } |
195 | 2 | return baos.toByteArray(); |
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
@Override |
202 | |
public byte[] createText(List<ExportElement> source, String template, String reportTitle) { |
203 | |
try { |
204 | 1 | JasperPrint jprint = prepare(template, reportTitle, null, source); |
205 | |
|
206 | 1 | return exportText(jprint, template); |
207 | 0 | } catch (JRException e) { |
208 | 0 | LOG.error(e); |
209 | 0 | return null; |
210 | |
} |
211 | |
} |
212 | |
|
213 | |
private byte[] exportText(JasperPrint jprint, String template) { |
214 | 1 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
215 | |
try { |
216 | 1 | JRTextExporter exporter = new JRTextExporter(); |
217 | |
|
218 | 1 | exporter.setParameter(JRExporterParameter.JASPER_PRINT, jprint); |
219 | 1 | exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); |
220 | |
|
221 | 1 | exporter.exportReport(); |
222 | 0 | } catch (JRException e) { |
223 | 0 | LOG.error(e); |
224 | 1 | } |
225 | 1 | return baos.toByteArray(); |
226 | |
} |
227 | |
|
228 | |
@Override |
229 | |
public byte[] createRtf(List<ExportElement> source, String template, String reportTitle) { |
230 | |
try { |
231 | 1 | JasperPrint jprint = prepare(template, reportTitle, null, source); |
232 | |
|
233 | 1 | return exportRtf(jprint, template); |
234 | 0 | } catch (JRException e) { |
235 | 0 | LOG.error(e); |
236 | 0 | return null; |
237 | |
} |
238 | |
} |
239 | |
|
240 | |
private byte[] exportRtf(JasperPrint jprint, String template) { |
241 | 1 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
242 | |
try { |
243 | 1 | JRRtfExporter exporter = new JRRtfExporter(); |
244 | |
|
245 | 1 | exporter.setParameter(JRExporterParameter.JASPER_PRINT, jprint); |
246 | 1 | exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); |
247 | |
|
248 | 1 | exporter.exportReport(); |
249 | 0 | } catch (JRException e) { |
250 | 0 | LOG.error(e); |
251 | 1 | } |
252 | 1 | return baos.toByteArray(); |
253 | |
} |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
@SuppressWarnings("unchecked") |
259 | |
private JasperPrint prepare(String template, String reportTitle, Data dataMap, List<ExportElement> dataList) throws JRException { |
260 | |
|
261 | 10 | String templateLocation = (String) jasperProperties.get(template); |
262 | 10 | InputStream is = this.getClass().getClassLoader().getResourceAsStream(templateLocation); |
263 | 10 | JasperReport jreport = JasperCompileManager.compileReport(is); |
264 | |
|
265 | |
|
266 | 10 | Map parameters = new HashMap(); |
267 | 10 | parameters.put("ReportTitle", reportTitle); |
268 | |
|
269 | |
|
270 | 10 | String subreportLocation = (String) jasperProperties.get(template + ".subreport"); |
271 | 10 | if (subreportLocation != null) { |
272 | 10 | InputStream subis = this.getClass().getClassLoader().getResourceAsStream(subreportLocation); |
273 | 10 | JasperReport subreport = JasperCompileManager.compileReport(subis); |
274 | 10 | parameters.put("SubReport", subreport); |
275 | |
} |
276 | |
|
277 | |
|
278 | |
JasperPrint jprint; |
279 | 10 | if (dataMap != null) { |
280 | 3 | jprint = JasperFillManager.fillReport(jreport, parameters, new KSCustomDataSource(dataMap.iterator())); |
281 | |
} else { |
282 | 7 | jprint = JasperFillManager.fillReport(jreport, parameters, new KSCollectionDataSource(dataList, null)); |
283 | |
|
284 | |
} |
285 | 10 | return jprint; |
286 | |
} |
287 | |
|
288 | |
} |