annotate OrthancServer/ResourceFinder.cpp @ 1450:a3efc289ec07

NEWS
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 02 Jul 2015 09:25:49 +0200
parents d6971e18a324
children f967bdf8534e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1358
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "PrecompiledHeadersServer.h"
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
34 #include "ResourceFinder.h"
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
35
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
36 #include "FromDcmtkBridge.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
37 #include "ServerContext.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
38
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
39 #include <glog/logging.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
40 #include <boost/algorithm/string/predicate.hpp>
1358
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 namespace Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 {
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
44 class ResourceFinder::CandidateResources
1358
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
46 private:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
47 typedef std::map<DicomTag, std::string> Query;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
48
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
49 ResourceFinder& finder_;
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
50 ServerIndex& index_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
51 ResourceType level_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
52 bool isFilterApplied_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
53 std::set<std::string> filtered_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
54
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
55
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
56 static void ListToSet(std::set<std::string>& target,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
57 const std::list<std::string>& source)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
58 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
59 for (std::list<std::string>::const_iterator
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
60 it = source.begin(); it != source.end(); ++it)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
61 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
62 target.insert(*it);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
63 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
64 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
65
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
66
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
67 public:
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
68 CandidateResources(ResourceFinder& finder) :
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
69 finder_(finder),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
70 index_(finder.context_.GetIndex()),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
71 level_(ResourceType_Patient),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
72 isFilterApplied_(false)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
73 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
74 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
75
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
76 ResourceType GetLevel() const
1358
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 {
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
78 return level_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
79 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
80
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
81 void GoDown()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
82 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
83 assert(level_ != ResourceType_Instance);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
84
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
85 if (isFilterApplied_)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
86 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
87 std::set<std::string> tmp = filtered_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
88
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
89 filtered_.clear();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
90
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
91 for (std::set<std::string>::const_iterator
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
92 it = tmp.begin(); it != tmp.end(); ++it)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
93 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
94 std::list<std::string> children;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
95 try
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
96 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
97 index_.GetChildren(children, *it);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
98 ListToSet(filtered_, children);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
99 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
100 catch (OrthancException&)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
101 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
102 // The resource was removed in the meantime
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
103 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
104 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
105 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
106
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
107 switch (level_)
1358
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
109 case ResourceType_Patient:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
110 level_ = ResourceType_Study;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
111 break;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
112
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
113 case ResourceType_Study:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
114 level_ = ResourceType_Series;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
115 break;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
116
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
117 case ResourceType_Series:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
118 level_ = ResourceType_Instance;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
119 break;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
120
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
121 default:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
122 throw OrthancException(ErrorCode_InternalError);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
123 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
124 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
125
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
126
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
127 void Flatten(std::list<std::string>& resources) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
128 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
129 resources.clear();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
130
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
131 if (isFilterApplied_)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
132 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
133 for (std::set<std::string>::const_iterator
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
134 it = filtered_.begin(); it != filtered_.end(); ++it)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
135 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
136 resources.push_back(*it);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
137 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
138 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
139 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
140 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
141 index_.GetAllUuids(resources, level_);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
142 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
143 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
144
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
145
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
146 void RestrictIdentifier(const IQuery& query,
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
147 const DicomTag& tag)
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
148 {
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
149 assert((level_ == ResourceType_Patient && tag == DICOM_TAG_PATIENT_ID) ||
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
150 (level_ == ResourceType_Study && tag == DICOM_TAG_STUDY_INSTANCE_UID) ||
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
151 (level_ == ResourceType_Study && tag == DICOM_TAG_ACCESSION_NUMBER) ||
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
152 (level_ == ResourceType_Series && tag == DICOM_TAG_SERIES_INSTANCE_UID) ||
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
153 (level_ == ResourceType_Instance && tag == DICOM_TAG_SOP_INSTANCE_UID));
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
154
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
155 std::string value;
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
156 if (!query.RestrictIdentifier(value, tag))
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
157 {
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
158 return;
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
159 }
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
160
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
161 LOG(INFO) << "Lookup for identifier tag "
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
162 << FromDcmtkBridge::GetName(tag) << " (value: " << value << ")";
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
163
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
164 std::list<std::string> resources;
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
165 index_.LookupIdentifier(resources, tag, value, level_);
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
166
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
167 if (isFilterApplied_)
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
168 {
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
169 std::set<std::string> s;
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
170 ListToSet(s, resources);
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
171
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
172 std::set<std::string> tmp = filtered_;
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
173 filtered_.clear();
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
174
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
175 for (std::set<std::string>::const_iterator
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
176 it = tmp.begin(); it != tmp.end(); ++it)
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
177 {
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
178 if (s.find(*it) != s.end())
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
179 {
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
180 filtered_.insert(*it);
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
181 }
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
182 }
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
183 }
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
184 else
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
185 {
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
186 assert(filtered_.empty());
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
187 isFilterApplied_ = true;
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
188 ListToSet(filtered_, resources);
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
189 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
190 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
191
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
192
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
193 void RestrictMainDicomTags(const IQuery& query)
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
194 {
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
195 if (!query.HasMainDicomTagsFilter(level_))
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
196 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
197 return;
1358
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
200 std::list<std::string> resources;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
201 Flatten(resources);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
202
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
203 isFilterApplied_ = true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
204 filtered_.clear();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
205
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
206 for (std::list<std::string>::const_iterator
1384
d6971e18a324 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1361
diff changeset
207 it = resources.begin(); it != resources.end(); ++it)
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
208 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
209 DicomMap mainTags;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
210 if (index_.GetMainDicomTags(mainTags, *it, level_))
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
211 {
1361
94ffb597d297 refactoring of C-Find SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1360
diff changeset
212 if (query.FilterMainDicomTags(*it, level_, mainTags))
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
213 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
214 filtered_.insert(*it);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
215 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
216 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
217 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
218 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
219 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
220
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
221
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
222 ResourceFinder::ResourceFinder(ServerContext& context) :
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
223 context_(context),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
224 maxResults_(0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
225 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
226 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
227
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
228
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
229 void ResourceFinder::ApplyAtLevel(CandidateResources& candidates,
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
230 const IQuery& query,
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
231 ResourceType level)
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
232 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
233 if (level != ResourceType_Patient)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
234 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
235 candidates.GoDown();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
236 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
237
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
238 switch (level)
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
239 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
240 case ResourceType_Patient:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
241 {
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
242 candidates.RestrictIdentifier(query, DICOM_TAG_PATIENT_ID);
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
243 break;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
244 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
245
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
246 case ResourceType_Study:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
247 {
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
248 candidates.RestrictIdentifier(query, DICOM_TAG_STUDY_INSTANCE_UID);
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
249 candidates.RestrictIdentifier(query, DICOM_TAG_ACCESSION_NUMBER);
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
250 break;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
251 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
252
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
253 case ResourceType_Series:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
254 {
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
255 candidates.RestrictIdentifier(query, DICOM_TAG_SERIES_INSTANCE_UID);
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
256 break;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
257 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
258
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
259 case ResourceType_Instance:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
260 {
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
261 candidates.RestrictIdentifier(query, DICOM_TAG_SOP_INSTANCE_UID);
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
262 break;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
263 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
264
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
265 default:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
266 throw OrthancException(ErrorCode_InternalError);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
267 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
268
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
269 candidates.RestrictMainDicomTags(query);
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
270 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
271
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
272
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
273
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
274 static bool LookupOneInstance(std::string& result,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
275 ServerIndex& index,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
276 const std::string& id,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
277 ResourceType type)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
278 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
279 if (type == ResourceType_Instance)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
280 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
281 result = id;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
282 return true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
283 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
284
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
285 std::string childId;
1358
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
287 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
288 std::list<std::string> children;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
289 index.GetChildInstances(children, id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
290
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
291 if (children.empty())
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
292 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
293 return false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
294 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
295
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
296 childId = children.front();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
297 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
298
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
299 return LookupOneInstance(result, index, childId, GetChildResourceType(type));
1358
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
302
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
303 bool ResourceFinder::Apply(std::list<std::string>& result,
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
304 const IQuery& query)
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
305 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
306 CandidateResources candidates(*this);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
307
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
308 ApplyAtLevel(candidates, query, ResourceType_Patient);
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
309
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
310 const ResourceType level = query.GetLevel();
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
311
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
312 if (level == ResourceType_Study ||
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
313 level == ResourceType_Series ||
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
314 level == ResourceType_Instance)
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
315 {
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
316 ApplyAtLevel(candidates, query, ResourceType_Study);
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
317 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
318
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
319 if (level == ResourceType_Series ||
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
320 level == ResourceType_Instance)
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
321 {
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
322 ApplyAtLevel(candidates, query, ResourceType_Series);
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
323 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
324
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
325 if (level == ResourceType_Instance)
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
326 {
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
327 ApplyAtLevel(candidates, query, ResourceType_Instance);
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
328 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
329
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
330 if (!query.HasInstanceFilter())
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
331 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
332 candidates.Flatten(result);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
333
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
334 if (maxResults_ != 0 &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
335 result.size() >= maxResults_)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
336 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
337 result.resize(maxResults_);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
338 return false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
339 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
340 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
341 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
342 return true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
343 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
344 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
345 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
346 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
347 std::list<std::string> tmp;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
348 candidates.Flatten(tmp);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
349
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
350 result.clear();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
351 for (std::list<std::string>::const_iterator
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
352 resource = tmp.begin(); resource != tmp.end(); ++resource)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
353 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
354 try
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
355 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
356 std::string instance;
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
357 if (LookupOneInstance(instance, context_.GetIndex(), *resource, level))
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
358 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
359 Json::Value content;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
360 context_.ReadJson(content, instance);
1360
0649c5aef34a DicomFindQuery
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1359
diff changeset
361 if (query.FilterInstance(*resource, content))
1359
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
362 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
363 result.push_back(*resource);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
364
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
365 if (maxResults_ != 0 &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
366 result.size() >= maxResults_)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
367 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
368 // Too many results, stop before recording this new match
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
369 return false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
370 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
371 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
372 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
373 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
374 catch (OrthancException&)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
375 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
376 // This resource has been deleted since the search was started
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
377 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
378 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
379 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
380
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
381 return true; // All the matching resources have been returned
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1358
diff changeset
382 }
1358
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383 }