Yo chat i'm cooked - 2

This commit is contained in:
2025-03-24 12:23:33 -05:00
parent eca6d173f0
commit 646a0484fb
2 changed files with 42 additions and 14 deletions

View File

@@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.nio.file.Path;
@@ -33,7 +34,8 @@ public class EHRGen extends JFrame {
private JButton updateButton;
private JButton insertButton;
private JPanel main;
private final ObjectOutputStream os;
private final ObjectInputStream oi;
private final Socket connection;
private final ObjectOutputStream outputStream;
private EHR currentEHR; // Stores the current EHR instance
@@ -60,6 +62,8 @@ public class EHRGen extends JFrame {
insertButton.addActionListener(e -> insertEHR());
updateButton.addActionListener(e -> updateEHR());
os = new ObjectOutputStream(connection.getOutputStream());
oi = new ObjectInputStream(connection.getInputStream());
}
/**

View File

@@ -178,16 +178,11 @@ public class ServerDaemon implements Runnable {
private Message handleMessage(Message in) throws IOException {
switch (in.messageVariant()) {
case GET: {
// Format of GET query:
// <asset_type> <asset_id> | asset types: EHR, XRAY | asset id: uuid field or the path
// Define regex pattern with capture groups
Pattern pattern = Pattern.compile("^(EHR|XRAY) (.+)$");
Matcher matcher = pattern.matcher(in.messageQuery());
if (matcher.matches()) {
String assetType = matcher.group(1); // Captures EHR or XRAY
String assetId = matcher.group(2); // Captures UUID or path
String assetType = matcher.group(1);
String assetId = matcher.group(2);
logger.debug("Asset Type: {}", assetType);
logger.debug("Asset ID: {}", assetId);
@@ -195,18 +190,14 @@ public class ServerDaemon implements Runnable {
case "EHR":
logger.info("EHR caught");
EHR found = EHRUtils.searchClosestEHR(assetId, Path.of(basePath));
if(found == null) {return null;}
//
if (found == null) return new Message(MessageVariant.SET, "ERROR: EHR not found.", 5, new byte[]{});
return new Message(MessageVariant.SET, "", 5, SerializationUtils.toBytes(found));
case "XRAY":
logger.info("XRAY caught");
// Construct full file path for XRAY asset
Path xrayFilePath = Path.of(basePath, assetId);
File xrayFile = xrayFilePath.toFile();
// Check if the file exists and is a valid file
if (xrayFile.exists() && xrayFile.isFile()) {
// Read the file into a byte array and return it
try (FileInputStream fis = new FileInputStream(xrayFile)) {
byte[] fileBytes = fis.readAllBytes();
return new Message(MessageVariant.SET, "", 5, fileBytes);
@@ -224,8 +215,41 @@ public class ServerDaemon implements Runnable {
}
break;
}
case INSERT: {
try {
EHR ehr = (EHR) SerializationUtils.fromBytes(in.data());
Path ehrDirPath = Path.of(basePath, "patients");
EHRUtils.writeEHRToFile(ehr, ehrDirPath);
return new Message(MessageVariant.SET, "SUCCESS: EHR inserted.", 5, new byte[]{});
} catch (IOException | ClassNotFoundException e) {
logger.error("Error inserting EHR: {}", e.getMessage());
return new Message(MessageVariant.SET, "ERROR: Failed to insert EHR.", 5, new byte[]{});
}
}
case UPSERT: {
try {
EHR ehr = (EHR) SerializationUtils.fromBytes(in.data());
Path ehrDirPath = Path.of(basePath, "patients");
EHR existingEHR = EHRUtils.readEHRFromFile(ehrDirPath, ehr.getUuid());
if (existingEHR != null) {
logger.info("Updating existing EHR with UUID: {}", ehr.getUuid());
} else {
logger.info("Inserting new EHR with UUID: {}", ehr.getUuid());
}
EHRUtils.writeEHRToFile(ehr, ehrDirPath);
return new Message(MessageVariant.SET, "SUCCESS: EHR upserted.", 5, new byte[]{});
} catch (IOException | ClassNotFoundException e) {
logger.error("Error upserting EHR: {}", e.getMessage());
return new Message(MessageVariant.SET, "ERROR: Failed to upsert EHR.", 5, new byte[]{});
}
}
}
return new Message(MessageVariant.SET, "THROW;", 5, new byte[]{});
}
}