@@ -309,6 +309,12 @@ class WorklistItemNotFoundError(Exception):
309309 pass
310310
311311
312+ class DuplicateWorklistItemError (Exception ):
313+ """Raised when a worklist item with the same accession number already exists."""
314+
315+ pass
316+
317+
312318class MWLStorage (Storage ):
313319 def __init__ (self , db_path : str = "/var/lib/pacs/worklist.db" ):
314320 """
@@ -336,20 +342,23 @@ def store_worklist_item(
336342 Raises:
337343 sqlite3.IntegrityError: If accession number already exists
338344 """
339- with self ._get_connection () as conn :
340- conn .execute (
341- (
342- "INSERT INTO worklist_items (accession_number, modality, patient_birth_date, "
343- "patient_id, patient_name, patient_sex, procedure_code, scheduled_date, "
344- "scheduled_time, source_message_id, study_description, study_instance_uid) "
345- "VALUES (:accession_number, :modality, :patient_birth_date, "
346- ":patient_id, :patient_name, :patient_sex, :procedure_code, "
347- ":scheduled_date, :scheduled_time, :source_message_id, "
348- ":study_description, :study_instance_uid)"
349- ),
350- worklist_item .__dict__ ,
351- )
352- conn .commit ()
345+ try :
346+ with self ._get_connection () as conn :
347+ conn .execute (
348+ (
349+ "INSERT INTO worklist_items (accession_number, modality, patient_birth_date, "
350+ "patient_id, patient_name, patient_sex, procedure_code, scheduled_date, "
351+ "scheduled_time, source_message_id, study_description, study_instance_uid) "
352+ "VALUES (:accession_number, :modality, :patient_birth_date, "
353+ ":patient_id, :patient_name, :patient_sex, :procedure_code, "
354+ ":scheduled_date, :scheduled_time, :source_message_id, "
355+ ":study_description, :study_instance_uid)"
356+ ),
357+ worklist_item .__dict__ ,
358+ )
359+ conn .commit ()
360+ except sqlite3 .IntegrityError :
361+ raise DuplicateWorklistItemError (f"Worklist item already exists: { worklist_item .accession_number } " )
353362
354363 return worklist_item .accession_number
355364
0 commit comments