Search in sources :

Example 1 with JpaBaseDao

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();
        }
    });
}
Also used : JpaDslBuilder(au.com.vaadinutils.dao.JpaDslBuilder) ClickEventLogged(au.com.vaadinutils.listener.ClickEventLogged) ExpanderComponent(au.com.vaadinutils.jasper.filter.ExpanderComponent) ReportParameter(au.com.vaadinutils.jasper.parameter.ReportParameter) JpaBaseDao(au.com.vaadinutils.dao.JpaBaseDao) ClickEvent(com.vaadin.ui.Button.ClickEvent) ParseException(java.text.ParseException) ConversionException(com.vaadin.data.util.converter.Converter.ConversionException) ReadOnlyException(com.vaadin.data.Property.ReadOnlyException) Collection(java.util.Collection) List(java.util.List) LinkedList(java.util.LinkedList) ReportSave(au.com.vaadinutils.jasper.scheduler.entities.ReportSave) ReportSaveParameter(au.com.vaadinutils.jasper.scheduler.entities.ReportSaveParameter)

Aggregations

JpaBaseDao (au.com.vaadinutils.dao.JpaBaseDao)1 JpaDslBuilder (au.com.vaadinutils.dao.JpaDslBuilder)1 ExpanderComponent (au.com.vaadinutils.jasper.filter.ExpanderComponent)1 ReportParameter (au.com.vaadinutils.jasper.parameter.ReportParameter)1 ReportSave (au.com.vaadinutils.jasper.scheduler.entities.ReportSave)1 ReportSaveParameter (au.com.vaadinutils.jasper.scheduler.entities.ReportSaveParameter)1 ClickEventLogged (au.com.vaadinutils.listener.ClickEventLogged)1 ReadOnlyException (com.vaadin.data.Property.ReadOnlyException)1 ConversionException (com.vaadin.data.util.converter.Converter.ConversionException)1 ClickEvent (com.vaadin.ui.Button.ClickEvent)1 ParseException (java.text.ParseException)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1