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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <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> <groupId>org.gcube.vremanagement</groupId>
<artifactId>vre-removed-publisher</artifactId> <artifactId>vre-removed-publisher</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>

View File

@ -15,14 +15,21 @@ public class App {
XmlManager xmlManager = new XmlManager(xmlFilePath); XmlManager xmlManager = new XmlManager(xmlFilePath);
// Parse CSV and XML files // Parse CSV and XML files
csvManager.parse(); HashMap<String, Gateway> xmlMap = xmlManager.parse();
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 // 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 // Write merged data to final XML
xmlManager.writeToXml("finalDoc.xml", mergedMap); xmlManager.writeToXml("finalDoc.xml", xmlMap);
// Methods to query and print information // Methods to query and print information
//xmlManager.getGatewaysInfo(); //xmlManager.getGatewaysInfo();
@ -31,10 +38,10 @@ public class App {
//xmlManager.findVre("D4Science.org Detached Gateway", "D4OS", "CNROutreach"); //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 // Implement merging logic here
// For simplicity, assume overwrite logic: CSV data overwrites XML data // For simplicity, assume overwrite logic: CSV data overwrites XML data
// You may need to implement more complex merging based on your requirements // 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; package org.gcube.vremanagement;
import com.opencsv.CSVReader; import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException; import com.opencsv.exceptions.CsvValidationException;
import java.io.FileReader; import java.io.FileReader;
@ -10,14 +11,14 @@ import java.util.HashMap;
public class CsvManager { public class CsvManager {
private String csvFilePath; private String csvFilePath;
private HashMap<String, ArrayList<Gateway>> gatewayMap; private HashMap<String, Gateway> gatewayMap;
public CsvManager(String csvFilePath) { public CsvManager(String csvFilePath) {
this.csvFilePath = csvFilePath; this.csvFilePath = csvFilePath;
this.gatewayMap = new HashMap<>(); this.gatewayMap = new HashMap<>();
} }
public void parse() { public HashMap<String, Gateway> parse() {
try (CSVReader reader = new CSVReader(new FileReader(csvFilePath))) { try (CSVReader reader = new CSVReader(new FileReader(csvFilePath))) {
String[] headers = reader.readNext(); // Read headers (optional, depending on usage) String[] headers = reader.readNext(); // Read headers (optional, depending on usage)
String[] line; String[] line;
@ -31,7 +32,7 @@ public class CsvManager {
String description = line[6]; String description = line[6];
// Extract gateway name from URL // Extract gateway name from URL
String gatewayName = extractGatewayName(gateways); String gatewayName = this.extractGatewayName(gateways);
// Ensure gateway exists in the HashMap // Ensure gateway exists in the HashMap
Gateway gateway = gatewayMap.computeIfAbsent(gatewayName, k -> new ArrayList<>()).stream() Gateway gateway = gatewayMap.computeIfAbsent(gatewayName, k -> new ArrayList<>()).stream()
@ -67,12 +68,16 @@ public class CsvManager {
}}); }});
} }
} }
return gatewayMap;
} catch (IOException | CsvValidationException e) { } catch (IOException | CsvValidationException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null;
} }
public HashMap<String, ArrayList<Gateway>> getGatewayMap() { public HashMap<String, Gateway> getGatewayMap() {
return gatewayMap; return gatewayMap;
} }

View File

@ -1,8 +1,11 @@
package org.gcube.vremanagement; package org.gcube.vremanagement;
import java.util.ArrayList;
import java.util.HashMap;
public interface VreRemPubInterface { 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) 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 //add here other common methods needed by both Csv and Xml Manager
void find(); void find();

View File

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