comparison Core/JobsEngine/SetOfInstancesJob.cpp @ 2585:4c809711149e jobs

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 14 May 2018 21:24:51 +0200
parents
children ec09641d6f41
comparison
equal deleted inserted replaced
2584:38b5045f2bff 2585:4c809711149e
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
116 void SetOfInstancesJob::Next()
117 {
118 if (IsDone())
119 {
120 throw OrthancException(ErrorCode_BadSequenceOfCalls);
121 }
122 else
123 {
124 position_ += 1;
125 }
126 }
127
128
129 const std::string& SetOfInstancesJob::GetCurrentInstance() const
130 {
131 if (IsDone())
132 {
133 throw OrthancException(ErrorCode_BadSequenceOfCalls);
134 }
135 else
136 {
137 return instances_[position_];
138 }
139 }
140
141
142 JobStepResult* SetOfInstancesJob::ExecuteStep()
143 {
144 if (IsDone())
145 {
146 return new JobStepResult(JobStepCode_Failure);
147 }
148
149 bool ok;
150
151 try
152 {
153 ok = HandleInstance(GetCurrentInstance());
154
155 if (!ok && !permissive_)
156 {
157 throw OrthancException(ErrorCode_InternalError);
158 }
159 }
160 catch (OrthancException& e)
161 {
162 if (permissive_)
163 {
164 ok = false;
165 }
166 else
167 {
168 throw;
169 }
170 }
171
172 if (!ok)
173 {
174 failedInstances_.insert(GetCurrentInstance());
175 }
176
177 Next();
178
179 if (IsDone())
180 {
181 return new JobStepResult(JobStepCode_Success);
182 }
183 else
184 {
185 return new JobStepResult(JobStepCode_Continue);
186 }
187 }
188
189
190 void SetOfInstancesJob::GetInternalContent(Json::Value& value)
191 {
192 Json::Value v = Json::arrayValue;
193
194 for (size_t i = 0; i < instances_.size(); i++)
195 {
196 v.append(instances_[i]);
197 }
198
199 value["Instances"] = v;
200
201
202 v = Json::arrayValue;
203
204 for (std::set<std::string>::const_iterator it = failedInstances_.begin();
205 it != failedInstances_.end(); ++it)
206 {
207 v.append(*it);
208 }
209
210 value["FailedInstances"] = v;
211 }
212 }