Implement EzDent patient search and original file transfers
This commit is contained in:
1 parent
69f70d0325
commit
017553f3db
4 files changed
+308
-13
No files matched your search
@@ -60,6 +60,11 @@ final class ServerStore {
|
||||
patient_id TEXT PRIMARY KEY NOT NULL REFERENCES patients(id) ON DELETE CASCADE,
|
||||
fields TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS legacy_image_metadata (
|
||||
image_id TEXT PRIMARY KEY NOT NULL REFERENCES images(id) ON DELETE CASCADE,
|
||||
study_uid TEXT NOT NULL,
|
||||
modality TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS password_policy (
|
||||
key TEXT PRIMARY KEY NOT NULL,
|
||||
value TEXT NOT NULL
|
||||
@@ -200,26 +205,39 @@ final class ServerStore {
|
||||
let importedAt: Date
|
||||
let dicomCount: Int?
|
||||
let firstFile: URL
|
||||
let uuid: UUID
|
||||
let studyUID: String
|
||||
let modality: String
|
||||
|
||||
var wireName: String {
|
||||
let suffix = (name as NSString).pathExtension
|
||||
let ext = (dicomCount ?? 0) > 1 ? "CT" : suffix
|
||||
return uuid.uuidString + (ext.isEmpty ? "" : "." + ext)
|
||||
}
|
||||
}
|
||||
|
||||
func legacyImages(chartNumber: String? = nil, offset: Int = 0, limit: Int = -1) throws -> [LegacyImage] {
|
||||
func legacyImages(chartNumber: String? = nil, imageID: UUID? = nil, offset: Int = 0, limit: Int = -1) throws -> [LegacyImage] {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return try query("""
|
||||
SELECT images.rowid, patients.rowid, patients.chart_number, images.id,
|
||||
images.name, images.imported_at, images.dicom_count
|
||||
images.name, images.imported_at, images.dicom_count,
|
||||
legacy_image_metadata.study_uid, legacy_image_metadata.modality
|
||||
FROM images JOIN patients ON patients.id = images.patient_id
|
||||
WHERE ? IS NULL OR patients.chart_number = ? COLLATE BINARY
|
||||
LEFT JOIN legacy_image_metadata ON legacy_image_metadata.image_id = images.id
|
||||
WHERE (? IS NULL OR patients.chart_number = ? COLLATE BINARY) AND (? IS NULL OR images.id = ?)
|
||||
ORDER BY images.imported_at DESC, images.id
|
||||
LIMIT ? OFFSET ?
|
||||
""", values: [chartNumber.map { .text($0) } ?? .null, chartNumber.map { .text($0) } ?? .null,
|
||||
.integer(limit), .integer(offset)]) { row in
|
||||
imageID.map { .text($0.uuidString) } ?? .null,
|
||||
imageID.map { .text($0.uuidString) } ?? .null, .integer(limit), .integer(offset)]) { row in
|
||||
guard let id = UUID(uuidString: self.text(row, 3)) else { throw StoreError.database }
|
||||
let count = sqlite3_column_type(row, 6) == SQLITE_NULL ? nil : Int(sqlite3_column_int(row, 6))
|
||||
return LegacyImage(id: sqlite3_column_int64(row, 0), patientID: sqlite3_column_int64(row, 1),
|
||||
chartNumber: self.text(row, 2), name: self.text(row, 4),
|
||||
importedAt: Date(timeIntervalSinceReferenceDate: sqlite3_column_double(row, 5)),
|
||||
dicomCount: count, firstFile: count == nil ? self.attachment(id) : self.slice(1, of: id))
|
||||
dicomCount: count, firstFile: count == nil ? self.attachment(id) : self.slice(1, of: id),
|
||||
uuid: id, studyUID: self.text(row, 7), modality: self.text(row, 8))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,7 +270,7 @@ final class ServerStore {
|
||||
return patient
|
||||
}
|
||||
|
||||
func addFile(from source: URL, patient id: UUID, imageID: UUID, name: String, dicom: Bool) throws -> PatientImage {
|
||||
func addFile(from source: URL, patient id: UUID, imageID: UUID, name: String, dicom: Bool, studyUID: String = "", modality: String = "") throws -> PatientImage {
|
||||
guard (1...255).contains(name.count), name.rangeOfCharacter(from: .controlCharacters) == nil,
|
||||
!name.contains("/"), !name.contains("\\") else { throw StoreError.invalid }
|
||||
lock.lock()
|
||||
@@ -261,8 +279,19 @@ final class ServerStore {
|
||||
let image = PatientImage(id: imageID, name: name, importedAt: Date(), dicomCount: dicom ? 1 : nil)
|
||||
let destination = attachment(imageID)
|
||||
guard !FileManager.default.fileExists(atPath: destination.path) else { throw StoreError.conflict }
|
||||
try transaction {
|
||||
try persist(image, patient: id, from: source, isSeries: false)
|
||||
var stored = false
|
||||
do {
|
||||
try transaction {
|
||||
try persist(image, patient: id, from: source, isSeries: false)
|
||||
stored = true
|
||||
if !studyUID.isEmpty || !modality.isEmpty {
|
||||
try run("INSERT INTO legacy_image_metadata(image_id, study_uid, modality) VALUES (?, ?, ?)",
|
||||
[.text(imageID.uuidString), .text(studyUID), .text(modality)])
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
if stored { try? FileManager.default.removeItem(at: destination) }
|
||||
throw error
|
||||
}
|
||||
return image
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user