comparison Resources/Orthanc/Databases/DatabaseConstraint.cpp @ 152:063aa53b5917

sync
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 01 Jul 2020 08:54:32 +0200
parents
children e712ff3eede3
comparison
equal deleted inserted replaced
150:d9101318442d 152:063aa53b5917
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-2020 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 #if !defined(ORTHANC_BUILDING_SERVER_LIBRARY)
35 # error Macro ORTHANC_BUILDING_SERVER_LIBRARY must be defined
36 #endif
37
38 #if ORTHANC_BUILDING_SERVER_LIBRARY == 1
39 # include "../PrecompiledHeadersServer.h"
40 #endif
41
42 #include "DatabaseConstraint.h"
43
44 #if ORTHANC_BUILDING_SERVER_LIBRARY == 1
45 # include "../../../OrthancFramework/Sources/OrthancException.h"
46 #else
47 # include <OrthancException.h>
48 #endif
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 }