Mercurial > hg > orthanc
comparison OrthancFramework/Sources/JobsEngine/SetOfInstancesJob.cpp @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Core/JobsEngine/SetOfInstancesJob.cpp@9201a7858cce |
children | bf7b9edf6b81 |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
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-2020 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 "../PrecompiledHeaders.h" | |
35 #include "SetOfInstancesJob.h" | |
36 | |
37 #include "../OrthancException.h" | |
38 #include "../SerializationToolbox.h" | |
39 | |
40 #include <cassert> | |
41 | |
42 namespace Orthanc | |
43 { | |
44 class SetOfInstancesJob::InstanceCommand : public SetOfInstancesJob::ICommand | |
45 { | |
46 private: | |
47 SetOfInstancesJob& that_; | |
48 std::string instance_; | |
49 | |
50 public: | |
51 InstanceCommand(SetOfInstancesJob& that, | |
52 const std::string& instance) : | |
53 that_(that), | |
54 instance_(instance) | |
55 { | |
56 } | |
57 | |
58 const std::string& GetInstance() const | |
59 { | |
60 return instance_; | |
61 } | |
62 | |
63 virtual bool Execute(const std::string& jobId) ORTHANC_OVERRIDE | |
64 { | |
65 if (!that_.HandleInstance(instance_)) | |
66 { | |
67 that_.failedInstances_.insert(instance_); | |
68 return false; | |
69 } | |
70 else | |
71 { | |
72 return true; | |
73 } | |
74 } | |
75 | |
76 virtual void Serialize(Json::Value& target) const ORTHANC_OVERRIDE | |
77 { | |
78 target = instance_; | |
79 } | |
80 }; | |
81 | |
82 | |
83 class SetOfInstancesJob::TrailingStepCommand : public SetOfInstancesJob::ICommand | |
84 { | |
85 private: | |
86 SetOfInstancesJob& that_; | |
87 | |
88 public: | |
89 TrailingStepCommand(SetOfInstancesJob& that) : | |
90 that_(that) | |
91 { | |
92 } | |
93 | |
94 virtual bool Execute(const std::string& jobId) ORTHANC_OVERRIDE | |
95 { | |
96 return that_.HandleTrailingStep(); | |
97 } | |
98 | |
99 virtual void Serialize(Json::Value& target) const ORTHANC_OVERRIDE | |
100 { | |
101 target = Json::nullValue; | |
102 } | |
103 }; | |
104 | |
105 | |
106 class SetOfInstancesJob::InstanceUnserializer : | |
107 public SetOfInstancesJob::ICommandUnserializer | |
108 { | |
109 private: | |
110 SetOfInstancesJob& that_; | |
111 | |
112 public: | |
113 InstanceUnserializer(SetOfInstancesJob& that) : | |
114 that_(that) | |
115 { | |
116 } | |
117 | |
118 virtual ICommand* Unserialize(const Json::Value& source) const | |
119 { | |
120 if (source.type() == Json::nullValue) | |
121 { | |
122 return new TrailingStepCommand(that_); | |
123 } | |
124 else if (source.type() == Json::stringValue) | |
125 { | |
126 return new InstanceCommand(that_, source.asString()); | |
127 } | |
128 else | |
129 { | |
130 throw OrthancException(ErrorCode_BadFileFormat); | |
131 } | |
132 } | |
133 }; | |
134 | |
135 | |
136 SetOfInstancesJob::SetOfInstancesJob() : | |
137 hasTrailingStep_(false) | |
138 { | |
139 } | |
140 | |
141 | |
142 void SetOfInstancesJob::AddInstance(const std::string& instance) | |
143 { | |
144 AddCommand(new InstanceCommand(*this, instance)); | |
145 } | |
146 | |
147 | |
148 void SetOfInstancesJob::AddTrailingStep() | |
149 { | |
150 AddCommand(new TrailingStepCommand(*this)); | |
151 hasTrailingStep_ = true; | |
152 } | |
153 | |
154 | |
155 size_t SetOfInstancesJob::GetInstancesCount() const | |
156 { | |
157 if (hasTrailingStep_) | |
158 { | |
159 assert(GetCommandsCount() > 0); | |
160 return GetCommandsCount() - 1; | |
161 } | |
162 else | |
163 { | |
164 return GetCommandsCount(); | |
165 } | |
166 } | |
167 | |
168 | |
169 const std::string& SetOfInstancesJob::GetInstance(size_t index) const | |
170 { | |
171 if (index >= GetInstancesCount()) | |
172 { | |
173 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
174 } | |
175 else | |
176 { | |
177 return dynamic_cast<const InstanceCommand&>(GetCommand(index)).GetInstance(); | |
178 } | |
179 } | |
180 | |
181 | |
182 void SetOfInstancesJob::Start() | |
183 { | |
184 SetOfCommandsJob::Start(); | |
185 } | |
186 | |
187 | |
188 void SetOfInstancesJob::Reset() | |
189 { | |
190 SetOfCommandsJob::Reset(); | |
191 | |
192 failedInstances_.clear(); | |
193 } | |
194 | |
195 | |
196 static const char* KEY_TRAILING_STEP = "TrailingStep"; | |
197 static const char* KEY_FAILED_INSTANCES = "FailedInstances"; | |
198 static const char* KEY_PARENT_RESOURCES = "ParentResources"; | |
199 | |
200 void SetOfInstancesJob::GetPublicContent(Json::Value& target) | |
201 { | |
202 SetOfCommandsJob::GetPublicContent(target); | |
203 target["InstancesCount"] = static_cast<uint32_t>(GetInstancesCount()); | |
204 target["FailedInstancesCount"] = static_cast<uint32_t>(failedInstances_.size()); | |
205 | |
206 if (!parentResources_.empty()) | |
207 { | |
208 SerializationToolbox::WriteSetOfStrings(target, parentResources_, KEY_PARENT_RESOURCES); | |
209 } | |
210 } | |
211 | |
212 | |
213 bool SetOfInstancesJob::Serialize(Json::Value& target) | |
214 { | |
215 if (SetOfCommandsJob::Serialize(target)) | |
216 { | |
217 target[KEY_TRAILING_STEP] = hasTrailingStep_; | |
218 SerializationToolbox::WriteSetOfStrings(target, failedInstances_, KEY_FAILED_INSTANCES); | |
219 SerializationToolbox::WriteSetOfStrings(target, parentResources_, KEY_PARENT_RESOURCES); | |
220 return true; | |
221 } | |
222 else | |
223 { | |
224 return false; | |
225 } | |
226 } | |
227 | |
228 | |
229 SetOfInstancesJob::SetOfInstancesJob(const Json::Value& source) : | |
230 SetOfCommandsJob(new InstanceUnserializer(*this), source) | |
231 { | |
232 SerializationToolbox::ReadSetOfStrings(failedInstances_, source, KEY_FAILED_INSTANCES); | |
233 | |
234 if (source.isMember(KEY_PARENT_RESOURCES)) | |
235 { | |
236 // Backward compatibility with Orthanc <= 1.5.6 | |
237 SerializationToolbox::ReadSetOfStrings(parentResources_, source, KEY_PARENT_RESOURCES); | |
238 } | |
239 | |
240 if (source.isMember(KEY_TRAILING_STEP)) | |
241 { | |
242 hasTrailingStep_ = SerializationToolbox::ReadBoolean(source, KEY_TRAILING_STEP); | |
243 } | |
244 else | |
245 { | |
246 // Backward compatibility with Orthanc <= 1.4.2 | |
247 hasTrailingStep_ = false; | |
248 } | |
249 } | |
250 | |
251 | |
252 } |