Mercurial > hg > orthanc
annotate Core/JobsEngine/SetOfInstancesJob.cpp @ 2648:e1893d31652a jobs
serialization of JobHandler
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 31 May 2018 18:44:05 +0200 |
parents | c691fcf66071 |
children | a3f0f61a14ca |
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 | |
5 * Copyright (C) 2017-2018 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 | |
39 namespace Orthanc | |
40 { | |
41 SetOfInstancesJob::SetOfInstancesJob() : | |
42 started_(false), | |
43 permissive_(false), | |
44 position_(0) | |
45 { | |
46 } | |
47 | |
48 | |
49 void SetOfInstancesJob::Reserve(size_t size) | |
50 { | |
51 if (started_) | |
52 { | |
53 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
54 } | |
55 else | |
56 { | |
57 instances_.reserve(size); | |
58 } | |
59 } | |
60 | |
61 | |
62 void SetOfInstancesJob::AddInstance(const std::string& instance) | |
63 { | |
64 if (started_) | |
65 { | |
66 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
67 } | |
68 else | |
69 { | |
70 instances_.push_back(instance); | |
71 } | |
72 } | |
73 | |
74 | |
75 void SetOfInstancesJob::SetPermissive(bool permissive) | |
76 { | |
77 if (IsStarted()) | |
78 { | |
79 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
80 } | |
81 else | |
82 { | |
83 permissive_ = permissive; | |
84 } | |
85 } | |
86 | |
87 | |
88 void SetOfInstancesJob::SignalResubmit() | |
89 { | |
90 if (started_) | |
91 { | |
92 position_ = 0; | |
93 failedInstances_.clear(); | |
94 } | |
95 else | |
96 { | |
97 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
98 } | |
99 } | |
100 | |
101 | |
102 float SetOfInstancesJob::GetProgress() | |
103 { | |
104 if (instances_.size() == 0) | |
105 { | |
106 return 0; | |
107 } | |
108 else | |
109 { | |
110 return (static_cast<float>(position_) / | |
111 static_cast<float>(instances_.size())); | |
112 } | |
113 } | |
114 | |
115 | |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2586
diff
changeset
|
116 JobStepResult SetOfInstancesJob::ExecuteStep() |
2585 | 117 { |
2586 | 118 if (!started_) |
119 { | |
120 throw OrthancException(ErrorCode_InternalError); | |
121 } | |
122 | |
123 if (instances_.empty() && | |
124 position_ == 0) | |
2585 | 125 { |
2586 | 126 // No instance to handle, we're done |
127 position_ = 1; | |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2586
diff
changeset
|
128 return JobStepResult::Success(); |
2586 | 129 } |
130 | |
131 if (position_ >= instances_.size()) | |
132 { | |
133 // Already done | |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2586
diff
changeset
|
134 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
2585 | 135 } |
136 | |
2586 | 137 const std::string currentInstance = instances_[position_]; |
138 | |
2585 | 139 bool ok; |
140 | |
141 try | |
142 { | |
2586 | 143 ok = HandleInstance(currentInstance); |
2585 | 144 |
145 if (!ok && !permissive_) | |
146 { | |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2586
diff
changeset
|
147 return JobStepResult::Failure(ErrorCode_InternalError); |
2585 | 148 } |
149 } | |
150 catch (OrthancException& e) | |
151 { | |
152 if (permissive_) | |
153 { | |
154 ok = false; | |
155 } | |
156 else | |
157 { | |
158 throw; | |
159 } | |
160 } | |
161 | |
162 if (!ok) | |
163 { | |
2586 | 164 failedInstances_.insert(currentInstance); |
2585 | 165 } |
166 | |
2586 | 167 position_ += 1; |
2585 | 168 |
2586 | 169 if (position_ == instances_.size()) |
2585 | 170 { |
2586 | 171 // We're done |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2586
diff
changeset
|
172 return JobStepResult::Success(); |
2585 | 173 } |
174 else | |
175 { | |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2586
diff
changeset
|
176 return JobStepResult::Continue(); |
2585 | 177 } |
178 } | |
179 | |
180 | |
2640
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
181 void SetOfInstancesJob::GetPublicContent(Json::Value& value) |
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
182 { |
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
183 value["Description"] = GetDescription(); |
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
184 value["InstancesCount"] = static_cast<uint32_t>(GetInstances().size()); |
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
185 value["FailedInstancesCount"] = static_cast<uint32_t>(GetFailedInstances().size()); |
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
186 } |
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
187 |
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
188 |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2640
diff
changeset
|
189 void SetOfInstancesJob::Serialize(Json::Value& value) |
2585 | 190 { |
191 Json::Value v = Json::arrayValue; | |
192 | |
193 for (size_t i = 0; i < instances_.size(); i++) | |
194 { | |
195 v.append(instances_[i]); | |
196 } | |
197 | |
198 value["Instances"] = v; | |
199 | |
200 v = Json::arrayValue; | |
201 | |
202 for (std::set<std::string>::const_iterator it = failedInstances_.begin(); | |
203 it != failedInstances_.end(); ++it) | |
204 { | |
205 v.append(*it); | |
206 } | |
207 | |
208 value["FailedInstances"] = v; | |
209 } | |
210 } |