improving code
This commit is contained in:
parent
715091f304
commit
efca1627a3
|
@ -42,7 +42,7 @@ public class SoftwareConfigAnalyser {
|
|||
this.jsonNode = objectMapper.readTree(json);
|
||||
}
|
||||
|
||||
protected SoftwareVersionConfig actualize(JsonNode version) throws Exception {
|
||||
protected SoftwareVersionConfig actualizeSoftwareVersionConfig(JsonNode version) throws Exception {
|
||||
Variables variables = objectMapper.treeToValue(version, Variables.class);
|
||||
Set<String> missingVariables = variables.parse();
|
||||
int size = missingVariables.size();
|
||||
|
@ -52,13 +52,20 @@ public class SoftwareConfigAnalyser {
|
|||
}
|
||||
JsonNode swVersion = objectMapper.convertValue(variables.getProperties(), JsonNode.class);
|
||||
SoftwareVersionConfig softwareVersion = objectMapper.treeToValue(swVersion, SoftwareVersionConfig.class);
|
||||
// softwareVersion.setVariables(variables);
|
||||
return softwareVersion;
|
||||
}
|
||||
|
||||
protected SoftwareConfig actualizeSoftwareConfig(JsonNode node) throws Exception {
|
||||
Variables variables = objectMapper.treeToValue(node, Variables.class);
|
||||
variables.parse();
|
||||
JsonNode sc = objectMapper.convertValue(variables.getProperties(), JsonNode.class);
|
||||
SoftwareConfig softwareConfig = objectMapper.treeToValue(sc, SoftwareConfig.class);
|
||||
return softwareConfig;
|
||||
}
|
||||
|
||||
public void analyse() throws Exception {
|
||||
ObjectNode originalGlobal = (ObjectNode) jsonNode.get(GLOBAL_PROPERTY_NAME).deepCopy();
|
||||
SoftwareConfig softwareConfig = objectMapper.treeToValue(originalGlobal, SoftwareConfig.class);
|
||||
SoftwareConfig softwareConfig = actualizeSoftwareConfig(originalGlobal);
|
||||
softwareConfig.setOriginalJson(originalGlobal);
|
||||
|
||||
ObjectNode global = originalGlobal.deepCopy();
|
||||
|
@ -78,7 +85,7 @@ public class SoftwareConfigAnalyser {
|
|||
|
||||
|
||||
JsonNode version = Utils.merge(global, originalVersion);
|
||||
SoftwareVersionConfig softwareVersion = actualize(version);
|
||||
SoftwareVersionConfig softwareVersion = actualizeSoftwareVersionConfig(version);
|
||||
softwareVersion.setOriginalJson(originalVersion);
|
||||
|
||||
boolean newDOI = false;
|
||||
|
@ -91,10 +98,7 @@ public class SoftwareConfigAnalyser {
|
|||
previous.setNext(softwareVersion);
|
||||
}
|
||||
|
||||
|
||||
logger.trace("Going to process {} {} (previous version {})",
|
||||
softwareVersion.getName(), softwareVersion.getVersion(),
|
||||
softwareVersion.getPrevious()!=null ? softwareVersion.getPrevious().getVersion(): null);
|
||||
logger.trace("Going to process {}", softwareVersion.getTitle());
|
||||
|
||||
try {
|
||||
SoftwareVersionConfigAnalyser softwareVersionAnalyser = new SoftwareVersionConfigAnalyser(softwareVersion);
|
||||
|
|
|
@ -66,8 +66,9 @@ public class SoftwareVersionConfigAnalyser {
|
|||
List<SoftwareVersionPublisher> svps = SoftwareVersionPublisher.getPublishers();
|
||||
for(SoftwareVersionPublisher svp: svps) {
|
||||
ProcessorConfig processorConfig = processors.get(svp.getClass().getSimpleName());
|
||||
svp.setProcessorConfig(processorConfig);
|
||||
svp.setSoftwareConfig(softwareConfig);
|
||||
svp.setSoftwareVersion(softwareVersionConfig);
|
||||
svp.setProcessorConfig(processorConfig);
|
||||
svp.setFirst(first);
|
||||
svp.publish();
|
||||
}
|
||||
|
@ -77,8 +78,9 @@ public class SoftwareVersionConfigAnalyser {
|
|||
List<SoftwareVersionExporter> sves = SoftwareVersionExporter.getExporters();
|
||||
for(SoftwareVersionExporter sve: sves) {
|
||||
ProcessorConfig processorConfig = exporters.get(sve.getClass().getSimpleName());
|
||||
sve.setProcessorConfig(processorConfig);
|
||||
sve.setSoftwareConfig(softwareConfig);
|
||||
sve.setSoftwareVersion(softwareVersionConfig);
|
||||
sve.setProcessorConfig(processorConfig);
|
||||
sve.setFirst(first);
|
||||
sve.export();
|
||||
}
|
||||
|
|
|
@ -51,9 +51,6 @@ public class SoftwareVersionConfig {
|
|||
@JsonIgnore
|
||||
protected ObjectNode originalJson;
|
||||
|
||||
@JsonProperty(NAME_PROPERTY_NAME)
|
||||
protected String name;
|
||||
|
||||
@JsonProperty(VERSION_PROPERTY_NAME)
|
||||
protected String version;
|
||||
|
||||
|
@ -155,10 +152,6 @@ public class SoftwareVersionConfig {
|
|||
this.originalJson = originalJson;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ import org.gcube.common.software.model.SoftwareVersionConfig;
|
|||
public abstract class SoftwareVersionProcessor {
|
||||
|
||||
protected SoftwareConfig softwareConfig;
|
||||
protected SoftwareVersionConfig softwareVersion;
|
||||
protected SoftwareVersionConfig softwareVersionConfig;
|
||||
protected ProcessorConfig processorConfig;
|
||||
protected boolean first;
|
||||
|
||||
|
@ -23,11 +23,11 @@ public abstract class SoftwareVersionProcessor {
|
|||
}
|
||||
|
||||
public SoftwareVersionConfig getSoftwareVersionConfig() {
|
||||
return softwareVersion;
|
||||
return softwareVersionConfig;
|
||||
}
|
||||
|
||||
public void setSoftwareVersion(SoftwareVersionConfig softwareVersionConfig) {
|
||||
this.softwareVersion = softwareVersionConfig;
|
||||
this.softwareVersionConfig = softwareVersionConfig;
|
||||
}
|
||||
|
||||
public ProcessorConfig getProcessorConfig() {
|
||||
|
|
|
@ -28,7 +28,7 @@ public class BibLaTeXSoftwareVersionExporter extends SoftwareVersionExporter {
|
|||
public static final String TEMPLATE_FILENAME = "biblatex.template";
|
||||
|
||||
public void export() throws Exception {
|
||||
String name = softwareVersion.getName();
|
||||
String title = softwareVersionConfig.getTitle();
|
||||
|
||||
ElaborationType export = processorConfig.getElaboration();
|
||||
|
||||
|
@ -38,16 +38,16 @@ public class BibLaTeXSoftwareVersionExporter extends SoftwareVersionExporter {
|
|||
break;
|
||||
|
||||
case UPDATE_ONLY:
|
||||
if (softwareVersion.isNewDeposition()) {
|
||||
logger.info("Skipping export for {} {}.", name, softwareVersion.getVersion());
|
||||
if (softwareVersionConfig.isNewDeposition()) {
|
||||
logger.info("Skipping export for {}.", title);
|
||||
return;
|
||||
}
|
||||
generate();
|
||||
break;
|
||||
|
||||
case NEW:
|
||||
if (!softwareVersion.isNewDeposition()) {
|
||||
logger.info("Skipping export for {} {}.", name, softwareVersion.getVersion());
|
||||
if (!softwareVersionConfig.isNewDeposition()) {
|
||||
logger.info("Skipping export for {}.", title);
|
||||
return;
|
||||
}
|
||||
generate();
|
||||
|
@ -55,7 +55,7 @@ public class BibLaTeXSoftwareVersionExporter extends SoftwareVersionExporter {
|
|||
|
||||
case NONE:
|
||||
default:
|
||||
logger.info("Skipping export for {} {}.", name, softwareVersion.getVersion());
|
||||
logger.info("Skipping export for {}.", title);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -143,12 +143,11 @@ public class BibLaTeXSoftwareVersionExporter extends SoftwareVersionExporter {
|
|||
// }
|
||||
|
||||
protected String parseTemplate(String template) throws Exception {
|
||||
// String s = Utils.replaceVariable("citation_id", getCitationID(), template);
|
||||
String s = template;
|
||||
s = Utils.replaceVariable("author", getAuthors(softwareVersion.getAuthors()), s);
|
||||
s = Utils.replaceVariable("keywords", getKeywords(softwareVersion.getKeywords()), s);
|
||||
s = Utils.replaceVariable("author", getAuthors(softwareVersionConfig.getAuthors()), s);
|
||||
s = Utils.replaceVariable("keywords", getKeywords(softwareVersionConfig.getKeywords()), s);
|
||||
|
||||
Variables variables = softwareVersion.getVariables();
|
||||
Variables variables = softwareVersionConfig.getVariables();
|
||||
s = variables.replaceAllVariables(s);
|
||||
// s = addNotes(s);
|
||||
|
||||
|
@ -156,12 +155,12 @@ public class BibLaTeXSoftwareVersionExporter extends SoftwareVersionExporter {
|
|||
}
|
||||
|
||||
protected void generate() throws Exception {
|
||||
String name = softwareVersion.getName();
|
||||
if(softwareVersion.getVersionDOIURL()==null) {
|
||||
logger.info("No Version DOI URL for {} {}. It will not be exported in BibLaTex format.", name, softwareVersion.getVersion());
|
||||
String title = softwareVersionConfig.getTitle();
|
||||
if(softwareVersionConfig.getVersionDOIURL()==null) {
|
||||
logger.info("No Version DOI URL for {}. It will not be exported in BibLaTex format.", title);
|
||||
return;
|
||||
}
|
||||
logger.info("Going to export {} {} in BibLaTex format.", name, softwareVersion.getVersion());
|
||||
logger.info("Going to export {} in BibLaTex format.", title);
|
||||
|
||||
String template = getTemplate();
|
||||
String toBeExported = parseTemplate(template);
|
||||
|
|
|
@ -96,7 +96,7 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
public ZenodoSoftwareVersionPublisher() {}
|
||||
|
||||
protected void addFilesToDeposition(List<File> files ) throws Exception {
|
||||
String depositID = getZenodoIDFromDOIURL(softwareVersion.getVersionDOIURL());
|
||||
String depositID = getZenodoIDFromDOIURL(softwareVersionConfig.getVersionDOIURL());
|
||||
String newFilePath = DEPOSTION_FILES_PATH.replace(":id", depositID);
|
||||
URL url = new URL(zenodoBaseURL, newFilePath);
|
||||
|
||||
|
@ -131,7 +131,7 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
gxHTTPStringRequest.header("Content-Type", "application/json");
|
||||
gxHTTPStringRequest.header("Accept", "application/json");
|
||||
|
||||
String id = getZenodoIDFromDOIURL(softwareVersion.getVersionDOIURL());
|
||||
String id = getZenodoIDFromDOIURL(softwareVersionConfig.getVersionDOIURL());
|
||||
gxHTTPStringRequest.path(DEPOSITION_PATH.replace(":id", id));
|
||||
|
||||
ObjectNode metadata = generateMetadata();
|
||||
|
@ -148,7 +148,7 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
gxHTTPStringRequest.header("Content-Type", "application/json");
|
||||
gxHTTPStringRequest.header("Accept", "application/json");
|
||||
|
||||
String id = getZenodoIDFromDOIURL(softwareVersion.getVersionDOIURL());
|
||||
String id = getZenodoIDFromDOIURL(softwareVersionConfig.getVersionDOIURL());
|
||||
gxHTTPStringRequest.path(DEPOSTION_PUBLISH_PATH.replace(":id", id));
|
||||
|
||||
HttpURLConnection httpURLConnection = gxHTTPStringRequest.post();
|
||||
|
@ -157,7 +157,7 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
|
||||
protected void finalize() throws Exception {
|
||||
List<File> files = new ArrayList<>();
|
||||
for(SoftwareVersionFile svf : softwareVersion.getFiles()) {
|
||||
for(SoftwareVersionFile svf : softwareVersionConfig.getFiles()) {
|
||||
File file = svf.downloadFile();
|
||||
files.add(file);
|
||||
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
|
||||
|
@ -272,15 +272,15 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
response = getResponse(httpURLConnection);
|
||||
|
||||
URL conceptDOIURL = createZenodoDOIURLFromID(response.get("conceptrecid").asText());
|
||||
softwareVersion.setDOIURL(conceptDOIURL);
|
||||
softwareVersionConfig.setDOIURL(conceptDOIURL);
|
||||
URL doiURL = new URL(doiBaseURL + response.get("id").asText());
|
||||
softwareVersion.setVersionDOIURL(doiURL);
|
||||
softwareVersionConfig.setVersionDOIURL(doiURL);
|
||||
|
||||
finalize();
|
||||
}
|
||||
|
||||
private ArrayNode getAuthors(){
|
||||
ArrayNode authors = softwareVersion.getAuthors().deepCopy();
|
||||
ArrayNode authors = softwareVersionConfig.getAuthors().deepCopy();
|
||||
return authors;
|
||||
}
|
||||
|
||||
|
@ -288,7 +288,7 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
StringBuffer stringBuffer = new StringBuffer();
|
||||
stringBuffer.append(processorConfig.getProperty(HTML_DESCRIPTION_CONFIG_FIELD_NAME).asText());
|
||||
|
||||
String codeLocation = softwareVersion.getCodeLocation();
|
||||
String codeLocation = softwareVersionConfig.getCodeLocation();
|
||||
if(codeLocation!=null) {
|
||||
String htmlCodeLocation = processorConfig.getProperty(HTML_CODE_LOCATION_CONFIG_FIELD_NAME).asText();
|
||||
htmlCodeLocation = Utils.replaceVariable("code_location", codeLocation, htmlCodeLocation);
|
||||
|
@ -306,7 +306,7 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
for(JsonNode idNode : arrayNode) {
|
||||
idToSkip.add(idNode.asText());
|
||||
}
|
||||
for(JsonNode g : softwareVersion.getGrants()) {
|
||||
for(JsonNode g : softwareVersionConfig.getGrants()) {
|
||||
String id = g.get("id").asText();
|
||||
if(idToSkip.contains(id)) {
|
||||
continue;
|
||||
|
@ -319,7 +319,7 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
}
|
||||
|
||||
private ArrayNode getKeywords(){
|
||||
Set<String> keywords = softwareVersion.getKeywords();
|
||||
Set<String> keywords = softwareVersionConfig.getKeywords();
|
||||
ObjectMapper objectMapper = Utils.getObjectMapper();
|
||||
ArrayNode keywordsArrayNode = objectMapper.createArrayNode();
|
||||
for(String keyword : keywords) {
|
||||
|
@ -329,11 +329,11 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
}
|
||||
|
||||
private String getLicense() {
|
||||
return softwareVersion.getLicense().get("id").asText();
|
||||
return softwareVersionConfig.getLicense().get("id").asText();
|
||||
}
|
||||
|
||||
private String getDate() {
|
||||
return Utils.getDateAsString(softwareVersion.getDate());
|
||||
return Utils.getDateAsString(softwareVersionConfig.getDate());
|
||||
}
|
||||
|
||||
private ObjectNode generateMetadata() {
|
||||
|
@ -349,8 +349,8 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
metadata.replace("keywords", getKeywords());
|
||||
metadata.put("license", getLicense());
|
||||
metadata.put("publication_date", getDate());
|
||||
metadata.put("title", softwareVersion.getTitle());
|
||||
metadata.put("version", softwareVersion.getVersion());
|
||||
metadata.put("title", softwareVersionConfig.getTitle());
|
||||
metadata.put("version", softwareVersionConfig.getVersion());
|
||||
|
||||
metadatWrapper.set(METADATA_FIELD_NAME, metadata);
|
||||
return metadatWrapper;
|
||||
|
@ -363,7 +363,7 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
gxHTTPStringRequest.from(GUCBE_ZENODO_SOFTWARE_DEPOSIT);
|
||||
gxHTTPStringRequest.queryParams(getAccessTokenQueryParamters());
|
||||
gxHTTPStringRequest.header("Accept", "application/json");
|
||||
String id = getZenodoIDFromDOIURL(softwareVersion.getVersionDOIURL());
|
||||
String id = getZenodoIDFromDOIURL(softwareVersionConfig.getVersionDOIURL());
|
||||
gxHTTPStringRequest.path(DEPOSTION_EDIT_PATH.replace(":id", id));
|
||||
HttpURLConnection httpURLConnection = gxHTTPStringRequest.post();
|
||||
getResponse(httpURLConnection);
|
||||
|
@ -402,7 +402,7 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
gxHTTPStringRequest.queryParams(getAccessTokenQueryParamters());
|
||||
gxHTTPStringRequest.header("Content-Type", "application/json");
|
||||
gxHTTPStringRequest.header("Accept", "application/json");
|
||||
String conceptID = getZenodoIDFromDOIURL(softwareVersion.getDOIURL());
|
||||
String conceptID = getZenodoIDFromDOIURL(softwareVersionConfig.getDOIURL());
|
||||
gxHTTPStringRequest.path(RECORD_PATH.replace(":id", conceptID));
|
||||
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
||||
JsonNode jsonNode = getResponse(httpURLConnection);
|
||||
|
@ -414,13 +414,13 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
* this should avoid errors on softwareConcept.
|
||||
*/
|
||||
String latestVersionDOI = jsonNode.get("links").get("doi").asText();
|
||||
String previousVersionDOI = softwareVersion.getPrevious().getVersionDOIURL().toString();
|
||||
String previousVersionDOI = softwareVersionConfig.getPrevious().getVersionDOIURL().toString();
|
||||
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 = softwareVersion.getPrevious().getVersion().toString();
|
||||
String previousVersionVersion = softwareVersionConfig.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.");
|
||||
|
@ -456,7 +456,7 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
|
||||
// The reserved DOI of this created new version will be
|
||||
URL doiURL = new URL(response.get("doi_url").asText());
|
||||
softwareVersion.setVersionDOIURL(doiURL);
|
||||
softwareVersionConfig.setVersionDOIURL(doiURL);
|
||||
|
||||
// Remove previous depositionFiles
|
||||
deletePreviousFiles();
|
||||
|
@ -486,39 +486,35 @@ public class ZenodoSoftwareVersionPublisher extends SoftwareVersionPublisher {
|
|||
public void publish() throws Exception {
|
||||
getZenodoConnectionConfig();
|
||||
|
||||
String name = softwareVersion.getName();
|
||||
String title = softwareVersionConfig.getTitle();
|
||||
|
||||
ElaborationType publish = processorConfig.getElaboration();
|
||||
|
||||
if(publish==ElaborationType.NONE) {
|
||||
logger.info("Deposit is disabled for {} {}.",
|
||||
name , softwareVersion.getVersion());
|
||||
logger.info("Deposit is disabled for {}.",title);
|
||||
return;
|
||||
}
|
||||
|
||||
if(softwareVersion.getVersionDOIURL()!=null) {
|
||||
if(softwareVersionConfig.getVersionDOIURL()!=null) {
|
||||
|
||||
softwareVersion.setNewDeposition(false);
|
||||
softwareVersionConfig.setNewDeposition(false);
|
||||
|
||||
if(publish==ElaborationType.ALL ||
|
||||
publish==ElaborationType.UPDATE_ONLY) {
|
||||
logger.info("Going to update {} {}.",
|
||||
name , softwareVersion.getVersion());
|
||||
logger.info("Going to update {}.",title);
|
||||
update();
|
||||
}else {
|
||||
logger.info("{} {} has been already deposited.",
|
||||
name , softwareVersion.getVersion());
|
||||
logger.info("{} has been already deposited.", title);
|
||||
}
|
||||
|
||||
}else {
|
||||
if(publish==ElaborationType.ALL ||
|
||||
publish==ElaborationType.NEW) {
|
||||
logger.info("Going to deposit {} {}",
|
||||
name , softwareVersion.getVersion());
|
||||
logger.info("Going to deposit {}", title);
|
||||
|
||||
softwareVersion.setNewDeposition(true);
|
||||
softwareVersionConfig.setNewDeposition(true);
|
||||
|
||||
if(softwareVersion.getDOIURL()==null) {
|
||||
if(softwareVersionConfig.getDOIURL()==null) {
|
||||
create();
|
||||
}else {
|
||||
newVersion();
|
||||
|
|
Loading…
Reference in New Issue