[WIP] implement dataverse repository deposit

This commit is contained in:
Bernaldo Mihasi 2022-11-10 10:12:11 +02:00
parent 6b2bc3c1e0
commit b4490cb0f3
8 changed files with 229 additions and 36 deletions

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/>
</parent>
<groupId>eu.eudat.depositinterface</groupId>
<artifactId>dataverseRepository</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dataverseRepository</name>
<properties>
<java.version>11</java.version>
</properties>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>eu.eudat</groupId>
<artifactId>depositinterface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.IQSS</groupId>
<artifactId>dataverse-client-java</artifactId>
<version>master-dv14-12-compatible-ge2dc343-7</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,159 @@
package eu.eudat.depositinterface.dataverserepository.interfaces;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.researchspace.dataverse.api.v1.DataverseAPI;
import com.researchspace.dataverse.api.v1.DataverseConfig;
import com.researchspace.dataverse.entities.DataverseContacts;
import com.researchspace.dataverse.entities.DataversePost;
import com.researchspace.dataverse.entities.DataverseResponse;
import com.researchspace.dataverse.entities.Identifier;
import com.researchspace.dataverse.entities.facade.DatasetAuthor;
import com.researchspace.dataverse.entities.facade.DatasetContact;
import com.researchspace.dataverse.entities.facade.DatasetDescription;
import com.researchspace.dataverse.entities.facade.DatasetFacade;
import com.researchspace.dataverse.http.DataverseAPIImpl;
import eu.eudat.depositinterface.models.DMPDepositModel;
import eu.eudat.depositinterface.repository.RepositoryDeposit;
import eu.eudat.depositinterface.repository.RepositoryDepositConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Objects;
import java.util.stream.Collectors;
import static eu.eudat.depositinterface.repository.RepositoryDepositConfiguration.DepositAccountStatus.SystemDeposit;
@Component
public class DataverseDeposit implements RepositoryDeposit {
private static final Logger logger = LoggerFactory.getLogger(DataverseDeposit.class);
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final String API_TOKEN = "";
private static final String SYSTEM_PARENT_DATAVERSE_ALIAS = "";
private static final String SERVER = "";
private static final String SYSTEM_EMAIL = "";
private DataverseAPI api;
private boolean isApiSet;
private Environment environment;
@Autowired
public DataverseDeposit(Environment environment){
this.environment = environment;
this.isApiSet = false;
}
private void setDataverseApi() throws MalformedURLException {
if(!this.isApiSet) {
this.api = new DataverseAPIImpl();
DataverseConfig config = new DataverseConfig(new URL(SERVER), API_TOKEN, SYSTEM_PARENT_DATAVERSE_ALIAS);
api.configure(config);
this.isApiSet = true;
}
}
@Override
public String deposit(DMPDepositModel dmpDepositModel, boolean update, String repositoryAccessToken) throws Exception {
if(!this.isApiSet)
this.setDataverseApi();
DataversePost entity = new DataversePost();
entity.setName(dmpDepositModel.getLabel());
entity.setAlias(dmpDepositModel.getLabel().replace(" ", "_"));
entity.setDescription(dmpDepositModel.getDescription());
entity.setCreationDate(new Date());
entity.setDataverseContacts(Collections.singletonList(new DataverseContacts(SYSTEM_EMAIL)));
DataverseResponse<DataversePost> response = this.api.getDataverseOperations().createNewDataverse(SYSTEM_PARENT_DATAVERSE_ALIAS, entity);
DatasetFacade dataset = DatasetFacade.builder()
.title(dmpDepositModel.getLabel())
.authors(dmpDepositModel.getUsers().stream().map(x -> DatasetAuthor.builder().authorName(x.getUser().getName()).build()).collect(Collectors.toList()))
.contacts(dmpDepositModel.getUsers().stream().map(x -> DatasetContact.builder().datasetContactEmail(x.getUser().getEmail()).build()).collect(Collectors.toList()))
.subject("Other")
.description(DatasetDescription.builder().description(dmpDepositModel.getDescription()).build())
.languages(new ArrayList<>())
.depositor("")
.build();
Identifier id = this.api.getDataverseOperations().createDataset(dataset, entity.getAlias());
String doi = this.api.getDatasetOperations().getDataset(id).getDoiId().orElse(null);
this.uploadFile(dmpDepositModel.getPdfFileName(), dmpDepositModel.getPdfFile(), doi);
String contentDisposition = dmpDepositModel.getRdaJson().getHeaders().get("Content-Disposition").get(0);
String jsonFileName = contentDisposition.substring(contentDisposition.lastIndexOf('=') + 1);
File rdaJson = new File(this.environment.getProperty("storage.temp") + jsonFileName);
OutputStream output = new FileOutputStream(rdaJson);
try {
output.write(Objects.requireNonNull(dmpDepositModel.getRdaJson().getBody()));
output.flush();
output.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
this.uploadFile(jsonFileName, rdaJson, doi);
Files.deleteIfExists(rdaJson.toPath());
return doi;
}
private void uploadFile(String filename, File file, String doi) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.set("X-Dataverse-key", API_TOKEN);
MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
ContentDisposition contentDisposition = ContentDisposition
.builder("form-data")
.name("file")
.filename(filename)
.build();
fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
HttpEntity<byte[]> fileEntity = new HttpEntity<>(Files.readAllBytes(file.toPath()), fileMap);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", fileEntity);
body.add("jsonData", "{\"restrict\":\"true\", \"tabIngest\":\"false\"}");
HttpEntity<MultiValueMap<String, Object>> requestEntity
= new HttpEntity<>(body, headers);
String serverUrl = SERVER + "/api/datasets/:persistentId/add?persistentId=doi:" + doi;
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Object> resp = restTemplate.postForEntity(serverUrl, requestEntity, Object.class);
}
@Override
public RepositoryDepositConfiguration getConfiguration() {
RepositoryDepositConfiguration conf = new RepositoryDepositConfiguration();
conf.setRepositoryId("Dataverse");
conf.setDepositAccountStatus(SystemDeposit.getValue());
conf.setAccessToken(API_TOKEN);
conf.setRepositoryUrl(SERVER + "/api/");
conf.setRepositoryRecordUrl(SERVER + "/dataverse/");
return conf;
}
@Override
public String authenticate(String code) {
return null;
}
}

View File

@ -0,0 +1 @@
storage.temp=

View File

@ -2,6 +2,7 @@ package eu.eudat.depositinterface.models;
public class UserInfoDepositModel { public class UserInfoDepositModel {
private String name; private String name;
private String email;
public String getName() { public String getName() {
return name; return name;
@ -9,4 +10,11 @@ public class UserInfoDepositModel {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
} }

View File

@ -2115,14 +2115,9 @@ public class DataManagementPlanManager {
throw new Exception("DMP already has a DOI");*/ throw new Exception("DMP already has a DOI");*/
FileEnvelope file = getWordDocument(depositRequest.getDmpId(), principal, configLoader); FileEnvelope file = getWordDocument(depositRequest.getDmpId(), principal, configLoader);
String name = file.getFilename().substring(0, file.getFilename().length() - 5); String name = file.getFilename().substring(0, file.getFilename().length() - 5).replace(" ", "_").replace(",", "_");
//File pdfFile = PDFUtils.convertToPDF(file, environment); File pdfFile = PDFUtils.convertToPDF(file, environment);
String uuid = UUID.randomUUID().toString();
File pdfFile = new File(environment.getProperty("temp.temp") + uuid + ".pdf");
FileOutputStream output = new FileOutputStream(pdfFile);
IOUtils.write("testing".getBytes(StandardCharsets.UTF_8), output);
String fileName = name + ".pdf"; String fileName = name + ".pdf";
output.close();
ResponseEntity<byte[]> jsonFile; ResponseEntity<byte[]> jsonFile;
try { try {
jsonFile = getRDAJsonDocument(depositRequest.getDmpId(), principal); jsonFile = getRDAJsonDocument(depositRequest.getDmpId(), principal);

View File

@ -40,6 +40,7 @@ public class DMPToDepositMapper {
private static UserInfoDepositModel fromUserInfo(UserInfo entity){ private static UserInfoDepositModel fromUserInfo(UserInfo entity){
UserInfoDepositModel deposit = new UserInfoDepositModel(); UserInfoDepositModel deposit = new UserInfoDepositModel();
deposit.setName(entity.getName()); deposit.setName(entity.getName());
deposit.setEmail(entity.getEmail());
return deposit; return deposit;
} }

View File

@ -1,29 +0,0 @@
package eu.eudat.depositinterface.dummyrepository;
import eu.eudat.depositinterface.models.DMPDepositModel;
import eu.eudat.depositinterface.repository.RepositoryDeposit;
import eu.eudat.depositinterface.repository.RepositoryDepositConfiguration;
import org.springframework.stereotype.Component;
import static eu.eudat.depositinterface.repository.RepositoryDepositConfiguration.DepositAccountStatus.BothWaysDeposit;
@Component
public class TestDummy implements RepositoryDeposit {
@Override
public String deposit(DMPDepositModel dmpDepositModel, boolean update, String repositoryAccessToken) throws Exception {
return "test2";
}
@Override
public RepositoryDepositConfiguration getConfiguration() {
RepositoryDepositConfiguration conf = new RepositoryDepositConfiguration();
conf.setRepositoryId("Dummy");
conf.setDepositAccountStatus(BothWaysDeposit.getValue());
return conf;
}
@Override
public String authenticate(String code) {
return "accessToken";
}
}

View File

@ -93,6 +93,18 @@ export class DmpDepositDialogComponent extends BaseComponent implements OnInit {
}); });
} }
else if(repo.depositAccountStatus == DepositConfigurationStatus.System){
this.depositRepositoriesService.createDoi(repo.repositoryId, this.dmp.id, null)
.pipe(takeUntil(this._destroyed))
.subscribe(doi =>{
this.onDOICallbackSuccess();
this.outputRepos.push(doi);
this.inputRepos = this.inputRepos.filter(function(x) { return x.repositoryId !== repo.repositoryId; });
if(this.inputRepos.length == 0){
this.close();
}
}, error => this.onDOICallbackError(error));
}
} }
onDOICallbackSuccess(): void { onDOICallbackSuccess(): void {