comparison OrthancServer/Search/DatabaseLookup.cpp @ 2892:ce310baccda6 db-changes

DicomTagConstraint and DatabaseLookup
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 16 Oct 2018 18:00:05 +0200
parents
children 1723cbba55c7
comparison
equal deleted inserted replaced
2884:497a637366b4 2892:ce310baccda6
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-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeadersServer.h"
35 #include "DatabaseLookup.h"
36
37 #include "../ServerToolbox.h"
38 #include "../../Core/DicomParsing/FromDcmtkBridge.h"
39
40 namespace Orthanc
41 {
42 void DatabaseLookup::LoadTags(ResourceType level)
43 {
44 const DicomTag* tags = NULL;
45 size_t size;
46
47 ServerToolbox::LoadIdentifiers(tags, size, level);
48
49 for (size_t i = 0; i < size; i++)
50 {
51 assert(tags_.find(tags[i]) == tags_.end());
52 tags_[tags[i]] = TagInfo(DicomTagType_Identifier, level);
53 }
54
55 DicomMap::LoadMainDicomTags(tags, size, level);
56
57 for (size_t i = 0; i < size; i++)
58 {
59 if (tags_.find(tags[i]) == tags_.end())
60 {
61 tags_[tags[i]] = TagInfo(DicomTagType_Main, level);
62 }
63 }
64 }
65
66
67 DatabaseLookup::DatabaseLookup()
68 {
69 LoadTags(ResourceType_Patient);
70 LoadTags(ResourceType_Study);
71 LoadTags(ResourceType_Series);
72 LoadTags(ResourceType_Instance);
73 }
74
75
76 DatabaseLookup::~DatabaseLookup()
77 {
78 for (size_t i = 0; i < constraints_.size(); i++)
79 {
80 assert(constraints_[i] != NULL);
81 delete constraints_[i];
82 }
83 }
84
85
86 const DicomTagConstraint& DatabaseLookup::GetConstraint(size_t index) const
87 {
88 if (index >= constraints_.size())
89 {
90 throw OrthancException(ErrorCode_ParameterOutOfRange);
91 }
92 else
93 {
94 assert(constraints_[index] != NULL);
95 return *constraints_[index];
96 }
97 }
98
99
100 void DatabaseLookup::AddConstraint(DicomTagConstraint* constraint)
101 {
102 if (constraint == NULL)
103 {
104 throw OrthancException(ErrorCode_NullPointer);
105 }
106 else
107 {
108 constraints_.push_back(constraint);
109
110 std::map<DicomTag, TagInfo>::const_iterator tag = tags_.find(constraint->GetTag());
111
112 if (tag == tags_.end())
113 {
114 constraint->SetTagInfo(DicomTagType_Generic, ResourceType_Instance);
115 }
116 else
117 {
118 constraint->SetTagInfo(tag->second.GetType(), tag->second.GetLevel());
119 }
120 }
121 }
122
123
124 bool DatabaseLookup::IsMatch(const DicomMap& value)
125 {
126 for (size_t i = 0; i < constraints_.size(); i++)
127 {
128 assert(constraints_[i] != NULL);
129 if (!constraints_[i]->IsMatch(value))
130 {
131 return false;
132 }
133 }
134
135 return true;
136 }
137
138
139 void DatabaseLookup::AddDicomConstraint(const DicomTag& tag,
140 const std::string& dicomQuery,
141 bool caseSensitivePN)
142 {
143 ValueRepresentation vr = FromDcmtkBridge::LookupValueRepresentation(tag);
144
145 if (vr == ValueRepresentation_Sequence)
146 {
147 throw OrthancException(ErrorCode_ParameterOutOfRange);
148 }
149
150 /**
151 * DICOM specifies that searches must always be case sensitive,
152 * except for tags with a PN value representation. For PN, Orthanc
153 * uses the configuration option "CaseSensitivePN" to decide
154 * whether matching is case-sensitive or case-insensitive.
155 *
156 * Reference: DICOM PS 3.4
157 * - C.2.2.2.1 ("Single Value Matching")
158 * - C.2.2.2.4 ("Wild Card Matching")
159 * http://medical.nema.org/Dicom/2011/11_04pu.pdf
160 *
161 * "Except for Attributes with a PN Value Representation, only
162 * entities with values which match exactly the value specified in the
163 * request shall match. This matching is case-sensitive, i.e.,
164 * sensitive to the exact encoding of the key attribute value in
165 * character sets where a letter may have multiple encodings (e.g.,
166 * based on its case, its position in a word, or whether it is
167 * accented)
168 *
169 * For Attributes with a PN Value Representation (e.g., Patient Name
170 * (0010,0010)), an application may perform literal matching that is
171 * either case-sensitive, or that is insensitive to some or all
172 * aspects of case, position, accent, or other character encoding
173 * variants."
174 *
175 * (0008,0018) UI SOPInstanceUID => Case-sensitive
176 * (0008,0050) SH AccessionNumber => Case-sensitive
177 * (0010,0020) LO PatientID => Case-sensitive
178 * (0020,000D) UI StudyInstanceUID => Case-sensitive
179 * (0020,000E) UI SeriesInstanceUID => Case-sensitive
180 **/
181 bool caseSensitive = true;
182 if (vr == ValueRepresentation_PersonName)
183 {
184 caseSensitive = caseSensitivePN;
185 }
186
187 if ((vr == ValueRepresentation_Date ||
188 vr == ValueRepresentation_DateTime ||
189 vr == ValueRepresentation_Time) &&
190 dicomQuery.find('-') != std::string::npos)
191 {
192 /**
193 * Range matching is only defined for TM, DA and DT value
194 * representations. This code fixes issues 35 and 37.
195 *
196 * Reference: "Range matching is not defined for types of
197 * Attributes other than dates and times", DICOM PS 3.4,
198 * C.2.2.2.5 ("Range Matching").
199 **/
200 size_t separator = dicomQuery.find('-');
201 std::string lower = dicomQuery.substr(0, separator);
202 std::string upper = dicomQuery.substr(separator + 1);
203
204 if (!lower.empty())
205 {
206 AddConstraint(new DicomTagConstraint
207 (tag, ConstraintType_GreaterOrEqual, lower, caseSensitive));
208 }
209
210 if (!upper.empty())
211 {
212 AddConstraint(new DicomTagConstraint
213 (tag, ConstraintType_SmallerOrEqual, upper, caseSensitive));
214 }
215 }
216 else if (dicomQuery.find('\\') != std::string::npos)
217 {
218 DicomTag fixedTag(tag);
219
220 if (tag == DICOM_TAG_MODALITIES_IN_STUDY)
221 {
222 // http://www.itk.org/Wiki/DICOM_QueryRetrieve_Explained
223 // http://dicomiseasy.blogspot.be/2012/01/dicom-queryretrieve-part-i.html
224 fixedTag = DICOM_TAG_MODALITY;
225 }
226
227 std::auto_ptr<DicomTagConstraint> constraint
228 (new DicomTagConstraint(fixedTag, ConstraintType_List, caseSensitive));
229
230 std::vector<std::string> items;
231 Toolbox::TokenizeString(items, dicomQuery, '\\');
232
233 for (size_t i = 0; i < items.size(); i++)
234 {
235 constraint->AddValue(items[i]);
236 }
237
238 AddConstraint(constraint.release());
239 }
240 else if (dicomQuery.find('*') != std::string::npos ||
241 dicomQuery.find('?') != std::string::npos)
242 {
243 AddConstraint(new DicomTagConstraint
244 (tag, ConstraintType_Wildcard, dicomQuery, caseSensitive));
245 }
246 else
247 {
248 AddConstraint(new DicomTagConstraint
249 (tag, ConstraintType_Equal, dicomQuery, caseSensitive));
250 }
251 }
252 }