when making a new version in dataverse delete previous files
This commit is contained in:
parent
da211ca756
commit
ed79d27cdc
|
@ -1,5 +1,6 @@
|
|||
package eu.eudat.depositinterface.dataverserepository.interfaces;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.researchspace.dataverse.api.v1.DataverseAPI;
|
||||
import com.researchspace.dataverse.api.v1.DataverseConfig;
|
||||
|
@ -13,6 +14,7 @@ import eu.eudat.depositinterface.dataverserepository.config.ConfigLoader;
|
|||
import eu.eudat.depositinterface.models.DMPDepositModel;
|
||||
import eu.eudat.depositinterface.repository.RepositoryDeposit;
|
||||
import eu.eudat.depositinterface.repository.RepositoryDepositConfiguration;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -29,6 +31,7 @@ import java.io.IOException;
|
|||
import java.io.OutputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -94,6 +97,12 @@ public class DataverseDeposit implements RepositoryDeposit {
|
|||
Map<String, Object> datasetJson = this.getDatasetIdentifier(dmpDepositModel.getPreviousDOI());
|
||||
Identifier id = new Identifier();
|
||||
id.setId(((Integer) datasetJson.get("id")).longValue());
|
||||
JsonNode jsonNode = this.objectMapper.readTree(new JSONObject(datasetJson).toString());
|
||||
List<JsonNode> files = jsonNode.findValues("dataFile");
|
||||
for(JsonNode file: files){
|
||||
int fileId = file.get("id").asInt();
|
||||
this.deleteFile(fileId);
|
||||
}
|
||||
|
||||
this.uploadFiles(dmpDepositModel, dmpDepositModel.getPreviousDOI());
|
||||
|
||||
|
@ -107,6 +116,23 @@ public class DataverseDeposit implements RepositoryDeposit {
|
|||
|
||||
}
|
||||
|
||||
private void deleteFile(int fileId){
|
||||
HttpHeaders headers = this.createBasicAuthHeaders(this.configLoader.getDataverseConfig().getApiToken(), "");
|
||||
String serverUrl = this.configLoader.getDataverseConfig().getServer() + "/dvn/api/data-deposit/v1.1/swordv2/edit-media/file/" + fileId;
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.exchange(serverUrl, HttpMethod.DELETE, new HttpEntity<>(headers), Object.class);
|
||||
}
|
||||
|
||||
private HttpHeaders createBasicAuthHeaders(String username, String password) {
|
||||
return new HttpHeaders() {{
|
||||
String auth = username + ":" + password;
|
||||
byte[] encodedAuth = Base64.getEncoder().encode(
|
||||
auth.getBytes(StandardCharsets.UTF_8));
|
||||
String authHeader = "Basic " + new String(encodedAuth);
|
||||
set("Authorization", authHeader);
|
||||
}};
|
||||
}
|
||||
|
||||
private void uploadFiles(DMPDepositModel dmpDepositModel, String doi) throws IOException {
|
||||
this.uploadFile(dmpDepositModel.getPdfFileName(), dmpDepositModel.getPdfFile(), doi);
|
||||
|
||||
|
|
Loading…
Reference in New Issue