79 lines
2.8 KiB
Java
79 lines
2.8 KiB
Java
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);
|
|
}
|
|
}
|