Refactoring

This commit is contained in:
Luca Frosini 2023-01-27 16:34:24 +01:00
parent 4119f3f444
commit 7961619ac0
7 changed files with 126 additions and 80 deletions

View File

@ -1,6 +1,5 @@
package org.gcube.common.software.analyser;
import java.io.File;
import java.net.URL;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@ -20,26 +19,40 @@ import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ConfigAnalyser {
public class Analyser {
private static final Logger logger = LoggerFactory.getLogger(ConfigAnalyser.class);
private static final Logger logger = LoggerFactory.getLogger(Analyser.class);
public static final String EXPORT_FILENAME_EXTENSION = ".json";
public static final String GLOBAL_PROPERTY_NAME = "global";
public static final String VERSIONS_PROPERTY_NAME = "versions";
protected ObjectMapper objectMapper;
protected JsonNode jsonNode;
public ConfigAnalyser(File jsonFile) throws Exception {
protected ObjectNode originalGlobal;
protected ArrayNode originalVersions;
protected ArrayNode exportingVersions;
public Analyser() throws Exception {
this.objectMapper = Utils.getObjectMapper();
this.jsonNode = objectMapper.readTree(jsonFile);
}
public ConfigAnalyser(String json) throws Exception {
this.objectMapper = Utils.getObjectMapper();
this.jsonNode = objectMapper.readTree(json);
public ObjectNode getOriginalGlobal() {
return originalGlobal;
}
public void setOriginalGlobal(ObjectNode originalGlobal) {
this.originalGlobal = originalGlobal.deepCopy();
}
public ArrayNode getOriginalVersions() {
return originalVersions;
}
public void setOriginalVersions(ArrayNode originalVersions) {
this.originalVersions = originalVersions.deepCopy();
}
public ArrayNode getExportingVersions() {
return exportingVersions;
}
protected SoftwareVersionConfig actualizeSoftwareVersionConfig(JsonNode version) throws Exception {
@ -59,32 +72,24 @@ public class ConfigAnalyser {
Variables variables = objectMapper.treeToValue(node, Variables.class);
variables.parse();
JsonNode sc = objectMapper.convertValue(variables.getProperties(), JsonNode.class);
GlobalConfig softwareConfig = objectMapper.treeToValue(sc, GlobalConfig.class);
return softwareConfig;
return objectMapper.treeToValue(sc, GlobalConfig.class);
}
public void analyse() throws Exception {
ObjectNode originalGlobal = (ObjectNode) jsonNode.get(GLOBAL_PROPERTY_NAME).deepCopy();
GlobalConfig globalConfig = actualizeGlobalConfig(originalGlobal);
globalConfig.setOriginalJson(originalGlobal);
ObjectNode global = originalGlobal.deepCopy();
global.remove(GlobalConfig.EXPORT_FILENAME_PROPERTY_NAME);
ArrayNode versions = (ArrayNode) jsonNode.get(VERSIONS_PROPERTY_NAME);
SoftwareVersionConfig previous = null;
int i = 0;
ArrayNode exportingVersions = objectMapper.createArrayNode();
exportingVersions = objectMapper.createArrayNode();
try {
for(i=0; i<versions.size(); i++) {
ObjectNode originalVersion = (ObjectNode) versions.get(i).deepCopy();
for(i=0; i<originalVersions.size(); i++) {
ObjectNode originalVersion = (ObjectNode) originalVersions.get(i).deepCopy();
exportingVersions.add(originalVersion);
JsonNode version = Utils.merge(global, originalVersion);
JsonNode version = Utils.merge(originalGlobal, originalVersion);
SoftwareVersionConfig softwareVersion = actualizeSoftwareVersionConfig(version);
softwareVersion.setOriginalJson(originalVersion);
@ -120,21 +125,19 @@ public class ConfigAnalyser {
}
if(newDOI) {
globalConfig.addAdditionalProperty(SoftwareVersionConfig.DOI_URL_PROPERTY_NAME, new TextNode(doiURL.toString()));
globalConfig.addProperty(SoftwareVersionConfig.DOI_URL_PROPERTY_NAME, new TextNode(doiURL.toString()));
originalGlobal.put(SoftwareVersionConfig.DOI_URL_PROPERTY_NAME, doiURL.toString());
}
}
Thread.sleep(TimeUnit.SECONDS.toMillis(2));
}
} catch (Exception e) {
for(int j=i+1; j<versions.size(); j++) {
ObjectNode originalVersion = (ObjectNode) versions.get(j).deepCopy();
for(int j=i+1; j<originalVersions.size(); j++) {
ObjectNode originalVersion = (ObjectNode) originalVersions.get(j).deepCopy();
exportingVersions.add(originalVersion);
}
throw e;
}finally {
ObjectNode toBeExported = objectMapper.createObjectNode();
toBeExported.replace(GLOBAL_PROPERTY_NAME, originalGlobal);
// if(originalGlobal.has(SoftwareGlobalConfig.DOI_URL_PROPERTY_NAME)
// && originalGlobal.get(SoftwareGlobalConfig.DOI_URL_PROPERTY_NAME).getNodeType()!=JsonNodeType.NULL) {
@ -152,13 +155,6 @@ public class ConfigAnalyser {
// }
// }
toBeExported.replace(VERSIONS_PROPERTY_NAME, exportingVersions);
String fileName = globalConfig.getExportFileName();
File file = new File(fileName+EXPORT_FILENAME_EXTENSION);
objectMapper.writeValue(file, toBeExported);
}
}

View File

@ -0,0 +1,78 @@
package org.gcube.common.software.analyser;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
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.common.software.utils.Utils;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class AnalyserFactory {
public static final String EXPORT_FILENAME_EXTENSION = ".json";
public static final String GLOBAL_PROPERTY_NAME = "global";
public static final String VERSIONS_PROPERTY_NAME = "versions";
public static Analyser getAnalyser(File jsonFile) throws Exception {
ObjectMapper objectMapper = Utils.getObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonFile);
return getAnalyser(jsonNode);
}
public static Analyser getAnalyser(String json) throws Exception {
ObjectMapper objectMapper = Utils.getObjectMapper();
JsonNode jsonNode = objectMapper.readTree(json);
return getAnalyser(jsonNode);
}
public static Analyser getAnalyser(JsonNode jsonNode) throws Exception {
Analyser analyser = new Analyser();
ObjectNode originalGlobal = (ObjectNode) jsonNode.get(GLOBAL_PROPERTY_NAME);
analyser.setOriginalGlobal(originalGlobal);
ArrayNode originalVersions = (ArrayNode) jsonNode.get(VERSIONS_PROPERTY_NAME);
analyser.setOriginalVersions(originalVersions);
return analyser;
}
public static ObjectNode getAnalysResultAsObjectNode(Analyser analyser) throws Exception {
ObjectMapper objectMapper = Utils.getObjectMapper();
ObjectNode toBeExported = objectMapper.createObjectNode();
toBeExported.replace(GLOBAL_PROPERTY_NAME, analyser.getOriginalGlobal());
toBeExported.replace(VERSIONS_PROPERTY_NAME, analyser.getExportingVersions());
return toBeExported;
}
public static String getExportFileName(String fileName) {
if(fileName==null || fileName.length()==0) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
return simpleDateFormat.format(Calendar.getInstance().getTime()) + EXPORT_FILENAME_EXTENSION;
}
return fileName;
}
public static void writeAnalysResultToFile(Analyser analyser) throws Exception {
String fileName = getExportFileName(null);
File file = new File(fileName);
writeAnalysResultToFile(analyser, file);
}
public static void writeAnalysResultToFile(Analyser analyser, File file) throws Exception {
ObjectNode toBeExported = getAnalysResultAsObjectNode(analyser);
writeObjectNodeToFile(toBeExported, file);
}
public static void writeObjectNodeToFile(ObjectNode toBeExported, File file) throws Exception {
ObjectMapper objectMapper = Utils.getObjectMapper();
objectMapper.writeValue(file, toBeExported);
}
}

View File

@ -1,15 +1,11 @@
package org.gcube.common.software.model;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.Map;
import org.gcube.com.fasterxml.jackson.annotation.JsonAnyGetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonAnySetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
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.ObjectNode;
@ -18,36 +14,13 @@ import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
*/
public class GlobalConfig {
public static final String EXPORT_FILENAME_PROPERTY_NAME = "export_filename";
@JsonProperty(EXPORT_FILENAME_PROPERTY_NAME)
protected String exportFileName;
protected Map<String, JsonNode> additionalProperties;
protected Map<String, JsonNode> properties;
@JsonIgnore
protected ObjectNode originalJson;
public GlobalConfig() {
this.additionalProperties = new LinkedHashMap<>();
}
@JsonIgnore
public String getExportFileName() {
if(exportFileName==null) {
if(additionalProperties.containsKey(EXPORT_FILENAME_PROPERTY_NAME)) {
exportFileName = additionalProperties.get(EXPORT_FILENAME_PROPERTY_NAME).asText();
}else {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
exportFileName = simpleDateFormat.format(Calendar.getInstance().getTime());
}
}
return exportFileName;
}
@JsonSetter(EXPORT_FILENAME_PROPERTY_NAME)
public void setExportFileName(String exportFileName) {
this.exportFileName = exportFileName;
this.properties = new LinkedHashMap<>();
}
@JsonIgnore
@ -61,18 +34,18 @@ public class GlobalConfig {
}
@JsonAnyGetter
public Map<String, JsonNode> getAdditionalProperties() {
return additionalProperties;
public Map<String, JsonNode> getProperties() {
return properties;
}
@JsonAnySetter
public void addAdditionalProperty(String key, JsonNode value) {
this.additionalProperties.put(key, value);
public void addProperty(String key, JsonNode value) {
this.properties.put(key, value);
}
@JsonIgnore
public JsonNode getAdditionalProperty(String key) {
return this.additionalProperties.get(key);
public JsonNode getProperty(String key) {
return this.properties.get(key);
}
}

View File

@ -17,8 +17,10 @@ public class SoftwareConceptAnalyserTest {
@Test
public void testUsingTestFile() throws Exception {
File file = FileUtils.getFileFromFilename(FILENAME);
ConfigAnalyser softwareConceptAnalyser = new ConfigAnalyser(file);
softwareConceptAnalyser.analyse();
Analyser analyser = AnalyserFactory.getAnalyser(file);
analyser.analyse();
File exportFile = new File("exported-"+file.getName());
AnalyserFactory.writeAnalysResultToFile(analyser, exportFile);
}
}

View File

@ -149,8 +149,7 @@
"BibLaTeXSoftwareVersionExporter": {
"elaboration": "ALL"
}
},
"export_filename": "{{name}}"
}
},
"versions":
[

View File

@ -139,8 +139,7 @@
"BibLaTeXSoftwareVersionExporter": {
"elaboration": "ALL"
}
},
"export_filename": "{{name}}"
}
},
"versions": [
{

View File

@ -139,8 +139,7 @@
"BibLaTeXSoftwareVersionExporter": {
"elaboration": "ALL"
}
},
"export_filename": "{{name}}"
}
},
"versions": [
{