Search in sources :

Example 1 with AttachmentType

use of au.com.vaadinutils.jasper.AttachmentType 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)

Aggregations

AttachmentType (au.com.vaadinutils.jasper.AttachmentType)1 JasperEmailBuilder (au.com.vaadinutils.jasper.JasperEmailBuilder)1 JasperManager (au.com.vaadinutils.jasper.JasperManager)1 OutputFormat (au.com.vaadinutils.jasper.JasperManager.OutputFormat)1 RenderedReport (au.com.vaadinutils.jasper.RenderedReport)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