com.save.reports.PromoDealAcknowledgementReport.java Source code

Java tutorial

Introduction

Here is the source code for com.save.reports.PromoDealAcknowledgementReport.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.save.reports;

import com.save.connection.DBConnection;
import com.vaadin.server.StreamResource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

/**
 *
 * @author jetdario
 */
public class PromoDealAcknowledgementReport extends Window {

    File file;
    private int promoId;

    public PromoDealAcknowledgementReport(int promoId) {
        this.promoId = promoId;

        setCaption("Acknowledgement Report");
        setWidth("800px");
        setHeight("600px");
        center();

        Connection conn = DBConnection.connect();

        HashMap hm = new HashMap();
        hm.put("PROMO_ID", getPromoId());

        InputStream template = this.getClass()
                .getResourceAsStream("/reports/PromoDealAcknowledgementFormReport.jasper");

        try {
            JasperPrint print = JasperFillManager.fillReport(template, hm, conn);
            file = File.createTempFile("output", ".pdf");
            JasperExportManager.exportReportToPdfFile(print, file.getPath());
        } catch (JRException | IOException ex) {
            Logger.getLogger(PromoDealAcknowledgementReport.class.getName()).log(Level.SEVERE, null, ex);
        }

        StreamResource.StreamSource source = () -> {
            try {
                FileInputStream fis = new FileInputStream(file);
                return fis;
            } catch (Exception e) {
                e.getMessage();
                return null;
            }
        };

        StreamResource resource = new StreamResource(source, "PromoDealAcknowledgementForm.pdf");
        resource.setMIMEType("application/pdf");

        VerticalLayout v = new VerticalLayout();
        v.setSizeFull();
        Embedded e = new Embedded();
        e.setSource(resource);
        e.setSizeFull();
        e.setType(Embedded.TYPE_BROWSER);
        v.addComponent(e);

        setContent(v);
    }

    private int getPromoId() {
        return promoId;
    }
}