comparison Plugin/OrthancResource.cpp @ 1:d5d3cb00556a

initial release
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 22 Mar 2017 16:13:52 +0100
parents
children c44013681a51
comparison
equal deleted inserted replaced
0:decac5df19c4 1:d5d3cb00556a
1 /**
2 * Advanced authorization plugin for Orthanc
3 * Copyright (C) 2017 Osimis, Belgium
4 *
5 * This program is free software: you can redistribute it and/or
6 * modify it under the terms of the GNU Affero General Public License
7 * as published by the Free Software Foundation, either version 3 of
8 * the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 **/
18
19 #include "OrthancResource.h"
20
21 #include "../Resources/Orthanc/Plugins/Samples/Common/OrthancPluginCppWrapper.h"
22
23 namespace OrthancPlugins
24 {
25 void OrthancResource::GetDicomUidInternal(std::string& result,
26 Orthanc::ResourceType level,
27 const Json::Value& content)
28 {
29 std::string uidTag;
30
31 switch (level)
32 {
33 case Orthanc::ResourceType_Patient:
34 uidTag = "PatientID";
35 break;
36
37 case Orthanc::ResourceType_Study:
38 uidTag = "StudyInstanceUID";
39 break;
40
41 case Orthanc::ResourceType_Series:
42 uidTag = "SeriesInstanceUID";
43 break;
44
45 case Orthanc::ResourceType_Instance:
46 uidTag = "SOPInstanceUID";
47 break;
48
49 default:
50 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
51 }
52
53 static const char* MAIN_DICOM_TAGS = "MainDicomTags";
54
55 if (content.type() != Json::objectValue ||
56 !content.isMember(MAIN_DICOM_TAGS))
57 {
58 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
59 }
60
61 const Json::Value& mainDicomTags = content[MAIN_DICOM_TAGS];
62 if (mainDicomTags.type() != Json::objectValue ||
63 (mainDicomTags.isMember(uidTag) &&
64 mainDicomTags[uidTag].type() != Json::stringValue))
65 {
66 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
67 }
68
69 if (!mainDicomTags.isMember(uidTag))
70 {
71 result.clear();
72 }
73 else
74 {
75 result = mainDicomTags[uidTag].asString();
76 }
77 }
78
79
80 Orthanc::ResourceType OrthancResource::GetLevel() const
81 {
82 if (IsValid())
83 {
84 return level_;
85 }
86 else
87 {
88 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
89 }
90 }
91
92
93 const std::string& OrthancResource::GetIdentifier() const
94 {
95 if (IsValid())
96 {
97 return id_;
98 }
99 else
100 {
101 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
102 }
103 }
104
105
106 bool OrthancResource::GetContent(Json::Value& content,
107 OrthancPluginContext* context) const
108 {
109 if (!IsValid())
110 {
111 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
112 }
113
114 std::string uri;
115 switch (level_)
116 {
117 case Orthanc::ResourceType_Patient:
118 uri = "patients";
119 break;
120
121 case Orthanc::ResourceType_Study:
122 uri = "studies";
123 break;
124
125 case Orthanc::ResourceType_Series:
126 uri = "series";
127 break;
128
129 case Orthanc::ResourceType_Instance:
130 uri = "instances";
131 break;
132
133 default:
134 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
135 }
136
137 uri = "/" + uri + "/" + id_;
138
139 return RestApiGet(content, context, uri, false /* ignore plugins */);
140 }
141
142
143 bool OrthancResource::GetDicomUid(std::string& dicomUid /* out */,
144 OrthancPluginContext* context) const
145 {
146 Json::Value content;
147
148 if (!GetContent(content, context))
149 {
150 return false;
151 }
152 else
153 {
154 GetDicomUidInternal(dicomUid, level_, content);
155 return true;
156 }
157 }
158
159
160 bool OrthancResource::GetHierarchy(std::string& dicomUid /* out */,
161 OrthancResource& parent /* out */,
162 std::list<OrthancResource>& children /* out */,
163 OrthancPluginContext* context) const
164 {
165 Json::Value content;
166
167 if (!GetContent(content, context))
168 {
169 return false;
170 }
171
172 std::string parentKey, childrenKey;
173
174 switch (level_)
175 {
176 case Orthanc::ResourceType_Patient:
177 childrenKey = "Studies";
178 break;
179
180 case Orthanc::ResourceType_Study:
181 parentKey = "ParentPatient";
182 childrenKey = "Series";
183 break;
184
185 case Orthanc::ResourceType_Series:
186 parentKey = "ParentStudy";
187 childrenKey = "Instances";
188 break;
189
190 case Orthanc::ResourceType_Instance:
191 parentKey = "ParentSeries";
192 break;
193
194 default:
195 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
196 }
197
198 GetDicomUidInternal(dicomUid, level_, content);
199
200 if (content.type() != Json::objectValue)
201 {
202 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
203 }
204
205 if (!parentKey.empty())
206 {
207 if (!content.isMember(parentKey) ||
208 content[parentKey].type() != Json::stringValue)
209 {
210 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
211 }
212
213 parent = OrthancResource(Orthanc::GetParentResourceType(level_),
214 content[parentKey].asString());
215 }
216
217 children.clear();
218
219 if (!childrenKey.empty())
220 {
221 if (!content.isMember(childrenKey) ||
222 content[childrenKey].type() != Json::arrayValue)
223 {
224 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
225 }
226
227 Orthanc::ResourceType childrenType = Orthanc::GetChildResourceType(level_);
228
229 for (Json::Value::ArrayIndex i = 0; i < content[childrenKey].size(); i++)
230 {
231 const Json::Value& child = content[childrenKey][i];
232
233 if (child.type() != Json::stringValue)
234 {
235 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
236 }
237
238 children.push_back(OrthancResource(childrenType, child.asString()));
239 }
240 }
241
242 return true;
243 }
244
245
246 bool OrthancResource::LookupOrthancId(std::string& result,
247 OrthancPluginContext* context,
248 Orthanc::ResourceType level,
249 const std::string& dicomUid)
250 {
251 OrthancString s(context);
252
253 switch (level)
254 {
255 case Orthanc::ResourceType_Patient:
256 s.Assign(OrthancPluginLookupPatient(context, dicomUid.c_str()));
257 break;
258
259 case Orthanc::ResourceType_Study:
260 s.Assign(OrthancPluginLookupStudy(context, dicomUid.c_str()));
261 break;
262
263 case Orthanc::ResourceType_Series:
264 s.Assign(OrthancPluginLookupSeries(context, dicomUid.c_str()));
265 break;
266
267 case Orthanc::ResourceType_Instance:
268 s.Assign(OrthancPluginLookupInstance(context, dicomUid.c_str()));
269 break;
270
271 default:
272 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
273 }
274
275 if (s.GetContent() == NULL)
276 {
277 // Inexisting resource
278 return false;
279 }
280 else
281 {
282 result.assign(s.GetContent());
283 return true;
284 }
285 }
286 }