Mercurial > hg > orthanc
annotate OrthancFramework/Sources/JobsEngine/SetOfCommandsJob.cpp @ 4224:38d446c9ee1d
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 30 Sep 2020 17:59:09 +0200 |
parents | bf7b9edf6b81 |
children | b30a8de92ad9 |
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 | |
52 | |
53 void SetOfCommandsJob::Reserve(size_t size) | |
54 { | |
55 if (started_) | |
56 { | |
57 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
58 } | |
59 else | |
60 { | |
61 commands_.reserve(size); | |
62 } | |
63 } | |
64 | |
65 | |
66 void SetOfCommandsJob::AddCommand(ICommand* command) | |
67 { | |
68 if (command == NULL) | |
69 { | |
70 throw OrthancException(ErrorCode_NullPointer); | |
71 } | |
72 else if (started_) | |
73 { | |
74 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
75 } | |
76 else | |
77 { | |
78 commands_.push_back(command); | |
79 } | |
80 } | |
81 | |
82 | |
83 void SetOfCommandsJob::SetPermissive(bool permissive) | |
84 { | |
85 if (started_) | |
86 { | |
87 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
88 } | |
89 else | |
90 { | |
91 permissive_ = permissive; | |
92 } | |
93 } | |
94 | |
95 | |
96 void SetOfCommandsJob::Reset() | |
97 { | |
98 if (started_) | |
99 { | |
100 position_ = 0; | |
101 } | |
102 else | |
103 { | |
104 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
105 } | |
106 } | |
107 | |
108 | |
109 float SetOfCommandsJob::GetProgress() | |
110 { | |
111 if (commands_.empty()) | |
112 { | |
113 return 1; | |
114 } | |
115 else | |
116 { | |
117 return (static_cast<float>(position_) / | |
118 static_cast<float>(commands_.size())); | |
119 } | |
120 } | |
121 | |
122 | |
123 const SetOfCommandsJob::ICommand& SetOfCommandsJob::GetCommand(size_t index) const | |
124 { | |
125 if (index >= commands_.size()) | |
126 { | |
127 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
128 } | |
129 else | |
130 { | |
131 assert(commands_[index] != NULL); | |
132 return *commands_[index]; | |
133 } | |
134 } | |
135 | |
136 | |
3658
2d90dd30858c
providing job ID to the IJob::Step() methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
137 JobStepResult SetOfCommandsJob::Step(const std::string& jobId) |
2860 | 138 { |
139 if (!started_) | |
140 { | |
141 throw OrthancException(ErrorCode_InternalError); | |
142 } | |
143 | |
144 if (commands_.empty() && | |
145 position_ == 0) | |
146 { | |
147 // No command to handle: We're done | |
148 position_ = 1; | |
149 return JobStepResult::Success(); | |
150 } | |
151 | |
152 if (position_ >= commands_.size()) | |
153 { | |
154 // Already done | |
155 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
156 } | |
157 | |
158 try | |
159 { | |
160 // 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
|
161 if (!commands_[position_]->Execute(jobId)) |
2860 | 162 { |
163 // Error | |
164 if (!permissive_) | |
165 { | |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
166 return JobStepResult::Failure(ErrorCode_InternalError, NULL); |
2860 | 167 } |
168 } | |
169 } | |
170 catch (OrthancException& e) | |
171 { | |
172 if (permissive_) | |
173 { | |
174 LOG(WARNING) << "Ignoring an error in a permissive job: " << e.What(); | |
175 } | |
176 else | |
177 { | |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
178 return JobStepResult::Failure(e); |
2860 | 179 } |
180 } | |
181 | |
182 position_ += 1; | |
183 | |
184 if (position_ == commands_.size()) | |
185 { | |
186 // We're done | |
187 return JobStepResult::Success(); | |
188 } | |
189 else | |
190 { | |
191 return JobStepResult::Continue(); | |
192 } | |
193 } | |
194 | |
195 | |
196 | |
197 static const char* KEY_DESCRIPTION = "Description"; | |
198 static const char* KEY_PERMISSIVE = "Permissive"; | |
199 static const char* KEY_POSITION = "Position"; | |
200 static const char* KEY_TYPE = "Type"; | |
201 static const char* KEY_COMMANDS = "Commands"; | |
202 | |
203 | |
204 void SetOfCommandsJob::GetPublicContent(Json::Value& value) | |
205 { | |
206 value[KEY_DESCRIPTION] = GetDescription(); | |
207 } | |
208 | |
209 | |
210 bool SetOfCommandsJob::Serialize(Json::Value& target) | |
211 { | |
212 target = Json::objectValue; | |
213 | |
214 std::string type; | |
215 GetJobType(type); | |
216 target[KEY_TYPE] = type; | |
217 | |
218 target[KEY_PERMISSIVE] = permissive_; | |
219 target[KEY_POSITION] = static_cast<unsigned int>(position_); | |
220 target[KEY_DESCRIPTION] = description_; | |
221 | |
222 target[KEY_COMMANDS] = Json::arrayValue; | |
223 Json::Value& tmp = target[KEY_COMMANDS]; | |
224 | |
225 for (size_t i = 0; i < commands_.size(); i++) | |
226 { | |
227 assert(commands_[i] != NULL); | |
228 | |
229 Json::Value command; | |
230 commands_[i]->Serialize(command); | |
231 tmp.append(command); | |
232 } | |
233 | |
234 return true; | |
235 } | |
236 | |
237 | |
238 SetOfCommandsJob::SetOfCommandsJob(ICommandUnserializer* unserializer, | |
239 const Json::Value& source) : | |
240 started_(false) | |
241 { | |
3712
2a170a8f1faf
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
242 std::unique_ptr<ICommandUnserializer> raii(unserializer); |
2860 | 243 |
244 permissive_ = SerializationToolbox::ReadBoolean(source, KEY_PERMISSIVE); | |
245 position_ = SerializationToolbox::ReadUnsignedInteger(source, KEY_POSITION); | |
246 description_ = SerializationToolbox::ReadString(source, KEY_DESCRIPTION); | |
247 | |
248 if (!source.isMember(KEY_COMMANDS) || | |
249 source[KEY_COMMANDS].type() != Json::arrayValue) | |
250 { | |
251 throw OrthancException(ErrorCode_BadFileFormat); | |
252 } | |
253 else | |
254 { | |
255 const Json::Value& tmp = source[KEY_COMMANDS]; | |
256 commands_.resize(tmp.size()); | |
257 | |
258 for (Json::Value::ArrayIndex i = 0; i < tmp.size(); i++) | |
259 { | |
260 try | |
261 { | |
262 commands_[i] = unserializer->Unserialize(tmp[i]); | |
263 } | |
264 catch (OrthancException&) | |
265 { | |
266 } | |
267 | |
268 if (commands_[i] == NULL) | |
269 { | |
270 for (size_t j = 0; j < i; j++) | |
271 { | |
272 delete commands_[j]; | |
273 } | |
274 | |
275 throw OrthancException(ErrorCode_BadFileFormat); | |
276 } | |
277 } | |
278 } | |
279 | |
280 if (commands_.empty()) | |
281 { | |
282 if (position_ > 1) | |
283 { | |
284 throw OrthancException(ErrorCode_BadFileFormat); | |
285 } | |
286 } | |
287 else if (position_ > commands_.size()) | |
288 { | |
289 throw OrthancException(ErrorCode_BadFileFormat); | |
290 } | |
291 } | |
292 } |