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;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -9,92 +12,101 @@ import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
public class CsvManager {
private String csvFilePath;
private HashMap<String, Gateway> gatewayMap;
private String csvFilePath;
private HashMap<String, Gateway> gatewayMap;
public CsvManager(String csvFilePath) {
this.csvFilePath = csvFilePath;
this.gatewayMap = new HashMap<>();
}
public CsvManager(String csvFilePath) {
this.csvFilePath = csvFilePath;
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) {
HashMap<String, Gateway> csvMap = new HashMap<>();
try {
FileReader filereader = new FileReader(csvFilePath);
CSVReader csvReader = new CSVReaderBuilder(filereader).withSkipLines(1).build();
try {
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) {
//XmlManager xmlManager = new XmlManager();
String url = row[5];
GatewayNameAndDescriptionReader gatewayNameAndDescription = gatewayNameAndDescriptionReaderMap.get(url);
if (gatewayNameAndDescription!=null) {
String gatewayName = gatewayNameAndDescription.getName();
String description = gatewayNameAndDescription.getDescription();
String voName = row[4];
String vreName = row[3];
String manager = row[9];
String startDate = row[7];
String endDate = row[8];
for (String[] row : allData) {
String url = row[5].trim();
GatewayNameAndDescriptionReader gatewayNameAndDescription = gatewayNameAndDescriptionReaderMap.get(url);
if (gatewayNameAndDescription != null) {
String gatewayName = gatewayNameAndDescription.getName();
String description = gatewayNameAndDescription.getDescription();
String voName = row[4].trim();
String vreName = row[3].trim();
String manager = row[9].trim();
// Parse and format the dates
String startDateStr = row[7].trim();
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, new Gateway(gatewayName, description));
Vos vos = gateway.getVosByName(voName);
if (vos == null) {
vos = new Vos(voName, voName, voName);
gateway.addVos(vos);
}
Gateway gateway = csvMap.getOrDefault(gatewayName.toLowerCase(), new Gateway(gatewayName, description));
Vos vos = gateway.getVosByName(voName);
if (vos == null) {
vos = new Vos(voName, voName, voName);
gateway.addVos(vos);
}
Vres vres = vos.getVresByName(vreName);
if (vres == null) {
vres = new Vres(vreName, vreName, description, manager, startDate, endDate);
vos.addVres(vres);
}
csvMap.put(gatewayName.toLowerCase(), gateway);
//csvMap.put(gatewayName, gateway);
}
}
Vres vres = vos.getVresByName(vreName);
if (vres == null) {
vres = new Vres(vreName, vreName, description, manager, formattedStartDate, formattedEndDate);
vos.addVres(vres);
}
csvMap.put(gatewayName.toLowerCase(), gateway);
}
}
csvReader.close();
} catch (Exception e) {
e.printStackTrace();
}
csvReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return csvMap;
}
return csvMap;
}
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
}
private String formatDateString(String dateStr, SimpleDateFormat inputFormat, SimpleDateFormat outputFormat) {
try {
Date date = inputFormat.parse(dateStr);
return outputFormat.format(date);
} catch (ParseException e) {
System.err.println("Error parsing date: " + dateStr);
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()));
vresValueElement.appendChild(vresDescriptionElement);
// Assuming managers are separated by newlines or another clear delimiter (adjust the regex if needed)
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);
/*Element vresManagerElement = document.createElement("managers");
vresManagerElement.appendChild(document.createTextNode(vres.getManager()));
vresValueElement.appendChild(vresManagerElement);*/
Element vresStartDateElement = document.createElement("startdate");
vresStartDateElement.appendChild(document.createTextNode(vres.getStartDate()));
vresValueElement.appendChild(vresStartDateElement);
@ -449,6 +471,8 @@ public class XmlManager implements VreRemPubInterface {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(outputXmlFilePath));
transformer.transform(source, result);