Improved code

This commit is contained in:
Luca Frosini 2023-01-05 16:37:18 +01:00
parent b3cc3896d2
commit 2e5124bf38
7 changed files with 411 additions and 345 deletions

View File

@ -2,6 +2,7 @@ package org.gcube.common.deposition;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
@ -19,16 +20,17 @@ public class ElaborateDeposition {
private static final Logger logger = LoggerFactory.getLogger(ElaborateDeposition.class);
protected ObjectMapper objectMapper;
protected File jsonFile;
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 {
this.jsonFile = jsonFile;
Deposition deposition = objectMapper.readValue(jsonFile, Deposition.class);
publish(deposition);
}
@ -41,10 +43,17 @@ public class ElaborateDeposition {
public void publish(Deposition deposition) throws Exception {
String name = deposition.getName();
DepositionVersion previous = null;
for(DepositionVersion depositionVersion : deposition.getDepositionVersions()) {
List<DepositionVersion> depositionVersions = deposition.getDepositionVersions();
for(int i=0; i<depositionVersions.size(); i++) {
DepositionVersion depositionVersion = depositionVersions.get(i);
depositionVersion.setDeposition(deposition);
depositionVersion.setPrevious(previous);
if((i+1)<depositionVersions.size()) {
depositionVersion.setNext(depositionVersions.get(i+1));
}
logger.trace("Going to elaborate {} {} (previous version {})",
name, depositionVersion.getVersion(),
@ -58,8 +67,12 @@ public class ElaborateDeposition {
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
}
// TODO Save content to file
logger.info("{}", objectMapper.writeValueAsString(deposition));
logger.debug("{}", objectMapper.writeValueAsString(deposition));
if(jsonFile!=null) {
File directory = jsonFile.getParentFile();
File newJsonFile = new File(directory, "exported-"+jsonFile.getName());
objectMapper.writeValue(newJsonFile, deposition);
}
}
}

View File

@ -1,20 +1,7 @@
package org.gcube.common.deposition;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.com.fasterxml.jackson.databind.node.TextNode;
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;
@ -34,187 +21,17 @@ public class ElaborateDepositionVersion {
this.depositionVersion = depositionVersion;
}
protected String replaceVariable(String variableName, String replace, String s) {
return s.replaceAll("\\{\\{" + variableName + "\\}\\}", replace);
}
protected String replaceVariables(String stringToAnalise) {
Deposition d = depositionVersion.getDeposition();
String s = replaceVariable("name", d.getName(), stringToAnalise);
s = replaceVariable("version", depositionVersion.getVersion(), s);
s = replaceVariable("date", DepositionVersion.getDateAsString(depositionVersion.getDate()), s);
return s;
}
protected ObjectNode parseObjectNode(ObjectNode objectNode) {
Iterator<String> iterator = objectNode.fieldNames();
while (iterator.hasNext()) {
String name = iterator.next();
JsonNode jn = objectNode.get(name);
jn = parseJsonNode(jn);
objectNode.replace(name, jn);
}
return objectNode;
}
protected ArrayNode parseArrayNode(ArrayNode arrayNode) {
for(int i=0; i<arrayNode.size(); i++) {
JsonNode jn = arrayNode.get(i);
jn = parseJsonNode(jn);
arrayNode.set(i, jn);
}
return arrayNode;
}
protected JsonNode parseJsonNode(JsonNode jsonNode) {
switch (jsonNode.getNodeType()) {
case OBJECT:
return parseObjectNode((ObjectNode) jsonNode);
case ARRAY:
return parseArrayNode((ArrayNode) jsonNode);
case STRING:
String text = jsonNode.asText();
text = replaceVariables(text);
TextNode textNode = new TextNode(text);
return textNode;
default:
return jsonNode;
}
}
protected JsonNode getMetadata() throws JsonProcessingException {
Deposition deposition = depositionVersion.getDeposition();
JsonNode jsonNode = deposition.getMetadata().deepCopy();
jsonNode = parseJsonNode(jsonNode);
if(!depositionVersion.getNoCodeLocation()) {
ObjectNode objectNode = (ObjectNode) jsonNode;
String description = objectNode.get("description").asText();
StringWriter stringWriter = new StringWriter();
stringWriter.append(description);
String codeLocationText = deposition.getCodeLocationAdditionalDescription();
codeLocationText = replaceVariables(codeLocationText);
codeLocationText = replaceVariable("code_location", depositionVersion.getCodeLocation().toString(), codeLocationText);
stringWriter.append(codeLocationText);
TextNode textNode = new TextNode(stringWriter.toString());
objectNode.replace("description", textNode);
}
logger.debug("Metadata:{}", objectMapper.writeValueAsString(jsonNode));
return jsonNode;
}
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;
}
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.trace("DepositionVersion as declared is:{}", objectMapper.writeValueAsString(depositionVersion));
addDefaultsToDepositionVersion();
logger.trace("DepositionVersion to be managed is:{}", objectMapper.writeValueAsString(depositionVersion));
logger.debug("DepositionVersion: {}", objectMapper.writeValueAsString(depositionVersion));
DepositionVersionExecutor dve = DepositionVersionExecutor.getDefaultExecutor();
dve.setDepositionVersion(depositionVersion);
dve.setObjectMapper(objectMapper);
dve.setMetadata(getMetadata());
if(depositionVersion.getDOIURL()!=null) {
Boolean update = depositionVersion.getUpdate();
if(update==null) {
update = depositionVersion.getDeposition().getDefaultUpdate();
}
if(update) {
logger.info("Going to update deposition {} {}.",
name , depositionVersion.getVersion());
dve.setDOIURL(depositionVersion.getDOIURL());
dve.update();
}else {
logger.info("Deposition {} {} has been already deposited.",
name , depositionVersion.getVersion());
}
}else {
logger.info("Going to deposit {} {}",
name , depositionVersion.getVersion());
dve.setDepositionFiles(depositionVersion.getFiles());
if(depositionVersion.getConceptDOIURL()==null) {
dve.create();
}else {
dve.setDOIURL(depositionVersion.getConceptDOIURL());
dve.newVersion();
}
}
dve.deposit();
// TODO Export bibtext
}
}

View File

@ -1,28 +1,24 @@
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.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.common.deposition.executor.zenodo.ZenodoDepositionVersionExecutor;
import org.gcube.common.deposition.model.DepositionFile;
import org.gcube.common.deposition.model.DepositionVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public abstract class DepositionVersionExecutor {
private static final Logger logger = LoggerFactory.getLogger(DepositionVersionExecutor.class);
public static DepositionVersionExecutor getDefaultExecutor() {
return new ZenodoDepositionVersionExecutor();
}
protected DepositionVersion depositionVersion;
protected ObjectMapper objectMapper;
protected JsonNode metadata;
protected URL doiURL;
protected List<DepositionFile> depositionFiles;
public DepositionVersion getDepositionVersion() {
return depositionVersion;
@ -40,30 +36,32 @@ public abstract class DepositionVersionExecutor {
this.objectMapper = objectMapper;
}
public JsonNode getMetadata() {
return metadata;
}
public void setMetadata(JsonNode metadata) {
this.metadata = metadata;
}
public URL getDOIURL() {
return doiURL;
}
public void setDOIURL(URL doiURL) {
this.doiURL = doiURL;
public void deposit() throws Exception {
String name = depositionVersion.getDeposition().getName();
if(depositionVersion.getDOIURL()!=null) {
if(depositionVersion.getUpdate()) {
logger.info("Going to update deposition {} {}.",
name , depositionVersion.getVersion());
update();
}else {
logger.info("Deposition {} {} has been already deposited.",
name , depositionVersion.getVersion());
}
}else {
logger.info("Going to deposit {} {}",
name , depositionVersion.getVersion());
if(depositionVersion.getConceptDOIURL()==null) {
create();
}else {
newVersion();
}
}
}
public List<DepositionFile> getDepositionFiles() {
return depositionFiles;
}
public void setDepositionFiles(List<DepositionFile> files) {
this.depositionFiles = files;
}
public abstract void create() throws Exception;
public abstract void update() throws Exception;

View File

@ -86,9 +86,6 @@ public class ZenodoDepositionVersionExecutor extends DepositionVersionExecutor {
try {
this.zenodoBaseURL = new URL(Config.getProperties().getProperty("zenodo_base_url"));
this.accessToken = Config.getProperties().getProperty("zenodo_access_token");
if(doiURL!=null) {
}
}catch (Exception e) {
throw new RuntimeException(e);
}
@ -115,7 +112,7 @@ public class ZenodoDepositionVersionExecutor extends DepositionVersionExecutor {
}
protected void addFilesToDeposition(List<File> files ) throws Exception {
String depositID = getZenodoIDFromDOIURL(doiURL);
String depositID = getZenodoIDFromDOIURL(depositionVersion.getDOIURL());
String newFilePath = DEPOSTION_FILES_PATH.replace(":id", depositID);
URL url = new URL(zenodoBaseURL, newFilePath);
@ -150,11 +147,11 @@ public class ZenodoDepositionVersionExecutor extends DepositionVersionExecutor {
gxHTTPStringRequest.header("Content-Type", "application/json");
gxHTTPStringRequest.header("Accept", "application/json");
String id = getZenodoIDFromDOIURL(doiURL);
String id = getZenodoIDFromDOIURL(depositionVersion.getDOIURL());
gxHTTPStringRequest.path(DEPOSITION_PATH.replace(":id", id));
ObjectNode body = objectMapper.createObjectNode();
body.set(METADATA_FIELD_NAME, metadata);
body.set(METADATA_FIELD_NAME, depositionVersion.getMetadata());
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(objectMapper.writeValueAsString(body));
getResponse(httpURLConnection);
@ -168,7 +165,7 @@ public class ZenodoDepositionVersionExecutor extends DepositionVersionExecutor {
gxHTTPStringRequest.header("Content-Type", "application/json");
gxHTTPStringRequest.header("Accept", "application/json");
String id = getZenodoIDFromDOIURL(doiURL);
String id = getZenodoIDFromDOIURL(depositionVersion.getDOIURL());
gxHTTPStringRequest.path(DEPOSTION_PUBLISH_PATH.replace(":id", id));
HttpURLConnection httpURLConnection = gxHTTPStringRequest.post();
@ -177,7 +174,7 @@ public class ZenodoDepositionVersionExecutor extends DepositionVersionExecutor {
protected void finalize() throws Exception {
List<File> files = new ArrayList<>();
for(DepositionFile df : depositionFiles) {
for(DepositionFile df : depositionVersion.getFiles()) {
File file = downloadFile(df);
files.add(file);
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
@ -274,14 +271,14 @@ public class ZenodoDepositionVersionExecutor extends DepositionVersionExecutor {
gxHTTPStringRequest.path(DEPOSITIONS_COLLECTION_PATH);
ObjectNode body = objectMapper.createObjectNode();
body.set(METADATA_FIELD_NAME, metadata);
body.set(METADATA_FIELD_NAME, depositionVersion.getMetadata());
HttpURLConnection httpURLConnection = gxHTTPStringRequest.post(objectMapper.writeValueAsString(body));
response = getResponse(httpURLConnection);
URL conceptDOIURL = createZenodoDOIURLFromID(response.get("conceptrecid").asText());
depositionVersion.setConceptDOIURL(conceptDOIURL);
doiURL = new URL(ZENODO_DOI_URL_BASE_PATH + response.get("id").asText());
URL doiURL = new URL(ZENODO_DOI_URL_BASE_PATH + response.get("id").asText());
depositionVersion.setDOIURL(doiURL);
finalize();
@ -295,7 +292,7 @@ public class ZenodoDepositionVersionExecutor extends DepositionVersionExecutor {
gxHTTPStringRequest.from(GUCBE_ZENODO_SOFTWARE_DEPOSIT);
gxHTTPStringRequest.queryParams(getAccessTokenQueryParamters());
gxHTTPStringRequest.header("Accept", "application/json");
String id = getZenodoIDFromDOIURL(doiURL);
String id = getZenodoIDFromDOIURL(depositionVersion.getDOIURL());
gxHTTPStringRequest.path(DEPOSTION_EDIT_PATH.replace(":id", id));
HttpURLConnection httpURLConnection = gxHTTPStringRequest.post();
getResponse(httpURLConnection);
@ -327,55 +324,68 @@ public class ZenodoDepositionVersionExecutor extends DepositionVersionExecutor {
@Override
public void newVersion() throws Exception {
// Reading Record using conceptID to get the latest published version
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(zenodoBaseURL.toString());
gxHTTPStringRequest.isExternalCall(true);
gxHTTPStringRequest.from(GUCBE_ZENODO_SOFTWARE_DEPOSIT);
gxHTTPStringRequest.queryParams(getAccessTokenQueryParamters());
gxHTTPStringRequest.header("Content-Type", "application/json");
gxHTTPStringRequest.header("Accept", "application/json");
// Creating the new version starting form doiURL which contains conceptID DOI
String conceptID = getZenodoIDFromDOIURL(doiURL);
String conceptID = getZenodoIDFromDOIURL(depositionVersion.getConceptDOIURL());
gxHTTPStringRequest.path(RECORD_PATH.replace(":id", conceptID));
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
JsonNode jsonNode = getResponse(httpURLConnection);
String latestDOI = jsonNode.get("links").get("doi").asText();
/*
* Comparing obtained latestDOI and its declared version with the previuos version DOI and its declared version.
* If they differs the configuration is not up to date and must be fixed
* this should avoid errors on deposition.
*/
String latestVersionDOI = jsonNode.get("links").get("doi").asText();
String previousVersionDOI = depositionVersion.getPrevious().getDOIURL().toString();
if(previousVersionDOI.compareTo(latestDOI)!=0) {
logger.error("Zenodo obtained latest DOI {} != {} DOI form previous version", latestDOI, previousVersionDOI);
if(previousVersionDOI.compareTo(latestVersionDOI)!=0) {
logger.error("Zenodo obtained latest DOI {} != {} DOI from previous version", latestVersionDOI, previousVersionDOI);
throw new RuntimeException("It seems that your json is not up to date with Zenodo.");
}
String latestVersionVersion = jsonNode.get("metadata").get("version").asText();
String previousVersionVersion = depositionVersion.getPrevious().getVersion().toString();
if(latestVersionVersion.compareTo(previousVersionVersion)!=0) {
logger.error("Zenodo obtained latest Version {} != {} Versoin from previous version", latestVersionVersion, previousVersionVersion);
throw new RuntimeException("It seems that your json is not up to date with Zenodo.");
}
// Creating new version from latest deposited version
gxHTTPStringRequest = GXHTTPStringRequest.newRequest(zenodoBaseURL.toString());
gxHTTPStringRequest.isExternalCall(true);
gxHTTPStringRequest.from(GUCBE_ZENODO_SOFTWARE_DEPOSIT);
gxHTTPStringRequest.queryParams(getAccessTokenQueryParamters());
gxHTTPStringRequest.header("Content-Type", "application/json");
gxHTTPStringRequest.header("Accept", "application/json");
// Creating the new version starting form doiURL which contains conceptID DOI
String latestID = getZenodoIDFromDOIURL(latestDOI);
String latestID = getZenodoIDFromDOIURL(latestVersionDOI);
gxHTTPStringRequest.path(DEPOSTION_NEW_VERSION_PATH.replace(":id", latestID));
httpURLConnection = gxHTTPStringRequest.post();
jsonNode = getResponse(httpURLConnection);
// Getting draft new Version ID
String draftURL = jsonNode.get("links").get("latest_draft").asText();
String draftID = draftURL.replace(zenodoBaseURL.toString() + DEPOSITIONS_COLLECTION_PATH + "/", "");
gxHTTPStringRequest = GXHTTPStringRequest.newRequest(zenodoBaseURL.toString());
gxHTTPStringRequest.isExternalCall(true);
gxHTTPStringRequest.from(GUCBE_ZENODO_SOFTWARE_DEPOSIT);
gxHTTPStringRequest.queryParams(getAccessTokenQueryParamters());
gxHTTPStringRequest.header("Accept", "application/json");
gxHTTPStringRequest.path(DEPOSITION_PATH.replace(":id", draftID));
httpURLConnection = gxHTTPStringRequest.get();
response = getResponse(httpURLConnection);
// The doi of this created new version
doiURL = new URL(response.get("doi_url").asText());
// The reserved DOI of this created new version will be
URL doiURL = new URL(response.get("doi_url").asText());
depositionVersion.setDOIURL(doiURL);
// Remove previous depositionFiles

View File

@ -0,0 +1,28 @@
package org.gcube.common.deposition.exporter;
import org.gcube.common.deposition.model.DepositionVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public abstract class DepositionVersionExporter {
private static final Logger logger = LoggerFactory.getLogger(DepositionVersionExporter.class);
protected DepositionVersion depositionVersion;
public DepositionVersion getDepositionVersion() {
return depositionVersion;
}
public void setDepositionVersion(DepositionVersion depositionVersion) {
this.depositionVersion = depositionVersion;
}
public abstract void export() throws Exception;
}

View File

@ -1,72 +1,96 @@
package org.gcube.common.deposition.model;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.gcube.com.fasterxml.jackson.annotation.JsonFormat;
import org.gcube.com.fasterxml.jackson.annotation.JsonGetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
import org.gcube.com.fasterxml.jackson.annotation.JsonInclude;
import org.gcube.com.fasterxml.jackson.annotation.JsonInclude.Include;
import org.gcube.com.fasterxml.jackson.annotation.JsonProperty;
import org.gcube.com.fasterxml.jackson.annotation.JsonSetter;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.com.fasterxml.jackson.databind.node.TextNode;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class DepositionVersion {
/**
* DateTime Pattern to be used to serialize Dates
* 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);
}
public static final String VERSION_PROPERTY_NAME = "version";
public static final String DATE_PROPERTY_NAME = "date";
public static final String FILES_PROPERTY_NAME = "files";
public static final String GCUBE_RELEASE_VERSION_PROPERTY_NAME = "gcube_release_version";
public static final String GCUBE_RELEASE_TICKET_PROPERTY_NAME = "gcube_release_ticket";
public static final String CODE_LOCATION_PROPERTY_NAME = "code_location";
public static final String CONCEPT_DOI_URL_PROPERTY_NAME = "concept_doi_url";
public static final String DOI_URL_PROPERTY_NAME = "doi_url";
public static final String UPDATE_PROPERTY_NAME = "update";
public static final String METADATA_PROPERTY_NAME = "metadata";
@JsonIgnore
protected Deposition deposition;
@JsonIgnore
protected DepositionVersion previous;
@JsonProperty(value = "version")
protected String version;
@JsonProperty(value = "date")
@JsonFormat(shape= JsonFormat.Shape.STRING, pattern=DepositionVersion.DATETIME_PATTERN)
protected Date date;
@JsonProperty(value = "files")
protected List<DepositionFile> files;
@JsonProperty(value = "gcube_release_version")
protected String gCubeReleaseVersion;
@JsonProperty(value = "release_ticket")
protected URL releaseTicket;
@JsonIgnore
protected Boolean noCodeLocation;
@JsonProperty(value = "code_location")
protected URL codeLocation;
protected DepositionVersion next;
@JsonProperty(value = "concept_doi_url")
@JsonProperty(VERSION_PROPERTY_NAME)
protected String version;
@JsonProperty(DATE_PROPERTY_NAME)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DepositionVersion.DATETIME_PATTERN)
protected Date date;
@JsonProperty(FILES_PROPERTY_NAME)
protected List<DepositionFile> files;
@JsonProperty(GCUBE_RELEASE_VERSION_PROPERTY_NAME)
protected String gCubeReleaseVersion;
@JsonProperty(GCUBE_RELEASE_TICKET_PROPERTY_NAME)
protected URL gCubeReleaseTicket;
@JsonProperty(CODE_LOCATION_PROPERTY_NAME)
protected Object codeLocation;
@JsonProperty(CONCEPT_DOI_URL_PROPERTY_NAME)
protected URL conceptDOIURL;
@JsonProperty(value = "doi_url")
@JsonProperty(DOI_URL_PROPERTY_NAME)
protected URL doiURL;
@JsonProperty(value = "update")
@JsonProperty(UPDATE_PROPERTY_NAME)
protected Boolean update;
public DepositionVersion() {
this.noCodeLocation = false;
}
@JsonProperty(METADATA_PROPERTY_NAME)
protected JsonNode metadata;
public DepositionVersion() {
}
@JsonIgnore
public Deposition getDeposition() {
return deposition;
@ -87,114 +111,290 @@ public class DepositionVersion {
this.previous = previous;
}
@JsonIgnore
public DepositionVersion getNext() {
return next;
}
@JsonIgnore
public void setNext(DepositionVersion next) {
this.next = next;
}
@JsonGetter(value = VERSION_PROPERTY_NAME)
public String getVersion() {
return version;
}
@JsonSetter(value = VERSION_PROPERTY_NAME)
public void setVersion(String version) {
if(version==null || version.length()==0) {
throw new RuntimeException("Invalid version");
}
this.version = version;
}
@JsonGetter(value = DATE_PROPERTY_NAME)
public Date getDate() {
return date;
}
@JsonSetter(value = DATE_PROPERTY_NAME)
public void setDate(Date date) {
this.date = date;
}
public List<DepositionFile> getFiles() {
@JsonIgnore
protected List<DepositionFile> parseFiles(List<DepositionFile> filesToParse){
List<DepositionFile> files = new ArrayList<>();
for(DepositionFile f : filesToParse) {
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 setFiles(List<DepositionFile> files) {
this.files = files;
}
public String getgCubeReleaseVersion() {
return gCubeReleaseVersion;
}
public void setgCubeReleaseVersion(String gCubeReleaseVersion) {
this.gCubeReleaseVersion = gCubeReleaseVersion;
}
public URL getReleaseTicket() {
return releaseTicket;
}
public void setReleaseTicket(URL releaseTicket) {
this.releaseTicket = releaseTicket;
}
@JsonIgnore
public Boolean getNoCodeLocation() {
return noCodeLocation;
}
public URL getCodeLocation() {
return codeLocation;
}
public void setCodeLocation(URL codeLocation) {
this.codeLocation = codeLocation;
}
@JsonSetter("code_location")
public void setCodeLocation(String codeLocation) {
try {
if(codeLocation==null) {
this.noCodeLocation = true;
protected void replaceVariablesInfiles(List<DepositionFile> files) {
for(DepositionFile file : files) {
file.setDesiredName(replaceVariables(file.getDesiredName()));
try {
file.setURL(replaceVariables(file.getURL().toString()));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
this.codeLocation = new URL(codeLocation);
}catch (Exception e) {
this.codeLocation = null;
}
}
@JsonIgnore
public List<DepositionFile> getFiles() {
if(files!=null) {
return parseFiles(files);
}
return parseFiles(deposition.getDefaultFiles());
}
@JsonGetter(value = FILES_PROPERTY_NAME)
@JsonInclude(Include.NON_NULL)
protected List<DepositionFile> getFilesForSerialization() {
return files;
}
@JsonSetter(value = FILES_PROPERTY_NAME)
public void setFiles(List<DepositionFile> files) {
this.files = files;
}
@JsonGetter(value = GCUBE_RELEASE_VERSION_PROPERTY_NAME)
public String getGCubeReleaseVersion() {
return gCubeReleaseVersion;
}
@JsonSetter(value = GCUBE_RELEASE_VERSION_PROPERTY_NAME)
public void setGCubeReleaseVersion(String gCubeReleaseVersion) {
this.gCubeReleaseVersion = gCubeReleaseVersion;
}
@JsonGetter(value = GCUBE_RELEASE_TICKET_PROPERTY_NAME)
public URL getGCubeReleaseTicket() {
return gCubeReleaseTicket;
}
@JsonSetter(value = GCUBE_RELEASE_TICKET_PROPERTY_NAME)
public void setGCubeReleaseTicket(URL gCubeReleaseTicket) {
this.gCubeReleaseTicket = gCubeReleaseTicket;
}
@JsonIgnore
public URL getCodeLocation() throws MalformedURLException {
if(codeLocation!=null) {
if(codeLocation instanceof Boolean && ((Boolean) codeLocation)==false) {
return null;
}
return new URL(replaceVariables(codeLocation.toString()));
}
return new URL(replaceVariables(deposition.getDefaultCodeLocation().toString()));
}
@JsonGetter(value = CODE_LOCATION_PROPERTY_NAME)
@JsonInclude(Include.NON_NULL)
public Object getCodeLocationForSerialization() throws MalformedURLException {
return codeLocation;
}
@JsonSetter(value = CODE_LOCATION_PROPERTY_NAME)
public void setCodeLocation(Object codeLocation) throws Exception {
if(codeLocation instanceof Boolean) {
if(((Boolean) codeLocation)==false) {
this.codeLocation = new Boolean(false);
return;
}else {
throw new RuntimeException(CODE_LOCATION_PROPERTY_NAME + " can only be false or the URL of the code_location");
}
}
this.codeLocation = new URL((String) codeLocation);
}
@JsonGetter(CONCEPT_DOI_URL_PROPERTY_NAME)
@JsonInclude(Include.NON_NULL)
public URL getConceptDOIURL() {
return conceptDOIURL;
}
@JsonIgnore
public void setConceptDOIURL(URL conceptDOIURL) {
this.conceptDOIURL = conceptDOIURL;
}
@JsonSetter("concept_doi_url")
@JsonSetter(CONCEPT_DOI_URL_PROPERTY_NAME)
public void setConceptDOIURL(String conceptDOIURL) throws MalformedURLException {
try {
this.conceptDOIURL = new URL(conceptDOIURL);
}catch (Exception e) {
} catch (Exception e) {
this.conceptDOIURL = null;
}
}
@JsonGetter(DOI_URL_PROPERTY_NAME)
public URL getDOIURL() {
return doiURL;
}
@JsonIgnore
public void setDOIURL(URL doiURL) {
this.doiURL = doiURL;
}
@JsonSetter("doi_url")
@JsonSetter(DOI_URL_PROPERTY_NAME)
public void setDOIURL(String doiURL) throws MalformedURLException {
try {
this.doiURL = new URL(doiURL);
}catch (Exception e) {
} catch (Exception e) {
this.doiURL = null;
}
}
@JsonIgnore
public Boolean getUpdate() {
if(update!=null) {
return update;
}
return deposition.getDefaultUpdate();
}
@JsonGetter(UPDATE_PROPERTY_NAME)
@JsonInclude(Include.NON_NULL)
public Boolean getUpdateForSerialization() {
return update;
}
@JsonSetter(UPDATE_PROPERTY_NAME)
public void setUpdate(Boolean update) {
this.update = update;
}
@JsonIgnore
protected ObjectNode parseObjectNode(ObjectNode objectNode) {
Iterator<String> iterator = objectNode.fieldNames();
while (iterator.hasNext()) {
String name = iterator.next();
JsonNode jn = objectNode.get(name);
jn = parseJsonNode(jn);
objectNode.replace(name, jn);
}
return objectNode;
}
@JsonIgnore
protected ArrayNode parseArrayNode(ArrayNode arrayNode) {
for(int i=0; i<arrayNode.size(); i++) {
JsonNode jn = arrayNode.get(i);
jn = parseJsonNode(jn);
arrayNode.set(i, jn);
}
return arrayNode;
}
@JsonIgnore
protected JsonNode parseJsonNode(JsonNode jsonNode) {
switch (jsonNode.getNodeType()) {
case OBJECT:
return parseObjectNode((ObjectNode) jsonNode);
case ARRAY:
return parseArrayNode((ArrayNode) jsonNode);
case STRING:
String text = jsonNode.asText();
text = replaceVariables(text);
TextNode textNode = new TextNode(text);
return textNode;
default:
return jsonNode;
}
}
@JsonIgnore
protected String replaceVariable(String variableName, String replace, String s) {
return s.replaceAll("\\{\\{" + variableName + "\\}\\}", replace);
}
@JsonIgnore
protected String replaceVariables(String stringToAnalise) {
String s = replaceVariable("name", deposition.getName(), stringToAnalise);
s = replaceVariable("version", version, s);
s = replaceVariable("date", DepositionVersion.getDateAsString(date), s);
return s;
}
@JsonIgnore
public JsonNode getMetadata() throws Exception {
if(metadata!=null) {
return metadata;
}
JsonNode jsonNode = deposition.getMetadata().deepCopy();
jsonNode = parseJsonNode(jsonNode);
if(getCodeLocation()!=null) {
ObjectNode objectNode = (ObjectNode) jsonNode;
String description = objectNode.get("description").asText();
StringWriter stringWriter = new StringWriter();
stringWriter.append(description);
String codeLocationText = deposition.getCodeLocationAdditionalDescription();
codeLocationText = replaceVariables(codeLocationText);
codeLocationText = replaceVariable("code_location", getCodeLocation().toString(), codeLocationText);
stringWriter.append(codeLocationText);
TextNode textNode = new TextNode(stringWriter.toString());
objectNode.replace("description", textNode);
}
return jsonNode;
}
@JsonGetter(METADATA_PROPERTY_NAME)
@JsonInclude(Include.NON_NULL)
protected JsonNode getMetadataJsonGetter() {
return metadata;
}
@JsonSetter(METADATA_PROPERTY_NAME)
public void setMetadata(JsonNode metadata) {
this.metadata = metadata;
}
}

View File

@ -1,6 +1,6 @@
{
"name": "gcat",
"default_update": false,
"default_update": true,
"default_files":
[
{
@ -22,22 +22,21 @@
{
"version": "1.0.0",
"date": "2019-01-10",
"gcube_release_version": null,
"files": [
{
"url": "https://nexus.d4science.org/nexus/service/local/repositories/gcube-snapshots/content/org/gcube/data-publishing/gcat/1.0.0-SNAPSHOT/gcat-1.0.0-20190109.172827-2.war",
"desired_name": "{{name}}-v{{version}}.war"
}
],
"release_ticket": null,
"code_location": null,
"gcube_release_version": null,
"gcube_release_ticket": null,
"code_location": false,
"concept_doi_url": "https://doi.org/10.5072/zenodo.1139445",
"doi_url": "https://doi.org/10.5072/zenodo.1139446"
},
{
"version": "1.1.0",
"date": "2019-02-26",
"gcube_release_version": "4.13.1",
"files": [
{
"url": "https://nexus.d4science.org/nexus/service/local/repo_groups/gcube-releases-all/content/org/gcube/data-publishing/gcat/1.1.0-4.13.1-177071/gcat-1.1.0-4.13.1-177071-src.zip",
@ -48,30 +47,30 @@
"desired_name": "{{name}}-v{{version}}.war"
}
],
"release_ticket": "https://support.d4science.org/issues/12988",
"code_location": null,
"gcube_release_version": "4.13.1",
"gcube_release_ticket": "https://support.d4science.org/issues/12988",
"code_location": false,
"concept_doi_url": "https://doi.org/10.5072/zenodo.1139445",
"doi_url": "https://doi.org/10.5072/zenodo.1140461"
},
{
"version": "1.2.0",
"date": "2019-05-20",
"gcube_release_version": null,
"files": [
{
"url": "https://nexus.d4science.org/nexus/service/local/repositories/gcube-snapshots/content/org/gcube/data-publishing/gcat/1.2.0-SNAPSHOT/gcat-1.2.0-20190520.132914-10.war",
"desired_name": "{{name}}-v{{version}}.war"
}
],
"release_ticket": null,
"code_location": null,
"gcube_release_version": null,
"gcube_release_ticket": null,
"code_location": false,
"concept_doi_url": "https://doi.org/10.5072/zenodo.1139445",
"doi_url": "https://doi.org/10.5072/zenodo.1140750"
},
{
"version": "1.3.0",
"date": "2019-06-27",
"gcube_release_version": "4.14.0",
"files": [
{
"url": "https://nexus.d4science.org/nexus/service/local/repo_groups/gcube-releases-all/content/org/gcube/data-publishing/gcat/1.3.0-4.14.0-179505/gcat-1.3.0-4.14.0-179505-src.zip",
@ -82,8 +81,9 @@
"desired_name": "{{name}}-v{{version}}.war"
}
],
"release_ticket": "https://support.d4science.org/issues/16743",
"code_location": null,
"gcube_release_version": "4.14.0",
"gcube_release_ticket": "https://support.d4science.org/issues/16743",
"code_location": false,
"concept_doi_url": "https://doi.org/10.5072/zenodo.1139445",
"doi_url": "https://doi.org/10.5072/zenodo.1143572"
},
@ -91,7 +91,7 @@
"version": "1.4.0",
"date": "2019-11-20",
"gcube_release_version": "4.15.0",
"release_ticket": "https://support.d4science.org/issues/17294",
"gcube_release_ticket": "https://support.d4science.org/issues/17294",
"concept_doi_url" : "https://doi.org/10.5072/zenodo.1139445",
"doi_url" : "https://doi.org/10.5072/zenodo.1143583"
},
@ -99,7 +99,7 @@
"version": "1.4.1",
"date": "2019-12-20",
"gcube_release_version": "4.18.0",
"release_ticket": "https://support.d4science.org/issues/18335",
"gcube_release_ticket": "https://support.d4science.org/issues/18335",
"concept_doi_url": "https://doi.org/10.5072/zenodo.1139445",
"doi_url" : "https://doi.org/10.5072/zenodo.1143585"
},
@ -107,7 +107,7 @@
"version": "1.4.2",
"date": "2020-02-14",
"gcube_release_version": "4.20.0",
"release_ticket": "https://support.d4science.org/issues/18507",
"gcube_release_ticket": "https://support.d4science.org/issues/18507",
"concept_doi_url": "https://doi.org/10.5072/zenodo.1139445",
"doi_url" : "https://doi.org/10.5072/zenodo.1143586"
},
@ -115,7 +115,7 @@
"version": "1.4.3",
"date": "2020-06-16",
"gcube_release_version": "4.23.0",
"release_ticket": "https://support.d4science.org/issues/19322",
"gcube_release_ticket": "https://support.d4science.org/issues/19322",
"concept_doi_url": "https://doi.org/10.5072/zenodo.1139445",
"doi_url" : "https://doi.org/10.5072/zenodo.1143587"
},
@ -123,7 +123,7 @@
"version": "1.4.4",
"date": "2021-02-24",
"gcube_release_version": "5.0.0",
"release_ticket": "https://support.d4science.org/issues/20648",
"gcube_release_ticket": "https://support.d4science.org/issues/20648",
"concept_doi_url": "https://doi.org/10.5072/zenodo.1139445",
"doi_url" : "https://doi.org/10.5072/zenodo.1143589"
},
@ -131,7 +131,7 @@
"version": "1.4.5",
"date": "2021-05-31",
"gcube_release_version": "5.1.0",
"release_ticket": "https://support.d4science.org/issues/20920",
"gcube_release_ticket": "https://support.d4science.org/issues/20920",
"concept_doi_url": "https://doi.org/10.5072/zenodo.1139445",
"doi_url" : "https://doi.org/10.5072/zenodo.1143590"
}