comparison OrthancServer/Search/HierarchicalMatcher.cpp @ 1795:af6840eb23ee worklists

HierarchicalMatcher
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Nov 2015 17:41:22 +0100
parents
children 5e08a5fe6b27
comparison
equal deleted inserted replaced
1794:bdfae6e17d23 1795:af6840eb23ee
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 "HierarchicalMatcher.h"
35
36 #include "../../Core/OrthancException.h"
37 #include "../FromDcmtkBridge.h"
38
39 #include <dcmtk/dcmdata/dcfilefo.h>
40
41 namespace Orthanc
42 {
43 HierarchicalMatcher::HierarchicalMatcher(ParsedDicomFile& query,
44 bool caseSensitivePN)
45 {
46 Setup(*query.GetDcmtkObject().getDataset(),
47 caseSensitivePN,
48 query.GetEncoding());
49 }
50
51
52 HierarchicalMatcher::~HierarchicalMatcher()
53 {
54 for (Constraints::iterator it = constraints_.begin();
55 it != constraints_.end(); ++it)
56 {
57 if (it->second != NULL)
58 {
59 delete it->second;
60 }
61 }
62
63 for (Sequences::iterator it = sequences_.begin();
64 it != sequences_.end(); ++it)
65 {
66 if (it->second != NULL)
67 {
68 delete it->second;
69 }
70 }
71 }
72
73
74 void HierarchicalMatcher::Setup(DcmItem& dataset,
75 bool caseSensitivePN,
76 Encoding encoding)
77 {
78 for (unsigned long i = 0; i < dataset.card(); i++)
79 {
80 DcmElement* element = dataset.getElement(i);
81 if (element == NULL)
82 {
83 throw OrthancException(ErrorCode_InternalError);
84 }
85
86 DicomTag tag(FromDcmtkBridge::Convert(element->getTag()));
87 ValueRepresentation vr = FromDcmtkBridge::GetValueRepresentation(tag);
88
89 if (constraints_.find(tag) != constraints_.end() ||
90 sequences_.find(tag) != sequences_.end())
91 {
92 throw OrthancException(ErrorCode_BadRequest);
93 }
94
95 if (vr == ValueRepresentation_Sequence)
96 {
97 DcmSequenceOfItems& sequence = dynamic_cast<DcmSequenceOfItems&>(*element);
98
99 if (sequence.card() == 0 ||
100 (sequence.card() == 1 && sequence.getItem(0)->card() == 0))
101 {
102 // Universal matching of a sequence
103 sequences_[tag] = NULL;
104 }
105 else if (sequence.card() == 1)
106 {
107 sequences_[tag] = new HierarchicalMatcher(*sequence.getItem(0), caseSensitivePN, encoding);
108 }
109 else
110 {
111 throw OrthancException(ErrorCode_BadRequest);
112 }
113 }
114 else
115 {
116 std::auto_ptr<DicomValue> value(FromDcmtkBridge::ConvertLeafElement
117 (*element, DicomToJsonFlags_None, encoding));
118
119 if (value->IsBinary() ||
120 value->IsNull())
121 {
122 throw OrthancException(ErrorCode_BadRequest);
123 }
124 else if (value->GetContent().empty())
125 {
126 // This is an universal matcher
127 constraints_[tag] = NULL;
128 }
129 else
130 {
131 // DICOM specifies that searches must be case sensitive, except
132 // for tags with a PN value representation
133 bool sensitive = true;
134 if (vr == ValueRepresentation_PatientName)
135 {
136 sensitive = caseSensitivePN;
137 }
138
139 constraints_[tag] = IFindConstraint::ParseDicomConstraint(tag, value->GetContent(), sensitive);
140 }
141 }
142 }
143 }
144
145
146 std::string HierarchicalMatcher::Format(const std::string& prefix) const
147 {
148 std::string s;
149
150 for (Constraints::const_iterator it = constraints_.begin();
151 it != constraints_.end(); ++it)
152 {
153 s += prefix + it->first.Format() + " ";
154
155 if (it->second == NULL)
156 {
157 s += "*\n";
158 }
159 else
160 {
161 s += it->second->Format() + "\n";
162 }
163 }
164
165 for (Sequences::const_iterator it = sequences_.begin();
166 it != sequences_.end(); ++it)
167 {
168 s += prefix + it->first.Format() + " ";
169
170 if (it->second == NULL)
171 {
172 s += "*\n";
173 }
174 else
175 {
176 s += "Sequence:\n" + it->second->Format(prefix + " ");
177 }
178 }
179
180 return s;
181 }
182 }