Example usage for com.vaadin.ui Grid getSortOrder

List of usage examples for com.vaadin.ui Grid getSortOrder

Introduction

In this page you can find the example usage for com.vaadin.ui Grid getSortOrder.

Prototype

public List<GridSortOrder<T>> getSortOrder() 

Source Link

Document

Get the current sort order list.

Usage

From source file:de.symeda.sormas.ui.utils.GridExportStreamResource.java

License:Open Source License

public GridExportStreamResource(Grid<?> grid, String tempFilePrefix, String filename,
        String... ignoredPropertyIds) {
    super(new StreamSource() {
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override/* w ww.  j  a  va 2s .  c o m*/
        public InputStream getStream() {
            List<String> ignoredPropertyIdsList = Arrays.asList(ignoredPropertyIds);
            List<Column> columns = new ArrayList<>(grid.getColumns());
            columns.removeIf(c -> c.isHidden());
            columns.removeIf(c -> ignoredPropertyIdsList.contains(c.getId()));

            try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {
                try (CSVWriter writer = CSVUtils.createCSVWriter(
                        new OutputStreamWriter(byteStream, StandardCharsets.UTF_8.name()),
                        FacadeProvider.getConfigFacade().getCsvSeparator())) {

                    List<String> headerRow = new ArrayList<>();
                    columns.forEach(c -> {
                        headerRow.add(c.getCaption());
                    });
                    writer.writeNext(headerRow.toArray(new String[headerRow.size()]));

                    String[] rowValues = new String[columns.size()];

                    int totalRowCount = grid.getDataProvider().size(new Query());
                    for (int i = 0; i < totalRowCount; i += 100) {
                        grid.getDataProvider().fetch(new Query(i, 100, grid.getSortOrder(), null, null))
                                .forEach(row -> {
                                    for (int c = 0; c < columns.size(); c++) {
                                        Column column = columns.get(c);
                                        Object value = column.getValueProvider().apply(row);
                                        String valueString;
                                        if (value != null) {
                                            if (value instanceof Date) {
                                                valueString = DateHelper.formatLocalDateTime((Date) value);
                                            } else if (value instanceof Boolean) {
                                                if ((Boolean) value == true) {
                                                    valueString = I18nProperties
                                                            .getEnumCaption(YesNoUnknown.YES);
                                                } else
                                                    valueString = I18nProperties
                                                            .getEnumCaption(YesNoUnknown.NO);
                                            } else {
                                                valueString = value.toString();
                                            }
                                        } else {
                                            valueString = "";
                                        }
                                        rowValues[c] = valueString;
                                    }
                                    writer.writeNext(rowValues);
                                });
                        writer.flush();
                    }
                }
                return new BufferedInputStream(new ByteArrayInputStream(byteStream.toByteArray()));
            } catch (IOException e) {
                // TODO This currently requires the user to click the "Export" button again or reload the page as the UI
                // is not automatically updated; this should be changed once Vaadin push is enabled (see #516)
                new Notification(I18nProperties.getString(Strings.headingExportFailed),
                        I18nProperties.getString(Strings.messageExportFailed), Type.ERROR_MESSAGE, false)
                                .show(Page.getCurrent());
                return null;
            }
        }
    }, filename);
    setMIMEType("text/csv");
    setCacheTime(0);
}