From b800606f1aa6be83b2d70137fa9f13bc9ccdfd02 Mon Sep 17 00:00:00 2001 From: mudabbir-ahmad Date: Wed, 2 Apr 2025 17:21:59 +0100 Subject: [PATCH] updated method as to how the csv is written to as to not get rid of lines for panels that aren't included. --- data/cv_repo_0.csv | 1 - src/main/java/cvbuilder/view/MainViewer.java | 114 ++++++++++++------- 2 files changed, 74 insertions(+), 41 deletions(-) diff --git a/data/cv_repo_0.csv b/data/cv_repo_0.csv index 11dce5d..d3d1420 100644 --- a/data/cv_repo_0.csv +++ b/data/cv_repo_0.csv @@ -1,6 +1,5 @@ Section,Sub-Section,Variants User,Name,Scheherazade Taylor,SJ Taylor,Shaz Taylor 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,Address,11 Rita Road%%%%London SW8 1ND,Flat 4//// Obnoxious House%%%%Rude Road%%%%London KT16 4RT,Unknown diff --git a/src/main/java/cvbuilder/view/MainViewer.java b/src/main/java/cvbuilder/view/MainViewer.java index 181ffb0..395b076 100644 --- a/src/main/java/cvbuilder/view/MainViewer.java +++ b/src/main/java/cvbuilder/view/MainViewer.java @@ -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 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 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 + } + } + } \ No newline at end of file