information from csv is read and merged into HashMap and new xml is creatd from this final hashmap
This commit is contained in:
parent
f0eda78d17
commit
9a28194ef9
|
@ -3,72 +3,38 @@ package org.gcube.vremanagement;
|
|||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 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 = "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();
|
||||
|
||||
xmlManager.parse();
|
||||
List<HashMap<String, String>> csvData = csvManager.readCSV(csvFilename);
|
||||
//csvManager.printCSVData(csvData);
|
||||
xmlManager.appendNewDataToXML(csvData);
|
||||
|
||||
|
||||
// Parse XML to HashMap
|
||||
// HashMap<String, ArrayList<Gateway>> gatewayMap = xmlManager.parseToHashMap();
|
||||
|
||||
/*for (String gatewayName: gatewayMap.keySet())
|
||||
{
|
||||
ArrayList<Gateway> listGateway = gatewayMap.get(gatewayName);
|
||||
for (Gateway gate : listGateway)
|
||||
{
|
||||
System.out.println("Gateway name: "+gate.getName());
|
||||
System.out.println("Gateway description: "+gate.getDescription());
|
||||
ArrayList<Vos> vosList = gate.getVosList();
|
||||
for (Vos voTemp: vosList)
|
||||
{
|
||||
ArrayList<Vres> vreList = voTemp.getVresList();
|
||||
System.out.println("VO name:" +voTemp.getName());
|
||||
System.out.println("VO description: "+voTemp.getDescription());
|
||||
for(Vres vreTemp:vreList)
|
||||
{
|
||||
System.out.println("VRE name: "+vreTemp.getName());
|
||||
System.out.println("Vre description: "+vreTemp.getDescription());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}*/
|
||||
// methods to query and print information
|
||||
//xmlManager.getGatewaysInfo();
|
||||
//xmlManager.getVosInfo("AGINFRAPlus Detached Gateway");
|
||||
//xmlManager.getVosInfo("PARTHENOS Detached 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();
|
||||
String csvFilePath = "src/main/resources/updated_VREDecommisioned-240326.csv";
|
||||
String xmlFilePath = "src/main/resources/doc.xml";
|
||||
|
||||
|
||||
// Initialize managers
|
||||
CsvManager csvManager = new CsvManager(csvFilePath);
|
||||
XmlManager xmlManager = new XmlManager(xmlFilePath);
|
||||
|
||||
// Parse CSV and XML files
|
||||
csvManager.parse();
|
||||
xmlManager.parse();
|
||||
|
||||
// Merge CSV data into XML data structure
|
||||
HashMap<String, ArrayList<Gateway>> mergedMap = mergeData(xmlManager.parseToHashMap(), csvManager.getGatewayMap());
|
||||
|
||||
// Write merged data to final XML
|
||||
xmlManager.writeToXml("finalDoc.xml", mergedMap);
|
||||
|
||||
// Methods to query and print information
|
||||
//xmlManager.getGatewaysInfo();
|
||||
//xmlManager.getVosInfo("D4Science.org Detached Gateway");
|
||||
//xmlManager.findVo("D4Science.org Detached Gateway", "gCubeApps");
|
||||
//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) {
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,119 +1,88 @@
|
|||
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.CSVReader;
|
||||
import com.opencsv.exceptions.CsvValidationException;
|
||||
|
||||
public class CsvManager implements VreRemPubInterface {
|
||||
private String filePath;
|
||||
private HashMap<String, Gateway> gatewaysMap;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public CsvManager(String filePath) {
|
||||
this.filePath = filePath;
|
||||
this.gatewaysMap = new HashMap<>();
|
||||
|
||||
public class CsvManager {
|
||||
private String csvFilePath;
|
||||
private HashMap<String, ArrayList<Gateway>> gatewayMap;
|
||||
|
||||
public CsvManager(String csvFilePath) {
|
||||
this.csvFilePath = csvFilePath;
|
||||
this.gatewayMap = new HashMap<>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void parse() {
|
||||
try (CSVReader reader = new CSVReader(new FileReader(csvFilePath))) {
|
||||
String[] headers = reader.readNext(); // Read headers (optional, depending on usage)
|
||||
String[] line;
|
||||
while ((line = reader.readNext()) != null) {
|
||||
String vreNames = line[0];
|
||||
String vo = line[1];
|
||||
String gateways = line[2];
|
||||
String startTime = line[3];
|
||||
String endTime = line[4];
|
||||
String managers = line[5];
|
||||
String description = line[6];
|
||||
|
||||
//CSVMANGAE
|
||||
public List<HashMap<String, String>> readCSV(String filePath) {
|
||||
List<HashMap<String, String>> 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<String, String> 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();
|
||||
}
|
||||
// Extract gateway name from URL
|
||||
String gatewayName = extractGatewayName(gateways);
|
||||
|
||||
} catch (IOException e) {
|
||||
// Ensure gateway exists in the HashMap
|
||||
Gateway gateway = gatewayMap.computeIfAbsent(gatewayName, k -> new ArrayList<>()).stream()
|
||||
.findFirst().orElse(new Gateway(gatewayName, "Description of " + gatewayName + " gateway"));
|
||||
|
||||
// Ensure VO exists in the gateway's VOs list
|
||||
Vos voObject = gateway.getVosList().stream()
|
||||
.filter(voTemp -> voTemp.getName().equals(vo))
|
||||
.findFirst().orElse(new Vos("/d4science.research-infrastructures.eu/" + vo, vo));
|
||||
|
||||
// Ensure VRE does not already exist (unique VRE names across all VOs and gateways)
|
||||
boolean vreExists = voObject.getVresList().stream()
|
||||
.anyMatch(vres -> vres.getName().equals(vreNames));
|
||||
|
||||
if (!vreExists) {
|
||||
Vres vres = new Vres("/d4science.research-infrastructures.eu/" + vo + "/" + vreNames, vreNames);
|
||||
vres.setDescription(description);
|
||||
vres.setManagers(new ArrayList<String>() {{
|
||||
add(managers);
|
||||
}});
|
||||
vres.setStartDate(startTime);
|
||||
vres.setEndDate(endTime);
|
||||
voObject.getVresList().add(vres);
|
||||
}
|
||||
|
||||
if (!gateway.getVosList().contains(voObject)) {
|
||||
gateway.getVosList().add(voObject);
|
||||
}
|
||||
|
||||
if (!gatewayMap.containsKey(gatewayName)) {
|
||||
gatewayMap.put(gatewayName, new ArrayList<Gateway>() {{
|
||||
add(gateway);
|
||||
}});
|
||||
}
|
||||
}
|
||||
} catch (IOException | CsvValidationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return csvData;
|
||||
}
|
||||
public static void printCSVData(List<HashMap<String, String>> 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<String, String> row : csvData) {
|
||||
for (String header : orderedHeaders) {
|
||||
System.out.print(row.getOrDefault(header, "") + "\t");
|
||||
}
|
||||
System.out.println();
|
||||
public HashMap<String, ArrayList<Gateway>> getGatewayMap() {
|
||||
return gatewayMap;
|
||||
}
|
||||
|
||||
private String extractGatewayName(String gateways) {
|
||||
if (gateways.startsWith("https://")) {
|
||||
return gateways.substring(8, gateways.indexOf("."));
|
||||
} else if (gateways.startsWith("http://")) {
|
||||
return gateways.substring(7, gateways.indexOf("."));
|
||||
} else {
|
||||
return gateways; // fallback
|
||||
}
|
||||
}
|
||||
|
||||
@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 parse() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -35,21 +35,5 @@ public class Gateway {
|
|||
public void addVos(Vos vos) {
|
||||
listOfVos.add(vos);
|
||||
}
|
||||
public Vos findVosByName(String vosName) {
|
||||
|
||||
for (Vos vos : listOfVos) {
|
||||
|
||||
if (vos.getName().equals(vosName)) {
|
||||
|
||||
return vos;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,12 @@ public class Vos {
|
|||
}
|
||||
|
||||
|
||||
public String getKey() {
|
||||
public Vos(String string, String vo) {
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package org.gcube.vremanagement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Vres {
|
||||
private String key;
|
||||
private String name;
|
||||
|
@ -21,6 +23,10 @@ public class Vres {
|
|||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public Vres(String string, String vreNames) {
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
@ -44,26 +50,6 @@ 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() {
|
||||
|
@ -84,4 +70,24 @@ public class Vres {
|
|||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setDescription(String description2) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void setManagers(ArrayList<String> arrayList) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void setStartDate(String startTime) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void setEndDate(String endTime) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package org.gcube.vremanagement;
|
|||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
@ -12,12 +11,7 @@ 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;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
@ -40,385 +34,111 @@ public class XmlManager implements VreRemPubInterface {
|
|||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
Document doc = dBuilder.parse(xmlFile);
|
||||
doc.getDocumentElement().normalize();
|
||||
NodeList gatewayList = doc.getElementsByTagName("gateways");
|
||||
//NodeList gatewayList = gatewayNodes.item(0).getChildNodes();
|
||||
NodeList gatewayNodes = doc.getElementsByTagName("gateways");
|
||||
|
||||
NodeList gatewayList = gatewayNodes.item(0).getChildNodes();
|
||||
|
||||
//iterate over gateways child
|
||||
for(int k =0; k<gatewayList.getLength(); k++)
|
||||
{
|
||||
Node gatewayNodeT = gatewayList.item(k);
|
||||
NodeList gatewayNodeTC = gatewayList.item(k).getChildNodes();
|
||||
for(int kk=0; kk<gatewayNodeTC.getLength(); kk++)
|
||||
{
|
||||
Node gateTest = gatewayNodeTC.item(kk);
|
||||
if(gateTest.getNodeType() == Node.ELEMENT_NODE)
|
||||
{
|
||||
Element gateTestElem = (Element)gateTest;
|
||||
String gatewayName = gateTestElem.getElementsByTagName("name").item(0).getTextContent().trim();
|
||||
String gatewayDescription = gateTestElem.getElementsByTagName("description").item(0)
|
||||
.getTextContent().trim();
|
||||
|
||||
//System.out.println("Gate Name"+gatewayName);
|
||||
|
||||
Gateway gateway = new Gateway(gatewayName, gatewayDescription);
|
||||
|
||||
NodeList vosListT = gateTestElem.getElementsByTagName("vos");
|
||||
|
||||
|
||||
for (int j = 0; j < vosListT.getLength(); j++) {
|
||||
NodeList vosListC = vosListT.item(j).getChildNodes();
|
||||
for (int jj=0; jj < vosListC.getLength();jj++)
|
||||
{
|
||||
for (int i = 0; i < gatewayList.getLength(); i++) {
|
||||
Node gatewayNode = gatewayList.item(i);
|
||||
if (gatewayNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element gatewayElement = (Element) gatewayNode;
|
||||
String gatewayName = gatewayElement.getElementsByTagName("name").item(0).getTextContent().trim();
|
||||
String gatewayDescription = gatewayElement.getElementsByTagName("description").item(0)
|
||||
.getTextContent().trim();
|
||||
|
||||
//System.out.println("gateway name:"+gatewayName);
|
||||
//System.out.println("Gateway descr:"+gatewayDescription);
|
||||
Gateway gateway = new Gateway(gatewayName, gatewayDescription);
|
||||
|
||||
// Find the <vos> element within the gateway entry
|
||||
NodeList vosList = gatewayElement.getElementsByTagName("vos");
|
||||
for (int j = 0; j < vosList.getLength(); j++) {
|
||||
Node vosNode = vosList.item(j);
|
||||
if (vosNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element vosElement = (Element) vosNode;
|
||||
|
||||
Node vosListCE = vosListC.item(jj);
|
||||
if (vosListCE.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element vosElement = (Element) vosListCE;
|
||||
|
||||
|
||||
|
||||
|
||||
String voKey = vosElement.getElementsByTagName("key").item(0).getTextContent().trim();
|
||||
String voName = vosElement.getElementsByTagName("name").item(0).getTextContent().trim();
|
||||
String voScope = vosElement.getElementsByTagName("scope").item(0).getTextContent().trim();
|
||||
|
||||
Vos vos = new Vos(voKey,voScope, voName);
|
||||
|
||||
//System.out.println("VO Name:"+voName);
|
||||
|
||||
NodeList vreListT = vosElement.getElementsByTagName("vres");
|
||||
|
||||
for (int x=0; x<vreListT.getLength();x++)
|
||||
{
|
||||
NodeList vreListC = vreListT.item(x).getChildNodes();
|
||||
|
||||
|
||||
|
||||
String voKey = vosElement.getElementsByTagName("key").item(0).getTextContent().trim();
|
||||
String voName = vosElement.getElementsByTagName("name").item(0).getTextContent().trim();
|
||||
String voScope = vosElement.getElementsByTagName("scope").item(0).getTextContent().trim();
|
||||
|
||||
//getting vo
|
||||
Vos vos = new Vos(voKey,voScope, voName);
|
||||
|
||||
//vos.printInfo();
|
||||
|
||||
// Iterate on the vres..
|
||||
//NodeList vreList = vosElement.getElementsByTagName("vres");
|
||||
//
|
||||
//for (int k = 0; k < vreList.getLength(); k++) {
|
||||
// Node voNode = voList.item(k);
|
||||
// if (voNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
// Element voElement = (Element) voNode;
|
||||
|
||||
for(int xx=0; xx<vreListC.getLength();xx++)
|
||||
{
|
||||
Node vreListCE = vreListC.item(xx);
|
||||
if(vreListCE.getNodeType()==Node.ELEMENT_NODE)
|
||||
{
|
||||
Element vreEntryElement = (Element) vreListCE;
|
||||
String vreKey = vreEntryElement.getElementsByTagName("key").item(0)
|
||||
.getTextContent().trim();
|
||||
String vreName = vreEntryElement.getElementsByTagName("name")
|
||||
.item(0).getTextContent().trim();
|
||||
String vreDescription = vreEntryElement
|
||||
.getElementsByTagName("description").item(0)
|
||||
.getTextContent().trim();
|
||||
String vreManager = vreEntryElement.getElementsByTagName("managers")
|
||||
.item(0).getTextContent().trim();
|
||||
String vreStartDate = vreEntryElement
|
||||
.getElementsByTagName("startdate").item(0).getTextContent().trim();
|
||||
String vreEndDate = vreEntryElement.getElementsByTagName("enddate")
|
||||
.item(0).getTextContent().trim();
|
||||
|
||||
// String voName = voElement.getElementsByTagName("name").item(0).getTextContent().trim();
|
||||
// String voScope = voElement.getElementsByTagName("scope").item(0).getTextContent().trim();
|
||||
// String voKey = voElement.getElementsByTagName("key").item(0).getTextContent().trim();
|
||||
|
||||
// Create a new Vres object and add it to the Vos's vreList
|
||||
Vres vres = new Vres(vreKey, vreName, vreDescription, vreManager,
|
||||
vreStartDate, vreEndDate);
|
||||
|
||||
//System.out.println("VRE Name:"+vreName);
|
||||
|
||||
vos.addVres(vres);
|
||||
|
||||
// Create a new Vos object and add it to the gateway's vosList
|
||||
// Vos vos = new Vos(voKey, voScope, voName);
|
||||
|
||||
// Find the <vres> element within the VO entry
|
||||
NodeList vresList = vosElement.getElementsByTagName("vres");
|
||||
for (int l = 0; l < vresList.getLength(); l++) {
|
||||
Node vresNode = vresList.item(l);
|
||||
if (vresNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element vresElement = (Element) vresNode;
|
||||
// Iterate through <entry> elements representing VREs within <vres>
|
||||
NodeList vreEntryList = vresElement.getElementsByTagName("entry");
|
||||
for (int m = 0; m < vreEntryList.getLength(); m++) {
|
||||
Node vreEntryNode = vreEntryList.item(m);
|
||||
if (vreEntryNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element vreEntryElement = (Element) vreEntryNode;
|
||||
String vreKey = vreEntryElement.getElementsByTagName("key").item(0)
|
||||
.getTextContent().trim();
|
||||
String vreName = vreEntryElement.getElementsByTagName("name")
|
||||
.item(0).getTextContent().trim();
|
||||
String vreDescription = vreEntryElement
|
||||
.getElementsByTagName("description").item(0)
|
||||
.getTextContent().trim();
|
||||
String vreManager = vreEntryElement.getElementsByTagName("managers")
|
||||
.item(0).getTextContent().trim();
|
||||
String vreStartDate = vreEntryElement
|
||||
.getElementsByTagName("startdate").item(0).getTextContent().trim();
|
||||
String vreEndDate = vreEntryElement.getElementsByTagName("enddate")
|
||||
.item(0).getTextContent().trim();
|
||||
|
||||
// Create a new Vres object and add it to the Vos's vreList
|
||||
Vres vres = new Vres(vreKey, vreName, vreDescription, vreManager,
|
||||
vreStartDate, vreEndDate);
|
||||
vos.addVres(vres);
|
||||
|
||||
//vres.printInfo();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
gateway.addVos(vos);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Add the Vos object to the gateway's vosList
|
||||
gateway.addVos(vos);
|
||||
//}
|
||||
//}
|
||||
}
|
||||
gatewaysMap.put(gatewayName, gateway);
|
||||
//getting vo
|
||||
}
|
||||
|
||||
// Store the gateway object in the gateways map
|
||||
|
||||
gatewaysMap.put(gatewayName, gateway);
|
||||
//computeIfAbsent(gatewayName, k -> new ArrayList<>()).add(gateway);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void appendNewDataToXML(List<HashMap<String, String>> newDataList) {
|
||||
|
||||
try {
|
||||
|
||||
File xmlFile = new File(filePath);
|
||||
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
|
||||
Document doc = dBuilder.parse(xmlFile);
|
||||
|
||||
Element rootElement = (Element) doc.getElementsByTagName("detachedres").item(0);
|
||||
|
||||
Node gatewaysNode = rootElement.getElementsByTagName("gateways").item(0);
|
||||
|
||||
for (Map<String, String> newData : newDataList) {
|
||||
|
||||
String gatewayName = newData.get("Gateways");
|
||||
|
||||
// String voKey = newData.get("voKey");
|
||||
|
||||
//String voScope = newData.get("voScope");
|
||||
|
||||
String voName = newData.get("VO");
|
||||
|
||||
String vreKey = newData.get("vreKey");
|
||||
|
||||
String vreName = newData.get("VRE Names");
|
||||
|
||||
String vreDescription = newData.get("Description");
|
||||
|
||||
String vreManager = newData.get("Managers");
|
||||
|
||||
String vreStartDate = newData.get("Start Time");
|
||||
|
||||
String vreEndDate = newData.get("End Time");
|
||||
|
||||
Gateway gateway = findGatewayInMap(gatewayName);
|
||||
|
||||
if (gateway == null) {
|
||||
|
||||
Element gatewayElement = doc.createElement("entry");
|
||||
|
||||
Element gatewayValueElement = doc.createElement("value");
|
||||
|
||||
Element gatewayNameElement = doc.createElement("name");
|
||||
|
||||
gatewayNameElement.appendChild(doc.createTextNode(gatewayName));
|
||||
|
||||
gatewayValueElement.appendChild(gatewayNameElement);
|
||||
|
||||
gatewayElement.appendChild(gatewayValueElement);
|
||||
|
||||
gatewaysNode.appendChild(gatewayElement);
|
||||
|
||||
gateway = new Gateway(gatewayName,"");
|
||||
|
||||
gatewaysMap.put(gatewayName,gateway);
|
||||
|
||||
}
|
||||
|
||||
Vos vos = gateway.findVosByName(voName);
|
||||
|
||||
if (vos == null) {
|
||||
|
||||
Element vosElement = doc.createElement("vos");
|
||||
|
||||
Element vosEntryElement = doc.createElement("entry");
|
||||
|
||||
Element vosNameElement = doc.createElement("name");
|
||||
|
||||
vosNameElement.appendChild(doc.createTextNode(voName));
|
||||
|
||||
vosEntryElement.appendChild(vosNameElement);
|
||||
|
||||
vosElement.appendChild(vosEntryElement);
|
||||
|
||||
Node gatewayNode = findGatewayNode(gatewaysNode, gatewayName);
|
||||
|
||||
if (gatewayNode != null) {
|
||||
|
||||
gatewayNode.appendChild(vosElement);
|
||||
|
||||
}
|
||||
|
||||
gateway.addVos(new Vos(voName,"",""));
|
||||
|
||||
}
|
||||
|
||||
|
||||
// TODO findVresByName
|
||||
|
||||
/*Vres vres = null;//vos.findVresByName(vreName);
|
||||
|
||||
if (vres == null) {
|
||||
|
||||
Element vresElement = doc.createElement("vres");
|
||||
|
||||
Element vresEntryElement = doc.createElement("entry");
|
||||
|
||||
Element vresNameElement = doc.createElement("name");
|
||||
|
||||
vresNameElement.appendChild(doc.createTextNode(vreName));
|
||||
|
||||
vresEntryElement.appendChild(vresNameElement);
|
||||
|
||||
vresElement.appendChild(vresEntryElement);
|
||||
|
||||
Node vosNode = findVosNode(gatewaysNode, gatewayName, voName);
|
||||
|
||||
if (vosNode != null) {
|
||||
|
||||
vosNode.appendChild(vresElement);
|
||||
|
||||
}
|
||||
|
||||
|
||||
vos.addVres(new Vres(vreKey,vreName,vreDescription,vreManager,vreStartDate,vreEndDate));
|
||||
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
|
||||
DOMSource source = new DOMSource(doc);
|
||||
|
||||
StreamResult result = new StreamResult(new File(filePath));
|
||||
|
||||
transformer.transform(source, result);
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Gateway findGatewayInMap(String gatewayName) {
|
||||
|
||||
return gatewaysMap.getOrDefault(gatewayName, null);
|
||||
|
||||
}
|
||||
private Node findGatewayNode(Node gatewaysNode, String gatewayName) {
|
||||
|
||||
NodeList entryList = ((Element) gatewaysNode).getElementsByTagName("entry");
|
||||
|
||||
for (int i = 0; i < entryList.getLength(); i++) {
|
||||
|
||||
Node node = entryList.item(i);
|
||||
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
|
||||
Element entryElement = (Element) node;
|
||||
|
||||
String name = entryElement.getElementsByTagName("name").item(0).getTextContent();
|
||||
|
||||
if (name.equals(gatewayName)) {
|
||||
|
||||
return entryElement;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
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"))
|
||||
|
@ -458,10 +178,7 @@ public class XmlManager implements VreRemPubInterface {
|
|||
Gateway gatewayItem = gatewaysMap.get(_gateway);
|
||||
//Gateway gateway = gatewayList.get(0); // Get the first gateway with the specified name
|
||||
ArrayList<Vos> vosList = gatewayItem.getVosList();
|
||||
|
||||
|
||||
if (vosList != null) {
|
||||
System.out.println("Num of VOs:"+vosList.size());
|
||||
for (Vos vos : vosList) {
|
||||
vos.printInfo();
|
||||
//System.out.println("Name of VREs: ");
|
||||
|
@ -476,7 +193,7 @@ public class XmlManager implements VreRemPubInterface {
|
|||
// System.out.println("----------------------------------------");
|
||||
}
|
||||
} else {
|
||||
System.out.println("No VOS found for this gateway.");
|
||||
//System.out.println("No VOS found for this gateway.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("No gateways found with the name: " + _gateway);
|
||||
|
@ -491,10 +208,8 @@ public class XmlManager implements VreRemPubInterface {
|
|||
Gateway gatewayItem = gatewaysMap.get(_gateway);
|
||||
boolean found = false;
|
||||
ArrayList<Vos> vosList = gatewayItem.getVosList();
|
||||
System.out.println("VO LIST length:"+ vosList.size());
|
||||
if (vosList != null) {
|
||||
for (Vos vos : vosList) {
|
||||
System.out.println("VO: "+vos.getName());
|
||||
if (vos.getName().equals(_vo)) {
|
||||
found = true;
|
||||
System.out.println("VO info for " + _vo + " in " + _gateway + ":");
|
||||
|
@ -544,6 +259,16 @@ public class XmlManager implements VreRemPubInterface {
|
|||
}
|
||||
}
|
||||
|
||||
public HashMap<String, ArrayList<Gateway>> parseToHashMap() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
public void writeToXml(String string, HashMap<String, ArrayList<Gateway>> mergedMap) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Other methods...
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue