lua script for automatic changing PatientID and PatientName

Based on the documentation and different examples i wrote a lua script to change to PatientID and PatientName of studies (instances) to the stationname. Despite different attempts I could not get the script to work and keep getting a “bad parameters to RestApiPost()-error”.
Any suggestions where I’m going wrong here?

Script:

function OnStoredInstance(instanceId, tags, metadata, origin)
if origin[‘RequestOrigin’] ~= ‘Lua’ then
local name = tags[“StationName”]

local replace = {}
replace[‘PatientID’] = name
replace[‘PatientName’] = name

local request = {}
request[‘Replace’] = replace
request[‘Force’] = true

local modified = RestApiPost(‘/instances/’ … instanceId … ‘/modify’, DumpJson(request, true))
RestApiPost(‘/instances/’ … modified)
RestApiDelete(‘/instances/’ … instanceId)
end
end

Hi Jaap,

Instead of

RestApiPost(‘/instances/’ … modified)
, you should write
RestApiPost(‘/instances/’, modified)

you were actually concatenating the content of the file into the url itself.

Best,

Alain

Nicely spotted, thanks!

Hi,

I wish to modify the ‘StationName’ with the username with which Orthanc server has been logged into. I modified the script to

function OnStoredInstance(instanceId, tags, metadata, origin)

if origin[‘RequestOrigin’] ~= ‘Lua’ then
local name = username

local replace = {}
replace[‘StationName’] = name

local request = {}
request[‘Replace’] = replace
request[‘Force’] = true

local modified = RestApiPost(‘/instances/’ … instanceId … ‘/modify’, DumpJson(request, true))
RestApiPost(‘/instances/’, modified)
RestApiDelete(‘/instances/’ … instanceId)
end
end

it gives me “bad parameters to RestApiPost()” error
Any suggestions!!

Hello,

Your script will obviously fail, as the variable “username” is not defined.

The “username” information is only available if the file was received through the REST API, and is available inside the “origin” argument:
https://book.orthanc-server.com/users/lua.html#origin-of-the-instances

Here is a working version of the script:

function OnStoredInstance(instanceId, tags, metadata, origin)
if origin[‘RequestOrigin’] == ‘RestApi’ then
local name = origin[‘Username’]

local replace = {}
replace[‘StationName’] = name

local request = {}
request[‘Replace’] = replace

local modified = RestApiPost(‘/instances/’ … instanceId … ‘/modify’, DumpJson(request, true))
RestApiPost(‘/instances/’, modified)
RestApiDelete(‘/instances/’ … instanceId)
end
end

Hello,

Thanks.

Now there is one issue. As seen in the attached file - in the ‘Series’ display box the StationName is still the original one ("HOST-336121” in this case) and in the details of a particular ‘Instance’ it has changed to the username (“harry” in this particular instance).

Regards

Screenshot 2020-03-16 at 7.09.48 AM.jpg

Hello,

The “StationName” is a series-level tag.

Your modification breaks the DICOM model by having 2 different values for the same series-level tag in the same series. In such a context, Orthanc keeps the first value it encountered (even after the old DICOM file is deleted).

You’ll have to make a POST call to “/instances/…/reconstruct” to force Orthanc recreate its database of tags. This would give something like the following script (modifications in bold):

function OnStoredInstance(instanceId, tags, metadata, origin)
if origin[‘RequestOrigin’] == ‘RestApi’ then
local name = origin[‘Username’]

local replace = {}
replace[‘StationName’] = name

local request = {}
request[‘Replace’] = replace

local modified = RestApiPost(‘/instances/’ … instanceId … ‘/modify’, DumpJson(request, true))
local a = ParseJson(RestApiPost(‘/instances/’, modified))
RestApiPost(‘/instances/’ … a[‘ID’] … ‘/reconstruct’, ‘’)

RestApiDelete(‘/instances/’ … instanceId)
end
end

HTH,

Sébastien-

Thanks. It works flawlessly now.