@Test public void export() throws IOException, HodErrorException { final HodSearchResult result1 = HodSearchResult.builder() .reference("1") .index("ClassicalDomain:GreekLiterature") .title("The Iliad") .summary("Sing goddess of the anger of Achilles") .weight(0.51) .date(ZonedDateTime.now()) .fieldMap(ImmutableMap.of( "author", fieldInfo("authors", "author", FieldType.STRING, "Homer"), "books", fieldInfo("books", "books", FieldType.NUMBER, 24), "epic", fieldInfo("epic", "epic", FieldType.BOOLEAN, true), "lastRead", fieldInfo("lastRead", "lastRead", FieldType.DATE, ZonedDateTime.now()))) .build(); final HodSearchResult result2 = HodSearchResult.builder() .reference("2") .index("ClassicalDomain:GreekLiterature") .title("The Theogony") .summary("Inspired by the Muses of Mount Helicon let us sing") .weight(0.62) .date(ZonedDateTime.now()) .fieldMap(ImmutableMap.of("categories", FieldInfo.builder() .id("categories") .name(fieldPathNormaliser.normaliseFieldPath("category")) .value(new FieldValue<>("Epic Literature", "Epic Literature")) .value(new FieldValue<>("Philosophy", "Philosophy")) .value(new FieldValue<>("Cosmogony", "Cosmogony")) .build())) .build(); final Documents<HodSearchResult> results = new Documents<>(Arrays.asList(result1, result2), 2, null, null, null, null); when(documentsService.queryTextIndex(any())).thenReturn(results); hodExportService.exportQueryResults(outputStream, queryRequest, ExportFormat.CSV, Collections.emptyList(), 10L); verify(exportStrategy, times(2)).exportRecord(eq(outputStream), anyListOf(String.class)); }
@Override public void exportQueryResults(final OutputStream outputStream, final HodQueryRequest queryRequest, final ExportFormat exportFormat, final Collection<String> selectedFieldIds, final long totalResults) throws HodErrorException { final PlatformDataExportStrategy exportStrategy = exportStrategies.get(exportFormat); final List<FieldInfo<?>> fieldInfoList = exportStrategy.getFieldNames(HodMetadataNode.values(), selectedFieldIds); final List<String> fieldIds = fieldInfoList.stream() .map(FieldInfo::getId) .collect(Collectors.toList()); try { exportStrategy.writeHeader(outputStream, fieldInfoList); for (int i = 0; i < totalResults; i += PAGINATION_SIZE) { final HodQueryRequest paginatedQueryRequest = queryRequest.toBuilder() .start(i + 1) .maxResults(Math.min(i + PAGINATION_SIZE, HodDocumentsService.HOD_MAX_RESULTS)) .build(); final Documents<HodSearchResult> documents = documentsService.queryTextIndex(paginatedQueryRequest.toBuilder() .printFields(fieldIds) .build()); final List<Function<HodSearchResult, String>> exportMetadataFunctions = Arrays.stream(HodMetadataNode.values()) .filter(node -> selectedFieldIds.isEmpty() || selectedFieldIds.contains(node.getName())) .map(node -> (Function<HodSearchResult, String>) hodSearchResult -> { final Object value = node.getGetter().apply(hodSearchResult); return value == null ? "" : value.toString(); }) .collect(Collectors.toList()); for (final HodSearchResult searchResult : documents.getDocuments()) { final Stream<String> metadataStream = exportMetadataFunctions.stream() .map(extractor -> extractor.apply(searchResult)); final Stream<String> nonMetadataStream = exportStrategy.getConfiguredFieldsById().values().stream() .filter(configuredField -> selectedFieldIds.isEmpty() || selectedFieldIds.contains(configuredField.getId())) .map(configuredField -> { final FieldInfo<?> fieldInfo = searchResult.getFieldMap().get(configuredField.getId()); return exportStrategy.combineValues(getValuesAsStrings(fieldInfo)); }); exportStrategy.exportRecord(outputStream, Stream.concat(metadataStream, nonMetadataStream).collect(Collectors.toList())); } } } catch (final IOException e) { throw new RuntimeException("Error parsing data", e); } }
HodPlatformDataExportService implements PlatformDataExportService<HodQueryRequest, HodErrorException> { @Override public void exportQueryResults(final OutputStream outputStream, final HodQueryRequest queryRequest, final ExportFormat exportFormat, final Collection<String> selectedFieldIds, final long totalResults) throws HodErrorException { final PlatformDataExportStrategy exportStrategy = exportStrategies.get(exportFormat); final List<FieldInfo<?>> fieldInfoList = exportStrategy.getFieldNames(HodMetadataNode.values(), selectedFieldIds); final List<String> fieldIds = fieldInfoList.stream() .map(FieldInfo::getId) .collect(Collectors.toList()); try { exportStrategy.writeHeader(outputStream, fieldInfoList); for (int i = 0; i < totalResults; i += PAGINATION_SIZE) { final HodQueryRequest paginatedQueryRequest = queryRequest.toBuilder() .start(i + 1) .maxResults(Math.min(i + PAGINATION_SIZE, HodDocumentsService.HOD_MAX_RESULTS)) .build(); final Documents<HodSearchResult> documents = documentsService.queryTextIndex(paginatedQueryRequest.toBuilder() .printFields(fieldIds) .build()); final List<Function<HodSearchResult, String>> exportMetadataFunctions = Arrays.stream(HodMetadataNode.values()) .filter(node -> selectedFieldIds.isEmpty() || selectedFieldIds.contains(node.getName())) .map(node -> (Function<HodSearchResult, String>) hodSearchResult -> { final Object value = node.getGetter().apply(hodSearchResult); return value == null ? "" : value.toString(); }) .collect(Collectors.toList()); for (final HodSearchResult searchResult : documents.getDocuments()) { final Stream<String> metadataStream = exportMetadataFunctions.stream() .map(extractor -> extractor.apply(searchResult)); final Stream<String> nonMetadataStream = exportStrategy.getConfiguredFieldsById().values().stream() .filter(configuredField -> selectedFieldIds.isEmpty() || selectedFieldIds.contains(configuredField.getId())) .map(configuredField -> { final FieldInfo<?> fieldInfo = searchResult.getFieldMap().get(configuredField.getId()); return exportStrategy.combineValues(getValuesAsStrings(fieldInfo)); }); exportStrategy.exportRecord(outputStream, Stream.concat(metadataStream, nonMetadataStream).collect(Collectors.toList())); } } } catch (final IOException e) { throw new RuntimeException("Error parsing data", e); } } }
HodPlatformDataExportService implements PlatformDataExportService<HodQueryRequest, HodErrorException> { @Override public void exportQueryResults(final OutputStream outputStream, final HodQueryRequest queryRequest, final ExportFormat exportFormat, final Collection<String> selectedFieldIds, final long totalResults) throws HodErrorException { final PlatformDataExportStrategy exportStrategy = exportStrategies.get(exportFormat); final List<FieldInfo<?>> fieldInfoList = exportStrategy.getFieldNames(HodMetadataNode.values(), selectedFieldIds); final List<String> fieldIds = fieldInfoList.stream() .map(FieldInfo::getId) .collect(Collectors.toList()); try { exportStrategy.writeHeader(outputStream, fieldInfoList); for (int i = 0; i < totalResults; i += PAGINATION_SIZE) { final HodQueryRequest paginatedQueryRequest = queryRequest.toBuilder() .start(i + 1) .maxResults(Math.min(i + PAGINATION_SIZE, HodDocumentsService.HOD_MAX_RESULTS)) .build(); final Documents<HodSearchResult> documents = documentsService.queryTextIndex(paginatedQueryRequest.toBuilder() .printFields(fieldIds) .build()); final List<Function<HodSearchResult, String>> exportMetadataFunctions = Arrays.stream(HodMetadataNode.values()) .filter(node -> selectedFieldIds.isEmpty() || selectedFieldIds.contains(node.getName())) .map(node -> (Function<HodSearchResult, String>) hodSearchResult -> { final Object value = node.getGetter().apply(hodSearchResult); return value == null ? "" : value.toString(); }) .collect(Collectors.toList()); for (final HodSearchResult searchResult : documents.getDocuments()) { final Stream<String> metadataStream = exportMetadataFunctions.stream() .map(extractor -> extractor.apply(searchResult)); final Stream<String> nonMetadataStream = exportStrategy.getConfiguredFieldsById().values().stream() .filter(configuredField -> selectedFieldIds.isEmpty() || selectedFieldIds.contains(configuredField.getId())) .map(configuredField -> { final FieldInfo<?> fieldInfo = searchResult.getFieldMap().get(configuredField.getId()); return exportStrategy.combineValues(getValuesAsStrings(fieldInfo)); }); exportStrategy.exportRecord(outputStream, Stream.concat(metadataStream, nonMetadataStream).collect(Collectors.toList())); } } } catch (final IOException e) { throw new RuntimeException("Error parsing data", e); } } @Autowired  HodPlatformDataExportService(final HodDocumentsService documentsService,
                                        final PlatformDataExportStrategy[] exportStrategies); }
HodPlatformDataExportService implements PlatformDataExportService<HodQueryRequest, HodErrorException> { @Override public void exportQueryResults(final OutputStream outputStream, final HodQueryRequest queryRequest, final ExportFormat exportFormat, final Collection<String> selectedFieldIds, final long totalResults) throws HodErrorException { final PlatformDataExportStrategy exportStrategy = exportStrategies.get(exportFormat); final List<FieldInfo<?>> fieldInfoList = exportStrategy.getFieldNames(HodMetadataNode.values(), selectedFieldIds); final List<String> fieldIds = fieldInfoList.stream() .map(FieldInfo::getId) .collect(Collectors.toList()); try { exportStrategy.writeHeader(outputStream, fieldInfoList); for (int i = 0; i < totalResults; i += PAGINATION_SIZE) { final HodQueryRequest paginatedQueryRequest = queryRequest.toBuilder() .start(i + 1) .maxResults(Math.min(i + PAGINATION_SIZE, HodDocumentsService.HOD_MAX_RESULTS)) .build(); final Documents<HodSearchResult> documents = documentsService.queryTextIndex(paginatedQueryRequest.toBuilder() .printFields(fieldIds) .build()); final List<Function<HodSearchResult, String>> exportMetadataFunctions = Arrays.stream(HodMetadataNode.values()) .filter(node -> selectedFieldIds.isEmpty() || selectedFieldIds.contains(node.getName())) .map(node -> (Function<HodSearchResult, String>) hodSearchResult -> { final Object value = node.getGetter().apply(hodSearchResult); return value == null ? "" : value.toString(); }) .collect(Collectors.toList()); for (final HodSearchResult searchResult : documents.getDocuments()) { final Stream<String> metadataStream = exportMetadataFunctions.stream() .map(extractor -> extractor.apply(searchResult)); final Stream<String> nonMetadataStream = exportStrategy.getConfiguredFieldsById().values().stream() .filter(configuredField -> selectedFieldIds.isEmpty() || selectedFieldIds.contains(configuredField.getId())) .map(configuredField -> { final FieldInfo<?> fieldInfo = searchResult.getFieldMap().get(configuredField.getId()); return exportStrategy.combineValues(getValuesAsStrings(fieldInfo)); }); exportStrategy.exportRecord(outputStream, Stream.concat(metadataStream, nonMetadataStream).collect(Collectors.toList())); } } } catch (final IOException e) { throw new RuntimeException("Error parsing data", e); } } @Autowired  HodPlatformDataExportService(final HodDocumentsService documentsService,
                                        final PlatformDataExportStrategy[] exportStrategies); @Override void exportQueryResults(final OutputStream outputStream, final HodQueryRequest queryRequest, final ExportFormat exportFormat, final Collection<String> selectedFieldIds, final long totalResults); @Override Collection<ExportFormat> handlesFormats(); }
HodPlatformDataExportService implements PlatformDataExportService<HodQueryRequest, HodErrorException> { @Override public void exportQueryResults(final OutputStream outputStream, final HodQueryRequest queryRequest, final ExportFormat exportFormat, final Collection<String> selectedFieldIds, final long totalResults) throws HodErrorException { final PlatformDataExportStrategy exportStrategy = exportStrategies.get(exportFormat); final List<FieldInfo<?>> fieldInfoList = exportStrategy.getFieldNames(HodMetadataNode.values(), selectedFieldIds); final List<String> fieldIds = fieldInfoList.stream() .map(FieldInfo::getId) .collect(Collectors.toList()); try { exportStrategy.writeHeader(outputStream, fieldInfoList); for (int i = 0; i < totalResults; i += PAGINATION_SIZE) { final HodQueryRequest paginatedQueryRequest = queryRequest.toBuilder() .start(i + 1) .maxResults(Math.min(i + PAGINATION_SIZE, HodDocumentsService.HOD_MAX_RESULTS)) .build(); final Documents<HodSearchResult> documents = documentsService.queryTextIndex(paginatedQueryRequest.toBuilder() .printFields(fieldIds) .build()); final List<Function<HodSearchResult, String>> exportMetadataFunctions = Arrays.stream(HodMetadataNode.values()) .filter(node -> selectedFieldIds.isEmpty() || selectedFieldIds.contains(node.getName())) .map(node -> (Function<HodSearchResult, String>) hodSearchResult -> { final Object value = node.getGetter().apply(hodSearchResult); return value == null ? "" : value.toString(); }) .collect(Collectors.toList()); for (final HodSearchResult searchResult : documents.getDocuments()) { final Stream<String> metadataStream = exportMetadataFunctions.stream() .map(extractor -> extractor.apply(searchResult)); final Stream<String> nonMetadataStream = exportStrategy.getConfiguredFieldsById().values().stream() .filter(configuredField -> selectedFieldIds.isEmpty() || selectedFieldIds.contains(configuredField.getId())) .map(configuredField -> { final FieldInfo<?> fieldInfo = searchResult.getFieldMap().get(configuredField.getId()); return exportStrategy.combineValues(getValuesAsStrings(fieldInfo)); }); exportStrategy.exportRecord(outputStream, Stream.concat(metadataStream, nonMetadataStream).collect(Collectors.toList())); } } } catch (final IOException e) { throw new RuntimeException("Error parsing data", e); } } @Autowired  HodPlatformDataExportService(final HodDocumentsService documentsService,
                                        final PlatformDataExportStrategy[] exportStrategies); @Override void exportQueryResults(final OutputStream outputStream, final HodQueryRequest queryRequest, final ExportFormat exportFormat, final Collection<String> selectedFieldIds, final long totalResults); @Override Collection<ExportFormat> handlesFormats();  }