Date format and finalDoc xml indentation are fixed

This commit is contained in:
Biniam Abraha Masa 2024-09-23 23:00:39 +02:00
parent 0aa84cf7ef
commit ccf663499b
2 changed files with 114 additions and 78 deletions

View File

@ -1,6 +1,9 @@
package org.gcube.vremanagement; package org.gcube.vremanagement;
import java.io.FileReader; import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -9,92 +12,101 @@ import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder; import com.opencsv.CSVReaderBuilder;
public class CsvManager { public class CsvManager {
private String csvFilePath; private String csvFilePath;
private HashMap<String, 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 HashMap<String, Gateway> parseCsvToMap(String csvFilePath, Map<String, GatewayNameAndDescriptionReader> gatewayNameAndDescriptionReaderMap) {
HashMap<String, Gateway> csvMap = new HashMap<>();
SimpleDateFormat inputFormat = new SimpleDateFormat("MMM dd yyyy hh:mm a");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
public HashMap<String, Gateway> parseCsvToMap(String csvFilePath, Map<String, GatewayNameAndDescriptionReader> gatewayNameAndDescriptionReaderMap) { try {
HashMap<String, Gateway> csvMap = new HashMap<>(); FileReader filereader = new FileReader(csvFilePath);
try { CSVReader csvReader = new CSVReaderBuilder(filereader).withSkipLines(1).build();
FileReader filereader = new FileReader(csvFilePath);
CSVReader csvReader = new CSVReaderBuilder(filereader).withSkipLines(1).build();
List<String[]> allData = csvReader.readAll(); List<String[]> allData = csvReader.readAll();
for (String[] row : allData) { for (String[] row : allData) {
//XmlManager xmlManager = new XmlManager(); String url = row[5].trim();
String url = row[5]; GatewayNameAndDescriptionReader gatewayNameAndDescription = gatewayNameAndDescriptionReaderMap.get(url);
GatewayNameAndDescriptionReader gatewayNameAndDescription = gatewayNameAndDescriptionReaderMap.get(url); if (gatewayNameAndDescription != null) {
if (gatewayNameAndDescription!=null) { String gatewayName = gatewayNameAndDescription.getName();
String description = gatewayNameAndDescription.getDescription();
String gatewayName = gatewayNameAndDescription.getName(); String voName = row[4].trim();
String description = gatewayNameAndDescription.getDescription(); String vreName = row[3].trim();
String voName = row[4]; String manager = row[9].trim();
String vreName = row[3];
String manager = row[9]; // Parse and format the dates
String startDate = row[7]; String startDateStr = row[7].trim();
String endDate = row[8]; String endDateStr = row[8].trim();
String formattedStartDate = formatDateString(startDateStr, inputFormat, outputFormat);
String formattedEndDate = formatDateString(endDateStr, inputFormat, outputFormat);
Gateway gateway = csvMap.getOrDefault(gatewayName.toLowerCase(), new Gateway(gatewayName, description)); Gateway gateway = csvMap.getOrDefault(gatewayName.toLowerCase(), new Gateway(gatewayName, description));
//Gateway gateway = csvMap.getOrDefault(gatewayName, new Gateway(gatewayName, description)); Vos vos = gateway.getVosByName(voName);
Vos vos = gateway.getVosByName(voName);
if (vos == null) { if (vos == null) {
vos = new Vos(voName, voName, voName); vos = new Vos(voName, voName, voName);
gateway.addVos(vos); gateway.addVos(vos);
} }
Vres vres = vos.getVresByName(vreName); Vres vres = vos.getVresByName(vreName);
if (vres == null) { if (vres == null) {
vres = new Vres(vreName, vreName, description, manager, startDate, endDate); vres = new Vres(vreName, vreName, description, manager, formattedStartDate, formattedEndDate);
vos.addVres(vres); vos.addVres(vres);
} }
csvMap.put(gatewayName.toLowerCase(), gateway); csvMap.put(gatewayName.toLowerCase(), gateway);
//csvMap.put(gatewayName, gateway); }
} }
}
csvReader.close(); csvReader.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return csvMap; return csvMap;
} }
private String extractGatewayName(String gateways) { private String formatDateString(String dateStr, SimpleDateFormat inputFormat, SimpleDateFormat outputFormat) {
if (gateways.startsWith("https://")) { try {
return gateways.substring(8, gateways.indexOf(".")); Date date = inputFormat.parse(dateStr);
} else if (gateways.startsWith("http://")) { return outputFormat.format(date);
return gateways.substring(7, gateways.indexOf(".")); } catch (ParseException e) {
} else { System.err.println("Error parsing date: " + dateStr);
return gateways; // fallback return ""; // or return a default date
} }
}
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
}
}
public HashMap<String, GatewayNameAndDescriptionReader> parseGatewayInfoCsv(String csvFilePath) {
HashMap<String, GatewayNameAndDescriptionReader> gatewayNameAndDescription = new HashMap<>();
try (CSVReader csvReader = new CSVReaderBuilder(new FileReader(csvFilePath)).withSkipLines(1).build()) {
List<String[]> allData = csvReader.readAll();
for (String[] row : allData) {
String name = row[0]; // assuming name is in the first column
String description = row[1]; // assuming description is in the second column
String url = row[2]; // assuming URL is in the third column
gatewayNameAndDescription.put(url.toLowerCase(), new GatewayNameAndDescriptionReader(name, description, url));
}
} catch (Exception e) {
e.printStackTrace();
}
return gatewayNameAndDescription;
}
} }
public HashMap<String, GatewayNameAndDescriptionReader> parseGatewayInfoCsv(String csvFilePath) {
HashMap<String, GatewayNameAndDescriptionReader> gatewayNameAndDescription = new HashMap<>();
try (CSVReader csvReader = new CSVReaderBuilder(new FileReader(csvFilePath)).withSkipLines(1).build()) {
List<String[]> allData = csvReader.readAll();
for (String[] row : allData) {
String name = row[0]; // assuming name is in the first column
String description = row[1]; // assuming description is in the second column
String url = row[2]; // assuming URL is in the third column
gatewayNameAndDescription.put(url.toLowerCase(), new GatewayNameAndDescriptionReader(name, description, url));
//gatewayNameAndDescription.put(url, new GatewayNameAndDescriptionReader(name, description, url));
}
} catch (Exception e) {
e.printStackTrace();
}
return gatewayNameAndDescription;
}
}

View File

@ -430,10 +430,32 @@ public class XmlManager implements VreRemPubInterface {
vresDescriptionElement.appendChild(document.createTextNode(vres.getDescription())); vresDescriptionElement.appendChild(document.createTextNode(vres.getDescription()));
vresValueElement.appendChild(vresDescriptionElement); vresValueElement.appendChild(vresDescriptionElement);
// Assuming managers are separated by newlines or another clear delimiter (adjust the regex if needed)
Element vresManagerElement = document.createElement("managers"); Element vresManagerElement = document.createElement("managers");
vresManagerElement.appendChild(document.createTextNode(vres.getManager()));
// Split the manager string on newline or other delimiters (like commas, semicolons, etc.)
// Adjust the delimiter based on the format you get (e.g. comma, newline, etc.)
String[] managers = vres.getManager().split("\n");
// Loop through each manager and create a <manager> tag for it
for (String manager : managers) {
manager = manager.trim(); // Trim any excess whitespace
if (!manager.isEmpty()) {
Element managerElement = document.createElement("manager");
managerElement.appendChild(document.createTextNode(manager));
vresManagerElement.appendChild(managerElement);
}
}
vresValueElement.appendChild(vresManagerElement); vresValueElement.appendChild(vresManagerElement);
/*Element vresManagerElement = document.createElement("managers");
vresManagerElement.appendChild(document.createTextNode(vres.getManager()));
vresValueElement.appendChild(vresManagerElement);*/
Element vresStartDateElement = document.createElement("startdate"); Element vresStartDateElement = document.createElement("startdate");
vresStartDateElement.appendChild(document.createTextNode(vres.getStartDate())); vresStartDateElement.appendChild(document.createTextNode(vres.getStartDate()));
vresValueElement.appendChild(vresStartDateElement); vresValueElement.appendChild(vresStartDateElement);
@ -449,6 +471,8 @@ public class XmlManager implements VreRemPubInterface {
TransformerFactory transformerFactory = TransformerFactory.newInstance(); TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(); Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
DOMSource source = new DOMSource(document); DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(outputXmlFilePath)); StreamResult result = new StreamResult(new File(outputXmlFilePath));
transformer.transform(source, result); transformer.transform(source, result);