DownloadUtil { public static StreamResource createVisitsExportStreamResource(ContactCriteria contactCriteria, final String exportFileName) { StreamResource extendedStreamResource = new StreamResource(() -> new DelayedInputStream((out) -> { try (CSVWriter writer = CSVUtils .createCSVWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8.name()), FacadeProvider.getConfigFacade().getCsvSeparator())) { final List<String> columnNames = new ArrayList<>(); final List<String> dayColumns = new ArrayList<>(); columnNames.add(I18nProperties.getPrefixCaption(ContactDto.I18N_PREFIX, ContactDto.UUID)); columnNames.add(I18nProperties.getPrefixCaption(PersonDto.I18N_PREFIX, PersonDto.FIRST_NAME)); columnNames.add(I18nProperties.getPrefixCaption(PersonDto.I18N_PREFIX, PersonDto.LAST_NAME)); dayColumns.add(""); dayColumns.add(""); dayColumns.add(""); final long maximumFollowUps = FacadeProvider.getContactFacade().countMaximumFollowUpDays(contactCriteria); for (int index = 0; index < maximumFollowUps; index++) { columnNames.add(I18nProperties.getPrefixCaption(VisitDto.I18N_PREFIX, VisitDto.VISIT_DATE_TIME)); columnNames.add(I18nProperties.getPrefixCaption(VisitDto.I18N_PREFIX, VisitDto.VISIT_STATUS)); columnNames.add(I18nProperties.getPrefixCaption(VisitDto.I18N_PREFIX, VisitDto.SYMPTOMS)); final String dayString = I18nProperties.getCaption(Captions.contactFollowUpDay) + " " + (index + 1); dayColumns.add(dayString); dayColumns.add(dayString); dayColumns.add(dayString); } writer.writeNext(columnNames.toArray(new String[columnNames.size()])); writer.writeNext(dayColumns.toArray(new String[columnNames.size()])); int startIndex = 0; List<VisitSummaryExportDto> exportRows = FacadeProvider.getContactFacade() .getVisitSummaryExportList(contactCriteria, 0, DETAILED_EXPORT_STEP_SIZE, I18nProperties.getUserLanguage()); while (!exportRows.isEmpty()) { for (VisitSummaryExportDto exportRow : exportRows) { final List<String> values = new ArrayList<>(); values.add(exportRow.getUuid()); values.add(exportRow.getFirstName()); values.add(exportRow.getLastName()); exportRow.getVisitDetails().forEach(contactVisitsDetailsExportDto -> { values.add(DateFormatHelper.formatDate(contactVisitsDetailsExportDto.getVisitDateTime())); values.add(contactVisitsDetailsExportDto.getVisitStatus().toString()); values.add(contactVisitsDetailsExportDto.getSymptoms()); }); writer.writeNext(values.toArray(new String[columnNames.size()])); } writer.flush(); startIndex += DETAILED_EXPORT_STEP_SIZE; exportRows = FacadeProvider.getContactFacade() .getVisitSummaryExportList(contactCriteria, startIndex, DETAILED_EXPORT_STEP_SIZE, I18nProperties.getUserLanguage()); } } }, e -> { VaadinSession.getCurrent() .access( () -> new Notification( I18nProperties.getString(Strings.headingExportFailed), I18nProperties.getString(Strings.messageExportFailed), Type.ERROR_MESSAGE, false).show(Page.getCurrent())); }), exportFileName); extendedStreamResource.setMIMEType("text/csv"); extendedStreamResource.setCacheTime(0); return extendedStreamResource; } private  DownloadUtil(); static StreamResource createDatabaseExportStreamResource(DatabaseExportView databaseExportView, String fileName, String mimeType); static StreamResource createGridExportStreamResource(
		Indexed container,
		List<Column> columns,
		String tempFilePrefix,
		String fileName,
		String... ignoredPropertyIds); static StreamResource createFileStreamResource(String filePath, String fileName, String mimeType, String errorTitle, String errorText); static StreamResource createStringStreamResource(String content, String fileName, String mimeType); @SuppressWarnings("serial") static StreamResource createPopulationDataExportResource(String exportFileName); static StreamResource createCaseManagementExportResource(CaseCriteria criteria, String exportFileName); static StreamResource createVisitsExportStreamResource(ContactCriteria contactCriteria, final String exportFileName); static StreamResource createCsvExportStreamResource(
		Class<T> exportRowClass,
		Enum<?> exportType,
		BiFunction<Integer, Integer, List<T>> exportRowsSupplier,
		BiFunction<String, Class<?>, String> propertyIdCaptionFunction,
		String exportFileName,
		ExportConfigurationDto exportConfiguration); static void showExportWaitDialog(AbstractComponent exportComponent, CloseListener closeListener); static String createFileNameWithCurrentDate(String fileNamePrefix, String fileExtension); static final int DETAILED_EXPORT_STEP_SIZE; }
@Test public void testCreateVisitsExportStreamResource() throws IOException { TestDataCreator.RDCF rdcf = creator.createRDCF("Region", "District", "Community", "Facility"); UserDto user = creator .createUser(rdcf.region.getUuid(), rdcf.district.getUuid(), rdcf.facility.getUuid(), "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR); String userUuid = user.getUuid(); PersonDto cazePerson = creator.createPerson("Case", "Person"); CaseDataDto caze = creator.createCase( user.toReference(), cazePerson.toReference(), Disease.EVD, CaseClassification.PROBABLE, InvestigationStatus.PENDING, new Date(), rdcf); PersonDto contactPerson = creator.createPerson("Contact", "Person"); ContactDto contact = creator.createContact(user.toReference(), user.toReference(), contactPerson.toReference(), caze, new Date(), new Date()); VisitDto visit = creator.createVisit(caze.getDisease(), contactPerson.toReference(), new Date(), VisitStatus.COOPERATIVE); visit.getSymptoms().setAbdominalPain(SymptomState.YES); FacadeProvider.getVisitFacade().saveVisit(visit); PersonDto contactPerson2 = creator.createPerson("Contact2", "Person2"); ContactDto contact2 = creator.createContact(user.toReference(), user.toReference(), contactPerson2.toReference(), caze, new Date(), null); VisitDto visit21 = creator.createVisit(caze.getDisease(), contactPerson2.toReference(), new Date(), VisitStatus.COOPERATIVE); visit21.getSymptoms().setAbdominalPain(SymptomState.YES); FacadeProvider.getVisitFacade().saveVisit(visit21); VisitDto visit22 = creator.createVisit(caze.getDisease(), contactPerson2.toReference(), new Date(), VisitStatus.COOPERATIVE); visit22.getSymptoms().setAgitation(SymptomState.YES); FacadeProvider.getVisitFacade().saveVisit(visit22); VisitDto visit23 = creator.createVisit( caze.getDisease(), contactPerson2.toReference(), DateHelper.subtractDays(new Date(), FollowUpLogic.ALLOWED_DATE_OFFSET + 1), VisitStatus.COOPERATIVE); visit23.getSymptoms().setAgitation(SymptomState.YES); FacadeProvider.getVisitFacade().saveVisit(visit23); PersonDto contactPerson3 = creator.createPerson("Contact3", "Person3"); ContactDto contact3 = creator.createContact(user.toReference(), user.toReference(), contactPerson3.toReference(), caze, new Date(), new Date()); for (int i = 0; i < 3; i++) { creator.createVisit(caze.getDisease(), contactPerson3.toReference(), new Date(), VisitStatus.COOPERATIVE); } StreamResource contactVisitsExport = DownloadUtil.createVisitsExportStreamResource(new ContactCriteria(), "test_contact_follow_up_export.csv"); Assert.assertNotNull(contactVisitsExport); Assert.assertEquals("test_contact_follow_up_export.csv", contactVisitsExport.getStream().getFileName()); InputStream stream = contactVisitsExport.getStream().getStream(); final String shortDate = DateFormatHelper.formatDate(new Date()); Assert.assertEquals( "\"Contact ID\",\"First name\",\"Last name\",\"Date and time of visit\",\"Person available and " + "cooperative?\",\"Symptoms\",\"Date and time of visit\",\"Person available and cooperative?\"," + "\"Symptoms\",\"Date and time of visit\",\"Person available and cooperative?\",\"Symptoms\"\n" + "\"\",\"\",\"\",\"Day 1\",\"Day 1\",\"Day 1\",\"Day 2\",\"Day 2\",\"Day 2\",\"Day 3\",\"Day 3\",\"Day" + " 3\"\n" + "\"" + contact.getUuid() + "\",\"Contact\",\"Person\",\"" + shortDate + "\",\"Available and cooperative\"," + "\"Abdominal pain\",,,,,,\n" + "\"" + contact2.getUuid() + "\",\"Contact2\",\"Person2\",\"" + shortDate + "\",\"Available and " + "cooperative\",\"Abdominal pain\",\"" + shortDate + "\",\"Available and cooperative\",\"\",,,\n" + "\"" + contact3.getUuid() + "\",\"Contact3\",\"Person3\",\"" + shortDate + "\",\"Available and " + "cooperative\",\"\",\"" + shortDate + "\",\"Available and cooperative\",\"\",\"" + shortDate + "\",\"Available and " + "cooperative\",\"\"\n", IOUtils.toString(stream, StandardCharsets.UTF_8.name())); }