updated method as to how the csv is written to as to not get rid of lines for panels that aren't included.

This commit is contained in:
bobbert committed 2025-04-02 17:21:59 +01:00
1 parent 44f5279d36
commit b800606f1a
2 files changed
+74 -41

No files matched your search

+74 -40
View File
@@ -7,7 +7,7 @@ import cvbuilder.model.UserGroup;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
public class MainViewer {
@@ -331,8 +331,8 @@ public class MainViewer {
menu.add(saveItem);
}
// Class variable to store the current CSV file path
private String currentCsvFile = null;
// Class variable to store the current CSV file path -- added default file data
private String currentCsvFile = "data/cv_repo_0.csv";
private void openCsvFile(JFrame viewer) {
JFileChooser fileChooser = new JFileChooser();
@@ -462,6 +462,26 @@ public class MainViewer {
}
private void writeToCSV(String filePath) {
// First read the existing file to preserve data for sections not included
Map<String, String[]> existingData = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
// Skip the header
reader.readLine();
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",", 3); // Split into section, subsection, and the rest
if (parts.length >= 2) {
String key = parts[0] + "," + parts[1];
// Store the whole line
existingData.put(key, line.split(","));
}
}
} catch (IOException e) {
// If file doesn't exist or can't be read, we'll create a new one
// No need to handle this exception
}
try (PrintWriter writer = new PrintWriter(new FileWriter(filePath))) {
// Get export settings
CsvExportSettings exportSettings = CsvExportSettings.getInstance();
@@ -472,48 +492,38 @@ public class MainViewer {
// Get all users
UserGroup userGroup = UserGroup.getInstance();
// Write name (always included)
writer.print("User,Name");
for (User user : userGroup.getUsers()) {
writer.print("," + user.getName());
}
writer.println();
// Build user data strings to append - we'll use these if the section should be included
String[] nameData = new String[userGroup.getUsers().size()];
String[] titleData = new String[userGroup.getUsers().size()];
String[] emailData = new String[userGroup.getUsers().size()];
String[] phoneData = new String[userGroup.getUsers().size()];
String[] addressData = new String[userGroup.getUsers().size()];
// Write email (always included)
writer.print("User,Email");
for (User user : userGroup.getUsers()) {
writer.print("," + user.getEmail());
}
writer.println();
// Write title only if included in settings
if (exportSettings.isIncluded("title")) {
writer.print("User,Title");
for (User user : userGroup.getUsers()) {
writer.print("," + user.getTitle());
}
writer.println();
for (int i = 0; i < userGroup.getUsers().size(); i++) {
User user = userGroup.getUsers().get(i);
nameData[i] = user.getName();
titleData[i] = user.getTitle();
emailData[i] = user.getEmail();
phoneData[i] = user.getPhone();
addressData[i] = user.getAddress();
}
// Write phone only if included in settings
if (exportSettings.isIncluded("phone")) {
writer.print("Contact Information,Phone");
for (User user : userGroup.getUsers()) {
writer.print("," + user.getPhone());
}
writer.println();
}
// Process and write each section based on inclusion settings
// Write address only if included in settings
if (exportSettings.isIncluded("address")) {
writer.print("Contact Information,Address");
for (User user : userGroup.getUsers()) {
writer.print("," + user.getAddress());
}
writer.println();
}
// Name - always included
writeSection(writer, existingData, "User,Name", nameData, true);
// Success message removed
// Title - conditionally included
writeSection(writer, existingData, "User,Title", titleData, exportSettings.isIncluded("title"));
// Email - always included
writeSection(writer, existingData, "User,Email", emailData, true);
// Phone - conditionally included
writeSection(writer, existingData, "Contact Information,Phone", phoneData, exportSettings.isIncluded("phone"));
// Address - conditionally included
writeSection(writer, existingData, "Contact Information,Address", addressData, exportSettings.isIncluded("address"));
} catch (IOException e) {
JOptionPane.showMessageDialog(null,
@@ -523,4 +533,28 @@ public class MainViewer {
}
}
private void writeSection(PrintWriter writer, Map<String, String[]> existingData, String section,
String[] userData, boolean include) {
if (include) {
// This section should be updated with new data
writer.print(section);
for (String data : userData) {
writer.print("," + data);
}
writer.println();
} else if (existingData.containsKey(section)) {
// This section should be preserved as-is
String[] parts = existingData.get(section);
writer.print(parts[0] + "," + parts[1]); // Section and Sub-Section
// Add the remaining data columns
for (int i = 2; i < parts.length; i++) {
writer.print("," + parts[i]);
}
writer.println();
} else {
// Section doesn't exist and shouldn't be included - do nothing
}
}
}