Mercurial > hg > orthanc
annotate Core/JobsEngine/SetOfInstancesJob.cpp @ 3332:79178122842c
fix warnings
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 22 Mar 2019 14:06:38 +0100 |
parents | 4e43e67f8ecf |
children | d0d6bd633e4c |
rev | line source |
---|---|
2585 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
3060
4e43e67f8ecf
preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2860
diff
changeset
|
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
2585 | 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" | |
2656
a6d3e45eeff5
SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
38 #include "../SerializationToolbox.h" |
2585 | 39 |
2860 | 40 #include <cassert> |
41 | |
2585 | 42 namespace Orthanc |
43 { | |
2860 | 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() | |
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 | |
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() | |
95 { | |
96 return that_.HandleTrailingStep(); | |
97 } | |
98 | |
99 virtual void Serialize(Json::Value& target) const | |
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) | |
2585 | 138 { |
139 } | |
140 | |
141 | |
142 void SetOfInstancesJob::AddInstance(const std::string& instance) | |
143 { | |
2860 | 144 AddCommand(new InstanceCommand(*this, instance)); |
2585 | 145 } |
146 | |
147 | |
2860 | 148 void SetOfInstancesJob::AddTrailingStep() |
2585 | 149 { |
2860 | 150 AddCommand(new TrailingStepCommand(*this)); |
151 hasTrailingStep_ = true; | |
152 } | |
153 | |
154 | |
155 size_t SetOfInstancesJob::GetInstancesCount() const | |
156 { | |
157 if (hasTrailingStep_) | |
2585 | 158 { |
2860 | 159 assert(GetCommandsCount() > 0); |
160 return GetCommandsCount() - 1; | |
2585 | 161 } |
162 else | |
163 { | |
2860 | 164 return GetCommandsCount(); |
2585 | 165 } |
166 } | |
167 | |
2860 | 168 |
2657
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
169 const std::string& SetOfInstancesJob::GetInstance(size_t index) const |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
170 { |
2860 | 171 if (index >= GetInstancesCount()) |
2657
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
172 { |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
173 throw OrthancException(ErrorCode_ParameterOutOfRange); |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
174 } |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
175 else |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
176 { |
2860 | 177 return dynamic_cast<const InstanceCommand&>(GetCommand(index)).GetInstance(); |
2585 | 178 } |
179 } | |
180 | |
2842
ff0ed5ea9e4e
trailing step in SetOfInstancesJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
181 |
2860 | 182 void SetOfInstancesJob::Start() |
183 { | |
184 SetOfCommandsJob::Start(); | |
185 } | |
2842
ff0ed5ea9e4e
trailing step in SetOfInstancesJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
186 |
2860 | 187 |
188 void SetOfInstancesJob::Reset() | |
189 { | |
190 SetOfCommandsJob::Reset(); | |
2842
ff0ed5ea9e4e
trailing step in SetOfInstancesJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
191 |
2860 | 192 failedInstances_.clear(); |
193 } | |
194 | |
195 | |
196 void SetOfInstancesJob::GetPublicContent(Json::Value& target) | |
2640
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
197 { |
2860 | 198 SetOfCommandsJob::GetPublicContent(target); |
199 target["InstancesCount"] = static_cast<uint32_t>(GetInstancesCount()); | |
200 target["FailedInstancesCount"] = static_cast<uint32_t>(failedInstances_.size()); | |
2640
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
201 } |
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
202 |
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
203 |
2655 | 204 |
2860 | 205 static const char* KEY_TRAILING_STEP = "TrailingStep"; |
206 static const char* KEY_FAILED_INSTANCES = "FailedInstances"; | |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
207 |
2860 | 208 bool SetOfInstancesJob::Serialize(Json::Value& target) |
209 { | |
210 if (SetOfCommandsJob::Serialize(target)) | |
211 { | |
212 target[KEY_TRAILING_STEP] = hasTrailingStep_; | |
213 SerializationToolbox::WriteSetOfStrings(target, failedInstances_, KEY_FAILED_INSTANCES); | |
214 return true; | |
215 } | |
216 else | |
217 { | |
218 return false; | |
219 } | |
2585 | 220 } |
2860 | 221 |
2652 | 222 |
2860 | 223 SetOfInstancesJob::SetOfInstancesJob(const Json::Value& source) : |
224 SetOfCommandsJob(new InstanceUnserializer(*this), source) | |
2652 | 225 { |
2860 | 226 SerializationToolbox::ReadSetOfStrings(failedInstances_, source, KEY_FAILED_INSTANCES); |
2652 | 227 |
2860 | 228 if (source.isMember(KEY_TRAILING_STEP)) |
2842
ff0ed5ea9e4e
trailing step in SetOfInstancesJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
229 { |
2860 | 230 hasTrailingStep_ = SerializationToolbox::ReadBoolean(source, KEY_TRAILING_STEP); |
2842
ff0ed5ea9e4e
trailing step in SetOfInstancesJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
231 } |
ff0ed5ea9e4e
trailing step in SetOfInstancesJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
232 else |
ff0ed5ea9e4e
trailing step in SetOfInstancesJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
233 { |
ff0ed5ea9e4e
trailing step in SetOfInstancesJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
234 // Backward compatibility with Orthanc <= 1.4.2 |
ff0ed5ea9e4e
trailing step in SetOfInstancesJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
235 hasTrailingStep_ = false; |
ff0ed5ea9e4e
trailing step in SetOfInstancesJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
236 } |
2860 | 237 } |
238 | |
2842
ff0ed5ea9e4e
trailing step in SetOfInstancesJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
239 |
2585 | 240 } |