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.JRXlsExporter; |
20 | |
import net.sf.jasperreports.engine.export.JRXlsExporterParameter; |
21 | |
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter; |
22 | |
|
23 | |
import org.apache.log4j.Logger; |
24 | |
import org.kuali.student.common.assembly.data.Data; |
25 | |
import org.kuali.student.common.ui.client.util.ExportElement; |
26 | |
import org.kuali.student.common.ui.server.messages.MessageRPCPreloader; |
27 | |
import org.kuali.student.common.ui.server.screenreport.ScreenReportProcessor; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class JasperScreenReportProcessorImpl implements ScreenReportProcessor { |
36 | |
|
37 | 1 | final Logger LOG = Logger.getLogger(MessageRPCPreloader.class); |
38 | |
|
39 | |
private static final String PROPERTIES_FILE = "jasper.properties"; |
40 | |
|
41 | 1 | private static Properties jasperProperties = new Properties(); |
42 | |
|
43 | |
public JasperScreenReportProcessorImpl() { |
44 | 1 | super(); |
45 | |
try { |
46 | 1 | InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE); |
47 | 1 | jasperProperties.load(inputStream); |
48 | 0 | } catch (FileNotFoundException e) { |
49 | 0 | LOG.error(e); |
50 | 0 | } catch (IOException e) { |
51 | 0 | LOG.error(e); |
52 | 1 | } |
53 | 1 | } |
54 | |
|
55 | |
@Override |
56 | |
public byte[] createPdf(Data source, String template, String reportTitle) { |
57 | |
try { |
58 | 1 | JasperPrint jprint = this.prepare(template, reportTitle, source, null); |
59 | 1 | return exportPdf(jprint); |
60 | |
|
61 | 0 | } catch (JRException e) { |
62 | 0 | LOG.error(e); |
63 | 0 | return null; |
64 | |
} |
65 | |
} |
66 | |
|
67 | |
@Override |
68 | |
public byte[] createPdf(List<ExportElement> source, String template, String reportTitle) { |
69 | |
try { |
70 | 2 | JasperPrint jprint = this.prepare(template, reportTitle, null, source); |
71 | 2 | return exportPdf(jprint); |
72 | |
|
73 | 0 | } catch (JRException e) { |
74 | 0 | LOG.error(e); |
75 | 0 | return null; |
76 | |
} |
77 | |
} |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
private byte[] exportPdf(JasperPrint jprint) throws JRException { |
87 | 3 | return JasperExportManager.exportReportToPdf(jprint); |
88 | |
} |
89 | |
|
90 | |
@Override |
91 | |
public byte[] createXls(Data source, String template, String reportTitle) { |
92 | |
try { |
93 | 1 | JasperPrint jprint = this.prepare(template, reportTitle, source, null); |
94 | 1 | return exportXls(jprint); |
95 | |
|
96 | 0 | } catch (JRException e) { |
97 | 0 | LOG.error(e); |
98 | 0 | return null; |
99 | |
} |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
public byte[] createXls(List<ExportElement> source, String template, String reportTitle) { |
104 | |
try { |
105 | 1 | JasperPrint jprint = this.prepare(template, reportTitle, null, source); |
106 | 1 | return exportXls(jprint); |
107 | |
|
108 | 0 | } catch (JRException e) { |
109 | 0 | LOG.error(e); |
110 | 0 | return null; |
111 | |
} |
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
private byte[] exportXls(JasperPrint jprint) throws JRException { |
122 | 2 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
123 | |
|
124 | 2 | JRXlsExporter exporter = new JRXlsExporter(); |
125 | |
|
126 | 2 | exporter.setParameter(JRExporterParameter.JASPER_PRINT, jprint); |
127 | 2 | exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); |
128 | 2 | exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); |
129 | |
|
130 | 2 | exporter.exportReport(); |
131 | |
|
132 | 2 | return baos.toByteArray(); |
133 | |
} |
134 | |
|
135 | |
@Override |
136 | |
public String createXml(Data source, String template, String reportTitle) { |
137 | |
try { |
138 | 0 | JasperPrint jprint = prepare(template, reportTitle, source, null); |
139 | 0 | return JasperExportManager.exportReportToXml(jprint); |
140 | |
|
141 | 0 | } catch (JRException e) { |
142 | 0 | LOG.error(e); |
143 | 0 | return null; |
144 | |
} |
145 | |
} |
146 | |
|
147 | |
@Override |
148 | |
public byte[] createDoc(Data source, String template, String reportTitle) { |
149 | |
try { |
150 | 1 | JasperPrint jprint = prepare(template, reportTitle, source, null); |
151 | |
|
152 | 1 | return exportDoc(jprint, template); |
153 | 0 | } catch (JRException e) { |
154 | 0 | LOG.error(e); |
155 | 0 | return null; |
156 | |
} |
157 | |
} |
158 | |
|
159 | |
@Override |
160 | |
public byte[] createDoc(List<ExportElement> source, String template, String reportTitle) { |
161 | |
try { |
162 | 1 | JasperPrint jprint = prepare(template, reportTitle, null, source); |
163 | |
|
164 | 1 | return exportDoc(jprint, template); |
165 | 0 | } catch (JRException e) { |
166 | 0 | LOG.error(e); |
167 | 0 | return null; |
168 | |
} |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
private byte[] exportDoc(JasperPrint jprint, String template) { |
179 | 2 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
180 | |
try { |
181 | 2 | JRDocxExporter exporter = new JRDocxExporter(); |
182 | |
|
183 | 2 | exporter.setParameter(JRExporterParameter.JASPER_PRINT, jprint); |
184 | 2 | exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); |
185 | |
|
186 | 2 | exporter.exportReport(); |
187 | 0 | } catch (JRException e) { |
188 | 0 | LOG.error(e); |
189 | 2 | } |
190 | 2 | return baos.toByteArray(); |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
@SuppressWarnings("unchecked") |
197 | |
private JasperPrint prepare(String template, String reportTitle, Data dataMap, List<ExportElement> dataList) throws JRException { |
198 | |
|
199 | 7 | String templateLocation = (String) jasperProperties.get(template); |
200 | 7 | InputStream is = this.getClass().getClassLoader().getResourceAsStream(templateLocation); |
201 | 7 | JasperReport jreport = JasperCompileManager.compileReport(is); |
202 | |
|
203 | |
|
204 | 7 | Map parameters = new HashMap(); |
205 | 7 | parameters.put("ReportTitle", reportTitle); |
206 | |
|
207 | |
|
208 | 7 | String subreportLocation = (String) jasperProperties.get(template + ".subreport"); |
209 | 7 | if (subreportLocation != null) { |
210 | 7 | InputStream subis = this.getClass().getClassLoader().getResourceAsStream(subreportLocation); |
211 | 7 | JasperReport subreport = JasperCompileManager.compileReport(subis); |
212 | 7 | parameters.put("SubReport", subreport); |
213 | |
} |
214 | |
|
215 | |
|
216 | |
JasperPrint jprint; |
217 | 7 | if (dataMap != null) { |
218 | 3 | jprint = JasperFillManager.fillReport(jreport, parameters, new KSCustomDataSource(dataMap.iterator())); |
219 | |
} else { |
220 | 4 | jprint = JasperFillManager.fillReport(jreport, parameters, new KSCollectionDataSource(dataList)); |
221 | |
|
222 | |
} |
223 | 7 | return jprint; |
224 | |
} |
225 | |
|
226 | |
} |