code refactoring and suggestion for the csv parsing and merging

This commit is contained in:
Marco Procaccini 2024-06-21 18:08:34 +02:00
parent 9a28194ef9
commit ac9f9219bd
5 changed files with 31 additions and 20 deletions

View File

@ -3,12 +3,6 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.gcube.tools</groupId>
<artifactId>maven-parent</artifactId>
<version>1.1.0</version>
</parent>
<groupId>org.gcube.vremanagement</groupId>
<artifactId>vre-removed-publisher</artifactId>
<version>1.0.0-SNAPSHOT</version>

View File

@ -15,14 +15,21 @@ public class App {
XmlManager xmlManager = new XmlManager(xmlFilePath);
// Parse CSV and XML files
csvManager.parse();
xmlManager.parse();
HashMap<String, Gateway> xmlMap = xmlManager.parse();
HashMap<String, Gateway> csvMap = csvManager.parse();
//Merging csvMap into the xmlMap
//Note that: in case of duplicate elements, the csvMap elements will overwrite the xmlMap
//this is ok for us because the csv elements are mode recent than xml element
//check this behaviour in the final xml doc
xmlMap.putAll(csvMap);
// Merge CSV data into XML data structure
HashMap<String, ArrayList<Gateway>> mergedMap = mergeData(xmlManager.parseToHashMap(), csvManager.getGatewayMap());
// HashMap<String, ArrayList<Gateway>> mergedMap = mergeData(xmlManager.parseToHashMap(), csvManager.getGatewayMap());
// Write merged data to final XML
xmlManager.writeToXml("finalDoc.xml", mergedMap);
xmlManager.writeToXml("finalDoc.xml", xmlMap);
// Methods to query and print information
//xmlManager.getGatewaysInfo();
@ -31,10 +38,10 @@ public class App {
//xmlManager.findVre("D4Science.org Detached Gateway", "D4OS", "CNROutreach");
}
private static HashMap<String, ArrayList<Gateway>> mergeData(HashMap<String, ArrayList<Gateway>> xmlData, HashMap<String, ArrayList<Gateway>> csvData) {
// private static HashMap<String, ArrayList<Gateway>> mergeData(HashMap<String, ArrayList<Gateway>> xmlData, HashMap<String, ArrayList<Gateway>> csvData) {
// Implement merging logic here
// For simplicity, assume overwrite logic: CSV data overwrites XML data
// You may need to implement more complex merging based on your requirements
return csvData;
}
// return csvData;
// }
}

View File

@ -1,6 +1,7 @@
package org.gcube.vremanagement;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import java.io.FileReader;
@ -10,14 +11,14 @@ import java.util.HashMap;
public class CsvManager {
private String csvFilePath;
private HashMap<String, ArrayList<Gateway>> gatewayMap;
private HashMap<String, Gateway> gatewayMap;
public CsvManager(String csvFilePath) {
this.csvFilePath = csvFilePath;
this.gatewayMap = new HashMap<>();
}
public void parse() {
public HashMap<String, Gateway> parse() {
try (CSVReader reader = new CSVReader(new FileReader(csvFilePath))) {
String[] headers = reader.readNext(); // Read headers (optional, depending on usage)
String[] line;
@ -31,7 +32,7 @@ public class CsvManager {
String description = line[6];
// Extract gateway name from URL
String gatewayName = extractGatewayName(gateways);
String gatewayName = this.extractGatewayName(gateways);
// Ensure gateway exists in the HashMap
Gateway gateway = gatewayMap.computeIfAbsent(gatewayName, k -> new ArrayList<>()).stream()
@ -67,12 +68,16 @@ public class CsvManager {
}});
}
}
return gatewayMap;
} catch (IOException | CsvValidationException e) {
e.printStackTrace();
}
return null;
}
public HashMap<String, ArrayList<Gateway>> getGatewayMap() {
public HashMap<String, Gateway> getGatewayMap() {
return gatewayMap;
}

View File

@ -1,8 +1,11 @@
package org.gcube.vremanagement;
import java.util.ArrayList;
import java.util.HashMap;
public interface VreRemPubInterface {
void parse(); //method used to parse the file (e.g., xml or csv) in a data structure
HashMap<String, Gateway> parse(); //method used to parse the file (e.g., xml or csv) in a data structure
void update(); //method used to update the file (e.g., xml or csv)
//add here other common methods needed by both Csv and Xml Manager
void find();

View File

@ -27,7 +27,7 @@ public class XmlManager implements VreRemPubInterface {
}
@Override
public void parse() {
public HashMap<String, Gateway> parse() {
try {
File xmlFile = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
@ -135,9 +135,11 @@ public class XmlManager implements VreRemPubInterface {
//computeIfAbsent(gatewayName, k -> new ArrayList<>()).add(gateway);
}
}
return gatewaysMap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void getGatewaysInfo() {
System.out.println("Name of the Gateway:");