annotate OrthancStone/Sources/Toolbox/OrthancDatasets/FullOrthancDataset.cpp @ 1592:0d4b11ba86df

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 23 Oct 2020 15:15:32 +0200
parents 244ad1e4e76a
children 4fb8fdf03314
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1504
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "FullOrthancDataset.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include <OrthancException.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include <stdio.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <cassert>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 namespace OrthancStone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 static const Json::Value* AccessTag(const Json::Value& dataset,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 const Orthanc::DicomTag& tag)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 if (dataset.type() != Json::objectValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 char name[16];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 sprintf(name, "%04x,%04x", tag.GetGroup(), tag.GetElement());
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 if (!dataset.isMember(name))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 return NULL;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 const Json::Value& value = dataset[name];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 if (value.type() != Json::objectValue ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 !value.isMember("Name") ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 !value.isMember("Type") ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 !value.isMember("Value") ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 value["Name"].type() != Json::stringValue ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 value["Type"].type() != Json::stringValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 return &value;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 static const Json::Value& GetSequenceContent(const Json::Value& sequence)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 assert(sequence.type() == Json::objectValue);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 assert(sequence.isMember("Type"));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 assert(sequence.isMember("Value"));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 const Json::Value& value = sequence["Value"];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 if (sequence["Type"].asString() != "Sequence" ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 value.type() != Json::arrayValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 return value;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 static bool GetStringInternal(std::string& result,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 const Json::Value& tag)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 assert(tag.type() == Json::objectValue);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 assert(tag.isMember("Type"));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 assert(tag.isMember("Value"));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 const Json::Value& value = tag["Value"];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 if (tag["Type"].asString() != "String" ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 value.type() != Json::stringValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 result = value.asString();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 return true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 const Json::Value* FullOrthancDataset::LookupPath(const DicomPath& path) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 const Json::Value* content = &root_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 for (unsigned int depth = 0; depth < path.GetPrefixLength(); depth++)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 const Json::Value* sequence = AccessTag(*content, path.GetPrefixTag(depth));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 if (sequence == NULL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 return NULL;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 const Json::Value& nextContent = GetSequenceContent(*sequence);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 size_t index = path.GetPrefixIndex(depth);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 if (index >= nextContent.size())
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 return NULL;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 content = &nextContent[static_cast<Json::Value::ArrayIndex>(index)];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 return AccessTag(*content, path.GetFinalTag());
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 void FullOrthancDataset::CheckRoot() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 if (root_.type() != Json::objectValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 FullOrthancDataset::FullOrthancDataset(IOrthancConnection& orthanc,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 const std::string& uri)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 IOrthancConnection::RestApiGet(root_, orthanc, uri);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 CheckRoot();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 FullOrthancDataset::FullOrthancDataset(const std::string& content)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 IOrthancConnection::ParseJson(root_, content);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 CheckRoot();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 FullOrthancDataset::FullOrthancDataset(const void* content,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 size_t size)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 IOrthancConnection::ParseJson(root_, content, size);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 CheckRoot();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 FullOrthancDataset::FullOrthancDataset(const Json::Value& root) :
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 root_(root)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 CheckRoot();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 bool FullOrthancDataset::GetStringValue(std::string& result,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 const DicomPath& path) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 const Json::Value* value = LookupPath(path);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 if (value == NULL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 return false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 return GetStringInternal(result, *value);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 bool FullOrthancDataset::GetSequenceSize(size_t& size,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 const DicomPath& path) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 const Json::Value* sequence = LookupPath(path);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 if (sequence == NULL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 return false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 size = GetSequenceContent(*sequence).size();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 return true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 }