diff --git a/src/main/java/net/ayyalasomayajula/net/client/Dashboard.java b/src/main/java/net/ayyalasomayajula/net/client/Dashboard.java index c158faf..6a25d7a 100644 --- a/src/main/java/net/ayyalasomayajula/net/client/Dashboard.java +++ b/src/main/java/net/ayyalasomayajula/net/client/Dashboard.java @@ -121,7 +121,7 @@ public class Dashboard extends JFrame { Object receivedMessage = inputStream.readObject(); if(receivedMessage instanceof Message) { Message message = (Message) receivedMessage; - logger.info("Received EHR response: {}", message); + logger.info("Received Message response response: {}", message); if(message.data().length == 0) {continue;} receivedMessages.add(message); updateMessageDisplay(message); // Update the GUI with the new message diff --git a/src/main/java/net/ayyalasomayajula/net/client/EHRGen.form b/src/main/java/net/ayyalasomayajula/net/client/EHRGen.form new file mode 100644 index 0000000..9739e67 --- /dev/null +++ b/src/main/java/net/ayyalasomayajula/net/client/EHRGen.form @@ -0,0 +1,141 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/net/ayyalasomayajula/net/client/EHRGen.java b/src/main/java/net/ayyalasomayajula/net/client/EHRGen.java new file mode 100644 index 0000000..0a41225 --- /dev/null +++ b/src/main/java/net/ayyalasomayajula/net/client/EHRGen.java @@ -0,0 +1,164 @@ +package net.ayyalasomayajula.net.client; + +import net.ayyalasomayajula.net.shared.Appointment; +import net.ayyalasomayajula.net.shared.Message; +import net.ayyalasomayajula.net.shared.MessageVariant; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.swing.*; +import java.awt.*; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.net.Socket; +import java.nio.file.Path; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; + +import net.ayyalasomayajula.net.shared.EHR; +/** + * A graphical user interface for viewing and managing an Electronic Health Record (EHR). + */ +public class EHRGen extends JFrame { + private static final Logger logger = LoggerFactory.getLogger(EHRGen.class); + + private JTextField patientNameValue; + private JTextField uuidValue; + private JList medicalNotesList; + private JList appointmentLogList; + private JList xrayList; + private JButton updateButton; + private JButton insertButton; + private JPanel main; + + private final Socket connection; + private final ObjectOutputStream outputStream; + private EHR currentEHR; // Stores the current EHR instance + + /** + * Constructs an EHRGen window with a given socket connection and EHR data. + * + * @param connection the socket connection to the server + * @param ehr the EHR object containing patient data + * @throws IOException if an I/O error occurs while creating the output stream + */ + public EHRGen(Socket connection, EHR ehr) throws IOException { + this.connection = connection; + this.currentEHR = ehr; + this.outputStream = new ObjectOutputStream(connection.getOutputStream()); + + setTitle("Electronic Health Record"); + setContentPane(main); + setSize(600, 400); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + setLocationRelativeTo(null); + + populateFields(); // Populate fields with EHR data if available + + insertButton.addActionListener(e -> insertEHR()); + updateButton.addActionListener(e -> updateEHR()); + } + + /** + * Populates the UI fields with data from the current EHR. + */ + private void populateFields() { + if (currentEHR != null) { + patientNameValue.setText(currentEHR.getPatientName()); + uuidValue.setText(currentEHR.getUuid().toString()); + + // Populate lists + populateList(medicalNotesList, currentEHR.getMedicalNotes()); + populateList(appointmentLogList, currentEHR.getAppointmentLog().stream().map(Appointment::toString).toList()); + populateList(xrayList, currentEHR.getXrays().stream().map(Path::toString).toList()); + } + } + + /** + * Populates a JList with data. + * + * @param list the JList to populate + * @param data the list of strings to display in the JList + */ + private void populateList(JList list, List data) { + if (data != null) { + list.setListData(data.toArray(new String[0])); + } + } + + /** + * Converts a date string from "yyyy-MM-dd" format to "dd/MM/yyyy" format. + * + * @param inputDate the date string in "yyyy-MM-dd" format + * @return the formatted date string in "dd/MM/yyyy" format, or the original string if parsing fails + */ + private String convertDate(String inputDate) { + try { + SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd"); + SimpleDateFormat ehrFormat = new SimpleDateFormat("dd/MM/yyyy"); + Date date = inputFormat.parse(inputDate); + return ehrFormat.format(date); + } catch (ParseException e) { + logger.error("Date conversion failed: {}", e.getMessage()); + return inputDate; // Return original in case of error + } + } + + /** + * Handles the insertion of a new EHR record. + * Displays an error message if required fields are missing. + */ + private void insertEHR() { + if (!fieldsPopulated()) { + JOptionPane.showMessageDialog(this, "All fields must be populated before inserting.", "Error", JOptionPane.ERROR_MESSAGE); + return; + } + // Insertion logic to be implemented + dispose(); + } + + /** + * Handles updating an existing EHR record. + * Displays an error message if required fields are missing. + */ + private void updateEHR() { + if (!fieldsPopulated()) { + JOptionPane.showMessageDialog(this, "All fields must be populated before updating.", "Error", JOptionPane.ERROR_MESSAGE); + return; + } + // Update logic to be implemented + dispose(); + } + + /** + * Checks if all required fields are populated. + * + * @return true if all required fields contain data, false otherwise + */ + private boolean fieldsPopulated() { + return !patientNameValue.getText().isEmpty() && + !uuidValue.getText().isEmpty() && + medicalNotesList.getModel().getSize() > 0 && + appointmentLogList.getModel().getSize() > 0 && + xrayList.getModel().getSize() > 0; + } + + /** + * Launches the EHRGen window in a separate UI thread. + * + * @param connection the socket connection to the server + * @param ehr the EHR object to display + */ + public static void showEHR(Socket connection, EHR ehr) { + SwingUtilities.invokeLater(() -> { + try { + new EHRGen(connection, ehr).setVisible(true); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } +} diff --git a/src/main/java/net/ayyalasomayajula/net/shared/Appointment.java b/src/main/java/net/ayyalasomayajula/net/shared/Appointment.java index c29058e..c809fca 100644 --- a/src/main/java/net/ayyalasomayajula/net/shared/Appointment.java +++ b/src/main/java/net/ayyalasomayajula/net/shared/Appointment.java @@ -18,4 +18,21 @@ public record Appointment(UUID uuid, UUID patient, LocalDateTime time, boolean a public boolean isPast() { return time.isBefore(LocalDateTime.now()); } + @Override +/** + * Returns a string representation of the Appointment record. + * The format includes the UUID, patient ID, time, attendance status, and description. + * + * @return A string containing the appointment details. + */ + public String toString() { + return "Appointment{" + + "uuid=" + uuid + + ", patient=" + patient + + ", time=" + time + + ", attendance=" + attendance + + ", description='" + description + '\'' + + '}'; + } + } diff --git a/target/classes/net/ayyalasomayajula/net/server/ServerDaemon.class b/target/classes/net/ayyalasomayajula/net/server/ServerDaemon.class index 5cf4b0e..c9d92ab 100644 Binary files a/target/classes/net/ayyalasomayajula/net/server/ServerDaemon.class and b/target/classes/net/ayyalasomayajula/net/server/ServerDaemon.class differ