Search in sources :

Example 1 with ExportingPanel

use of com.evolveum.midpoint.web.component.dialog.ExportingPanel in project midpoint by Evolveum.

the class CsvDownloadButtonPanel method initLayout.

private void initLayout() {
    CSVDataExporter csvDataExporter = new CSVDataExporter() {

        private static final long serialVersionUID = 1L;

        @Override
        public <T> void exportData(IDataProvider<T> dataProvider, List<IExportableColumn<T, ?>> columns, OutputStream outputStream) {
            if (dataProvider instanceof SelectableBeanContainerDataProvider) {
                // TODO implement more nicely
                ((SelectableBeanContainerDataProvider) dataProvider).setExport(true);
            }
            try {
                ((BaseSortableDataProvider) dataProvider).setExportSize(true);
                super.exportData(dataProvider, getExportableColumns(), outputStream);
                ((BaseSortableDataProvider) dataProvider).setExportSize(false);
            } catch (Exception ex) {
                LOGGER.error("Unable to export data,", ex);
            } finally {
                if (dataProvider instanceof SelectableBeanContainerDataProvider) {
                    ((SelectableBeanContainerDataProvider) dataProvider).setExport(false);
                }
            }
        }

        @Override
        protected <T> IModel<T> wrapModel(IModel<T> model) {
            if (model.getObject() == null) {
                return () -> (T) "";
            }
            if (model.getObject() instanceof Referencable) {
                return () -> {
                    String value = WebModelServiceUtils.resolveReferenceName((Referencable) model.getObject(), getPageBase());
                    return (T) (value == null ? "" : value);
                };
            }
            return super.wrapModel(model);
        }
    };
    IModel<String> name = Model.of("");
    final AbstractAjaxDownloadBehavior ajaxDownloadBehavior = new AbstractAjaxDownloadBehavior() {

        private static final long serialVersionUID = 1L;

        @Override
        public IResourceStream getResourceStream() {
            return new ExportToolbar.DataExportResourceStreamWriter(csvDataExporter, getDataTable());
        }

        public String getFileName() {
            if (StringUtils.isEmpty(name.getObject())) {
                return CsvDownloadButtonPanel.this.getFilename();
            }
            return name.getObject();
        }
    };
    add(ajaxDownloadBehavior);
    AjaxIconButton exportDataLink = new AjaxIconButton(ID_EXPORT_DATA, new Model<>("fa fa-download"), createStringResource("CsvDownloadButtonPanel.export")) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(AjaxRequestTarget target) {
            long exportSizeLimit = -1;
            try {
                CompiledGuiProfile adminGuiConfig = getPageBase().getCompiledGuiProfile();
                if (adminGuiConfig.getDefaultExportSettings() != null && adminGuiConfig.getDefaultExportSettings().getSizeLimit() != null) {
                    exportSizeLimit = adminGuiConfig.getDefaultExportSettings().getSizeLimit();
                }
            } catch (Exception ex) {
                LOGGER.error("Unable to get csv export size limit,", ex);
            }
            boolean askForSizeLimitConfirmation;
            if (exportSizeLimit < 0) {
                askForSizeLimitConfirmation = false;
            } else {
                if (canCountBeforeExporting) {
                    IDataProvider<?> dataProvider = getDataTable().getDataProvider();
                    long size = dataProvider.size();
                    askForSizeLimitConfirmation = size > exportSizeLimit;
                } else {
                    // size is unknown
                    askForSizeLimitConfirmation = true;
                }
            }
            Long useExportSizeLimit = null;
            if (askForSizeLimitConfirmation) {
                useExportSizeLimit = exportSizeLimit;
            }
            exportableColumnsIndex.clear();
            ExportingPanel exportingPanel = new ExportingPanel(getPageBase().getMainPopupBodyId(), getDataTable(), exportableColumnsIndex, useExportSizeLimit, name) {

                private static final long serialVersionUID = 1L;

                @Override
                public void exportPerformed(AjaxRequestTarget target) {
                    ajaxDownloadBehavior.initiate(target);
                }
            };
            getPageBase().showMainPopup(exportingPanel, target);
        }
    };
    add(exportDataLink);
}
Also used : Referencable(com.evolveum.midpoint.prism.Referencable) CSVDataExporter(org.apache.wicket.extensions.markup.html.repeater.data.table.export.CSVDataExporter) IModel(org.apache.wicket.model.IModel) OutputStream(java.io.OutputStream) IDataProvider(org.apache.wicket.markup.repeater.data.IDataProvider) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) BaseSortableDataProvider(com.evolveum.midpoint.web.component.data.BaseSortableDataProvider) SelectableBeanContainerDataProvider(com.evolveum.midpoint.web.component.data.SelectableBeanContainerDataProvider) AjaxIconButton(com.evolveum.midpoint.web.component.AjaxIconButton) ArrayList(java.util.ArrayList) List(java.util.List) ExportingPanel(com.evolveum.midpoint.web.component.dialog.ExportingPanel) AbstractAjaxDownloadBehavior(com.evolveum.midpoint.web.component.AbstractAjaxDownloadBehavior) CompiledGuiProfile(com.evolveum.midpoint.model.api.authentication.CompiledGuiProfile)

Aggregations

CompiledGuiProfile (com.evolveum.midpoint.model.api.authentication.CompiledGuiProfile)1 Referencable (com.evolveum.midpoint.prism.Referencable)1 AbstractAjaxDownloadBehavior (com.evolveum.midpoint.web.component.AbstractAjaxDownloadBehavior)1 AjaxIconButton (com.evolveum.midpoint.web.component.AjaxIconButton)1 BaseSortableDataProvider (com.evolveum.midpoint.web.component.data.BaseSortableDataProvider)1 SelectableBeanContainerDataProvider (com.evolveum.midpoint.web.component.data.SelectableBeanContainerDataProvider)1 ExportingPanel (com.evolveum.midpoint.web.component.dialog.ExportingPanel)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)1 CSVDataExporter (org.apache.wicket.extensions.markup.html.repeater.data.table.export.CSVDataExporter)1 IDataProvider (org.apache.wicket.markup.repeater.data.IDataProvider)1 IModel (org.apache.wicket.model.IModel)1