comparison OrthancServer/Search/LookupIdentifierQuery.cpp @ 1747:ca69082ab200 db-changes

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 26 Oct 2015 16:58:11 +0100
parents OrthancServer/LookupIdentifierQuery.cpp@d143db00a794
children 92203f713205
comparison
equal deleted inserted replaced
1746:d143db00a794 1747:ca69082ab200
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 #include "../PrecompiledHeadersServer.h"
34 #include "LookupIdentifierQuery.h"
35
36 #include "../../Core/OrthancException.h"
37 #include "SetOfResources.h"
38
39 #include <cassert>
40
41
42
43 namespace Orthanc
44 {
45 static const DicomTag patientIdentifiers[] =
46 {
47 DICOM_TAG_PATIENT_ID,
48 DICOM_TAG_PATIENT_NAME,
49 DICOM_TAG_PATIENT_BIRTH_DATE
50 };
51
52 static const DicomTag studyIdentifiers[] =
53 {
54 DICOM_TAG_PATIENT_ID,
55 DICOM_TAG_PATIENT_NAME,
56 DICOM_TAG_PATIENT_BIRTH_DATE,
57 DICOM_TAG_STUDY_INSTANCE_UID,
58 DICOM_TAG_ACCESSION_NUMBER,
59 DICOM_TAG_STUDY_DESCRIPTION,
60 DICOM_TAG_STUDY_DATE
61 };
62
63 static const DicomTag seriesIdentifiers[] =
64 {
65 DICOM_TAG_SERIES_INSTANCE_UID
66 };
67
68 static const DicomTag instanceIdentifiers[] =
69 {
70 DICOM_TAG_SOP_INSTANCE_UID
71 };
72
73 static void LoadIdentifiers(const DicomTag*& tags,
74 size_t& size,
75 ResourceType level)
76 {
77 switch (level)
78 {
79 case ResourceType_Patient:
80 tags = patientIdentifiers;
81 size = sizeof(patientIdentifiers) / sizeof(DicomTag);
82 break;
83
84 case ResourceType_Study:
85 tags = studyIdentifiers;
86 size = sizeof(studyIdentifiers) / sizeof(DicomTag);
87 break;
88
89 case ResourceType_Series:
90 tags = seriesIdentifiers;
91 size = sizeof(seriesIdentifiers) / sizeof(DicomTag);
92 break;
93
94 case ResourceType_Instance:
95 tags = instanceIdentifiers;
96 size = sizeof(instanceIdentifiers) / sizeof(DicomTag);
97 break;
98
99 default:
100 throw OrthancException(ErrorCode_ParameterOutOfRange);
101 }
102 }
103
104
105
106 LookupIdentifierQuery::~LookupIdentifierQuery()
107 {
108 for (Constraints::iterator it = constraints_.begin();
109 it != constraints_.end(); ++it)
110 {
111 delete *it;
112 }
113 }
114
115
116
117 void LookupIdentifierQuery::CheckIndex(size_t index) const
118 {
119 if (index >= constraints_.size())
120 {
121 throw OrthancException(ErrorCode_ParameterOutOfRange);
122 }
123 }
124
125
126 bool LookupIdentifierQuery::IsIdentifier(const DicomTag& tag) const
127 {
128 const DicomTag* tags;
129 size_t size;
130
131 LoadIdentifiers(tags, size, level_);
132
133 for (size_t i = 0; i < size; i++)
134 {
135 if (tag == tags[i])
136 {
137 return true;
138 }
139 }
140
141 return false;
142 }
143
144
145 void LookupIdentifierQuery::AddConstraint(DicomTag tag,
146 IdentifierConstraintType type,
147 const std::string& value)
148 {
149 assert(IsIdentifier(tag));
150 constraints_.push_back(new Constraint(tag, type, NormalizeIdentifier(value)));
151 }
152
153
154 const DicomTag& LookupIdentifierQuery::GetTag(size_t index) const
155 {
156 CheckIndex(index);
157 return constraints_[index]->tag_;
158 }
159
160
161 IdentifierConstraintType LookupIdentifierQuery::GetType(size_t index) const
162 {
163 CheckIndex(index);
164 return constraints_[index]->type_;
165 }
166
167
168 const std::string& LookupIdentifierQuery::GetValue(size_t index) const
169 {
170 CheckIndex(index);
171 return constraints_[index]->value_;
172 }
173
174
175 std::string LookupIdentifierQuery::NormalizeIdentifier(const std::string& value)
176 {
177 std::string s = Toolbox::ConvertToAscii(Toolbox::StripSpaces(value));
178 Toolbox::ToUpperCase(s);
179 return s;
180 }
181
182
183 void LookupIdentifierQuery::StoreIdentifiers(IDatabaseWrapper& database,
184 int64_t resource,
185 ResourceType level,
186 const DicomMap& map)
187 {
188 const DicomTag* tags;
189 size_t size;
190
191 LoadIdentifiers(tags, size, level);
192
193 for (size_t i = 0; i < size; i++)
194 {
195 const DicomValue* value = map.TestAndGetValue(tags[i]);
196 if (value != NULL &&
197 !value->IsNull() &&
198 !value->IsBinary())
199 {
200 std::string s = NormalizeIdentifier(value->GetContent());
201 database.SetIdentifierTag(resource, tags[i], s);
202 }
203 }
204 }
205
206
207 void LookupIdentifierQuery::Apply(std::list<std::string>& result,
208 IDatabaseWrapper& database)
209 {
210 SetOfResources resources(database, level_);
211
212 for (size_t i = 0; i < GetSize(); i++)
213 {
214 std::list<int64_t> tmp;
215 database.LookupIdentifier(tmp, level_, GetTag(i), GetType(i), GetValue(i));
216 resources.Intersect(tmp);
217 }
218
219 resources.Flatten(result);
220 }
221 }