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