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