Mercurial > hg > orthanc
annotate OrthancServer/ServerJobs/ResourceModificationJob.cpp @ 2727:815e30657dad jobs
fix warning
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 12 Jul 2018 21:50:15 +0200 |
parents | a21b244efb37 |
children | 8aa6aef11b70 |
rev | line source |
---|---|
2642 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "../PrecompiledHeadersServer.h" | |
35 #include "ResourceModificationJob.h" | |
36 | |
37 #include "../../Core/Logging.h" | |
2664
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
38 #include "../../Core/SerializationToolbox.h" |
2642 | 39 |
40 namespace Orthanc | |
41 { | |
42 ResourceModificationJob::Output::Output(ResourceType level) : | |
43 level_(level), | |
44 isFirst_(true) | |
45 { | |
46 if (level_ != ResourceType_Patient && | |
47 level_ != ResourceType_Study && | |
48 level_ != ResourceType_Series) | |
49 { | |
50 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
51 } | |
52 } | |
53 | |
54 | |
55 void ResourceModificationJob::Output::Update(DicomInstanceHasher& hasher) | |
56 { | |
57 boost::mutex::scoped_lock lock(mutex_); | |
58 | |
59 if (isFirst_) | |
60 { | |
61 switch (level_) | |
62 { | |
63 case ResourceType_Series: | |
64 id_ = hasher.HashSeries(); | |
65 break; | |
66 | |
67 case ResourceType_Study: | |
68 id_ = hasher.HashStudy(); | |
69 break; | |
70 | |
71 case ResourceType_Patient: | |
72 id_ = hasher.HashPatient(); | |
73 break; | |
74 | |
75 default: | |
76 throw OrthancException(ErrorCode_InternalError); | |
77 } | |
78 | |
79 patientId_ = hasher.HashPatient(); | |
80 isFirst_ = false; | |
81 } | |
82 } | |
83 | |
84 | |
85 bool ResourceModificationJob::Output::Format(Json::Value& target) | |
86 { | |
87 boost::mutex::scoped_lock lock(mutex_); | |
88 | |
89 if (isFirst_) | |
90 { | |
91 return false; | |
92 } | |
93 else | |
94 { | |
95 target = Json::objectValue; | |
96 target["Type"] = EnumerationToString(level_); | |
97 target["ID"] = id_; | |
98 target["Path"] = GetBasePath(level_, id_); | |
99 target["PatientID"] = patientId_; | |
100 return true; | |
101 } | |
102 } | |
103 | |
104 | |
105 bool ResourceModificationJob::Output::GetIdentifier(std::string& id) | |
106 { | |
107 boost::mutex::scoped_lock lock(mutex_); | |
108 | |
109 if (isFirst_) | |
110 { | |
111 return false; | |
112 } | |
113 else | |
114 { | |
115 id = id_; | |
116 return true; | |
117 } | |
118 } | |
119 | |
120 | |
121 bool ResourceModificationJob::HandleInstance(const std::string& instance) | |
122 { | |
123 if (modification_.get() == NULL) | |
124 { | |
125 LOG(ERROR) << "No modification was provided for this job"; | |
126 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
127 } | |
128 | |
129 | |
130 LOG(INFO) << "Modifying instance in a job: " << instance; | |
131 | |
132 std::auto_ptr<ServerContext::DicomCacheLocker> locker; | |
133 | |
134 try | |
135 { | |
136 locker.reset(new ServerContext::DicomCacheLocker(context_, instance)); | |
137 } | |
138 catch (OrthancException&) | |
139 { | |
140 LOG(WARNING) << "An instance was removed after the job was issued: " << instance; | |
141 return false; | |
142 } | |
143 | |
144 | |
145 ParsedDicomFile& original = locker->GetDicom(); | |
146 DicomInstanceHasher originalHasher = original.GetHasher(); | |
147 | |
148 | |
149 /** | |
150 * Compute the resulting DICOM instance. | |
151 **/ | |
152 | |
153 std::auto_ptr<ParsedDicomFile> modified(original.Clone(true)); | |
154 modification_->Apply(*modified); | |
155 | |
156 DicomInstanceToStore toStore; | |
157 toStore.SetOrigin(origin_); | |
158 toStore.SetParsedDicomFile(*modified); | |
159 | |
160 | |
161 /** | |
162 * Prepare the metadata information to associate with the | |
163 * resulting DICOM instance (AnonymizedFrom/ModifiedFrom). | |
164 **/ | |
165 | |
166 DicomInstanceHasher modifiedHasher = modified->GetHasher(); | |
167 | |
168 MetadataType metadataType = (isAnonymization_ ? | |
169 MetadataType_AnonymizedFrom : | |
170 MetadataType_ModifiedFrom); | |
171 | |
172 if (originalHasher.HashSeries() != modifiedHasher.HashSeries()) | |
173 { | |
174 toStore.AddMetadata(ResourceType_Series, metadataType, originalHasher.HashSeries()); | |
175 } | |
176 | |
177 if (originalHasher.HashStudy() != modifiedHasher.HashStudy()) | |
178 { | |
179 toStore.AddMetadata(ResourceType_Study, metadataType, originalHasher.HashStudy()); | |
180 } | |
181 | |
182 if (originalHasher.HashPatient() != modifiedHasher.HashPatient()) | |
183 { | |
184 toStore.AddMetadata(ResourceType_Patient, metadataType, originalHasher.HashPatient()); | |
185 } | |
186 | |
187 assert(instance == originalHasher.HashInstance()); | |
188 toStore.AddMetadata(ResourceType_Instance, metadataType, instance); | |
189 | |
190 | |
191 /** | |
192 * Store the resulting DICOM instance into the Orthanc store. | |
193 **/ | |
194 | |
195 std::string modifiedInstance; | |
196 if (context_.Store(modifiedInstance, toStore) != StoreStatus_Success) | |
197 { | |
198 LOG(ERROR) << "Error while storing a modified instance " << instance; | |
199 throw OrthancException(ErrorCode_CannotStoreInstance); | |
200 } | |
201 | |
202 // Sanity checks in debug mode | |
203 assert(modifiedInstance == modifiedHasher.HashInstance()); | |
204 | |
205 | |
206 if (output_.get() != NULL) | |
207 { | |
208 output_->Update(modifiedHasher); | |
209 } | |
210 | |
211 return true; | |
212 } | |
213 | |
214 | |
215 | |
216 void ResourceModificationJob::SetModification(DicomModification* modification, | |
217 bool isAnonymization) | |
218 { | |
219 if (modification == NULL) | |
220 { | |
221 throw OrthancException(ErrorCode_NullPointer); | |
222 } | |
223 else if (IsStarted()) | |
224 { | |
225 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
226 } | |
227 else | |
228 { | |
229 modification_.reset(modification); | |
230 isAnonymization_ = isAnonymization; | |
231 } | |
232 } | |
233 | |
234 | |
235 void ResourceModificationJob::SetOutput(boost::shared_ptr<Output>& output) | |
236 { | |
237 if (IsStarted()) | |
238 { | |
239 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
240 } | |
241 else | |
242 { | |
243 output_ = output; | |
244 } | |
245 } | |
246 | |
247 | |
248 void ResourceModificationJob::SetOrigin(const DicomInstanceOrigin& origin) | |
249 { | |
250 if (IsStarted()) | |
251 { | |
252 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
253 } | |
254 else | |
255 { | |
256 origin_ = origin; | |
257 } | |
258 } | |
259 | |
260 | |
261 void ResourceModificationJob::SetOrigin(const RestApiCall& call) | |
262 { | |
2664
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
263 SetOrigin(DicomInstanceOrigin::FromRest(call)); |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
264 } |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
265 |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
266 |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
267 const DicomModification& ResourceModificationJob::GetModification() const |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
268 { |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
269 if (modification_.get() == NULL) |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
270 { |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
271 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
272 } |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
273 else |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
274 { |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
275 return *modification_; |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
276 } |
2642 | 277 } |
278 | |
279 | |
280 void ResourceModificationJob::GetPublicContent(Json::Value& value) | |
281 { | |
282 SetOfInstancesJob::GetPublicContent(value); | |
283 | |
284 value["IsAnonymization"] = isAnonymization_; | |
285 } | |
286 | |
2664
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
287 |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
288 static const char* MODIFICATION = "Modification"; |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
289 static const char* ORIGIN = "Origin"; |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
290 static const char* IS_ANONYMIZATION = "IsAnonymization"; |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
291 |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
292 |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
293 ResourceModificationJob::ResourceModificationJob(ServerContext& context, |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
294 const Json::Value& serialized) : |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
295 SetOfInstancesJob(serialized), |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
296 context_(context) |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
297 { |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
298 isAnonymization_ = SerializationToolbox::ReadBoolean(serialized, IS_ANONYMIZATION); |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
299 origin_ = DicomInstanceOrigin(serialized[ORIGIN]); |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
300 modification_.reset(new DicomModification(serialized[MODIFICATION])); |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
301 } |
2642 | 302 |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2648
diff
changeset
|
303 bool ResourceModificationJob::Serialize(Json::Value& value) |
2642 | 304 { |
2664
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
305 if (!SetOfInstancesJob::Serialize(value)) |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
306 { |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
307 return false; |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
308 } |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
309 else |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
310 { |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
311 value[IS_ANONYMIZATION] = isAnonymization_; |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
312 origin_.Serialize(value[ORIGIN]); |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
313 |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
314 Json::Value tmp; |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
315 modification_->Serialize(tmp); |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
316 value[MODIFICATION] = tmp; |
2642 | 317 |
2664
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
318 return true; |
a21b244efb37
serialization of DicomModalityStoreJob, OrthancPeerStoreJob and ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
319 } |
2642 | 320 } |
321 } |