use of au.com.vaadinutils.dao.JpaBaseDao in project VaadinUtils by rlsutton1.
the class JasperReportLayout method addButtonListener.
private void addButtonListener(Button button, final OutputFormat format) {
button.addClickListener(new ClickEventLogged.ClickListener() {
private static final long serialVersionUID = 1L;
@Override
public void clicked(ClickEvent event) {
try {
createFrequentlyUsed();
createHistory();
printButton.setEnabled(false);
exportButton.setEnabled(false);
showButton.setEnabled(false);
for (ExpanderComponent componet : components) {
componet.getComponent().setEnabled(false);
}
generateReport(format, JasperReportLayout.this.builder.getReportParameters());
} catch (Exception e) {
printButton.setEnabled(true);
exportButton.setEnabled(true);
showButton.setEnabled(true);
for (ExpanderComponent componet : components) {
componet.getComponent().setEnabled(true);
}
Notification.show(e.getMessage(), Type.ERROR_MESSAGE);
logger.error(e, e);
} finally {
}
}
private void createHistory() {
Collection<ReportParameter<?>> params = builder.getReportParameters();
ReportSave reportSave = new ReportSave();
reportSave.setReportClass(reportProperties.getReportClass().getName());
reportSave.setUserDescription("");
reportSave.setUser(reportProperties.getUsername());
reportSave.setSaveType(SaveType.HISTORY);
JpaBaseDao.getEntityManager().persist(reportSave);
for (ReportParameter<?> param : params) {
for (String pname : param.getParameterNames()) {
if (StringUtils.isNotBlank(param.getLabel(pname))) {
ReportSaveParameter reportSaveparam = new ReportSaveParameter();
reportSaveparam.setParameterName(param.getLabel(pname));
reportSaveparam.setTextualRepresentation(param.getDisplayValue(pname));
reportSaveparam.setParameterValue(param.getValue(pname).toString());
reportSaveparam.setMetaData(param.getSaveMetaData());
reportSaveparam.setMetaDataComment(param.getMetaDataComment());
reportSave.addParameter(reportSaveparam);
JpaBaseDao.getEntityManager().persist(reportSaveparam);
}
}
}
// remove excess history
JpaDslBuilder<ReportSave> q = new JpaBaseDao<>(ReportSave.class).select();
List<ReportSave> history = q.where(q.eq(ReportSave_.user, reportProperties.getUsername()).and(q.eq(ReportSave_.saveType, SaveType.HISTORY))).orderBy(ReportSave_.lastUsed, true).getResultList();
if (history.size() > 50) {
ReportSave old = history.get(0);
for (ReportSaveParameter param : old.getParameters()) {
EntityManagerProvider.remove(param);
}
EntityManagerProvider.remove(old);
}
}
private void createFrequentlyUsed() {
JpaDslBuilder<ReportSave> q = new JpaBaseDao<>(ReportSave.class).select();
ReportSave reportSave = q.where(q.eq(ReportSave_.reportClass, reportProperties.getReportClass().getName()).and(q.eq(ReportSave_.user, reportProperties.getUsername()).and(q.eq(ReportSave_.saveType, SaveType.FREQUENTLY_USED)))).getSingleResultOrNull();
if (reportSave == null) {
reportSave = new ReportSave();
reportSave.setReportClass(reportProperties.getReportClass().getName());
reportSave.setUserDescription("Frequently used");
reportSave.setUser(reportProperties.getUsername());
reportSave.setSaveType(SaveType.FREQUENTLY_USED);
JpaBaseDao.getEntityManager().persist(reportSave);
}
reportSave.incrementRunCounter();
}
});
}
Aggregations