From 1bc653f3a24ccffce1b52f111c0671de54306c3e Mon Sep 17 00:00:00 2001 From: Thomas Jejkal Date: Tue, 25 Jul 2023 14:27:47 +0200 Subject: [PATCH] Fix #26, deleting mappings is now possible --- .../impl/MappingAdministrationController.java | 6 +- src/main/resources/static/JS/listSchemes.js | 62 ++++++++++--------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/main/java/edu/kit/datamanager/mappingservice/rest/impl/MappingAdministrationController.java b/src/main/java/edu/kit/datamanager/mappingservice/rest/impl/MappingAdministrationController.java index 558560a..cdb1bff 100644 --- a/src/main/java/edu/kit/datamanager/mappingservice/rest/impl/MappingAdministrationController.java +++ b/src/main/java/edu/kit/datamanager/mappingservice/rest/impl/MappingAdministrationController.java @@ -219,8 +219,12 @@ public ResponseEntity getMappingDocumentById( LOG.trace("Mapping document at path {} either does not exist or is no file or is not readable. Returning HTTP INTERNAL_SERVER_ERROR.", mappingDocumentPath); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Metadata document on server either does not exist or is no file or is not readable."); } + + LOG.trace("Get ETag of MappingRecord."); + String etag = record.getEtag(); + LOG.trace("Mapping document found. Returning result."); - return ResponseEntity.ok().header(HttpHeaders.CONTENT_LENGTH, String.valueOf(mappingDocumentPath.toFile().length())).body(new FileSystemResource(mappingDocumentPath.toFile())); + return ResponseEntity.ok().eTag("\"" + etag + "\"").header(HttpHeaders.CONTENT_LENGTH, String.valueOf(mappingDocumentPath.toFile().length())).body(new FileSystemResource(mappingDocumentPath.toFile())); } @Override diff --git a/src/main/resources/static/JS/listSchemes.js b/src/main/resources/static/JS/listSchemes.js index c18cf50..56aef5b 100644 --- a/src/main/resources/static/JS/listSchemes.js +++ b/src/main/resources/static/JS/listSchemes.js @@ -7,59 +7,60 @@ function getRecords() { http.open("GET", apiUrl); http.send(); http.onprogress = () => { - document.getElementById("progress").hidden = false + document.getElementById("progress").hidden = false; } http.onload = (e) => { const results = JSON.parse(http.responseText); if (results.length > 0) { document.getElementById("nothingHere").hidden = true; - document.getElementById("progress").hidden = true + document.getElementById("progress").hidden = true; } for (let i = 0; i < results.length; i++) { - document.getElementById("progress").hidden = false - console.log(results[i].mappingId) - const schemaHttp = new XMLHttpRequest() + document.getElementById("progress").hidden = false; + console.log(results[i].mappingId); + const schemaHttp = new XMLHttpRequest(); var schema var ETAG - schemaHttp.open("GET", apiUrl + results[i].mappingId) - schemaHttp.setRequestHeader("Content-Type", "application/json") - schemaHttp.send() + schemaHttp.open("GET", apiUrl + results[i].mappingId); + schemaHttp.setRequestHeader("Content-Type", "application/json"); + schemaHttp.send(); schemaHttp.onload = (e) => { - schema = JSON.parse(schemaHttp.responseText) - ETAG = '"' + schemaHttp.getResponseHeader("If-Match") + '"' - console.log("Received Data:" + { + schema = JSON.parse(schemaHttp.responseText); + ETAG = schemaHttp.getResponseHeader("ETag"); + console.log("Received Data:"); + console.log({ "record": results[i], "schema": schema, "ETAG": ETAG - }) + }); records.set(results[i].mappingId, { "record": results[i], "schema": schema, "ETAG": ETAG }) - console.log(records) - addListElement(results[i].mappingId, results[i].mappingType, results[i].title, results[i].description) - } + console.log(records); + addListElement(results[i].mappingId, results[i].mappingType, results[i].title, results[i].description); + }; } - document.getElementById("progress").hidden = true - } - document.getElementById("progress").hidden = true + document.getElementById("progress").hidden = true; + }; + document.getElementById("progress").hidden = true; } function mapWithMapping(id) { const data = { id: id, type: records.get(id).record.mappingType - } - window.sessionStorage.setItem("data", JSON.stringify(data)) - window.location = "mapDocument.html" + }; + window.sessionStorage.setItem("data", JSON.stringify(data)); + window.location = "mapDocument.html"; } function editMapping(id) { let sessionData = JSON.stringify(records.get(id)) window.sessionStorage.setItem("data", sessionData) - window.location = "./addScheme.html" + window.location = "./addScheme.html"; } function downloadMapping(id) { @@ -74,22 +75,23 @@ function downloadMapping(id) { document.body.appendChild(element); element.click(); document.body.removeChild(element); - } + }; } function deleteMapping(id) { let mapEntry = records.get(id); - if (mapEntry != null && mapEntry.record.mappingId === id) { + console.log("Deleting mapping with id " + id); + if (mapEntry !== null && mapEntry.record.mappingId === id) { const http = new XMLHttpRequest(); - http.open("DELETE", apiUrl + id) - http.setRequestHeader("If-Match", mapEntry.ETAG) + http.open("DELETE", apiUrl + id); + http.setRequestHeader("If-Match", mapEntry.ETAG); http.send(); http.onload = (e) => { - records.delete(id) - document.getElementById(id).remove() + records.delete(id); + document.getElementById(id).remove(); if (records.size < 1) document.getElementById("nothingHere").hidden = false; - console.log("Successfully deleted mapping with id " + id) - } + console.log("Successfully deleted mapping with id " + id); + }; } }