Implementing library

This commit is contained in:
Luca Frosini 2023-01-03 15:35:54 +01:00
parent 8d44387aab
commit e0cff5e51b
9 changed files with 168 additions and 36 deletions

View File

@ -1,8 +1,10 @@
package org.gcube.common.deposition;
import java.io.File;
import java.text.SimpleDateFormat;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.SerializationFeature;
import org.gcube.common.deposition.model.Deposition;
import org.gcube.common.deposition.model.DepositionVersion;
import org.slf4j.Logger;
@ -19,6 +21,10 @@ public class ElaborateDeposition {
public ElaborateDeposition() {
this.objectMapper = new ObjectMapper();
SimpleDateFormat sdf = new SimpleDateFormat(DepositionVersion.DATETIME_PATTERN);
this.objectMapper.setDateFormat(sdf);
this.objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
}
public void publish(File jsonFile) throws Exception {
@ -43,7 +49,7 @@ public class ElaborateDeposition {
name, depositionVersion.getVersion(),
depositionVersion.getPrevious()!=null ? depositionVersion.getPrevious().getVersion(): null);
ElaborateDepositionVersion elaborateDeposition = new ElaborateDepositionVersion(depositionVersion);
ElaborateDepositionVersion elaborateDeposition = new ElaborateDepositionVersion(objectMapper, depositionVersion);
elaborateDeposition.elaborate();
previous = depositionVersion;

View File

@ -1,8 +1,16 @@
package org.gcube.common.deposition;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.common.deposition.config.Config;
import org.gcube.common.deposition.executor.DepositionVersionExecutor;
import org.gcube.common.deposition.model.Deposition;
import org.gcube.common.deposition.model.DepositionFile;
import org.gcube.common.deposition.model.DepositionVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -14,26 +22,89 @@ public class ElaborateDepositionVersion {
private static final Logger logger = LoggerFactory.getLogger(ElaborateDeposition.class);
protected ObjectMapper objectMapper;
protected DepositionVersion depositionVersion;
public ElaborateDepositionVersion(DepositionVersion depositionVersion) {
public ElaborateDepositionVersion(ObjectMapper objectMapper, DepositionVersion depositionVersion) {
this.objectMapper = objectMapper;
this.depositionVersion = depositionVersion;
}
protected String replaceVariables(String stringToAnalise) {
Deposition d = depositionVersion.getDeposition();
String s = stringToAnalise.replaceAll("\\{\\{name\\}\\}", d.getName());
s = s.replaceAll("\\{\\{version\\}\\}", depositionVersion.getVersion());
s = s.replaceAll("\\{\\{date\\}\\}", DepositionVersion.getDateAsString(depositionVersion.getDate()));
return s;
}
protected JsonNode getMetadata() {
// TODO replace variables in metadata and add variables part is any
// TODO replace variables in metadata and add variables part if any
return null;
}
protected void addDefaultsToDepositionVersion() {
// TODO add the missing content with
protected List<DepositionFile> getDefaultFiles(){
List<DepositionFile> defaultFiles = depositionVersion.getDeposition().getDefaultFiles();
List<DepositionFile> files = new ArrayList<>();
for(DepositionFile f : defaultFiles) {
DepositionFile file = new DepositionFile();
file.setDesiredName(replaceVariables(f.getDesiredName()));
try {
file.setURL(replaceVariables(f.getURL().toString()));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
files.add(file);
}
return files;
}
public void elaborate() {
protected void replaceVariablesInfiles() {
List<DepositionFile> files = depositionVersion.getFiles();
for(DepositionFile file : files) {
file.setDesiredName(replaceVariables(file.getDesiredName()));
try {
file.setURL(replaceVariables(file.getURL().toString()));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
}
protected String getDefaultCodeLocation() {
String defaultCodeLocation = depositionVersion.getDeposition().getDefaultCodeLocation();
return replaceVariables(defaultCodeLocation);
}
protected void addDefaultsToDepositionVersion() throws MalformedURLException {
String version = depositionVersion.getVersion();
if(version==null || version.length()==0) {
throw new RuntimeException("Invalid version");
}
List<DepositionFile> files = depositionVersion.getFiles();
if(files==null || files.size()==0) {
depositionVersion.setFiles(getDefaultFiles());
}else {
replaceVariablesInfiles();
}
if(depositionVersion.getCodeLocation()==null && !depositionVersion.getNoCodeLocation()) {
depositionVersion.setCodeLocation(getDefaultCodeLocation());
}
}
public void elaborate() throws Exception {
String name = depositionVersion.getDeposition().getName();
logger.debug("DepositionVersion as is:\n{}", objectMapper.writeValueAsString(depositionVersion));
addDefaultsToDepositionVersion();
logger.debug("DepositionVersion wit defaults:\n{}", objectMapper.writeValueAsString(depositionVersion));
DepositionVersionExecutor dve = DepositionVersionExecutor.getDefaultExecutor();
dve.setMetadata(getMetadata());
@ -64,6 +135,8 @@ public class ElaborateDepositionVersion {
logger.info("Going to deposit {} {}",
name , depositionVersion.getVersion());
dve.setDepositionFiles(depositionVersion.getFiles());
if(depositionVersion.getConceptDOIURL()==null) {
dve.create();
}else {

View File

@ -1,9 +1,11 @@
package org.gcube.common.deposition.executor;
import java.net.URL;
import java.util.List;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.common.deposition.executor.zenodo.ZenodoDepositionVersionExecutor;
import org.gcube.common.deposition.model.DepositionFile;
/**
* @author Luca Frosini (ISTI - CNR)
@ -16,6 +18,7 @@ public abstract class DepositionVersionExecutor {
protected JsonNode metadata;
protected URL doiURL;
protected List<DepositionFile> depositionFiles;
public JsonNode getMetadata() {
return metadata;
@ -33,10 +36,18 @@ public abstract class DepositionVersionExecutor {
this.doiURL = doiURL;
}
public abstract void create();
public List<DepositionFile> getDepositionFiles() {
return depositionFiles;
}
public abstract void update();
public void setDepositionFiles(List<DepositionFile> files) {
this.depositionFiles = files;
}
public abstract void create() throws Exception;
public abstract void update() throws Exception;
public abstract void newVersion();
public abstract void newVersion() throws Exception;
}

View File

@ -1,9 +1,17 @@
package org.gcube.common.deposition.executor.zenodo;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import org.gcube.common.deposition.config.Config;
import org.gcube.common.deposition.executor.DepositionVersionExecutor;
import org.gcube.common.deposition.model.DepositionFile;
/**
* @author Luca Frosini (ISTI - CNR)
@ -22,28 +30,44 @@ public class ZenodoDepositionVersionExecutor extends DepositionVersionExecutor {
}
}
protected void finalize() {
// TODO Add files
protected File downloadFile(DepositionFile df) throws IOException {
Path path = Paths.get(df.getDesiredName());
try (InputStream inputStream = df.getURL().openStream()) {
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
}
File file = new File(df.getDesiredName());
if(!file.exists()) {
throw new RuntimeException(file.getAbsolutePath() + "does not exist");
}
return file;
}
protected void finalize() throws IOException {
for(DepositionFile df : depositionFiles) {
downloadFile(df);
}
// TODO Add depositionFiles
// TODO Update deposit metadata
// TODO Publish the version
}
@Override
public void create() {
public void create() throws Exception {
// TODO New deposit on Zenodo
finalize();
}
@Override
public void update() {
public void update() throws Exception {
// TODO edit a published version
}
@Override
public void newVersion() {
public void newVersion() throws Exception {
// TODO New version on Zenodo from previous using the id in the DOI
// TODO Remove previous files
// TODO Remove previous depositionFiles
finalize();
}

View File

@ -28,13 +28,6 @@ public class Deposition {
@JsonProperty(value = "metadata")
protected JsonNode metadata;
public static String parseRawString(String rawString, String name, String version, String date) {
String s = rawString.replaceAll("{{name}}", name);
s = s.replaceAll("{{version}}", version);
s = s.replaceAll("{{date}}", date);
return s;
}
public String getName() {
return name;
}

View File

@ -18,7 +18,7 @@ public class DepositionFile {
protected String desiredName;
public URL getUrl() {
public URL getURL() {
return url;
}

View File

@ -2,8 +2,11 @@ package org.gcube.common.deposition.model;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.gcube.com.fasterxml.jackson.annotation.JsonFormat;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
import org.gcube.com.fasterxml.jackson.annotation.JsonProperty;
import org.gcube.com.fasterxml.jackson.annotation.JsonSetter;
@ -13,6 +16,16 @@ import org.gcube.com.fasterxml.jackson.annotation.JsonSetter;
*/
public class DepositionVersion {
/**
* DateTime Pattern to be used to serialize Dates
*/
public static final String DATETIME_PATTERN = "yyyy-MM-dd";
public static String getDateAsString(Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATETIME_PATTERN);
return simpleDateFormat.format(date);
}
@JsonIgnore
protected Deposition deposition;
@ -23,7 +36,8 @@ public class DepositionVersion {
protected String version;
@JsonProperty(value = "date")
protected String date;
@JsonFormat(shape= JsonFormat.Shape.STRING, pattern=DepositionVersion.DATETIME_PATTERN)
protected Date date;
@JsonProperty(value = "files")
protected List<DepositionFile> files;
@ -78,11 +92,11 @@ public class DepositionVersion {
this.version = version;
}
public String getDate() {
public Date getDate() {
return date;
}
public void setDate(String date) {
public void setDate(Date date) {
this.date = date;
}
@ -90,6 +104,10 @@ public class DepositionVersion {
return files;
}
public void setFiles(List<DepositionFile> files) {
this.files = files;
}
public String getgCubeReleaseVersion() {
return gCubeReleaseVersion;
}

View File

@ -4,12 +4,16 @@ import java.io.File;
import java.net.URL;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ZenodoDepositTest {
private static final Logger logger = LoggerFactory.getLogger(ZenodoDepositTest.class);
protected static final String ZENODO_DEPOSIT_JSON_FILENAME = "zenodo-deposit.json";
public File getZenodoDepositFile() throws Exception {
@ -24,4 +28,11 @@ public class ZenodoDepositTest {
zenodoDeposit.publish(getZenodoDepositFile());
}
@Test
public void replateTest() {
String test = "https://code-repo.d4science.org/gCubeSystem/{{name}}/archive/v{{version}}.tar.gz";
test = test.replaceAll("\\{\\{name\\}\\}", "gcat");
logger.info(test);
}
}

View File

@ -16,7 +16,7 @@
}
],
"default_code_location": "https://code-repo.d4science.org/gCubeSystem/{{name}}/releases/tag/v{{version}}",
"code_location_text": "\n\n<p>The official source code location of this software version is available at:</p>\n\n<p><a href=\"{{default_code_location}}\">{{default_code_location}}</a></p>",
"code_location_text": "\n\n<p>The official source code location of this software version is available at:</p>\n\n<p><a href=\"{{code_location}}\">{{code_location}}</a></p>",
"versions": [
{
"version": "1.0.0",
@ -128,11 +128,9 @@
"concept_doi_url": "https://doi.org/10.5072/zenodo.1139445"
}
],
"metadata":
{
"metadata": {
"access_right": "open",
"creators":
[
"creators": [
{
"affiliation": "Istituto di Scienza e Tecnologie dell'Informazione \"A. Faedo\" - CNR, Italy",
"name": "Frosini, Luca",
@ -140,8 +138,7 @@
}
],
"description": "<p><a href=\"https://www.gcube-system.org/\">gCube</a> Catalogue (gCat) Service allows any client to publish on the gCube Catalogue.</p>\n\n<p><a href=\"https://www.gcube-system.org/\">gCube</a> is an open-source software toolkit used for building and operating Hybrid Data Infrastructures enabling the dynamic deployment of Virtual Research Environments, such as the <a href=\"https://www.d4science.org/\">D4Science Infrastructure</a>, by favouring the realisation of reuse-oriented policies.</p>\n\n<p><a href=\"https://www.gcube-system.org/\">gCube</a> has been used to successfully build and operate infrastructures and virtual research environments for application domains ranging from biodiversity to environmental data management and cultural heritage.</p>\n\n<p><a href=\"https://www.gcube-system.org/\">gCube</a> offers components supporting typical data management workflows including data access, curation, processing, and visualisation on a large set of data typologies ranging from primary biodiversity data to geospatial and tabular data.</p>\n\n<p><a href=\"https://www.d4science.org/\">D4Science</a> is a Hybrid Data Infrastructure combining over 500 software components and integrating data from more than 50 different data providers into a coherent and managed system of hardware, software, and data resources. The D4Science infrastructure drastically reduces the cost of ownership, maintenance, and operation thanks to the exploitation of gCube.</p>\n\n<p>&nbsp;</p>",
"grants":
[
"grants": [
{
"id": "283644"
},
@ -197,8 +194,7 @@
"id": "10.13039/501100000780::654024"
}
],
"keywords":
[
"keywords": [
"gCube",
"Catalogue",
"D4Science"