List of usage examples for com.vaadin.v7.data Property getType
public Class<? extends T> getType();
From source file:de.symeda.sormas.ui.utils.V7GridExportStreamResource.java
License:Open Source License
public V7GridExportStreamResource(Indexed container, List<Column> gridColumns, String tempFilePrefix, String filename, String... ignoredPropertyIds) { super(new StreamSource() { @Override/*from ww w . j a v a 2 s.c o m*/ public InputStream getStream() { List<String> ignoredPropertyIdsList = Arrays.asList(ignoredPropertyIds); List<Column> columns = new ArrayList<>(gridColumns); columns.removeIf(c -> c.isHidden()); columns.removeIf(c -> ignoredPropertyIdsList.contains(c.getPropertyId())); Collection<?> itemIds = container.getItemIds(); 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.getHeaderCaption()); }); writer.writeNext(headerRow.toArray(new String[headerRow.size()])); itemIds.forEach(i -> { List<String> row = new ArrayList<>(); columns.forEach(c -> { Property<?> property = container.getItem(i).getItemProperty(c.getPropertyId()); if (property.getValue() != null) { if (property.getType() == Date.class) { row.add(DateHelper.formatLocalDateTime((Date) property.getValue())); } else if (property.getType() == Boolean.class) { if ((Boolean) property.getValue() == true) { row.add(I18nProperties.getEnumCaption(YesNoUnknown.YES)); } else row.add(I18nProperties.getEnumCaption(YesNoUnknown.NO)); } else { row.add(property.getValue().toString()); } } else { row.add(""); } }); writer.writeNext(row.toArray(new String[row.size()])); }); 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); }