Search in sources :

Example 1 with ReportEmailRecipient

use of au.com.vaadinutils.jasper.scheduler.entities.ReportEmailRecipient in project VaadinUtils by rlsutton1.

the class EmailTargetLayout method getValidEmailContacts.

@SuppressWarnings("unchecked")
private void getValidEmailContacts(ComboBox targetAddress) {
    JpaBaseDao<ReportEmailRecipient, Long> reportEmailRecipient = JpaBaseDao.getGenericDao(ReportEmailRecipient.class);
    targetAddress.addContainerProperty("id", String.class, null);
    targetAddress.addContainerProperty("email", String.class, null);
    targetAddress.addContainerProperty("namedemail", String.class, null);
    for (final ReportEmailRecipient contact : reportEmailRecipient.findAll()) {
        if (contact.getEmail() != null) {
            Item item = targetAddress.addItem(contact.getEmail());
            if (item != null) {
                item.getItemProperty("email").setValue(contact.getEmail());
                item.getItemProperty("id").setValue(contact.getEmail());
                item.getItemProperty("namedemail").setValue(contact.getEmail());
            }
        }
    }
}
Also used : Item(com.vaadin.data.Item) ReportEmailRecipient(au.com.vaadinutils.jasper.scheduler.entities.ReportEmailRecipient)

Example 2 with ReportEmailRecipient

use of au.com.vaadinutils.jasper.scheduler.entities.ReportEmailRecipient in project VaadinUtils by rlsutton1.

the class JasperReportEmailWindow method sendEmail.

private ReportEmailScheduleEntity sendEmail(Collection<ReportParameter<?>> params, JasperReportProperties reportProperties) {
    String errorMessage = "";
    boolean hasValidTargets = false;
    for (EmailTargetLine target : emailTargetLayout.getTargets()) {
        if (target.targetAddress.getValue() != null) {
            if (!target.targetAddress.isValid()) {
                errorMessage = target.targetAddress.getValue() + " is not a valid email address";
                Notification.show(errorMessage, Type.ERROR_MESSAGE);
                return null;
            }
            hasValidTargets = true;
        }
    }
    if (!hasValidTargets) {
        Notification.show("Set at least one Recipient.", Type.ERROR_MESSAGE);
        return null;
    }
    ReportEmailScheduleEntity schedule = new ReportEmailScheduleEntity();
    schedule.setTitle(reportProperties.getReportTitle());
    schedule.setReportFilename(reportProperties.getReportFileName());
    schedule.setReportTemplateIdentifier(reportProperties.getReportIdentifier());
    schedule.setMessage(message.getValue());
    schedule.setSubject(subject.getValue());
    schedule.setReportClass(reportProperties.getReportClass());
    schedule.setScheduleMode(ScheduleMode.ONE_TIME);
    schedule.setOneTimeRunTime(new Date());
    schedule.setEnabled(true);
    EntityManager entityManager = EntityManagerProvider.getEntityManager();
    List<ReportEmailParameterEntity> rparams = new LinkedList<ReportEmailParameterEntity>();
    List<ReportEmailScheduledDateParameter> dparams = new LinkedList<ReportEmailScheduledDateParameter>();
    for (ReportParameter<?> param : params) {
        // omit report choosers, as they would complicate and confuse
        // if (!(param instanceof ReportChooser))
        {
            if (param instanceof ReportChooser) {
                String[] names = param.getParameterNames().toArray(new String[] {});
                // add non date fields
                ReportEmailParameterEntity rparam = new ReportEmailParameterEntity();
                rparam.setName(names[0]);
                rparam.setValue(param.getValue(names[0]).toString(), param.getDisplayValue(names[0]));
                rparams.add(rparam);
                entityManager.persist(rparam);
            } else if (!param.isDateField()) {
                String[] names = param.getParameterNames().toArray(new String[] {});
                // add non date fields
                ReportEmailParameterEntity rparam = new ReportEmailParameterEntity();
                rparam.setName(names[0]);
                rparam.setValue(param.getValue(names[0]).toString(), param.getDisplayValue(names[0]));
                rparams.add(rparam);
                entityManager.persist(rparam);
            } else {
                // add date fields
                ReportEmailScheduledDateParameter rparam = new ReportEmailScheduledDateParameter();
                String[] names = param.getParameterNames().toArray(new String[] {});
                rparam.setStartName(names[0]);
                rparam.setStartDate(param.getStartDate());
                rparam.setEndName(names[1]);
                rparam.setEndDate(param.getEndDate());
                rparam.setType(param.getDateParameterType());
                rparam.setOffsetType(DateParameterOffsetType.CONSTANT);
                rparam.setLabel(param.getLabel(names[0]));
                dparams.add(rparam);
                entityManager.persist(rparam);
            }
        }
    }
    schedule.setParameters(rparams);
    schedule.setDateParameters(dparams);
    ReportEmailSender reportEmailSender = new ReportEmailSender();
    reportEmailSender.setUserName(reportProperties.getUsername());
    reportEmailSender.setEmailAddress(reportProperties.getUserEmailAddress());
    schedule.setSender(reportEmailSender);
    entityManager.persist(reportEmailSender);
    List<ReportEmailRecipient> recips = new LinkedList<ReportEmailRecipient>();
    for (EmailTargetLine target : emailTargetLayout.getTargets()) {
        ReportEmailRecipient recipient = new ReportEmailRecipient();
        recipient.setEmail((String) target.targetAddress.getValue());
        recipient.setVisibility((ReportEmailRecipientVisibility) target.targetTypeCombo.getValue());
        entityManager.persist(recipient);
        recips.add(recipient);
    }
    schedule.setRecipients(recips);
    schedule.setNextScheduledRunTime(schedule.getScheduleMode().getNextRuntime(schedule, new Date()));
    schedule.setOutputFormat(OutputFormat.PDF);
    entityManager.persist(schedule);
    entityManager.flush();
    reportEmailSender = entityManager.merge(reportEmailSender);
    return schedule;
}
Also used : ReportChooser(au.com.vaadinutils.jasper.parameter.ReportChooser) ReportEmailScheduleEntity(au.com.vaadinutils.jasper.scheduler.entities.ReportEmailScheduleEntity) Date(java.util.Date) LinkedList(java.util.LinkedList) EntityManager(javax.persistence.EntityManager) ReportEmailSender(au.com.vaadinutils.jasper.scheduler.entities.ReportEmailSender) ReportEmailScheduledDateParameter(au.com.vaadinutils.jasper.scheduler.entities.ReportEmailScheduledDateParameter) ReportEmailRecipient(au.com.vaadinutils.jasper.scheduler.entities.ReportEmailRecipient) ReportEmailParameterEntity(au.com.vaadinutils.jasper.scheduler.entities.ReportEmailParameterEntity)

Example 3 with ReportEmailRecipient

use of au.com.vaadinutils.jasper.scheduler.entities.ReportEmailRecipient 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, URISyntaxException {
    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);
    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 4 with ReportEmailRecipient

use of au.com.vaadinutils.jasper.scheduler.entities.ReportEmailRecipient in project VaadinUtils by rlsutton1.

the class JasperReportScheduleLayout method emailRecipientsHandleRowChange.

private void emailRecipientsHandleRowChange(EntityItem<ReportEmailScheduleEntity> item) {
    int ctr = 0;
    emailTargetLayout.clear();
    if (item != null) {
        ReportEmailScheduleEntity entity = item.getEntity();
        if (entity != null) {
            for (ReportEmailRecipient recip : entity.getRecipients()) {
                ctr++;
                emailTargetLayout.add(recip);
            }
            if (ctr == 0) {
                emailTargetLayout.add(null);
            }
        }
    }
}
Also used : ReportEmailRecipient(au.com.vaadinutils.jasper.scheduler.entities.ReportEmailRecipient) ReportEmailScheduleEntity(au.com.vaadinutils.jasper.scheduler.entities.ReportEmailScheduleEntity)

Example 5 with ReportEmailRecipient

use of au.com.vaadinutils.jasper.scheduler.entities.ReportEmailRecipient in project VaadinUtils by rlsutton1.

the class JasperReportScheduleLayout method removeDeletedRecipients.

private void removeDeletedRecipients(ReportEmailScheduleEntity entityItem) {
    List<ReportEmailRecipient> toRemove = new LinkedList<ReportEmailRecipient>();
    for (ReportEmailRecipient recip : entityItem.getRecipients()) {
        boolean found = false;
        for (EmailTargetLine line : emailTargetLayout.getTargets()) {
            String email = (String) line.targetAddress.getValue();
            if (email != null && email.equalsIgnoreCase(recip.getEmail())) {
                found = true;
                break;
            }
        }
        if (!found) {
            toRemove.add(recip);
        }
    }
    entityItem.getRecipients().removeAll(toRemove);
}
Also used : ReportEmailRecipient(au.com.vaadinutils.jasper.scheduler.entities.ReportEmailRecipient) LinkedList(java.util.LinkedList)

Aggregations

ReportEmailRecipient (au.com.vaadinutils.jasper.scheduler.entities.ReportEmailRecipient)6 ReportEmailScheduleEntity (au.com.vaadinutils.jasper.scheduler.entities.ReportEmailScheduleEntity)3 Date (java.util.Date)2 LinkedList (java.util.LinkedList)2 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 ReportChooser (au.com.vaadinutils.jasper.parameter.ReportChooser)1 ReportParameter (au.com.vaadinutils.jasper.parameter.ReportParameter)1 ReportEmailParameterEntity (au.com.vaadinutils.jasper.scheduler.entities.ReportEmailParameterEntity)1 ReportEmailScheduledDateParameter (au.com.vaadinutils.jasper.scheduler.entities.ReportEmailScheduledDateParameter)1 ReportEmailSender (au.com.vaadinutils.jasper.scheduler.entities.ReportEmailSender)1 JasperReportPropertiesAlternateFile (au.com.vaadinutils.jasper.ui.JasperReportPropertiesAlternateFile)1 Item (com.vaadin.data.Item)1 InvalidValueException (com.vaadin.data.Validator.InvalidValueException)1 HashSet (java.util.HashSet)1 EntityManager (javax.persistence.EntityManager)1