Mercurial > hg > orthanc-authorization
comparison Plugin/ResourceHierarchyCache.cpp @ 1:d5d3cb00556a
initial release
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 22 Mar 2017 16:13:52 +0100 |
parents | |
children | c44013681a51 |
comparison
equal
deleted
inserted
replaced
0:decac5df19c4 | 1:d5d3cb00556a |
---|---|
1 /** | |
2 * Advanced authorization plugin for Orthanc | |
3 * Copyright (C) 2017 Osimis, Belgium | |
4 * | |
5 * This program is free software: you can redistribute it and/or | |
6 * modify it under the terms of the GNU Affero General Public License | |
7 * as published by the Free Software Foundation, either version 3 of | |
8 * the License, or (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Affero General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Affero General Public License | |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 **/ | |
18 | |
19 #include "ResourceHierarchyCache.h" | |
20 | |
21 #include "../Resources/Orthanc/Core/Logging.h" | |
22 #include "../Resources/Orthanc/Core/OrthancException.h" | |
23 | |
24 #include <boost/lexical_cast.hpp> | |
25 | |
26 namespace OrthancPlugins | |
27 { | |
28 std::string ResourceHierarchyCache::ComputeKey(Orthanc::ResourceType level, | |
29 const std::string identifier) const | |
30 { | |
31 return boost::lexical_cast<std::string>(level) + "|" + identifier; | |
32 } | |
33 | |
34 | |
35 void ResourceHierarchyCache::LinkParent(const OrthancResource& child, | |
36 const OrthancResource& parent) | |
37 { | |
38 LOG(INFO) << "Linking " << Orthanc::EnumerationToString(child.GetLevel()) | |
39 << " \"" << child.GetIdentifier() << "\" to its parent " | |
40 << Orthanc::EnumerationToString(parent.GetLevel()) | |
41 << " \"" << parent.GetIdentifier() << "\""; | |
42 | |
43 cache_->Store(ComputeKey(child), parent.GetIdentifier(), 0 /* no expiration */); | |
44 } | |
45 | |
46 | |
47 bool ResourceHierarchyCache::LookupParent(std::string& target, | |
48 const OrthancResource& resource) | |
49 { | |
50 std::string key = ComputeKey(resource); | |
51 | |
52 if (cache_->Retrieve(target, key)) | |
53 { | |
54 // The parent was already stored in the cache | |
55 return true; | |
56 } | |
57 | |
58 // Not in the cache, reading the resource from the Orthanc store | |
59 std::string dicomUid; | |
60 OrthancResource parent; | |
61 std::list<OrthancResource> children; | |
62 | |
63 if (!resource.GetHierarchy(dicomUid, parent, children, context_)) | |
64 { | |
65 // The resource is non-existing (*) | |
66 return false; | |
67 } | |
68 | |
69 orthancToDicom_->Store(key, dicomUid, 0 /* no expiration */); | |
70 dicomToOrthanc_->Store(ComputeKey(resource.GetLevel(), dicomUid), | |
71 resource.GetIdentifier(), 0 /* no expiration */); | |
72 | |
73 for (std::list<OrthancResource>::const_iterator | |
74 it = children.begin(); it != children.end(); ++it) | |
75 { | |
76 // Cache the relation of the resource with its children | |
77 LinkParent(*it, resource); | |
78 } | |
79 | |
80 if (parent.IsValid()) | |
81 { | |
82 LinkParent(resource, parent); | |
83 target = parent.GetIdentifier(); | |
84 return true; | |
85 } | |
86 else | |
87 { | |
88 // We reached the patient level, or the resource was removed | |
89 // from Orthanc since (*) | |
90 return false; | |
91 } | |
92 } | |
93 | |
94 | |
95 ResourceHierarchyCache::ResourceHierarchyCache(OrthancPluginContext* context, | |
96 ICacheFactory& factory) : | |
97 context_(context), | |
98 cache_(factory.Create()), | |
99 orthancToDicom_(factory.Create()), | |
100 dicomToOrthanc_(factory.Create()) | |
101 { | |
102 if (cache_.get() == NULL) | |
103 { | |
104 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
105 } | |
106 } | |
107 | |
108 | |
109 void ResourceHierarchyCache::Invalidate(Orthanc::ResourceType level, | |
110 const std::string& identifier) | |
111 { | |
112 LOG(INFO) << "Invalidating " << Orthanc::EnumerationToString(level) | |
113 << " resource with ID: " << identifier; | |
114 | |
115 std::string key = ComputeKey(level, identifier); | |
116 cache_->Invalidate(key); | |
117 orthancToDicom_->Invalidate(key); | |
118 } | |
119 | |
120 | |
121 bool ResourceHierarchyCache::LookupSeries(std::string& patient, | |
122 std::string& study, | |
123 const std::string& series) | |
124 { | |
125 if (LookupParent(study, Orthanc::ResourceType_Series, series)) | |
126 { | |
127 return LookupStudy(patient, study); | |
128 } | |
129 else | |
130 { | |
131 return false; | |
132 } | |
133 } | |
134 | |
135 | |
136 bool ResourceHierarchyCache::LookupInstance(std::string& patient, | |
137 std::string& study, | |
138 std::string& series, | |
139 const std::string& instance) | |
140 { | |
141 if (LookupParent(series, Orthanc::ResourceType_Instance, instance)) | |
142 { | |
143 return LookupSeries(patient, study, series); | |
144 } | |
145 else | |
146 { | |
147 return false; | |
148 } | |
149 } | |
150 | |
151 | |
152 bool ResourceHierarchyCache::LookupDicomUid(std::string& target, | |
153 Orthanc::ResourceType level, | |
154 const std::string& orthancId) | |
155 { | |
156 std::string key = ComputeKey(level, orthancId); | |
157 | |
158 if (orthancToDicom_->Retrieve(target, key)) | |
159 { | |
160 return true; | |
161 } | |
162 | |
163 OrthancResource resource(level, orthancId); | |
164 | |
165 if (resource.GetDicomUid(target, context_)) | |
166 { | |
167 orthancToDicom_->Store(key, target, 0 /* no expiration */); | |
168 return true; | |
169 } | |
170 else | |
171 { | |
172 return false; | |
173 } | |
174 } | |
175 | |
176 | |
177 bool ResourceHierarchyCache::LookupOrthancId(std::string& target, | |
178 Orthanc::ResourceType level, | |
179 const std::string& dicomUid) | |
180 { | |
181 std::string key = ComputeKey(level, dicomUid); | |
182 | |
183 if (dicomToOrthanc_->Retrieve(target, key)) | |
184 { | |
185 return true; | |
186 } | |
187 | |
188 OrthancResource resource(level, dicomUid); | |
189 | |
190 if (OrthancResource::LookupOrthancId(target, context_, level, dicomUid)) | |
191 { | |
192 dicomToOrthanc_->Store(key, target, 0 /* no expiration */); | |
193 return true; | |
194 } | |
195 else | |
196 { | |
197 return false; | |
198 } | |
199 } | |
200 } |