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

-1
View File
@@ -1,6 +1,5 @@
Section,Sub-Section,Variants Section,Sub-Section,Variants
User,Name,Scheherazade Taylor,SJ Taylor,Shaz Taylor User,Name,Scheherazade Taylor,SJ Taylor,Shaz Taylor
User,Email,strongshaz@bob.com,gmail_account@gmail.com,k2320390@kingston.ac.uk User,Email,strongshaz@bob.com,gmail_account@gmail.com,k2320390@kingston.ac.uk
User,Title,Ms.,Miss,Unknown
Contact Information,Phone,07910582346,02083412275,Unknown Contact Information,Phone,07910582346,02083412275,Unknown
Contact Information,Address,11 Rita Road%%%%London SW8 1ND,Flat 4//// Obnoxious House%%%%Rude Road%%%%London KT16 4RT,Unknown Contact Information,Address,11 Rita Road%%%%London SW8 1ND,Flat 4//// Obnoxious House%%%%Rude Road%%%%London KT16 4RT,Unknown
1 Section,Sub-Section,Variants
2 User,Name,Scheherazade Taylor,SJ Taylor,Shaz Taylor
3 User,Email,strongshaz@bob.com,gmail_account@gmail.com,k2320390@kingston.ac.uk
User,Title,Ms.,Miss,Unknown
4 Contact Information,Phone,07910582346,02083412275,Unknown
5 Contact Information,Address,11 Rita Road%%%%London SW8 1ND,Flat 4//// Obnoxious House%%%%Rude Road%%%%London KT16 4RT,Unknown
+74 -40
View File
@@ -7,7 +7,7 @@ import cvbuilder.model.UserGroup;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.io.*; import java.io.*;
import java.util.*;
public class MainViewer { public class MainViewer {
@@ -331,8 +331,8 @@ public class MainViewer {
menu.add(saveItem); menu.add(saveItem);
} }
// Class variable to store the current CSV file path // Class variable to store the current CSV file path -- added default file data
private String currentCsvFile = null; private String currentCsvFile = "data/cv_repo_0.csv";
private void openCsvFile(JFrame viewer) { private void openCsvFile(JFrame viewer) {
JFileChooser fileChooser = new JFileChooser(); JFileChooser fileChooser = new JFileChooser();
@@ -462,6 +462,26 @@ public class MainViewer {
} }
private void writeToCSV(String filePath) { 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))) { try (PrintWriter writer = new PrintWriter(new FileWriter(filePath))) {
// Get export settings // Get export settings
CsvExportSettings exportSettings = CsvExportSettings.getInstance(); CsvExportSettings exportSettings = CsvExportSettings.getInstance();
@@ -472,48 +492,38 @@ public class MainViewer {
// Get all users // Get all users
UserGroup userGroup = UserGroup.getInstance(); UserGroup userGroup = UserGroup.getInstance();
// Write name (always included) // Build user data strings to append - we'll use these if the section should be included
writer.print("User,Name"); String[] nameData = new String[userGroup.getUsers().size()];
for (User user : userGroup.getUsers()) { String[] titleData = new String[userGroup.getUsers().size()];
writer.print("," + user.getName()); String[] emailData = new String[userGroup.getUsers().size()];
} String[] phoneData = new String[userGroup.getUsers().size()];
writer.println(); String[] addressData = new String[userGroup.getUsers().size()];
// Write email (always included) for (int i = 0; i < userGroup.getUsers().size(); i++) {
writer.print("User,Email"); User user = userGroup.getUsers().get(i);
for (User user : userGroup.getUsers()) { nameData[i] = user.getName();
writer.print("," + user.getEmail()); titleData[i] = user.getTitle();
} emailData[i] = user.getEmail();
writer.println(); phoneData[i] = user.getPhone();
addressData[i] = user.getAddress();
// 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();
} }
// Write phone only if included in settings // Process and write each section based on inclusion settings
if (exportSettings.isIncluded("phone")) {
writer.print("Contact Information,Phone");
for (User user : userGroup.getUsers()) {
writer.print("," + user.getPhone());
}
writer.println();
}
// Write address only if included in settings // Name - always included
if (exportSettings.isIncluded("address")) { writeSection(writer, existingData, "User,Name", nameData, true);
writer.print("Contact Information,Address");
for (User user : userGroup.getUsers()) {
writer.print("," + user.getAddress());
}
writer.println();
}
// 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) { } catch (IOException e) {
JOptionPane.showMessageDialog(null, 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
}
}
} }