comparison Framework/Plugins/DatabaseConstraint.cpp @ 547:b8e6e7a19424

un-sharing DatabaseConstraint and ISqlLookupFormatter with Orthanc core
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 09 Sep 2024 13:18:35 +0200
parents
children 25005693297b
comparison
equal deleted inserted replaced
542:a8f9d44e7842 547:b8e6e7a19424
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-2023 Osimis S.A., Belgium
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
8 *
9 * This program is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU Affero General Public License
11 * as published by the Free Software Foundation, either version 3 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 **/
22
23
24 /**
25 * NB: Until 2024-09-09, this file was synchronized with the following
26 * folder from the Orthanc main project:
27 * https://orthanc.uclouvain.be/hg/orthanc/file/default/OrthancServer/Sources/Search/
28 **/
29
30
31 #if !defined(ORTHANC_BUILDING_SERVER_LIBRARY)
32 # error Macro ORTHANC_BUILDING_SERVER_LIBRARY must be defined
33 #endif
34
35 #if ORTHANC_BUILDING_SERVER_LIBRARY == 1
36 # include "../PrecompiledHeadersServer.h"
37 #endif
38
39 #include "DatabaseConstraint.h"
40
41 #if ORTHANC_BUILDING_SERVER_LIBRARY == 1
42 # include "../../../OrthancFramework/Sources/OrthancException.h"
43 #else
44 # include <OrthancException.h>
45 #endif
46
47 #include <cassert>
48
49
50 namespace Orthanc
51 {
52 namespace Plugins
53 {
54 #if ORTHANC_ENABLE_PLUGINS == 1
55 OrthancPluginResourceType Convert(ResourceType type)
56 {
57 switch (type)
58 {
59 case ResourceType_Patient:
60 return OrthancPluginResourceType_Patient;
61
62 case ResourceType_Study:
63 return OrthancPluginResourceType_Study;
64
65 case ResourceType_Series:
66 return OrthancPluginResourceType_Series;
67
68 case ResourceType_Instance:
69 return OrthancPluginResourceType_Instance;
70
71 default:
72 throw OrthancException(ErrorCode_ParameterOutOfRange);
73 }
74 }
75 #endif
76
77
78 #if ORTHANC_ENABLE_PLUGINS == 1
79 ResourceType Convert(OrthancPluginResourceType type)
80 {
81 switch (type)
82 {
83 case OrthancPluginResourceType_Patient:
84 return ResourceType_Patient;
85
86 case OrthancPluginResourceType_Study:
87 return ResourceType_Study;
88
89 case OrthancPluginResourceType_Series:
90 return ResourceType_Series;
91
92 case OrthancPluginResourceType_Instance:
93 return ResourceType_Instance;
94
95 default:
96 throw OrthancException(ErrorCode_ParameterOutOfRange);
97 }
98 }
99 #endif
100
101
102 #if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
103 OrthancPluginConstraintType Convert(ConstraintType constraint)
104 {
105 switch (constraint)
106 {
107 case ConstraintType_Equal:
108 return OrthancPluginConstraintType_Equal;
109
110 case ConstraintType_GreaterOrEqual:
111 return OrthancPluginConstraintType_GreaterOrEqual;
112
113 case ConstraintType_SmallerOrEqual:
114 return OrthancPluginConstraintType_SmallerOrEqual;
115
116 case ConstraintType_Wildcard:
117 return OrthancPluginConstraintType_Wildcard;
118
119 case ConstraintType_List:
120 return OrthancPluginConstraintType_List;
121
122 default:
123 throw OrthancException(ErrorCode_ParameterOutOfRange);
124 }
125 }
126 #endif
127
128
129 #if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
130 ConstraintType Convert(OrthancPluginConstraintType constraint)
131 {
132 switch (constraint)
133 {
134 case OrthancPluginConstraintType_Equal:
135 return ConstraintType_Equal;
136
137 case OrthancPluginConstraintType_GreaterOrEqual:
138 return ConstraintType_GreaterOrEqual;
139
140 case OrthancPluginConstraintType_SmallerOrEqual:
141 return ConstraintType_SmallerOrEqual;
142
143 case OrthancPluginConstraintType_Wildcard:
144 return ConstraintType_Wildcard;
145
146 case OrthancPluginConstraintType_List:
147 return ConstraintType_List;
148
149 default:
150 throw OrthancException(ErrorCode_ParameterOutOfRange);
151 }
152 }
153 #endif
154 }
155
156 DatabaseConstraint::DatabaseConstraint(ResourceType level,
157 const DicomTag& tag,
158 bool isIdentifier,
159 ConstraintType type,
160 const std::vector<std::string>& values,
161 bool caseSensitive,
162 bool mandatory) :
163 level_(level),
164 tag_(tag),
165 isIdentifier_(isIdentifier),
166 constraintType_(type),
167 values_(values),
168 caseSensitive_(caseSensitive),
169 mandatory_(mandatory)
170 {
171 if (type != ConstraintType_List &&
172 values_.size() != 1)
173 {
174 throw OrthancException(ErrorCode_ParameterOutOfRange);
175 }
176 }
177
178
179 #if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
180 DatabaseConstraint::DatabaseConstraint(const OrthancPluginDatabaseConstraint& constraint) :
181 level_(Plugins::Convert(constraint.level)),
182 tag_(constraint.tagGroup, constraint.tagElement),
183 isIdentifier_(constraint.isIdentifierTag),
184 constraintType_(Plugins::Convert(constraint.type)),
185 caseSensitive_(constraint.isCaseSensitive),
186 mandatory_(constraint.isMandatory)
187 {
188 if (constraintType_ != ConstraintType_List &&
189 constraint.valuesCount != 1)
190 {
191 throw OrthancException(ErrorCode_ParameterOutOfRange);
192 }
193
194 values_.resize(constraint.valuesCount);
195
196 for (uint32_t i = 0; i < constraint.valuesCount; i++)
197 {
198 assert(constraint.values[i] != NULL);
199 values_[i].assign(constraint.values[i]);
200 }
201 }
202 #endif
203
204
205 const std::string& DatabaseConstraint::GetValue(size_t index) const
206 {
207 if (index >= values_.size())
208 {
209 throw OrthancException(ErrorCode_ParameterOutOfRange);
210 }
211 else
212 {
213 return values_[index];
214 }
215 }
216
217
218 const std::string& DatabaseConstraint::GetSingleValue() const
219 {
220 if (values_.size() != 1)
221 {
222 throw OrthancException(ErrorCode_BadSequenceOfCalls);
223 }
224 else
225 {
226 return values_[0];
227 }
228 }
229
230
231 #if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
232 void DatabaseConstraint::EncodeForPlugins(OrthancPluginDatabaseConstraint& constraint,
233 std::vector<const char*>& tmpValues) const
234 {
235 memset(&constraint, 0, sizeof(constraint));
236
237 tmpValues.resize(values_.size());
238
239 for (size_t i = 0; i < values_.size(); i++)
240 {
241 tmpValues[i] = values_[i].c_str();
242 }
243
244 constraint.level = Plugins::Convert(level_);
245 constraint.tagGroup = tag_.GetGroup();
246 constraint.tagElement = tag_.GetElement();
247 constraint.isIdentifierTag = isIdentifier_;
248 constraint.isCaseSensitive = caseSensitive_;
249 constraint.isMandatory = mandatory_;
250 constraint.type = Plugins::Convert(constraintType_);
251 constraint.valuesCount = values_.size();
252 constraint.values = (tmpValues.empty() ? NULL : &tmpValues[0]);
253 }
254 #endif
255
256
257 void DatabaseConstraints::Clear()
258 {
259 for (size_t i = 0; i < constraints_.size(); i++)
260 {
261 assert(constraints_[i] != NULL);
262 delete constraints_[i];
263 }
264
265 constraints_.clear();
266 }
267
268
269 void DatabaseConstraints::AddConstraint(DatabaseConstraint* constraint)
270 {
271 if (constraint == NULL)
272 {
273 throw OrthancException(ErrorCode_NullPointer);
274 }
275 else
276 {
277 constraints_.push_back(constraint);
278 }
279 }
280
281
282 const DatabaseConstraint& DatabaseConstraints::GetConstraint(size_t index) const
283 {
284 if (index >= constraints_.size())
285 {
286 throw OrthancException(ErrorCode_ParameterOutOfRange);
287 }
288 else
289 {
290 assert(constraints_[index] != NULL);
291 return *constraints_[index];
292 }
293 }
294 }