use of au.com.vaadinutils.jasper.scheduler.entities.ReportEmailParameterEntity 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;
}
use of au.com.vaadinutils.jasper.scheduler.entities.ReportEmailParameterEntity in project VaadinUtils by rlsutton1.
the class JasperReportScheduleLayout method saveChangesToReportParameters.
private void saveChangesToReportParameters(ReportEmailScheduleEntity entityItem) {
for (ReportParameter<?> bParam : builder.getReportParameters()) {
boolean set = false;
for (ReportEmailParameter eParam : entityItem.getReportParameters()) {
for (String parameterName : bParam.getParameterNames()) {
if (parameterName.equalsIgnoreCase(eParam.getName())) {
eParam.setValue((String) bParam.getValue(parameterName), bParam.getDisplayValue(parameterName));
set = true;
changedReportProperties(entityItem, bParam);
break;
}
}
}
if (!set && !bParam.isDateField()) {
// add missing parameter
ReportEmailParameterEntity reportEmailParameterEntity = new ReportEmailParameterEntity();
reportEmailParameterEntity.setLabel(bParam.getLabel(""));
String[] names = bParam.getParameterNames().toArray(new String[] {});
reportEmailParameterEntity.setName(names[0]);
reportEmailParameterEntity.setValue((String) bParam.getValue(names[0]), bParam.getDisplayValue(names[0]));
changedReportProperties(entityItem, bParam);
EntityManagerProvider.getEntityManager().persist(reportEmailParameterEntity);
entityItem.addReportParameter(reportEmailParameterEntity);
}
}
}
Aggregations