Search in sources :

Example 1 with JasperManager

use of au.com.vaadinutils.jasper.JasperManager in project VaadinUtils by rlsutton1.

the class SendEmailTask method sendMessages.

private void sendMessages(final List<JasperTransmission> targets, final JasperProxy proxy) {
    new EntityManagerThread<Void>(new CallableUI<Void>(UI.getCurrent()) {

        @Override
        protected Void call(UI ui) throws Exception {
            int sent = 0;
            try {
                for (JasperTransmission transmission : targets) {
                    if (cancel == true)
                        break;
                    try {
                        JasperManager manager = proxy.getManager();
                        RenderedReport renderedHtml = manager.export(OutputFormat.HTML, null);
                        RenderedReport renderedPDF = manager.export(OutputFormat.PDF, null);
                        JasperEmailBuilder builder = new JasperEmailBuilder(proxy.getEmailSettings());
                        builder.setFrom(proxy.getSenderEmailAddress()).setSubject(proxy.getSubject()).setHtmlBody(renderedHtml).addTo(transmission.getRecipientEmailAddress()).addAttachement(renderedPDF.getBodyAsDataSource("report.pdf", AttachmentType.PDF));
                        builder.send(false);
                    } catch (EmailException e) {
                        logger.error(e, e);
                        transmission.setException(e);
                        SendEmailTask.super.taskItemError(transmission);
                        VUNotification.show(e, Type.ERROR_MESSAGE);
                    }
                }
            } catch (Exception e) {
                VUNotification.show(e, Type.ERROR_MESSAGE);
                throw e;
            } finally {
                SendEmailTask.super.taskComplete(sent);
            }
            return null;
        }
    });
}
Also used : EntityManagerThread(au.com.vaadinutils.dao.EntityManagerThread) UI(com.vaadin.ui.UI) CallableUI(au.com.vaadinutils.dao.CallableUI) JasperManager(au.com.vaadinutils.jasper.JasperManager) RenderedReport(au.com.vaadinutils.jasper.RenderedReport) EmailException(org.apache.commons.mail.EmailException) JasperEmailBuilder(au.com.vaadinutils.jasper.JasperEmailBuilder) EmailException(org.apache.commons.mail.EmailException)

Example 2 with JasperManager

use of au.com.vaadinutils.jasper.JasperManager in project VaadinUtils by rlsutton1.

the class ReportEmailRunnerImpl method runReport.

// Logger logger = org.apache.logging.log4j.LogManager.getLogger();
@Override
public boolean runReport(ReportEmailSchedule schedule, Date scheduledTime, JasperEmailSettings emailSettings) throws InterruptedException, IOException, EmailException, InstantiationException, IllegalAccessException, AddressException, ClassNotFoundException {
    Preconditions.checkNotNull(schedule.getSendersEmailAddress(), "Missing senders email address.");
    Preconditions.checkNotNull(schedule.getRecipients(), "Missing recipient email address");
    Preconditions.checkArgument(schedule.getRecipients().size() > 0, "Missing recipient email address");
    Class<? extends JasperReportProperties> jrpClass = schedule.getJasperReportPropertiesClass();
    jasperReportProperties = jrpClass.newInstance();
    this.schedule = schedule;
    jasperReportProperties = new JasperReportPropertiesAlternateFile(schedule.getReportTitle(), schedule.getReportFileName(), jasperReportProperties);
    Collection<ReportParameter<?>> params = buildParams(schedule, scheduledTime, this);
    JasperManager manager = new JasperManager(this);
    if (manager.checkQueueSize() > 0) {
        return false;
    }
    JasperEmailBuilder builder = new JasperEmailBuilder(emailSettings);
    OutputFormat outputFormat = schedule.getOutputFormat();
    RenderedReport export = manager.export(outputFormat, params);
    try {
        AttachmentType attachementType = outputFormat.getAttachementType();
        builder.setFrom(schedule.getSendersEmailAddress().toString()).setSubject(schedule.subject()).setHtmlBody(schedule.message()).addAttachement(export.getBodyAsDataSource(schedule.getReportTitle() + attachementType.getFileExtension(), attachementType));
        for (ReportEmailRecipient address : schedule.getRecipients()) {
            switch(address.getVisibility()) {
                case TO:
                    builder.addTo(address.getEmail());
                    break;
                case CC:
                    builder.addCC(address.getEmail());
                    break;
                case BCC:
                    builder.addBCC(address.getEmail());
                    break;
            }
        }
        builder.send(false);
    } finally {
        export.close();
    }
    return true;
}
Also used : AttachmentType(au.com.vaadinutils.jasper.AttachmentType) ReportParameter(au.com.vaadinutils.jasper.parameter.ReportParameter) ReportEmailRecipient(au.com.vaadinutils.jasper.scheduler.entities.ReportEmailRecipient) JasperManager(au.com.vaadinutils.jasper.JasperManager) OutputFormat(au.com.vaadinutils.jasper.JasperManager.OutputFormat) RenderedReport(au.com.vaadinutils.jasper.RenderedReport) JasperEmailBuilder(au.com.vaadinutils.jasper.JasperEmailBuilder) JasperReportPropertiesAlternateFile(au.com.vaadinutils.jasper.ui.JasperReportPropertiesAlternateFile)

Example 3 with JasperManager

use of au.com.vaadinutils.jasper.JasperManager in project VaadinUtils by rlsutton1.

the class JasperReportLayout method generateReport.

private void generateReport(final JasperManager.OutputFormat outputFormat, final Collection<ReportParameter<?>> params) {
    streamConnected = false;
    streamReady = false;
    splitPanel.setSecondComponent(splash);
    manager = null;
    boolean validParams = true;
    // manager for the selected report.
    for (ReportParameter<?> p : params) {
        if (p instanceof ReportChooser) {
            ReportChooser chooser = (ReportChooser) p;
            manager = new JasperManager(chooser.getReportProperties(reportProperties));
            Preconditions.checkNotNull(manager, "chooser returned a NULL JasperManager.");
        } else {
            validParams &= p.validate();
        }
    }
    if (!validParams) {
        Notification.show("Please correct your filters and try again", Type.ERROR_MESSAGE);
        printButton.setEnabled(true);
        exportButton.setEnabled(true);
        showButton.setEnabled(true);
        for (ExpanderComponent componet : components) {
            componet.getComponent().setEnabled(true);
        }
        return;
    }
    if (manager == null) {
        this.manager = new JasperManager(reportProperties);
    }
    CancelListener cancelListener = getProgressDialogCancelListener();
    final WorkingDialog dialog = new WorkingDialog("Generating report, please be patient", "Please wait", cancelListener);
    dialog.setHeight("150");
    UI.getCurrent().addWindow(dialog);
    getProgressDialogRefreshListener(dialog);
    getStreamConnectorRefreshListener(outputFormat);
    JasperProgressListener listener = getJasperManagerProgressListener(UI.getCurrent(), dialog, outputFormat);
    manager.exportAsync(outputFormat, params, listener);
}
Also used : JasperProgressListener(au.com.vaadinutils.jasper.JasperProgressListener) WorkingDialog(au.com.vaadinutils.ui.WorkingDialog) ExpanderComponent(au.com.vaadinutils.jasper.filter.ExpanderComponent) ReportChooser(au.com.vaadinutils.jasper.parameter.ReportChooser) JasperManager(au.com.vaadinutils.jasper.JasperManager) CancelListener(au.com.vaadinutils.listener.CancelListener)

Aggregations

JasperManager (au.com.vaadinutils.jasper.JasperManager)3 JasperEmailBuilder (au.com.vaadinutils.jasper.JasperEmailBuilder)2 RenderedReport (au.com.vaadinutils.jasper.RenderedReport)2 CallableUI (au.com.vaadinutils.dao.CallableUI)1 EntityManagerThread (au.com.vaadinutils.dao.EntityManagerThread)1 AttachmentType (au.com.vaadinutils.jasper.AttachmentType)1 OutputFormat (au.com.vaadinutils.jasper.JasperManager.OutputFormat)1 JasperProgressListener (au.com.vaadinutils.jasper.JasperProgressListener)1 ExpanderComponent (au.com.vaadinutils.jasper.filter.ExpanderComponent)1 ReportChooser (au.com.vaadinutils.jasper.parameter.ReportChooser)1 ReportParameter (au.com.vaadinutils.jasper.parameter.ReportParameter)1 ReportEmailRecipient (au.com.vaadinutils.jasper.scheduler.entities.ReportEmailRecipient)1 JasperReportPropertiesAlternateFile (au.com.vaadinutils.jasper.ui.JasperReportPropertiesAlternateFile)1 CancelListener (au.com.vaadinutils.listener.CancelListener)1 WorkingDialog (au.com.vaadinutils.ui.WorkingDialog)1 UI (com.vaadin.ui.UI)1 EmailException (org.apache.commons.mail.EmailException)1