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