Mercurial > hg > orthanc
annotate OrthancFramework/Sources/JobsEngine/SetOfCommandsJob.cpp @ 5040:1c08cd68250a
removed the 'name' of MainDicomTags to simplify code -> DicomMap::ParseMainDicomTags has been deprecated and one should use DicomMap::FromDicomAsJson on 'full' formatted tags
author | Alain Mazy <am@osimis.io> |
---|---|
date | Mon, 27 Jun 2022 12:39:51 +0200 |
parents | 43e613a7756b |
children | 0ea402b4d901 |
rev | line source |
---|---|
2860 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
2860 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * the License, or (at your option) any later version. |
2860 | 12 * |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
16 * Lesser General Public License for more details. |
2860 | 17 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
2860 | 21 **/ |
22 | |
23 | |
24 #include "../PrecompiledHeaders.h" | |
25 #include "SetOfCommandsJob.h" | |
26 | |
27 #include "../Logging.h" | |
28 #include "../OrthancException.h" | |
29 #include "../SerializationToolbox.h" | |
30 | |
31 #include <cassert> | |
2866 | 32 #include <memory> |
2860 | 33 |
34 namespace Orthanc | |
35 { | |
36 SetOfCommandsJob::SetOfCommandsJob() : | |
37 started_(false), | |
38 permissive_(false), | |
39 position_(0) | |
40 { | |
41 } | |
42 | |
43 | |
44 SetOfCommandsJob::~SetOfCommandsJob() | |
45 { | |
46 for (size_t i = 0; i < commands_.size(); i++) | |
47 { | |
48 assert(commands_[i] != NULL); | |
49 delete commands_[i]; | |
50 } | |
51 } | |
52 | |
4300 | 53 size_t SetOfCommandsJob::GetPosition() const |
54 { | |
55 return position_; | |
56 } | |
57 | |
58 void SetOfCommandsJob::SetDescription(const std::string &description) | |
59 { | |
60 description_ = description; | |
61 } | |
62 | |
4304 | 63 const std::string& SetOfCommandsJob::GetDescription() const |
64 { | |
65 return description_; | |
66 } | |
4300 | 67 |
2860 | 68 void SetOfCommandsJob::Reserve(size_t size) |
69 { | |
70 if (started_) | |
71 { | |
72 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
73 } | |
74 else | |
75 { | |
76 commands_.reserve(size); | |
77 } | |
78 } | |
79 | |
4300 | 80 size_t SetOfCommandsJob::GetCommandsCount() const |
81 { | |
82 return commands_.size(); | |
83 } | |
84 | |
85 | |
2860 | 86 void SetOfCommandsJob::AddCommand(ICommand* command) |
87 { | |
88 if (command == NULL) | |
89 { | |
90 throw OrthancException(ErrorCode_NullPointer); | |
91 } | |
92 else if (started_) | |
93 { | |
94 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
95 } | |
96 else | |
97 { | |
98 commands_.push_back(command); | |
99 } | |
100 } | |
101 | |
4300 | 102 bool SetOfCommandsJob::IsPermissive() const |
103 { | |
104 return permissive_; | |
105 } | |
106 | |
2860 | 107 |
108 void SetOfCommandsJob::SetPermissive(bool permissive) | |
109 { | |
110 if (started_) | |
111 { | |
112 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
113 } | |
114 else | |
115 { | |
116 permissive_ = permissive; | |
117 } | |
118 } | |
119 | |
120 | |
121 void SetOfCommandsJob::Reset() | |
122 { | |
123 if (started_) | |
124 { | |
125 position_ = 0; | |
126 } | |
127 else | |
128 { | |
129 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
130 } | |
131 } | |
132 | |
4300 | 133 void SetOfCommandsJob::Start() |
134 { | |
135 started_ = true; | |
136 } | |
137 | |
138 | |
2860 | 139 float SetOfCommandsJob::GetProgress() |
140 { | |
141 if (commands_.empty()) | |
142 { | |
143 return 1; | |
144 } | |
145 else | |
146 { | |
147 return (static_cast<float>(position_) / | |
148 static_cast<float>(commands_.size())); | |
149 } | |
150 } | |
151 | |
4300 | 152 bool SetOfCommandsJob::IsStarted() const |
153 { | |
154 return started_; | |
155 } | |
156 | |
2860 | 157 |
158 const SetOfCommandsJob::ICommand& SetOfCommandsJob::GetCommand(size_t index) const | |
159 { | |
160 if (index >= commands_.size()) | |
161 { | |
162 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
163 } | |
164 else | |
165 { | |
166 assert(commands_[index] != NULL); | |
167 return *commands_[index]; | |
168 } | |
169 } | |
170 | |
171 | |
3658
2d90dd30858c
providing job ID to the IJob::Step() methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
172 JobStepResult SetOfCommandsJob::Step(const std::string& jobId) |
2860 | 173 { |
174 if (!started_) | |
175 { | |
176 throw OrthancException(ErrorCode_InternalError); | |
177 } | |
178 | |
179 if (commands_.empty() && | |
180 position_ == 0) | |
181 { | |
182 // No command to handle: We're done | |
183 position_ = 1; | |
184 return JobStepResult::Success(); | |
185 } | |
186 | |
187 if (position_ >= commands_.size()) | |
188 { | |
189 // Already done | |
190 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
191 } | |
192 | |
193 try | |
194 { | |
195 // Not at the trailing step: Handle the current command | |
3658
2d90dd30858c
providing job ID to the IJob::Step() methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
196 if (!commands_[position_]->Execute(jobId)) |
2860 | 197 { |
198 // Error | |
199 if (!permissive_) | |
200 { | |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
201 return JobStepResult::Failure(ErrorCode_InternalError, NULL); |
2860 | 202 } |
203 } | |
204 } | |
205 catch (OrthancException& e) | |
206 { | |
207 if (permissive_) | |
208 { | |
209 LOG(WARNING) << "Ignoring an error in a permissive job: " << e.What(); | |
210 } | |
211 else | |
212 { | |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
213 return JobStepResult::Failure(e); |
2860 | 214 } |
215 } | |
216 | |
217 position_ += 1; | |
218 | |
219 if (position_ == commands_.size()) | |
220 { | |
221 // We're done | |
222 return JobStepResult::Success(); | |
223 } | |
224 else | |
225 { | |
226 return JobStepResult::Continue(); | |
227 } | |
228 } | |
229 | |
230 | |
231 | |
232 static const char* KEY_DESCRIPTION = "Description"; | |
233 static const char* KEY_PERMISSIVE = "Permissive"; | |
234 static const char* KEY_POSITION = "Position"; | |
235 static const char* KEY_TYPE = "Type"; | |
236 static const char* KEY_COMMANDS = "Commands"; | |
237 | |
238 | |
239 void SetOfCommandsJob::GetPublicContent(Json::Value& value) | |
240 { | |
241 value[KEY_DESCRIPTION] = GetDescription(); | |
242 } | |
243 | |
244 | |
245 bool SetOfCommandsJob::Serialize(Json::Value& target) | |
246 { | |
247 target = Json::objectValue; | |
248 | |
249 std::string type; | |
250 GetJobType(type); | |
251 target[KEY_TYPE] = type; | |
252 | |
253 target[KEY_PERMISSIVE] = permissive_; | |
254 target[KEY_POSITION] = static_cast<unsigned int>(position_); | |
255 target[KEY_DESCRIPTION] = description_; | |
256 | |
257 target[KEY_COMMANDS] = Json::arrayValue; | |
258 Json::Value& tmp = target[KEY_COMMANDS]; | |
259 | |
260 for (size_t i = 0; i < commands_.size(); i++) | |
261 { | |
262 assert(commands_[i] != NULL); | |
263 | |
264 Json::Value command; | |
265 commands_[i]->Serialize(command); | |
266 tmp.append(command); | |
267 } | |
268 | |
269 return true; | |
270 } | |
271 | |
4300 | 272 bool SetOfCommandsJob::GetOutput(std::string &output, |
273 MimeType &mime, | |
4805
0a38000b086d
Archive jobs response now contains a header Content-Disposition:filename='archive.zip'
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
274 std::string& filename, |
4300 | 275 const std::string &key) |
276 { | |
277 return false; | |
278 } | |
279 | |
2860 | 280 |
281 SetOfCommandsJob::SetOfCommandsJob(ICommandUnserializer* unserializer, | |
282 const Json::Value& source) : | |
283 started_(false) | |
284 { | |
3712
2a170a8f1faf
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
285 std::unique_ptr<ICommandUnserializer> raii(unserializer); |
2860 | 286 |
287 permissive_ = SerializationToolbox::ReadBoolean(source, KEY_PERMISSIVE); | |
288 position_ = SerializationToolbox::ReadUnsignedInteger(source, KEY_POSITION); | |
289 description_ = SerializationToolbox::ReadString(source, KEY_DESCRIPTION); | |
290 | |
291 if (!source.isMember(KEY_COMMANDS) || | |
292 source[KEY_COMMANDS].type() != Json::arrayValue) | |
293 { | |
294 throw OrthancException(ErrorCode_BadFileFormat); | |
295 } | |
296 else | |
297 { | |
298 const Json::Value& tmp = source[KEY_COMMANDS]; | |
299 commands_.resize(tmp.size()); | |
300 | |
301 for (Json::Value::ArrayIndex i = 0; i < tmp.size(); i++) | |
302 { | |
303 try | |
304 { | |
305 commands_[i] = unserializer->Unserialize(tmp[i]); | |
306 } | |
307 catch (OrthancException&) | |
308 { | |
309 } | |
310 | |
311 if (commands_[i] == NULL) | |
312 { | |
313 for (size_t j = 0; j < i; j++) | |
314 { | |
315 delete commands_[j]; | |
316 } | |
317 | |
318 throw OrthancException(ErrorCode_BadFileFormat); | |
319 } | |
320 } | |
321 } | |
322 | |
323 if (commands_.empty()) | |
324 { | |
325 if (position_ > 1) | |
326 { | |
327 throw OrthancException(ErrorCode_BadFileFormat); | |
328 } | |
329 } | |
330 else if (position_ > commands_.size()) | |
331 { | |
332 throw OrthancException(ErrorCode_BadFileFormat); | |
333 } | |
334 } | |
335 } |