Mercurial > hg > orthanc
annotate OrthancServer/Sources/ServerJobs/Operations/ModifyInstanceOperation.cpp @ 5610:d4b570834d3a find-refactoring
adding safeguards around FindRequest::SetRetrieveOneInstanceIdentifier()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 09 May 2024 10:36:03 +0200 |
parents | 48b8dae6dc77 |
children | f7adfb22e20e |
rev | line source |
---|---|
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 /** |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium |
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 * |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 * This program is free software: you can redistribute it and/or |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 * modify it under the terms of the GNU General Public License as |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 * published by the Free Software Foundation, either version 3 of the |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 * License, or (at your option) any later version. |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 * |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 * This program is distributed in the hope that it will be useful, but |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 * General Public License for more details. |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 * |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 * You should have received a copy of the GNU General Public License |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 **/ |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 |
2641 | 23 #include "../../PrecompiledHeadersServer.h" |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 #include "ModifyInstanceOperation.h" |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 #include "DicomInstanceOperationValue.h" |
3095
beeeb6096f27
removing dependencies upon ServerContext
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
27 #include "../../ServerContext.h" |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 |
4045 | 29 #include "../../../../OrthancFramework/Sources/Logging.h" |
30 #include "../../../../OrthancFramework/Sources/SerializationToolbox.h" | |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 namespace Orthanc |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 { |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 ModifyInstanceOperation::ModifyInstanceOperation(ServerContext& context, |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 RequestOrigin origin, |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 DicomModification* modification) : |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 context_(context), |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 origin_(origin), |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 modification_(modification) |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 { |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 if (modification == NULL) |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 { |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 throw OrthancException(ErrorCode_NullPointer); |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 } |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 |
5122
8638522eeda1
revert changeset df4a90a65af9 that removes features used elsewhere, and protect by Force the change of UIDs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5087
diff
changeset
|
46 modification_->SetAllowManualIdentifiers(true); |
8638522eeda1
revert changeset df4a90a65af9 that removes features used elsewhere, and protect by Force the change of UIDs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5087
diff
changeset
|
47 |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 if (modification_->IsReplaced(DICOM_TAG_PATIENT_ID)) |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 { |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 modification_->SetLevel(ResourceType_Patient); |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 } |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 else if (modification_->IsReplaced(DICOM_TAG_STUDY_INSTANCE_UID)) |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 { |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 modification_->SetLevel(ResourceType_Study); |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 } |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 else if (modification_->IsReplaced(DICOM_TAG_SERIES_INSTANCE_UID)) |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
57 { |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
58 modification_->SetLevel(ResourceType_Series); |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 } |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 else |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 { |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 modification_->SetLevel(ResourceType_Instance); |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
63 } |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
64 |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 if (origin_ != RequestOrigin_Lua) |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
66 { |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 // TODO If issued from HTTP, "remoteIp" and "username" must be provided |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 throw OrthancException(ErrorCode_NotImplemented); |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 } |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 } |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
71 |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
72 void ModifyInstanceOperation::Apply(JobOperationValues& outputs, |
4310
2ae905070221
renaming pure interface JobOperationValue as IJobOperationValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4045
diff
changeset
|
73 const IJobOperationValue& input) |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 { |
4310
2ae905070221
renaming pure interface JobOperationValue as IJobOperationValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4045
diff
changeset
|
75 if (input.GetType() != IJobOperationValue::Type_DicomInstance) |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 { |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 throw OrthancException(ErrorCode_BadParameterType); |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 } |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
80 const DicomInstanceOperationValue& instance = |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
81 dynamic_cast<const DicomInstanceOperationValue&>(input); |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
82 |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
83 LOG(INFO) << "Lua: Modifying instance " << instance.GetId(); |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
84 |
3712
2a170a8f1faf
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
85 std::unique_ptr<ParsedDicomFile> modified; |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
86 |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
87 { |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
88 ServerContext::DicomCacheLocker lock(context_, instance.GetId()); |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
89 modified.reset(lock.GetDicom().Clone(true)); |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
90 } |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
91 |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
92 try |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
93 { |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 modification_->Apply(*modified); |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
95 |
4508
8f9090b137f1
Optimization in C-STORE SCP by avoiding an unnecessary DICOM parsing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
96 std::unique_ptr<DicomInstanceToStore> toStore(DicomInstanceToStore::CreateFromParsedDicomFile(*modified)); |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
97 assert(origin_ == RequestOrigin_Lua); |
4508
8f9090b137f1
Optimization in C-STORE SCP by avoiding an unnecessary DICOM parsing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
98 toStore->SetOrigin(DicomInstanceOrigin::FromLua()); |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
99 |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
100 // TODO other metadata |
4508
8f9090b137f1
Optimization in C-STORE SCP by avoiding an unnecessary DICOM parsing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
101 toStore->AddMetadata(ResourceType_Instance, MetadataType_ModifiedFrom, instance.GetId()); |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
102 |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
103 std::string modifiedId; |
4508
8f9090b137f1
Optimization in C-STORE SCP by avoiding an unnecessary DICOM parsing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
104 context_.Store(modifiedId, *toStore, StoreInstanceMode_Default); |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
105 |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
106 // Only chain with other commands if this command succeeds |
2616
2f3007bf0708
event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2608
diff
changeset
|
107 outputs.Append(new DicomInstanceOperationValue(instance.GetServerContext(), modifiedId)); |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
108 } |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
109 catch (OrthancException& e) |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
110 { |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
111 LOG(ERROR) << "Lua: Unable to modify instance " << instance.GetId() |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
112 << ": " << e.What(); |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
113 } |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
114 } |
2616
2f3007bf0708
event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2608
diff
changeset
|
115 |
2f3007bf0708
event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2608
diff
changeset
|
116 |
2f3007bf0708
event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2608
diff
changeset
|
117 void ModifyInstanceOperation::Serialize(Json::Value& target) const |
2f3007bf0708
event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2608
diff
changeset
|
118 { |
2655 | 119 target = Json::objectValue; |
2616
2f3007bf0708
event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2608
diff
changeset
|
120 target["Type"] = "ModifyInstance"; |
2f3007bf0708
event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2608
diff
changeset
|
121 target["Origin"] = EnumerationToString(origin_); |
2f3007bf0708
event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2608
diff
changeset
|
122 modification_->Serialize(target["Modification"]); |
2f3007bf0708
event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2608
diff
changeset
|
123 } |
2655 | 124 |
125 | |
126 ModifyInstanceOperation::ModifyInstanceOperation(ServerContext& context, | |
127 const Json::Value& serialized) : | |
128 context_(context) | |
129 { | |
2656
a6d3e45eeff5
SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
130 if (SerializationToolbox::ReadString(serialized, "Type") != "ModifyInstance" || |
2655 | 131 !serialized.isMember("Modification")) |
132 { | |
133 throw OrthancException(ErrorCode_BadFileFormat); | |
134 } | |
135 | |
2656
a6d3e45eeff5
SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
136 origin_ = StringToRequestOrigin(SerializationToolbox::ReadString(serialized, "Origin")); |
2655 | 137 |
138 modification_.reset(new DicomModification(serialized["Modification"])); | |
139 } | |
2607
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
140 } |
44e268605478
ModifyInstanceOperation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
141 |