Yo chat i'm cooked - 2
This commit is contained in:
@@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@@ -33,7 +34,8 @@ public class EHRGen extends JFrame {
|
|||||||
private JButton updateButton;
|
private JButton updateButton;
|
||||||
private JButton insertButton;
|
private JButton insertButton;
|
||||||
private JPanel main;
|
private JPanel main;
|
||||||
|
private final ObjectOutputStream os;
|
||||||
|
private final ObjectInputStream oi;
|
||||||
private final Socket connection;
|
private final Socket connection;
|
||||||
private final ObjectOutputStream outputStream;
|
private final ObjectOutputStream outputStream;
|
||||||
private EHR currentEHR; // Stores the current EHR instance
|
private EHR currentEHR; // Stores the current EHR instance
|
||||||
@@ -60,6 +62,8 @@ public class EHRGen extends JFrame {
|
|||||||
|
|
||||||
insertButton.addActionListener(e -> insertEHR());
|
insertButton.addActionListener(e -> insertEHR());
|
||||||
updateButton.addActionListener(e -> updateEHR());
|
updateButton.addActionListener(e -> updateEHR());
|
||||||
|
os = new ObjectOutputStream(connection.getOutputStream());
|
||||||
|
oi = new ObjectInputStream(connection.getInputStream());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -178,16 +178,11 @@ public class ServerDaemon implements Runnable {
|
|||||||
private Message handleMessage(Message in) throws IOException {
|
private Message handleMessage(Message in) throws IOException {
|
||||||
switch (in.messageVariant()) {
|
switch (in.messageVariant()) {
|
||||||
case GET: {
|
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) (.+)$");
|
Pattern pattern = Pattern.compile("^(EHR|XRAY) (.+)$");
|
||||||
|
|
||||||
Matcher matcher = pattern.matcher(in.messageQuery());
|
Matcher matcher = pattern.matcher(in.messageQuery());
|
||||||
if (matcher.matches()) {
|
if (matcher.matches()) {
|
||||||
String assetType = matcher.group(1); // Captures EHR or XRAY
|
String assetType = matcher.group(1);
|
||||||
String assetId = matcher.group(2); // Captures UUID or path
|
String assetId = matcher.group(2);
|
||||||
logger.debug("Asset Type: {}", assetType);
|
logger.debug("Asset Type: {}", assetType);
|
||||||
logger.debug("Asset ID: {}", assetId);
|
logger.debug("Asset ID: {}", assetId);
|
||||||
|
|
||||||
@@ -195,18 +190,14 @@ public class ServerDaemon implements Runnable {
|
|||||||
case "EHR":
|
case "EHR":
|
||||||
logger.info("EHR caught");
|
logger.info("EHR caught");
|
||||||
EHR found = EHRUtils.searchClosestEHR(assetId, Path.of(basePath));
|
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));
|
return new Message(MessageVariant.SET, "", 5, SerializationUtils.toBytes(found));
|
||||||
|
|
||||||
case "XRAY":
|
case "XRAY":
|
||||||
logger.info("XRAY caught");
|
logger.info("XRAY caught");
|
||||||
// Construct full file path for XRAY asset
|
|
||||||
Path xrayFilePath = Path.of(basePath, assetId);
|
Path xrayFilePath = Path.of(basePath, assetId);
|
||||||
File xrayFile = xrayFilePath.toFile();
|
File xrayFile = xrayFilePath.toFile();
|
||||||
|
|
||||||
// Check if the file exists and is a valid file
|
|
||||||
if (xrayFile.exists() && xrayFile.isFile()) {
|
if (xrayFile.exists() && xrayFile.isFile()) {
|
||||||
// Read the file into a byte array and return it
|
|
||||||
try (FileInputStream fis = new FileInputStream(xrayFile)) {
|
try (FileInputStream fis = new FileInputStream(xrayFile)) {
|
||||||
byte[] fileBytes = fis.readAllBytes();
|
byte[] fileBytes = fis.readAllBytes();
|
||||||
return new Message(MessageVariant.SET, "", 5, fileBytes);
|
return new Message(MessageVariant.SET, "", 5, fileBytes);
|
||||||
@@ -224,8 +215,41 @@ public class ServerDaemon implements Runnable {
|
|||||||
}
|
}
|
||||||
break;
|
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[]{});
|
return new Message(MessageVariant.SET, "THROW;", 5, new byte[]{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user