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-2021 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 #include <cassert>
51 
52 
53 namespace Orthanc
54 {
55   namespace Plugins
56   {
57 #if ORTHANC_ENABLE_PLUGINS == 1
Convert(ResourceType type)58     OrthancPluginResourceType Convert(ResourceType type)
59     {
60       switch (type)
61       {
62         case ResourceType_Patient:
63           return OrthancPluginResourceType_Patient;
64 
65         case ResourceType_Study:
66           return OrthancPluginResourceType_Study;
67 
68         case ResourceType_Series:
69           return OrthancPluginResourceType_Series;
70 
71         case ResourceType_Instance:
72           return OrthancPluginResourceType_Instance;
73 
74         default:
75           throw OrthancException(ErrorCode_ParameterOutOfRange);
76       }
77     }
78 #endif
79 
80 
81 #if ORTHANC_ENABLE_PLUGINS == 1
Convert(OrthancPluginResourceType type)82     ResourceType Convert(OrthancPluginResourceType type)
83     {
84       switch (type)
85       {
86         case OrthancPluginResourceType_Patient:
87           return ResourceType_Patient;
88 
89         case OrthancPluginResourceType_Study:
90           return ResourceType_Study;
91 
92         case OrthancPluginResourceType_Series:
93           return ResourceType_Series;
94 
95         case OrthancPluginResourceType_Instance:
96           return ResourceType_Instance;
97 
98         default:
99           throw OrthancException(ErrorCode_ParameterOutOfRange);
100       }
101     }
102 #endif
103 
104 
105 #if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
Convert(ConstraintType constraint)106     OrthancPluginConstraintType Convert(ConstraintType constraint)
107     {
108       switch (constraint)
109       {
110         case ConstraintType_Equal:
111           return OrthancPluginConstraintType_Equal;
112 
113         case ConstraintType_GreaterOrEqual:
114           return OrthancPluginConstraintType_GreaterOrEqual;
115 
116         case ConstraintType_SmallerOrEqual:
117           return OrthancPluginConstraintType_SmallerOrEqual;
118 
119         case ConstraintType_Wildcard:
120           return OrthancPluginConstraintType_Wildcard;
121 
122         case ConstraintType_List:
123           return OrthancPluginConstraintType_List;
124 
125         default:
126           throw OrthancException(ErrorCode_ParameterOutOfRange);
127       }
128     }
129 #endif
130 
131 
132 #if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
Convert(OrthancPluginConstraintType constraint)133     ConstraintType Convert(OrthancPluginConstraintType constraint)
134     {
135       switch (constraint)
136       {
137         case OrthancPluginConstraintType_Equal:
138           return ConstraintType_Equal;
139 
140         case OrthancPluginConstraintType_GreaterOrEqual:
141           return ConstraintType_GreaterOrEqual;
142 
143         case OrthancPluginConstraintType_SmallerOrEqual:
144           return ConstraintType_SmallerOrEqual;
145 
146         case OrthancPluginConstraintType_Wildcard:
147           return ConstraintType_Wildcard;
148 
149         case OrthancPluginConstraintType_List:
150           return ConstraintType_List;
151 
152         default:
153           throw OrthancException(ErrorCode_ParameterOutOfRange);
154       }
155     }
156 #endif
157   }
158 
DatabaseConstraint(ResourceType level,const DicomTag & tag,bool isIdentifier,ConstraintType type,const std::vector<std::string> & values,bool caseSensitive,bool mandatory)159   DatabaseConstraint::DatabaseConstraint(ResourceType level,
160                                          const DicomTag& tag,
161                                          bool isIdentifier,
162                                          ConstraintType type,
163                                          const std::vector<std::string>& values,
164                                          bool caseSensitive,
165                                          bool mandatory) :
166     level_(level),
167     tag_(tag),
168     isIdentifier_(isIdentifier),
169     constraintType_(type),
170     values_(values),
171     caseSensitive_(caseSensitive),
172     mandatory_(mandatory)
173   {
174     if (type != ConstraintType_List &&
175         values_.size() != 1)
176     {
177       throw OrthancException(ErrorCode_ParameterOutOfRange);
178     }
179   }
180 
181 
182 #if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
DatabaseConstraint(const OrthancPluginDatabaseConstraint & constraint)183   DatabaseConstraint::DatabaseConstraint(const OrthancPluginDatabaseConstraint& constraint) :
184     level_(Plugins::Convert(constraint.level)),
185     tag_(constraint.tagGroup, constraint.tagElement),
186     isIdentifier_(constraint.isIdentifierTag),
187     constraintType_(Plugins::Convert(constraint.type)),
188     caseSensitive_(constraint.isCaseSensitive),
189     mandatory_(constraint.isMandatory)
190   {
191     if (constraintType_ != ConstraintType_List &&
192         constraint.valuesCount != 1)
193     {
194       throw OrthancException(ErrorCode_ParameterOutOfRange);
195     }
196 
197     values_.resize(constraint.valuesCount);
198 
199     for (uint32_t i = 0; i < constraint.valuesCount; i++)
200     {
201       assert(constraint.values[i] != NULL);
202       values_[i].assign(constraint.values[i]);
203     }
204   }
205 #endif
206 
207 
GetValue(size_t index) const208   const std::string& DatabaseConstraint::GetValue(size_t index) const
209   {
210     if (index >= values_.size())
211     {
212       throw OrthancException(ErrorCode_ParameterOutOfRange);
213     }
214     else
215     {
216       return values_[index];
217     }
218   }
219 
220 
GetSingleValue() const221   const std::string& DatabaseConstraint::GetSingleValue() const
222   {
223     if (values_.size() != 1)
224     {
225       throw OrthancException(ErrorCode_BadSequenceOfCalls);
226     }
227     else
228     {
229       return values_[0];
230     }
231   }
232 
233 
234 #if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1
EncodeForPlugins(OrthancPluginDatabaseConstraint & constraint,std::vector<const char * > & tmpValues) const235   void DatabaseConstraint::EncodeForPlugins(OrthancPluginDatabaseConstraint& constraint,
236                                             std::vector<const char*>& tmpValues) const
237   {
238     memset(&constraint, 0, sizeof(constraint));
239 
240     tmpValues.resize(values_.size());
241 
242     for (size_t i = 0; i < values_.size(); i++)
243     {
244       tmpValues[i] = values_[i].c_str();
245     }
246 
247     constraint.level = Plugins::Convert(level_);
248     constraint.tagGroup = tag_.GetGroup();
249     constraint.tagElement = tag_.GetElement();
250     constraint.isIdentifierTag = isIdentifier_;
251     constraint.isCaseSensitive = caseSensitive_;
252     constraint.isMandatory = mandatory_;
253     constraint.type = Plugins::Convert(constraintType_);
254     constraint.valuesCount = values_.size();
255     constraint.values = (tmpValues.empty() ? NULL : &tmpValues[0]);
256   }
257 #endif
258 }
259