Mercurial > hg > orthanc
annotate OrthancServer/DicomFindQuery.cpp @ 1400:1c8df4424437
install plugin SDK
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 01 Jun 2015 14:05:35 +0200 |
parents | d6971e18a324 |
children | 8e23f16a198d |
rev | line source |
---|---|
1360 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 | |
34 #include "PrecompiledHeadersServer.h" | |
35 #include "DicomFindQuery.h" | |
36 | |
37 #include "FromDcmtkBridge.h" | |
38 | |
39 #include <boost/regex.hpp> | |
40 | |
41 | |
42 namespace Orthanc | |
43 { | |
44 class DicomFindQuery::ValueConstraint : public DicomFindQuery::IConstraint | |
45 { | |
46 private: | |
47 bool isCaseSensitive_; | |
48 std::string expected_; | |
49 | |
50 public: | |
51 ValueConstraint(const std::string& value, | |
52 bool caseSensitive) : | |
53 isCaseSensitive_(caseSensitive), | |
54 expected_(value) | |
55 { | |
56 } | |
57 | |
58 const std::string& GetValue() const | |
59 { | |
60 return expected_; | |
61 } | |
62 | |
63 virtual bool IsExactConstraint() const | |
64 { | |
65 return isCaseSensitive_; | |
66 } | |
67 | |
68 virtual bool Apply(const std::string& value) const | |
69 { | |
70 if (isCaseSensitive_) | |
71 { | |
72 return expected_ == value; | |
73 } | |
74 else | |
75 { | |
76 std::string v, c; | |
77 Toolbox::ToLowerCase(v, value); | |
78 Toolbox::ToLowerCase(c, expected_); | |
79 return v == c; | |
80 } | |
81 } | |
82 }; | |
83 | |
84 | |
85 class DicomFindQuery::ListConstraint : public DicomFindQuery::IConstraint | |
86 { | |
87 private: | |
88 std::set<std::string> values_; | |
89 | |
90 public: | |
91 ListConstraint(const std::string& values) | |
92 { | |
93 std::vector<std::string> items; | |
94 Toolbox::TokenizeString(items, values, '\\'); | |
95 | |
96 for (size_t i = 0; i < items.size(); i++) | |
97 { | |
98 std::string lower; | |
99 Toolbox::ToLowerCase(lower, items[i]); | |
100 values_.insert(lower); | |
101 } | |
102 } | |
103 | |
104 virtual bool Apply(const std::string& value) const | |
105 { | |
106 std::string tmp; | |
107 Toolbox::ToLowerCase(tmp, value); | |
108 return values_.find(tmp) != values_.end(); | |
109 } | |
110 }; | |
111 | |
112 | |
113 class DicomFindQuery::RangeConstraint : public DicomFindQuery::IConstraint | |
114 { | |
115 private: | |
116 std::string lower_; | |
117 std::string upper_; | |
118 | |
119 public: | |
120 RangeConstraint(const std::string& range) | |
121 { | |
122 size_t separator = range.find('-'); | |
123 Toolbox::ToLowerCase(lower_, range.substr(0, separator)); | |
124 Toolbox::ToLowerCase(upper_, range.substr(separator + 1)); | |
125 } | |
126 | |
127 virtual bool Apply(const std::string& value) const | |
128 { | |
129 std::string v; | |
130 Toolbox::ToLowerCase(v, value); | |
131 | |
132 if (lower_.size() == 0 && | |
133 upper_.size() == 0) | |
134 { | |
135 return false; | |
136 } | |
137 | |
138 if (lower_.size() == 0) | |
139 { | |
140 return v <= upper_; | |
141 } | |
142 | |
143 if (upper_.size() == 0) | |
144 { | |
145 return v >= lower_; | |
146 } | |
147 | |
148 return (v >= lower_ && v <= upper_); | |
149 } | |
150 }; | |
151 | |
152 | |
153 class DicomFindQuery::WildcardConstraint : public DicomFindQuery::IConstraint | |
154 { | |
155 private: | |
156 boost::regex pattern_; | |
157 | |
158 public: | |
1374
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
159 WildcardConstraint(const std::string& wildcard, |
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
160 bool caseSensitive) |
1360 | 161 { |
1374
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
162 std::string re = Toolbox::WildcardToRegularExpression(wildcard); |
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
163 |
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
164 if (caseSensitive) |
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
165 { |
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
166 pattern_ = boost::regex(re); |
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
167 } |
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
168 else |
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
169 { |
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
170 pattern_ = boost::regex(re, boost::regex::icase /* case insensitive search */); |
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
171 } |
1360 | 172 } |
173 | |
174 virtual bool Apply(const std::string& value) const | |
175 { | |
176 return boost::regex_match(value, pattern_); | |
177 } | |
178 }; | |
179 | |
180 | |
181 void DicomFindQuery::PrepareMainDicomTags(ResourceType level) | |
182 { | |
183 std::set<DicomTag> tags; | |
184 DicomMap::GetMainDicomTags(tags, level); | |
185 | |
186 for (std::set<DicomTag>::const_iterator | |
187 it = tags.begin(); it != tags.end(); ++it) | |
188 { | |
189 mainDicomTags_[*it] = level; | |
190 } | |
191 } | |
192 | |
193 | |
194 DicomFindQuery::DicomFindQuery() : | |
195 level_(ResourceType_Patient), | |
196 filterJson_(false) | |
197 { | |
198 PrepareMainDicomTags(ResourceType_Patient); | |
199 PrepareMainDicomTags(ResourceType_Study); | |
200 PrepareMainDicomTags(ResourceType_Series); | |
201 PrepareMainDicomTags(ResourceType_Instance); | |
202 } | |
203 | |
204 | |
205 DicomFindQuery::~DicomFindQuery() | |
206 { | |
207 for (Constraints::iterator it = constraints_.begin(); | |
1384 | 208 it != constraints_.end(); ++it) |
1360 | 209 { |
210 delete it->second; | |
211 } | |
212 } | |
213 | |
214 | |
215 | |
216 | |
217 void DicomFindQuery::AssignConstraint(const DicomTag& tag, | |
218 IConstraint* constraint) | |
219 { | |
220 Constraints::iterator it = constraints_.find(tag); | |
221 | |
222 if (it != constraints_.end()) | |
223 { | |
224 constraints_.erase(it); | |
225 } | |
226 | |
227 constraints_[tag] = constraint; | |
228 | |
229 MainDicomTags::const_iterator tmp = mainDicomTags_.find(tag); | |
230 if (tmp == mainDicomTags_.end()) | |
231 { | |
232 // The query depends upon a DICOM tag that is not a main tag | |
233 // from the point of view of Orthanc, we need to decode the | |
234 // JSON file on the disk. | |
235 filterJson_ = true; | |
236 } | |
237 else | |
238 { | |
239 filteredLevels_.insert(tmp->second); | |
240 } | |
241 } | |
242 | |
243 | |
244 void DicomFindQuery::SetConstraint(const DicomTag& tag, | |
1374
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
245 const std::string& constraint, |
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
246 bool caseSensitivePN) |
1360 | 247 { |
1374
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
248 bool sensitive = (FromDcmtkBridge::IsPNValueRepresentation(tag) ? caseSensitivePN : true); |
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
249 |
1360 | 250 // http://www.itk.org/Wiki/DICOM_QueryRetrieve_Explained |
251 // http://dicomiseasy.blogspot.be/2012/01/dicom-queryretrieve-part-i.html | |
252 | |
253 if (constraint.find('-') != std::string::npos) | |
254 { | |
255 AssignConstraint(tag, new RangeConstraint(constraint)); | |
256 } | |
257 else if (constraint.find('\\') != std::string::npos) | |
258 { | |
259 AssignConstraint(tag, new ListConstraint(constraint)); | |
260 } | |
261 else if (constraint.find('*') != std::string::npos || | |
262 constraint.find('?') != std::string::npos) | |
263 { | |
1374
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
264 AssignConstraint(tag, new WildcardConstraint(constraint, sensitive)); |
1360 | 265 } |
266 else | |
267 { | |
268 /** | |
269 * Case-insensitive match for PN value representation (Patient | |
270 * Name). Case-senstive match for all the other value | |
271 * representations. | |
272 * | |
273 * Reference: DICOM PS 3.4 | |
274 * - C.2.2.2.1 ("Single Value Matching") | |
275 * - C.2.2.2.4 ("Wild Card Matching") | |
276 * http://medical.nema.org/Dicom/2011/11_04pu.pdf | |
277 * | |
278 * "Except for Attributes with a PN Value Representation, only | |
279 * entities with values which match exactly the value specified in the | |
280 * request shall match. This matching is case-sensitive, i.e., | |
281 * sensitive to the exact encoding of the key attribute value in | |
282 * character sets where a letter may have multiple encodings (e.g., | |
283 * based on its case, its position in a word, or whether it is | |
284 * accented) | |
285 * | |
286 * For Attributes with a PN Value Representation (e.g., Patient Name | |
287 * (0010,0010)), an application may perform literal matching that is | |
288 * either case-sensitive, or that is insensitive to some or all | |
289 * aspects of case, position, accent, or other character encoding | |
290 * variants." | |
291 * | |
292 * (0008,0018) UI SOPInstanceUID => Case-sensitive | |
293 * (0008,0050) SH AccessionNumber => Case-sensitive | |
294 * (0010,0020) LO PatientID => Case-sensitive | |
295 * (0020,000D) UI StudyInstanceUID => Case-sensitive | |
296 * (0020,000E) UI SeriesInstanceUID => Case-sensitive | |
297 **/ | |
298 | |
1374
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
299 AssignConstraint(tag, new ValueConstraint(constraint, sensitive)); |
1360 | 300 } |
301 } | |
302 | |
303 | |
304 bool DicomFindQuery::RestrictIdentifier(std::string& value, | |
305 DicomTag identifier) const | |
306 { | |
307 Constraints::const_iterator it = constraints_.find(identifier); | |
308 if (it == constraints_.end() || | |
309 !it->second->IsExactConstraint()) | |
310 { | |
311 return false; | |
312 } | |
313 else | |
314 { | |
315 value = dynamic_cast<ValueConstraint*>(it->second)->GetValue(); | |
316 return true; | |
317 } | |
318 } | |
319 | |
320 bool DicomFindQuery::HasMainDicomTagsFilter(ResourceType level) const | |
321 { | |
322 return filteredLevels_.find(level) != filteredLevels_.end(); | |
323 } | |
324 | |
1361
94ffb597d297
refactoring of C-Find SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1360
diff
changeset
|
325 bool DicomFindQuery::FilterMainDicomTags(const std::string& resourceId, |
94ffb597d297
refactoring of C-Find SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1360
diff
changeset
|
326 ResourceType level, |
94ffb597d297
refactoring of C-Find SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1360
diff
changeset
|
327 const DicomMap& mainTags) const |
1360 | 328 { |
329 std::set<DicomTag> tags; | |
330 mainTags.GetTags(tags); | |
331 | |
332 for (std::set<DicomTag>::const_iterator | |
333 it = tags.begin(); it != tags.end(); ++it) | |
334 { | |
335 Constraints::const_iterator constraint = constraints_.find(*it); | |
1361
94ffb597d297
refactoring of C-Find SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1360
diff
changeset
|
336 if (constraint != constraints_.end() && |
94ffb597d297
refactoring of C-Find SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1360
diff
changeset
|
337 !constraint->second->Apply(mainTags.GetValue(*it).AsString())) |
1360 | 338 { |
339 return false; | |
340 } | |
341 } | |
342 | |
343 return true; | |
344 } | |
345 | |
346 bool DicomFindQuery::HasInstanceFilter() const | |
347 { | |
348 return filterJson_; | |
349 } | |
350 | |
351 bool DicomFindQuery::FilterInstance(const std::string& instanceId, | |
352 const Json::Value& content) const | |
353 { | |
354 for (Constraints::const_iterator it = constraints_.begin(); | |
355 it != constraints_.end(); ++it) | |
356 { | |
357 std::string tag = it->first.Format(); | |
358 std::string value; | |
359 if (content.isMember(tag)) | |
360 { | |
361 value = content.get(tag, Json::arrayValue).get("Value", "").asString(); | |
362 } | |
363 | |
364 if (!it->second->Apply(value)) | |
365 { | |
366 return false; | |
367 } | |
368 } | |
369 | |
370 return true; | |
371 } | |
372 } |