diff --git a/.classpath b/.classpath
index f7e4a1d..273e0c6 100755
--- a/.classpath
+++ b/.classpath
@@ -19,13 +19,6 @@
-
-
-
-
-
-
-
diff --git a/pom.xml b/pom.xml
index 5992b82..913d26b 100755
--- a/pom.xml
+++ b/pom.xml
@@ -62,7 +62,11 @@
slf4j-api
provided
-
+
+ com.opencsv
+ opencsv
+ 5.5.2
+
diff --git a/src/main/java/org/gcube/vremanagement/App.java b/src/main/java/org/gcube/vremanagement/App.java
index faec466..21df603 100755
--- a/src/main/java/org/gcube/vremanagement/App.java
+++ b/src/main/java/org/gcube/vremanagement/App.java
@@ -3,6 +3,7 @@ package org.gcube.vremanagement;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
public class App {
@@ -10,12 +11,20 @@ public class App {
// Create here the CSV and XML managers object to parse and sync the XML with the CSV file
//CsvManager csvManager = new CsvManager("/home/biniam/eclipse-workspace/vre-removed-publisher/src/main/resources/updated_VREDecommisioned-240326.csv");
String filePath = "src/main/resources";
- String filename = "doc.xml";
- XmlManager xmlManager = new XmlManager(filePath+File.separator+filename);
-
+ String filename = "src/main/resources/doc.xml";
+ String csvFilename = "src/main/resources/updated_VREDecommisioned-240326.csv";
+ XmlManager xmlManager = new XmlManager(filename);
+ CsvManager csvManager= new CsvManager(csvFilename);
+
// Parse CSV and XML files
- //csvManager.parse();
+
+ csvManager.parse();
+
xmlManager.parse();
+ List> csvData = csvManager.readCSV(csvFilename);
+ csvManager.printCSVData(csvData);
+ xmlManager.appendNewDataToXML(csvData,filename);
+
// Parse XML to HashMap
// HashMap> gatewayMap = xmlManager.parseToHashMap();
@@ -46,14 +55,20 @@ public class App {
}*/
// methods to query and print information
- // xmlManager.getGatewaysInfo();
- //xmlManager.getVosInfo("AGINFRAPlus Detached Gateway");
+ //xmlManager.getGatewaysInfo();
+ //xmlManager.getVosInfo("AGINFRAPlus Detached Gateway");
//xmlManager.getVosInfo("PARTHENOS Detached Gateway");
- xmlManager.getVosInfo("BlueBridge Gateway");
+ //xmlManager.getVosInfo("BlueBridge Gateway");
//xmlManager.getVosInfo("D4Science.org Detached Gateway");
//xmlManager.getVosInfo("EOSC-Secretariat Detached Gateway");
//xmlManager.findVo("BlueBridge Gateway", "FARM");
//xmlManager.findVre("D4Science.org Detached Gateway", "D4OS", "CNROutreach");
+ // methods to query and print information for csv
+ //csvManager.getGatewaysInfo();
+
+
}
+
}
+
diff --git a/src/main/java/org/gcube/vremanagement/CsvManager.java b/src/main/java/org/gcube/vremanagement/CsvManager.java
index 4f8ef45..aa6cb58 100755
--- a/src/main/java/org/gcube/vremanagement/CsvManager.java
+++ b/src/main/java/org/gcube/vremanagement/CsvManager.java
@@ -1,65 +1,119 @@
package org.gcube.vremanagement;
import java.io.BufferedReader;
+import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
-import com.opencsv.CSVParser;
-
+import com.opencsv.CSVReader;
+import com.opencsv.exceptions.CsvValidationException;
public class CsvManager implements VreRemPubInterface {
private String filePath;
- private ArrayList dataList;
+ private HashMap gatewaysMap;
public CsvManager(String filePath) {
this.filePath = filePath;
- System.out.println("CSV manager created!");
- this.dataList = new ArrayList<>();
+ this.gatewaysMap = new HashMap<>();
+
}
+
+
+
+
+ //CSVMANGAE
+ public List> readCSV(String filePath) {
+ List> csvData = new ArrayList<>();
+ try (CSVReader reader = new CSVReader(new FileReader(filePath))) {
+ String[] headers;
+ try {
+ headers = reader.readNext();
+ String[] nextLine;
+ while ((nextLine = reader.readNext()) != null) {
+ HashMap row = new HashMap<>();
+ for (int i = 0; i < headers.length; i++) {
+ row.put(headers[i], nextLine[i]);
+ }
+ csvData.add(row);
+ }
+ } catch (CsvValidationException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
- @Override
- public void parse() {
- try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
- String line;
- while ((line = br.readLine()) != null) {
- String[] values = line.split(",");
- dataList.add(values);
- }
- System.out.println("CSV file parsed successfully.");
- // Optionally, print the data here
- System.out.println("Parsed CSV data: " + dataList);
} catch (IOException e) {
e.printStackTrace();
}
+ return csvData;
}
+ public static void printCSVData(List> csvData) {
+ if (csvData.isEmpty()) {
+ System.out.println("No data to display.");
+ return;
+ }
+ // Define the headers in the desired order
+ String[] orderedHeaders = {
+ "Ticket #", "Status", "Subject", "VRE Names", "VO", "Gateways",
+ "Ticket Closed", "Start Time", "End Time", "Managers", "Description"
+ };
+// Print headers
+ for (String header : orderedHeaders) {
+ System.out.print(header + "\t");
+ }
+ System.out.println();
+// Print rows
+ for (HashMap row : csvData) {
+ for (String header : orderedHeaders) {
+ System.out.print(row.getOrDefault(header, "") + "\t");
+ }
+ System.out.println();
+ }
+ }
@Override
public void update() {
// Implement update logic if needed
}
+ @Override
+ public void find() {
+ // Implement find logic if needed
+ }
+
+ @Override
+ public void read() {
+ // Implement read logic if needed
+ }
+
+ @Override
+ public void write() {
+ // Implement write logic if needed
+ }
+
@Override
- public void find() {
+ public void parse() {
// TODO Auto-generated method stub
}
- @Override
- public void read() {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void write() {
- // TODO Auto-generated method stub
-
- }
-
- // Other methods...
+
}
+
+
+
diff --git a/src/main/java/org/gcube/vremanagement/Vres.java b/src/main/java/org/gcube/vremanagement/Vres.java
index 2c5696e..8246f06 100755
--- a/src/main/java/org/gcube/vremanagement/Vres.java
+++ b/src/main/java/org/gcube/vremanagement/Vres.java
@@ -44,6 +44,26 @@ public class Vres {
public String getEndDate() {
return endDate;
}
+ // Setter methods
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+ public void setManager(String manager) {
+ this.manager = manager;
+ }
+
+ public void setStartDate(String startDate) {
+ this.startDate = startDate;
+ }
+
+ public void setEndDate(String endDate) {
+ this.endDate = endDate;
+ }
// Method to print VRE information
public void printInfo() {
diff --git a/src/main/java/org/gcube/vremanagement/XmlManager.java b/src/main/java/org/gcube/vremanagement/XmlManager.java
index d290d3d..752f83a 100755
--- a/src/main/java/org/gcube/vremanagement/XmlManager.java
+++ b/src/main/java/org/gcube/vremanagement/XmlManager.java
@@ -11,6 +11,10 @@ import java.util.stream.Collectors;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.w3c.dom.Document;
@@ -134,6 +138,176 @@ public class XmlManager implements VreRemPubInterface {
e.printStackTrace();
}
}
+
+ public void appendNewDataToXML(List> data, String filePath) {
+ try {
+ DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
+ Document doc;
+ File xmlFile = new File(filePath);
+ if (xmlFile.exists()) {
+ doc = docBuilder.parse(xmlFile);
+ doc.getDocumentElement().normalize();
+ } else {
+ doc = docBuilder.newDocument();
+ Element rootElement = doc.createElement("Data");
+ doc.appendChild(rootElement);
+ }
+
+ Element root = doc.getDocumentElement();
+
+ for (HashMap rowData : data) {
+ String gatewayName = rowData.get("Gateways");
+ String gatewayDescription = rowData.get("Description");
+ Gateway gateway = gatewaysMap.get(gatewayName);
+ if (gateway == null) {
+ gateway = new Gateway(gatewayName, gatewayDescription);
+ gatewaysMap.put(gatewayName, gateway);
+ appendGatewayToXML(doc, root, gateway);
+ }
+
+ String voKey = rowData.get("vo_key");
+ String voName = rowData.get("vo_name");
+ String voScope = rowData.get("vo_scope");
+ /*Vos vos = findVosInGateway(gateway, voKey);
+ if (vos == null) {
+ vos = new Vos(voKey, voScope, voName);
+ gateway.addVos(vos);
+ appendVosToXML(doc, gateway, vos);
+ }
+
+ String vreKey = rowData.get("vre_key");
+ String vreName = rowData.get("VRE names");
+ String vreDescription = rowData.get("vre_description");
+ String vreManager = rowData.get("Managers");
+ String vreStartDate = rowData.get("Start Time");
+ String vreEndDate = rowData.get("End Time");
+
+ Vres vres = findVresInVos(vos, vreKey);
+ if (vres == null) {
+ vres = new Vres(vreKey, vreName, vreDescription, vreManager, vreStartDate, vreEndDate);
+ vos.addVres(vres);
+ //appendVresToXML(doc, vos, vres);
+ }*/
+ }
+
+ TransformerFactory transformerFactory = TransformerFactory.newInstance();
+ Transformer transformer = transformerFactory.newTransformer();
+ DOMSource source = new DOMSource(doc);
+ StreamResult result = new StreamResult(new File(filePath));
+ transformer.transform(source, result);
+
+ System.out.println("New data appended to " + filePath);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ private Vos findVosInGateway(Gateway gateway, String voKey) {
+ for (Vos vos : gateway.getVosList()) {
+ if (vos.getKey().equals(voKey)) {
+ return vos;
+ }
+ }
+ return null;
+ }
+
+ private Vres findVresInVos(Vos vos, String vreKey) {
+ for (Vres vres : vos.getVresList()) {
+ if (vres.getKey().equals(vreKey)) {
+ return vres;
+ }
+ }
+ return null;
+ }
+
+ private void appendGatewayToXML(Document doc, Element root, Gateway gateway) {
+ Element gatewayElement = doc.createElement("gateway");
+ root.appendChild(gatewayElement);
+
+ Element name = doc.createElement("name");
+ name.appendChild(doc.createTextNode(gateway.getName()));
+ gatewayElement.appendChild(name);
+
+ Element description = doc.createElement("description");
+ description.appendChild(doc.createTextNode(gateway.getDescription()));
+ gatewayElement.appendChild(description);
+ }
+
+ private void appendVosToXML(Document doc, Gateway gateway, Vos vos) {
+ Element gatewayElement = findGatewayElement(doc, gateway.getName());
+
+ Element vosElement = doc.createElement("vos");
+ gatewayElement.appendChild(vosElement);
+
+ Element key = doc.createElement("key");
+ key.appendChild(doc.createTextNode(vos.getKey()));
+ vosElement.appendChild(key);
+
+ Element name = doc.createElement("name");
+ name.appendChild(doc.createTextNode(vos.getName()));
+ vosElement.appendChild(name);
+
+ Element scope = doc.createElement("scope");
+ scope.appendChild(doc.createTextNode(vos.getScope()));
+ vosElement.appendChild(scope);
+ }
+
+ private void appendVresToXML(Document doc, Vos vos, Vres vres) {
+ Element vosElement = findVosElement(doc, vos.getKey());
+
+ Element vresElement = doc.createElement("vres");
+ vosElement.appendChild(vresElement);
+
+ Element key = doc.createElement("key");
+ key.appendChild(doc.createTextNode(vres.getKey()));
+ vresElement.appendChild(key);
+
+ Element name = doc.createElement("name");
+ name.appendChild(doc.createTextNode(vres.getName()));
+ vresElement.appendChild(name);
+
+ Element description = doc.createElement("description");
+ description.appendChild(doc.createTextNode(vres.getDescription()));
+ vresElement.appendChild(description);
+
+ Element managers = doc.createElement("managers");
+ managers.appendChild(doc.createTextNode(vres.getManager()));
+ vresElement.appendChild(managers);
+
+ Element startDate = doc.createElement("startdate");
+ startDate.appendChild(doc.createTextNode(vres.getStartDate()));
+ vresElement.appendChild(startDate);
+
+ Element endDate = doc.createElement("enddate");
+ endDate.appendChild(doc.createTextNode(vres.getEndDate()));
+ vresElement.appendChild(endDate);
+ }
+
+ private Element findGatewayElement(Document doc, String gatewayName) {
+ NodeList gatewayList = doc.getElementsByTagName("gateway");
+ for (int i = 0; i < gatewayList.getLength(); i++) {
+ Element gatewayElement = (Element) gatewayList.item(i);
+ String name = gatewayElement.getElementsByTagName("name").item(0).getTextContent().trim();
+ if (name.equals(gatewayName)) {
+ return gatewayElement;
+ }
+ }
+ return null;
+ }
+
+ private Element findVosElement(Document doc, String voKey) {
+ NodeList vosList = doc.getElementsByTagName("vos");
+ for (int i = 0; i < vosList.getLength(); i++) {
+ Element vosElement = (Element) vosList.item(i);
+ String key = vosElement.getElementsByTagName("key").item(0).getTextContent().trim();
+ if (key.equals(voKey)) {
+ return vosElement;
+ }
+ }
+ return null;
+ }
public void getGatewaysInfo() {
System.out.println("Name of the Gateway:");
gatewaysMap.keySet().stream().filter(gatewayName -> gatewayName.endsWith("Gateway"))
diff --git a/src/main/resources/doc.xml b/src/main/resources/doc.xml
index 2f9bb0f..2fe8ddc 100755
--- a/src/main/resources/doc.xml
+++ b/src/main/resources/doc.xml
@@ -1,5 +1,4 @@
-
-
+
68322b8f-b916-44b9-9c93-bad8cceb9106
GenericResource
@@ -1096,4 +1095,25 @@