comparison Plugins/Engine/PluginsJob.cpp @ 2884:497a637366b4 db-changes

integration mainline->db-changes
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 12 Oct 2018 15:18:10 +0200
parents 925d8dc03a23
children bfee0b9f3209
comparison
equal deleted inserted replaced
1762:2b91363cc1d1 2884:497a637366b4
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 "../../OrthancServer/PrecompiledHeadersServer.h"
35 #include "PluginsJob.h"
36
37 #if ORTHANC_ENABLE_PLUGINS != 1
38 #error The plugin support is disabled
39 #endif
40
41
42 #include "../../Core/Logging.h"
43 #include "../../Core/OrthancException.h"
44
45 #include <json/reader.h>
46 #include <cassert>
47
48 namespace Orthanc
49 {
50 PluginsJob::PluginsJob(const _OrthancPluginCreateJob& parameters) :
51 parameters_(parameters)
52 {
53 if (parameters_.job == NULL)
54 {
55 throw OrthancException(ErrorCode_NullPointer);
56 }
57
58 if (parameters_.target == NULL ||
59 parameters_.finalize == NULL ||
60 parameters_.type == NULL ||
61 parameters_.getProgress == NULL ||
62 parameters_.getContent == NULL ||
63 parameters_.getSerialized == NULL ||
64 parameters_.step == NULL ||
65 parameters_.stop == NULL ||
66 parameters_.reset == NULL)
67 {
68 parameters_.finalize(parameters.job);
69 throw OrthancException(ErrorCode_NullPointer);
70 }
71
72 type_.assign(parameters.type);
73 }
74
75 PluginsJob::~PluginsJob()
76 {
77 assert(parameters_.job != NULL);
78 parameters_.finalize(parameters_.job);
79 }
80
81 JobStepResult PluginsJob::Step()
82 {
83 OrthancPluginJobStepStatus status = parameters_.step(parameters_.job);
84
85 switch (status)
86 {
87 case OrthancPluginJobStepStatus_Success:
88 return JobStepResult::Success();
89
90 case OrthancPluginJobStepStatus_Failure:
91 return JobStepResult::Failure(ErrorCode_Plugin);
92
93 case OrthancPluginJobStepStatus_Continue:
94 return JobStepResult::Continue();
95
96 default:
97 throw OrthancException(ErrorCode_ParameterOutOfRange);
98 }
99 }
100
101 void PluginsJob::Reset()
102 {
103 parameters_.reset(parameters_.job);
104 }
105
106 void PluginsJob::Stop(JobStopReason reason)
107 {
108 switch (reason)
109 {
110 case JobStopReason_Success:
111 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Success);
112 break;
113
114 case JobStopReason_Failure:
115 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Failure);
116 break;
117
118 case JobStopReason_Canceled:
119 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Canceled);
120 break;
121
122 case JobStopReason_Paused:
123 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Paused);
124 break;
125
126 default:
127 throw OrthancException(ErrorCode_ParameterOutOfRange);
128 }
129 }
130
131 float PluginsJob::GetProgress()
132 {
133 return parameters_.getProgress(parameters_.job);
134 }
135
136 void PluginsJob::GetPublicContent(Json::Value& value)
137 {
138 const char* content = parameters_.getContent(parameters_.job);
139
140 if (content == NULL)
141 {
142 value = Json::objectValue;
143 }
144 else
145 {
146 Json::Reader reader;
147
148 if (!reader.parse(content, value) ||
149 value.type() != Json::objectValue)
150 {
151 LOG(ERROR) << "A job plugin must provide a JSON object as its public content";
152 throw OrthancException(ErrorCode_Plugin);
153 }
154 }
155 }
156
157 bool PluginsJob::Serialize(Json::Value& value)
158 {
159 const char* serialized = parameters_.getSerialized(parameters_.job);
160
161 if (serialized == NULL)
162 {
163 return false;
164 }
165 else
166 {
167 Json::Reader reader;
168
169 if (!reader.parse(serialized, value) ||
170 value.type() != Json::objectValue)
171 {
172 LOG(ERROR) << "A job plugin must provide a JSON object as its serialized content";
173 throw OrthancException(ErrorCode_Plugin);
174 }
175
176
177 static const char* KEY_TYPE = "Type";
178
179 if (value.isMember(KEY_TYPE))
180 {
181 LOG(ERROR) << "The \"Type\" field is for reserved use for serialized job";
182 throw OrthancException(ErrorCode_Plugin);
183 }
184
185 value[KEY_TYPE] = type_;
186 return true;
187 }
188 }
189 }