Mercurial > hg > orthanc
annotate OrthancServer/DicomFindQuery.cpp @ 1420:5ddbadd17cde
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 23 Jun 2015 11:47:43 +0200 |
parents | 8e23f16a198d |
children | ec66a16aa398 |
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 { |
1417
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
248 ValueRepresentation vr = FromDcmtkBridge::GetValueRepresentation(tag); |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
249 |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
250 bool sensitive = true; |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
251 if (vr == ValueRepresentation_PatientName) |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
252 { |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
253 sensitive = caseSensitivePN; |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
254 } |
1374
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
255 |
1360 | 256 // http://www.itk.org/Wiki/DICOM_QueryRetrieve_Explained |
257 // http://dicomiseasy.blogspot.be/2012/01/dicom-queryretrieve-part-i.html | |
258 | |
1417
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
259 if ((vr == ValueRepresentation_Date || |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
260 vr == ValueRepresentation_DateTime || |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
261 vr == ValueRepresentation_Time) && |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
262 constraint.find('-') != std::string::npos) |
1360 | 263 { |
1417
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
264 /** |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
265 * Range matching is only defined for TM, DA and DT value |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
266 * representations. This code fixes issues 35 and 37. |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
267 * |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
268 * Reference: "Range matching is not defined for types of |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
269 * Attributes other than dates and times", DICOM PS 3.4, |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
270 * C.2.2.2.5 ("Range Matching"). |
8e23f16a198d
fix issues 35 and 37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
271 **/ |
1360 | 272 AssignConstraint(tag, new RangeConstraint(constraint)); |
273 } | |
274 else if (constraint.find('\\') != std::string::npos) | |
275 { | |
276 AssignConstraint(tag, new ListConstraint(constraint)); | |
277 } | |
278 else if (constraint.find('*') != std::string::npos || | |
279 constraint.find('?') != std::string::npos) | |
280 { | |
1374
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
281 AssignConstraint(tag, new WildcardConstraint(constraint, sensitive)); |
1360 | 282 } |
283 else | |
284 { | |
285 /** | |
286 * Case-insensitive match for PN value representation (Patient | |
287 * Name). Case-senstive match for all the other value | |
288 * representations. | |
289 * | |
290 * Reference: DICOM PS 3.4 | |
291 * - C.2.2.2.1 ("Single Value Matching") | |
292 * - C.2.2.2.4 ("Wild Card Matching") | |
293 * http://medical.nema.org/Dicom/2011/11_04pu.pdf | |
294 * | |
295 * "Except for Attributes with a PN Value Representation, only | |
296 * entities with values which match exactly the value specified in the | |
297 * request shall match. This matching is case-sensitive, i.e., | |
298 * sensitive to the exact encoding of the key attribute value in | |
299 * character sets where a letter may have multiple encodings (e.g., | |
300 * based on its case, its position in a word, or whether it is | |
301 * accented) | |
302 * | |
303 * For Attributes with a PN Value Representation (e.g., Patient Name | |
304 * (0010,0010)), an application may perform literal matching that is | |
305 * either case-sensitive, or that is insensitive to some or all | |
306 * aspects of case, position, accent, or other character encoding | |
307 * variants." | |
308 * | |
309 * (0008,0018) UI SOPInstanceUID => Case-sensitive | |
310 * (0008,0050) SH AccessionNumber => Case-sensitive | |
311 * (0010,0020) LO PatientID => Case-sensitive | |
312 * (0020,000D) UI StudyInstanceUID => Case-sensitive | |
313 * (0020,000E) UI SeriesInstanceUID => Case-sensitive | |
314 **/ | |
315 | |
1374
a1745d9be6e9
CaseSensitivePN configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1361
diff
changeset
|
316 AssignConstraint(tag, new ValueConstraint(constraint, sensitive)); |
1360 | 317 } |
318 } | |
319 | |
320 | |
321 bool DicomFindQuery::RestrictIdentifier(std::string& value, | |
322 DicomTag identifier) const | |
323 { | |
324 Constraints::const_iterator it = constraints_.find(identifier); | |
325 if (it == constraints_.end() || | |
326 !it->second->IsExactConstraint()) | |
327 { | |
328 return false; | |
329 } | |
330 else | |
331 { | |
332 value = dynamic_cast<ValueConstraint*>(it->second)->GetValue(); | |
333 return true; | |
334 } | |
335 } | |
336 | |
337 bool DicomFindQuery::HasMainDicomTagsFilter(ResourceType level) const | |
338 { | |
339 return filteredLevels_.find(level) != filteredLevels_.end(); | |
340 } | |
341 | |
1361
94ffb597d297
refactoring of C-Find SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1360
diff
changeset
|
342 bool DicomFindQuery::FilterMainDicomTags(const std::string& resourceId, |
94ffb597d297
refactoring of C-Find SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1360
diff
changeset
|
343 ResourceType level, |
94ffb597d297
refactoring of C-Find SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1360
diff
changeset
|
344 const DicomMap& mainTags) const |
1360 | 345 { |
346 std::set<DicomTag> tags; | |
347 mainTags.GetTags(tags); | |
348 | |
349 for (std::set<DicomTag>::const_iterator | |
350 it = tags.begin(); it != tags.end(); ++it) | |
351 { | |
352 Constraints::const_iterator constraint = constraints_.find(*it); | |
1361
94ffb597d297
refactoring of C-Find SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1360
diff
changeset
|
353 if (constraint != constraints_.end() && |
94ffb597d297
refactoring of C-Find SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1360
diff
changeset
|
354 !constraint->second->Apply(mainTags.GetValue(*it).AsString())) |
1360 | 355 { |
356 return false; | |
357 } | |
358 } | |
359 | |
360 return true; | |
361 } | |
362 | |
363 bool DicomFindQuery::HasInstanceFilter() const | |
364 { | |
365 return filterJson_; | |
366 } | |
367 | |
368 bool DicomFindQuery::FilterInstance(const std::string& instanceId, | |
369 const Json::Value& content) const | |
370 { | |
371 for (Constraints::const_iterator it = constraints_.begin(); | |
372 it != constraints_.end(); ++it) | |
373 { | |
374 std::string tag = it->first.Format(); | |
375 std::string value; | |
376 if (content.isMember(tag)) | |
377 { | |
378 value = content.get(tag, Json::arrayValue).get("Value", "").asString(); | |
379 } | |
380 | |
381 if (!it->second->Apply(value)) | |
382 { | |
383 return false; | |
384 } | |
385 } | |
386 | |
387 return true; | |
388 } | |
389 } |