Java tutorial
/* * Copyright (C) 2010 {Apertum}Projects. web: www.apertum.ru email: info@apertum.ru * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package ru.apertum.qsystem.reports.model; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.util.HashMap; import java.util.Map; import javax.persistence.Column; import javax.persistence.MappedSuperclass; import net.sf.jasperreports.engine.JRDataSource; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JRParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JRHtmlExporter; import net.sf.jasperreports.engine.export.JRPdfExporter; import net.sf.jasperreports.engine.export.JRRtfExporter; import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter; import org.apache.http.HttpRequest; import ru.apertum.qsystem.client.Locales; import ru.apertum.qsystem.common.Uses; import ru.apertum.qsystem.common.QLog; import ru.apertum.qsystem.common.exceptions.ReportException; import ru.apertum.qsystem.reports.common.Response; import ru.apertum.qsystem.reports.net.NetUtil; /** * ?? . ? ?? ? HashMap [ String, IGenerator ] generators. ? ? ? ? * IFormirovator. process . * * @author Evgeniy Egorov */ @MappedSuperclass public abstract class AGenerator implements IGenerator { /** * ? hibernate. */ public AGenerator() { // javax.imageio.ImageIO.read(getClass().getResource("/ru/apertum/qsystem/reports/eximbank/resources/banner1.jpg")); } private String template; @Column(name = "template") public String getTemplate() { return template; } public void setTemplate(String template) { this.template = template; } private String href; @Column(name = "href") @Override public String getHref() { return href; } public void setHref(String href) { this.href = href; } public AGenerator(String href, String resourceNameTemplate) { this.href = href; this.template = resourceNameTemplate; } /** * ?? ? . * * @param request * @return */ abstract protected JRDataSource getDataSource(HttpRequest request); /** * ?? ? ? . * * @param request * @return */ abstract protected Map getParameters(HttpRequest request); /** * ? ? ??? . * * @param request * @return ?? null. */ abstract protected Connection getConnection(HttpRequest request); /** * ?? ? ?? ? . ? ?? , , * ? null . * * @param request * @return ?? ? . null ? . */ abstract protected Response preparationReport(HttpRequest request); /** * * * @param request * @param errorMessage ? , null * @return */ abstract protected Response getDialog(HttpRequest request, String errorMessage); /** * ? * * @param request * @param params request * @return ? ? , null */ abstract protected String validate(HttpRequest request, HashMap<String, String> params); /** * ? - ?? . ? ? ? IFormirovator ? ? . * * @param request ? (html, pdf, rtf) * @return . */ @Override public Response process(HttpRequest request) { QLog.l().logRep().debug(" : \"" + href + "\""); /* * . * ? ? ? ? ? . * ? ? . */ /* * ??: * * ? , ? ? * ? ? null, ??. ? ? ?? * , ? . * * ? ? ? null, * ? null ? ? getParameters(...) . * ? ??, - ? ? * ?? . */ //? ? final HashMap<String, String> params = NetUtil.getParameters(request); if (params.isEmpty()) { final Response dialog = getDialog(request, null); if (dialog != null) { return dialog; } } else { String err = validate(request, params); if (err != null) { final Response dialog = getDialog(request, err); if (dialog != null) { return dialog; } } } // ? ? final Response before = preparationReport(request); if (before != null) { return before; } // ?? , ?, ? //InputStream is = getClass().getResourceAsStream(template); //JasperReport jasperReport = JasperCompileManager.compileReport(is); // ?? //JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, hm, xmlDataSource); - ? ? ? // ? , http String dataType = null; try { // , ?? final File f_temp = new File(template); final InputStream inStr; if (f_temp.exists()) { inStr = new FileInputStream(f_temp); } else { inStr = getClass().getResourceAsStream(template); } if (inStr == null) { throw new ReportException(" . \"" + template + "\" ?? , ? ? ."); } // ?, ? //? ? , ? , . final Connection conn = getConnection(request); final Map paramsForFilling = getParameters(request); paramsForFilling.put(JRParameter.REPORT_LOCALE, Locales.getInstance().getLangCurrent()); final JasperPrint jasperPrint; if (conn == null) { jasperPrint = JasperFillManager.fillReport(inStr, paramsForFilling, getDataSource(request));//? ?? } else { jasperPrint = JasperFillManager.fillReport(inStr, paramsForFilling, conn);//? ?? } byte[] result = null; final String subject = NetUtil.getUrl(request); int dot = subject.lastIndexOf("."); final String format = subject.substring(dot + 1); final File ff = new File(Uses.TEMP_FOLDER); if (!ff.exists()) { ff.mkdir(); } if (Uses.REPORT_FORMAT_HTML.equalsIgnoreCase(format)) { final JRHtmlExporter exporter = new JRHtmlExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); // ? // ? ? , ? ?? ? html ??? , , // ?? ? ? ? ? . // ? ? ? ? ? . final JRHtmlExporter exporterToTempFile = new JRHtmlExporter(); exporterToTempFile.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporterToTempFile.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, Uses.TEMP_FOLDER + File.separator + "temphtml.html"); exporterToTempFile.exportReport(); final StringBuffer buf = new StringBuffer("UTF-8"); exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, buf); exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); exporter.exportReport(); result = buf.toString().replaceAll("nullpx", "resources/px") .replaceFirst("<body text=\"#000000\"", "<body text=\"#000000\" background=\"resources/fp.png\" bgproperties=\"fixed\"") .replaceAll("bgcolor=\"white\"", "bgcolor=\"CCDDEE\"").replaceAll("nullimg_", "img_") .getBytes("UTF-8"); dataType = "text/html"; } else if (Uses.REPORT_FORMAT_RTF.equalsIgnoreCase(format)) { final JRRtfExporter exporter = new JRRtfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); exporter.exportReport(); result = baos.toByteArray(); dataType = "application/rtf"; } else if (Uses.REPORT_FORMAT_XLSX.equalsIgnoreCase(format)) { //final JROdsExporter exporter = new JROdsExporter(); final JRXlsxExporter exporter = new JRXlsxExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); exporter.exportReport(); result = baos.toByteArray(); //dataType = "application/ods"; dataType = "application/xlsx"; } else if (Uses.REPORT_FORMAT_PDF.equalsIgnoreCase(format)) { // ? ? ? final File f = new File("tahoma.ttf"); if (!f.exists()) { try (FileOutputStream fo = new FileOutputStream(f)) { final InputStream inStream = getClass() .getResourceAsStream("/ru/apertum/qsystem/reports/fonts/tahoma.ttf"); final byte[] b = Uses.readInputStream(inStream); fo.write(b); fo.flush(); } } result = genPDF(jasperPrint); dataType = "application/pdf"; } return new Response(result, dataType); } catch (FileNotFoundException ex) { throw new ReportException( "? ? PDF. " + ex); } catch (IOException ex) { throw new ReportException( " ? /. " + ex); } catch (JRException ex) { throw new ReportException(" . " + ex); } } /** * PDF- . ? ? ?. * * @param jasperPrint ? ?? PDF * @return ?? * @throws net.sf.jasperreports.engine.JRException * @throws java.io.FileNotFoundException * @throws java.io.IOException */ synchronized private static byte[] genPDF(JasperPrint jasperPrint) throws JRException, FileNotFoundException, IOException { // ? JRPdfExporter exporter = new JRPdfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, Uses.TEMP_FOLDER + File.separator + "temppdf.pdf"); exporter.exportReport(); // final File pdf = new File(Uses.TEMP_FOLDER + File.separator + "temppdf.pdf"); final FileInputStream inStream = new FileInputStream(pdf); pdf.delete(); return Uses.readInputStream(inStream); } }