From 55cbba8bc6af91b163e85f2bc69999965df22a6b Mon Sep 17 00:00:00 2001 From: Brendan Le Ny <bleny@codelutin.com> Date: Fri, 18 Jun 2021 15:28:30 +0200 Subject: [PATCH 1/2] =?UTF-8?q?API=20de=20t=C3=A9l=C3=A9chargement=20de=20?= =?UTF-8?q?r=C3=A9f=C3=A9rentiel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fr/inra/oresing/rest/OreSiResources.java | 11 ++++++++ .../fr/inra/oresing/rest/OreSiService.java | 28 +++++++++++++++++++ .../inra/oresing/rest/OreSiResourcesTest.java | 8 ++++++ 3 files changed, 47 insertions(+) diff --git a/src/main/java/fr/inra/oresing/rest/OreSiResources.java b/src/main/java/fr/inra/oresing/rest/OreSiResources.java index b94fbf109..fd2ad9a12 100644 --- a/src/main/java/fr/inra/oresing/rest/OreSiResources.java +++ b/src/main/java/fr/inra/oresing/rest/OreSiResources.java @@ -180,6 +180,8 @@ public class OreSiResources { @PathVariable("refType") String refType, @RequestParam MultiValueMap<String, String> params) { List<ReferenceValue> list = service.findReference(nameOrId, refType, params); + + ImmutableSet<GetReferenceResult.ReferenceValue> referenceValues = list.stream() .map(referenceValue -> new GetReferenceResult.ReferenceValue( @@ -192,6 +194,15 @@ public class OreSiResources { return ResponseEntity.ok(new GetReferenceResult(referenceValues)); } + @GetMapping(value = "/applications/{nameOrId}/references/{refType}/csv", produces = MediaType.TEXT_PLAIN_VALUE) + public ResponseEntity<String> listReferencesCsv( + @PathVariable("nameOrId") String nameOrId, + @PathVariable("refType") String refType, + @RequestParam MultiValueMap<String, String> params) { + String csv = service.getReferenceValuesCsv(nameOrId, refType, params); + return ResponseEntity.ok(csv); + } + @GetMapping(value = "/applications/{nameOrId}/references/{refType}/{column}", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<List<String>> listReferences(@PathVariable("nameOrId") String nameOrId, @PathVariable("refType") String refType, @PathVariable("column") String column) { Application application = service.getApplication(nameOrId); diff --git a/src/main/java/fr/inra/oresing/rest/OreSiService.java b/src/main/java/fr/inra/oresing/rest/OreSiService.java index 97e92e7ec..dfb47cdf5 100644 --- a/src/main/java/fr/inra/oresing/rest/OreSiService.java +++ b/src/main/java/fr/inra/oresing/rest/OreSiService.java @@ -906,6 +906,34 @@ public class OreSiService { return list; } + public String getReferenceValuesCsv(String applicationNameOrId, String referenceType, MultiValueMap<String, String> params) { + Configuration.ReferenceDescription referenceDescription = getApplication(applicationNameOrId) + .getConfiguration() + .getReferences() + .get(referenceType); + ImmutableMap<String, Function<ReferenceValue, String>> model = referenceDescription.getColumns().keySet().stream() + .collect(ImmutableMap.toImmutableMap(Function.identity(), column -> referenceValue -> referenceValue.getRefValues().get(column))); + CSVFormat csvFormat = CSVFormat.DEFAULT + .withDelimiter(referenceDescription.getSeparator()) + .withSkipHeaderRecord(); + StringWriter out = new StringWriter(); + try { + CSVPrinter csvPrinter = new CSVPrinter(out, csvFormat); + csvPrinter.printRecord(model.keySet()); + List<ReferenceValue> list = repo.getRepository(applicationNameOrId).referenceValue().findAllByReferenceType(referenceType, params); + for (ReferenceValue referenceValue : list) { + ImmutableList<String> rowAsRecord = model.values().stream() + .map(getCellContentFn -> getCellContentFn.apply(referenceValue)) + .collect(ImmutableList.toImmutableList()); + csvPrinter.printRecord(rowAsRecord); + } + } catch (IOException e) { + throw new OreSiTechnicalException("erreur lors de la génération du fichier CSV", e); + } + String csv = out.toString(); + return csv; + } + public Optional<BinaryFile> getFile(String name, UUID id) { authenticationService.setRoleForClient(); Optional<BinaryFile> optionalBinaryFile = repo.getRepository(name).binaryFile().tryFindById(id); diff --git a/src/test/java/fr/inra/oresing/rest/OreSiResourcesTest.java b/src/test/java/fr/inra/oresing/rest/OreSiResourcesTest.java index ed6123d07..1f9cac794 100644 --- a/src/test/java/fr/inra/oresing/rest/OreSiResourcesTest.java +++ b/src/test/java/fr/inra/oresing/rest/OreSiResourcesTest.java @@ -399,6 +399,14 @@ public class OreSiResourcesTest { refs = objectMapper.readValue(getReferenceResponse, GetReferenceResult.class); Assert.assertEquals(103, refs.getReferenceValues().size()); + String getReferenceCsvResponse = mockMvc.perform(get("/api/v1/applications/acbb/references/parcelles/csv") + .cookie(authCookie) + .accept(MediaType.TEXT_PLAIN)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + Assert.assertEquals(31, StringUtils.countMatches(getReferenceCsvResponse, "theix")); + // ajout de data try (InputStream in = getClass().getResourceAsStream(fixtures.getFluxToursDataResourceName())) { MockMultipartFile file = new MockMultipartFile("file", "Flux_tours.csv", "text/plain", in); -- GitLab From 72084458681f1a8eafb21d54c708b28c855fd46d Mon Sep 17 00:00:00 2001 From: Aurore Lecointe <lecointe@codelutin.com> Date: Mon, 28 Jun 2021 15:59:13 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Branche=20le=20t=C3=A9l=C3=A9chargement=20d?= =?UTF-8?q?e=20r=C3=A9f=C3=A9rentiel=20au=20back.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui2/src/services/rest/ReferenceService.js | 4 ++++ .../references/ReferencesManagementView.vue | 21 +++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/ui2/src/services/rest/ReferenceService.js b/ui2/src/services/rest/ReferenceService.js index 36c03c446..c3211dbe3 100644 --- a/ui2/src/services/rest/ReferenceService.js +++ b/ui2/src/services/rest/ReferenceService.js @@ -11,6 +11,10 @@ export class ReferenceService extends Fetcher { return this.get(`applications/${applicationName}/references/${referenceId}`); } + async getReferenceCsv(applicationName, referenceId) { + return this.downloadFile(`applications/${applicationName}/references/${referenceId}/csv`); + } + async createReference(applicationName, referenceId, refFile) { return this.post(`applications/${applicationName}/references/${referenceId}`, { file: refFile, diff --git a/ui2/src/views/references/ReferencesManagementView.vue b/ui2/src/views/references/ReferencesManagementView.vue index 295a39a5f..5ca712013 100644 --- a/ui2/src/views/references/ReferencesManagementView.vue +++ b/ui2/src/views/references/ReferencesManagementView.vue @@ -61,7 +61,9 @@ export default class ReferencesManagementView extends Vue { (label) => this.consultReference(label), "is-primary" ), - new Button(this.$t("referencesManagement.download"), "download"), + new Button(this.$t("referencesManagement.download"), "download", (label) => + this.downloadReference(label) + ), ]; created() { @@ -90,18 +92,25 @@ export default class ReferencesManagementView extends Vue { openRefDetails(event, label) { event.stopPropagation(); this.openPanel = this.chosenRef && this.chosenRef.label === label ? !this.openPanel : true; - this.chosenRef = Object.values(this.application.references).find((ref) => ref.label === label); + this.chosenRef = this.findReferenceByLabel(label); } consultReference(label) { - const ref = Object.values(this.application.references).find((ref) => ref.label === label); + const ref = this.findReferenceByLabel(label); if (ref) { this.$router.push(`/applications/${this.applicationName}/references/${ref.id}`); } } + downloadReference(label) { + const reference = this.findReferenceByLabel(label); + if (reference) { + this.referenceService.getReferenceCsv(this.applicationName, reference.id); + } + } + async uploadReferenceCsv(label, refFile) { - const reference = Object.values(this.application.references).find((ref) => ref.label === label); + const reference = this.findReferenceByLabel(label); try { await this.referenceService.createReference(this.applicationName, reference.id, refFile); this.alertService.toastSuccess(this.$t("alert.reference-updated")); @@ -109,5 +118,9 @@ export default class ReferencesManagementView extends Vue { this.alertService.toastError(this.$t("alert.reference-csv-upload-error"), error); } } + + findReferenceByLabel(label) { + return Object.values(this.application.references).find((ref) => ref.label === label); + } } </script> -- GitLab