Skip to content

Commit

Permalink
Add more study tags for DICOM update (#3250)
Browse files Browse the repository at this point in the history
* Add more studyTag to update

* Update documentation
  • Loading branch information
bcarthic authored Dec 8, 2023
1 parent 384d661 commit 974ad54
Show file tree
Hide file tree
Showing 15 changed files with 6,628 additions and 7 deletions.
9 changes: 8 additions & 1 deletion docs/concepts/bulk-update.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Bulk update overview
Bulk update is a feature that enables updates of DICOM attributes/metadata without needing to delete and re-add. The currently supported attributes include those in the [Patient Identification Module](https://dicom.nema.org/dicom/2013/output/chtml/part03/sect_C.2.html#table_C.2-2) and the [Patient Demographic Module](https://dicom.nema.org/dicom/2013/output/chtml/part03/sect_C.2.html#table_C.2-3) that are not sequences, also listed below.
Bulk update is a feature that enables updates of DICOM attributes/metadata without needing to delete and re-add. The currently supported attributes include those in the [Patient Identification Module](https://dicom.nema.org/dicom/2013/output/chtml/part03/sect_C.2.html#table_C.2-2), [Patient Demographic Module](https://dicom.nema.org/dicom/2013/output/chtml/part03/sect_C.2.html#table_C.2-3) and the [General Study Module](https://dicom.nema.org/medical/dicom/2020b/output/chtml/part03/sect_C.7.2.html#table_C.7-3) that are not sequences, also listed below.

**Patient Identification Module**
| Attribute Name | Tag | Description |
Expand Down Expand Up @@ -42,6 +42,13 @@ Bulk update is a feature that enables updates of DICOM attributes/metadata witho
| Patient Breed Description | (0010,2292) | The breed of the patient.See Section C.7.1.1.1.1. |
| Breed Registration Number | (0010,2295) | Identification number of a veterinary patient within the registry. |

**General study module**
| Attribute Name | Tag | Description |
| ---------------- | --------------| --------------------- |
| Referring Physician's Name | (0008,0090) | Name of the Patient's referring physician |
| Accession Number | (0008,0050) | A RIS generated number that identifies the order for the Study. |
| Study Description | (0008,1030) | Institution-generated description or classification of the Study (component) performed.

After a study is updated, there are two versions of the instances that can be retrieved: the original, unmodified instances and the latest version with updated attributes. Intermediate versions are not persisted.

## API Design
Expand Down
3 changes: 3 additions & 0 deletions src/Microsoft.Health.Dicom.Core/Features/Update/UpdateTags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@ internal static class UpdateTags
DicomTag.PatientBreedDescription,
DicomTag.BreedRegistrationNumber,
DicomTag.IssuerOfPatientID,
DicomTag.AccessionNumber,
DicomTag.ReferringPhysicianName,
DicomTag.StudyDescription,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
SET XACT_ABORT ON

BEGIN TRANSACTION
GO

/*************************************************************
Stored procedures for updating an instance status.
**************************************************************/
--
-- STORED PROCEDURE
-- EndUpdateInstanceV52
--
-- DESCRIPTION
-- Bulk update all instances in a study, creates new entry in changefeed and fileProperty for each new file added.
--
-- PARAMETERS
-- @partitionKey
-- * The partition key.
-- @studyInstanceUid
-- * The study instance UID.
-- @patientId
-- * The Id of the patient.
-- @patientName
-- * The name of the patient.
-- @patientBirthDate
-- * The patient's birth date.
-- @insertFileProperties
-- * A table type containing the file properties to insert.
--
-- RETURN VALUE
-- None
--
CREATE OR ALTER PROCEDURE dbo.EndUpdateInstanceV52
@partitionKey INT,
@studyInstanceUid VARCHAR(64),
@patientId NVARCHAR(64) = NULL,
@patientName NVARCHAR(325) = NULL,
@patientBirthDate DATE = NULL,
@referringPhysicianName NVARCHAR(325) = NULL,
@studyDescription NVARCHAR(64) = NULL,
@accessionNumber NVARCHAR(64) = NULL,
@insertFileProperties dbo.FilePropertyTableType READONLY,
@stringExtendedQueryTags dbo.InsertStringExtendedQueryTagTableType_1 READONLY,
@longExtendedQueryTags dbo.InsertLongExtendedQueryTagTableType_1 READONLY,
@doubleExtendedQueryTags dbo.InsertDoubleExtendedQueryTagTableType_1 READONLY,
@dateTimeExtendedQueryTags dbo.InsertDateTimeExtendedQueryTagTableType_2 READONLY,
@personNameExtendedQueryTags dbo.InsertPersonNameExtendedQueryTagTableType_1 READONLY
AS
BEGIN
SET NOCOUNT ON

SET XACT_ABORT ON
BEGIN TRANSACTION

DECLARE @currentDate DATETIME2(7) = SYSUTCDATETIME()
DECLARE @resourceType TINYINT = 0
DECLARE @studyKey BIGINT
DECLARE @maxWatermark BIGINT

CREATE TABLE #UpdatedInstances
(PartitionKey INT,
StudyInstanceUid VARCHAR(64),
SeriesInstanceUid VARCHAR(64),
SopInstanceUid VARCHAR(64),
Watermark BIGINT,
OriginalWatermark BIGINT,
InstanceKey BIGINT)

DELETE FROM #UpdatedInstances

UPDATE dbo.Instance
SET LastStatusUpdatedDate = @currentDate,
OriginalWatermark = ISNULL(OriginalWatermark, Watermark),
Watermark = NewWatermark,
NewWatermark = NULL
OUTPUT deleted.PartitionKey, @studyInstanceUid, deleted.SeriesInstanceUid, deleted.SopInstanceUid, inserted.Watermark, inserted.OriginalWatermark, deleted.InstanceKey INTO #UpdatedInstances
WHERE PartitionKey = @partitionKey
AND StudyInstanceUid = @studyInstanceUid
AND Status = 1
AND NewWatermark IS NOT NULL

-- Create index on temp table so we can join on watermark when we need to update change feed with file path.
IF NOT EXISTS (SELECT *
FROM tempdb.sys.indexes
WHERE name = 'IXC_UpdatedInstances')
CREATE UNIQUE INDEX IXC_UpdatedInstances ON #UpdatedInstances (Watermark)

-- Create index on temp table so we can join on instance key and watermark when we need to insert file
-- properties when instance key alone is does not specify a unique row on the table
IF NOT EXISTS (SELECT *
FROM tempdb.sys.indexes
WHERE name = 'IXC_UpdatedInstanceKeyWatermark')
CREATE UNIQUE CLUSTERED INDEX IXC_UpdatedInstanceKeyWatermark ON #UpdatedInstances (InstanceKey, OriginalWatermark)

-- Only updating patient information in a study
UPDATE dbo.Study
SET PatientId = ISNULL(@patientId, PatientId),
PatientName = ISNULL(@patientName, PatientName),
PatientBirthDate = ISNULL(@patientBirthDate, PatientBirthDate),
ReferringPhysicianName = ISNULL(@referringPhysicianName, ReferringPhysicianName),
StudyDescription = ISNULL(@studyDescription, StudyDescription),
AccessionNumber = ISNULL(@accessionNumber, AccessionNumber),
@studyKey = StudyKey
WHERE PartitionKey = @partitionKey
AND StudyInstanceUid = @studyInstanceUid

-- The study does not exist. May be deleted
IF @@ROWCOUNT = 0
THROW 50404, 'Study does not exist', 1

-- Delete from file properties any rows with "stale" watermarks if we will be inserting new ones
IF EXISTS (SELECT 1 FROM @insertFileProperties)
DELETE FP
FROM dbo.FileProperty as FP
INNER JOIN #UpdatedInstances U
ON U.InstanceKey = FP.InstanceKey
WHERE U.OriginalWatermark != FP.Watermark

-- Insert new file properties from added blobs, @insertFileProperties will be empty when external store not
-- enabled
INSERT INTO dbo.FileProperty
(InstanceKey, Watermark, FilePath, ETag)
SELECT U.InstanceKey, I.Watermark, I.FilePath, I.ETag
FROM @insertFileProperties I
INNER JOIN #UpdatedInstances U
ON U.Watermark = I.Watermark

SELECT
@maxWatermark = max(Watermark)
FROM #UpdatedInstances

-- Update extended query tags value if any
BEGIN TRY
EXEC dbo.IIndexInstanceCoreV9
@partitionKey,
@studyKey,
null, -- passing null to series key and instance
null,
@maxWatermark,
@stringExtendedQueryTags,
@longExtendedQueryTags,
@doubleExtendedQueryTags,
@dateTimeExtendedQueryTags,
@personNameExtendedQueryTags
END TRY
BEGIN CATCH
THROW
END CATCH

-- Insert into change feed table for update action type
INSERT INTO dbo.ChangeFeed
(Action, PartitionKey, StudyInstanceUid, SeriesInstanceUid, SopInstanceUid, OriginalWatermark)
SELECT 2, PartitionKey, StudyInstanceUid, SeriesInstanceUid, SopInstanceUid, Watermark
FROM #UpdatedInstances

-- Update existing instance currentWatermark to latest and update file path
UPDATE C
SET CurrentWatermark = U.Watermark, FilePath = I.FilePath
FROM dbo.ChangeFeed C
JOIN #UpdatedInstances U
ON C.PartitionKey = U.PartitionKey
AND C.StudyInstanceUid = U.StudyInstanceUid
AND C.SeriesInstanceUid = U.SeriesInstanceUid
AND C.SopInstanceUid = U.SopInstanceUid
LEFT OUTER JOIN @insertFileProperties I
ON I.Watermark = U.Watermark

COMMIT TRANSACTION
END
GO

COMMIT TRANSACTION
Loading

0 comments on commit 974ad54

Please sign in to comment.