Mercurial > hg > orthanc
annotate OrthancServer/Search/LookupIdentifierQuery.cpp @ 2109:d5c29fd74ffa
OrthancPlugins::CheckMinimalOrthancVersion
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 26 Oct 2016 13:32:02 +0200 |
parents | b1291df2f780 |
children | a657f7772e69 |
rev | line source |
---|---|
1745 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1745 | 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 | |
1747 | 33 #include "../PrecompiledHeadersServer.h" |
1745 | 34 #include "LookupIdentifierQuery.h" |
35 | |
1747 | 36 #include "../../Core/OrthancException.h" |
1746 | 37 #include "SetOfResources.h" |
1758 | 38 #include "../FromDcmtkBridge.h" |
1745 | 39 |
40 #include <cassert> | |
41 | |
42 | |
43 | |
44 namespace Orthanc | |
45 { | |
46 static const DicomTag patientIdentifiers[] = | |
47 { | |
48 DICOM_TAG_PATIENT_ID, | |
49 DICOM_TAG_PATIENT_NAME, | |
50 DICOM_TAG_PATIENT_BIRTH_DATE | |
51 }; | |
52 | |
53 static const DicomTag studyIdentifiers[] = | |
54 { | |
55 DICOM_TAG_PATIENT_ID, | |
56 DICOM_TAG_PATIENT_NAME, | |
57 DICOM_TAG_PATIENT_BIRTH_DATE, | |
58 DICOM_TAG_STUDY_INSTANCE_UID, | |
59 DICOM_TAG_ACCESSION_NUMBER, | |
60 DICOM_TAG_STUDY_DESCRIPTION, | |
61 DICOM_TAG_STUDY_DATE | |
62 }; | |
63 | |
64 static const DicomTag seriesIdentifiers[] = | |
65 { | |
66 DICOM_TAG_SERIES_INSTANCE_UID | |
67 }; | |
68 | |
69 static const DicomTag instanceIdentifiers[] = | |
70 { | |
71 DICOM_TAG_SOP_INSTANCE_UID | |
72 }; | |
73 | |
1749
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
74 |
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
75 void LookupIdentifierQuery::LoadIdentifiers(const DicomTag*& tags, |
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
76 size_t& size, |
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
77 ResourceType level) |
1745 | 78 { |
79 switch (level) | |
80 { | |
81 case ResourceType_Patient: | |
82 tags = patientIdentifiers; | |
83 size = sizeof(patientIdentifiers) / sizeof(DicomTag); | |
84 break; | |
85 | |
86 case ResourceType_Study: | |
87 tags = studyIdentifiers; | |
88 size = sizeof(studyIdentifiers) / sizeof(DicomTag); | |
89 break; | |
90 | |
91 case ResourceType_Series: | |
92 tags = seriesIdentifiers; | |
93 size = sizeof(seriesIdentifiers) / sizeof(DicomTag); | |
94 break; | |
95 | |
96 case ResourceType_Instance: | |
97 tags = instanceIdentifiers; | |
98 size = sizeof(instanceIdentifiers) / sizeof(DicomTag); | |
99 break; | |
100 | |
101 default: | |
102 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
103 } | |
104 } | |
105 | |
106 | |
1749
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
107 LookupIdentifierQuery::Disjunction::~Disjunction() |
1748 | 108 { |
1749
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
109 for (size_t i = 0; i < disjunction_.size(); i++) |
1748 | 110 { |
1749
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
111 delete disjunction_[i]; |
1748 | 112 } |
113 } | |
114 | |
115 | |
1749
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
116 void LookupIdentifierQuery::Disjunction::Add(const DicomTag& tag, |
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
117 IdentifierConstraintType type, |
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
118 const std::string& value) |
1748 | 119 { |
1749
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
120 disjunction_.push_back(new Constraint(tag, type, value)); |
1748 | 121 } |
122 | |
1746 | 123 |
124 LookupIdentifierQuery::~LookupIdentifierQuery() | |
125 { | |
126 for (Constraints::iterator it = constraints_.begin(); | |
127 it != constraints_.end(); ++it) | |
128 { | |
129 delete *it; | |
130 } | |
131 } | |
132 | |
133 | |
134 | |
1748 | 135 bool LookupIdentifierQuery::IsIdentifier(const DicomTag& tag, |
136 ResourceType level) | |
1745 | 137 { |
138 const DicomTag* tags; | |
139 size_t size; | |
140 | |
1748 | 141 LoadIdentifiers(tags, size, level); |
1745 | 142 |
143 for (size_t i = 0; i < size; i++) | |
144 { | |
145 if (tag == tags[i]) | |
146 { | |
147 return true; | |
148 } | |
149 } | |
150 | |
151 return false; | |
152 } | |
153 | |
154 | |
155 void LookupIdentifierQuery::AddConstraint(DicomTag tag, | |
156 IdentifierConstraintType type, | |
157 const std::string& value) | |
158 { | |
159 assert(IsIdentifier(tag)); | |
1751
fb569ee09a69
LookupResource complete
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1750
diff
changeset
|
160 constraints_.push_back(new Disjunction); |
1749
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
161 constraints_.back()->Add(tag, type, value); |
1745 | 162 } |
163 | |
164 | |
1749
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
165 LookupIdentifierQuery::Disjunction& LookupIdentifierQuery::AddDisjunction() |
1745 | 166 { |
1749
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
167 constraints_.push_back(new Disjunction); |
99f4a05f39fa
various types of constraints
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1748
diff
changeset
|
168 return *constraints_.back(); |
1745 | 169 } |
170 | |
171 | |
172 std::string LookupIdentifierQuery::NormalizeIdentifier(const std::string& value) | |
173 { | |
1764 | 174 std::string t; |
175 t.reserve(value.size()); | |
176 | |
177 for (size_t i = 0; i < value.size(); i++) | |
178 { | |
179 if (value[i] == '%' || | |
180 value[i] == '_') | |
181 { | |
182 t.push_back(' '); // These characters might break wildcard queries in SQL | |
183 } | |
184 else if (isascii(value[i]) && | |
185 !iscntrl(value[i]) && | |
186 (!isspace(value[i]) || value[i] == ' ')) | |
187 { | |
188 t.push_back(value[i]); | |
189 } | |
190 } | |
191 | |
192 Toolbox::ToUpperCase(t); | |
193 | |
194 return Toolbox::StripSpaces(t); | |
1745 | 195 } |
196 | |
197 | |
198 void LookupIdentifierQuery::StoreIdentifiers(IDatabaseWrapper& database, | |
199 int64_t resource, | |
200 ResourceType level, | |
201 const DicomMap& map) | |
202 { | |
203 const DicomTag* tags; | |
204 size_t size; | |
205 | |
206 LoadIdentifiers(tags, size, level); | |
207 | |
208 for (size_t i = 0; i < size; i++) | |
209 { | |
210 const DicomValue* value = map.TestAndGetValue(tags[i]); | |
211 if (value != NULL && | |
212 !value->IsNull() && | |
213 !value->IsBinary()) | |
214 { | |
215 std::string s = NormalizeIdentifier(value->GetContent()); | |
216 database.SetIdentifierTag(resource, tags[i], s); | |
217 } | |
218 } | |
219 } | |
1746 | 220 |
221 | |
222 void LookupIdentifierQuery::Apply(std::list<std::string>& result, | |
223 IDatabaseWrapper& database) | |
224 { | |
225 SetOfResources resources(database, level_); | |
1750
55d52567bebb
LookupResource implemented
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1749
diff
changeset
|
226 Apply(resources, database); |
55d52567bebb
LookupResource implemented
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1749
diff
changeset
|
227 |
55d52567bebb
LookupResource implemented
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1749
diff
changeset
|
228 resources.Flatten(result); |
55d52567bebb
LookupResource implemented
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1749
diff
changeset
|
229 } |
55d52567bebb
LookupResource implemented
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1749
diff
changeset
|
230 |
55d52567bebb
LookupResource implemented
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1749
diff
changeset
|
231 |
55d52567bebb
LookupResource implemented
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1749
diff
changeset
|
232 void LookupIdentifierQuery::Apply(SetOfResources& result, |
55d52567bebb
LookupResource implemented
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1749
diff
changeset
|
233 IDatabaseWrapper& database) |
55d52567bebb
LookupResource implemented
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1749
diff
changeset
|
234 { |
1746 | 235 for (size_t i = 0; i < GetSize(); i++) |
236 { | |
1748 | 237 std::list<int64_t> a; |
238 | |
239 for (size_t j = 0; j < constraints_[i]->GetSize(); j++) | |
240 { | |
241 const Constraint& constraint = constraints_[i]->GetConstraint(j); | |
242 std::list<int64_t> b; | |
243 database.LookupIdentifier(b, level_, constraint.GetTag(), constraint.GetType(), constraint.GetValue()); | |
244 | |
245 a.splice(a.end(), b); | |
246 } | |
247 | |
1750
55d52567bebb
LookupResource implemented
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1749
diff
changeset
|
248 result.Intersect(a); |
1746 | 249 } |
1750
55d52567bebb
LookupResource implemented
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1749
diff
changeset
|
250 } |
1746 | 251 |
1758 | 252 |
253 void LookupIdentifierQuery::Print(std::ostream& s) const | |
254 { | |
255 s << "Constraint: " << std::endl; | |
256 for (Constraints::const_iterator | |
257 it = constraints_.begin(); it != constraints_.end(); ++it) | |
258 { | |
259 if (it == constraints_.begin()) | |
260 s << " "; | |
261 else | |
262 s << "OR "; | |
263 | |
264 for (size_t j = 0; j < (*it)->GetSize(); j++) | |
265 { | |
266 const Constraint& c = (*it)->GetConstraint(j); | |
267 s << FromDcmtkBridge::GetName(c.GetTag()); | |
268 | |
269 switch (c.GetType()) | |
270 { | |
271 case IdentifierConstraintType_Equal: s << " == "; break; | |
272 case IdentifierConstraintType_SmallerOrEqual: s << " <= "; break; | |
273 case IdentifierConstraintType_GreaterOrEqual: s << " >= "; break; | |
274 case IdentifierConstraintType_Wildcard: s << " ~= "; break; | |
275 default: | |
276 s << " ? "; | |
277 } | |
278 | |
279 s << c.GetValue() << std::endl; | |
280 } | |
281 } | |
282 } | |
1745 | 283 } |