comparison OrthancServer/Plugins/Samples/MultitenantDicom/FindRequestHandler.cpp @ 5273:7cb1b851f5c8

Added a sample plugin bringing multitenant DICOM support through labels
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 14 Apr 2023 11:49:24 +0200
parents
children a8385880902f
comparison
equal deleted inserted replaced
5272:a45e8b6115f6 5273:7cb1b851f5c8
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-2023 Osimis S.A., Belgium
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
7 *
8 * This program is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation, either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 **/
22
23
24 #include "FindRequestHandler.h"
25
26 #include "PluginToolbox.h"
27
28 #include "../../../../OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h"
29 #include "../../../../OrthancFramework/Sources/OrthancException.h"
30
31 #include "../Common/OrthancPluginCppWrapper.h"
32
33
34 void FindRequestHandler::Handle(Orthanc::DicomFindAnswers& answers,
35 const Orthanc::DicomMap& input,
36 const std::list<Orthanc::DicomTag>& sequencesToReturn,
37 const std::string& remoteIp,
38 const std::string& remoteAet,
39 const std::string& calledAet,
40 Orthanc::ModalityManufacturer manufacturer)
41 {
42 std::set<Orthanc::DicomTag> tags;
43 input.GetTags(tags);
44
45 Json::Value request = Json::objectValue;
46 request["Expand"] = true;
47 PluginToolbox::AddLabelsToFindRequest(request, labels_, constraint_);
48
49 Json::Value query = Json::objectValue;
50 std::string level;
51
52 for (std::set<Orthanc::DicomTag>::const_iterator it = tags.begin(); it != tags.end(); ++it)
53 {
54 std::string s;
55
56 if (input.LookupStringValue(s, *it, false) &&
57 !s.empty())
58 {
59 if (*it == Orthanc::DICOM_TAG_QUERY_RETRIEVE_LEVEL)
60 {
61 level = s;
62 }
63 else
64 {
65 query[it->Format()] = s;
66 }
67 }
68 }
69
70 if (level.empty())
71 {
72 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, "Missing QueryRetrieveLevel in DICOM C-FIND request");
73 }
74
75 request["Level"] = EnumerationToString(PluginToolbox::ParseQueryRetrieveLevel(level));
76 request["Query"] = query;
77
78 Json::Value response;
79 if (!OrthancPlugins::RestApiPost(response, "/tools/find", request, false) ||
80 response.type() != Json::arrayValue)
81 {
82 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, "Invalid DICOM C-FIND request");
83 }
84
85 for (Json::Value::ArrayIndex i = 0; i < response.size(); i++)
86 {
87 if (response[i].type() != Json::objectValue ||
88 !response[i].isMember(KEY_MAIN_DICOM_TAGS))
89 {
90 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
91 }
92
93 if (response[i].isMember(KEY_PATIENT_MAIN_DICOM_TAGS) &&
94 response[i][KEY_PATIENT_MAIN_DICOM_TAGS].type() != Json::objectValue)
95 {
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
97 }
98
99 Orthanc::DicomMap m;
100
101 for (std::set<Orthanc::DicomTag>::const_iterator it = tags.begin(); it != tags.end(); ++it)
102 {
103 const std::string tag = Orthanc::FromDcmtkBridge::GetTagName(*it, "");
104
105 if (response[i][KEY_MAIN_DICOM_TAGS].isMember(tag) &&
106 response[i][KEY_MAIN_DICOM_TAGS][tag].type() == Json::stringValue)
107 {
108 m.SetValue(*it, response[i][KEY_MAIN_DICOM_TAGS][tag].asString(), false);
109 }
110 else if (response[i].isMember(KEY_PATIENT_MAIN_DICOM_TAGS) &&
111 response[i][KEY_PATIENT_MAIN_DICOM_TAGS].isMember(tag) &&
112 response[i][KEY_PATIENT_MAIN_DICOM_TAGS][tag].type() == Json::stringValue)
113 {
114 m.SetValue(*it, response[i][KEY_PATIENT_MAIN_DICOM_TAGS][tag].asString(), false);
115 }
116 }
117
118 m.SetValue(Orthanc::DICOM_TAG_QUERY_RETRIEVE_LEVEL, level, false);
119 m.SetValue(Orthanc::DICOM_TAG_RETRIEVE_AE_TITLE, retrieveAet_, false);
120 answers.Add(m);
121 }
122
123 answers.SetComplete(true);
124 }