Mercurial > hg > orthanc
annotate Plugins/Samples/ModalityWorklists/Plugin.cpp @ 2215:028214a95194
refactoring the worklist sample
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 10 Dec 2016 11:41:17 +0100 |
parents | b1d93286b315 |
children | 9a8fab016145 |
rev | line source |
---|---|
1802 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1802 | 4 * Department, University Hospital of Liege, Belgium |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 **/ | |
19 | |
20 | |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
21 #include "../Common/OrthancPluginCppWrapper.h" |
1802 | 22 |
23 #include <boost/filesystem.hpp> | |
24 #include <json/value.h> | |
25 #include <json/reader.h> | |
26 #include <string.h> | |
27 #include <iostream> | |
1814 | 28 #include <algorithm> |
1802 | 29 |
30 static OrthancPluginContext* context_ = NULL; | |
31 static std::string folder_; | |
32 | |
33 | |
34 /** | |
35 * This is the main function for matching a DICOM worklist against a query. | |
36 **/ | |
2214 | 37 static void MatchWorklist(OrthancPluginWorklistAnswers* answers, |
38 const OrthancPluginWorklistQuery* query, | |
39 const OrthancPlugins::FindMatcher& matcher, | |
2206
27106f7e3759
more refactoring of the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
40 const std::string& path) |
1802 | 41 { |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
42 OrthancPlugins::MemoryBuffer dicom(context_); |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
43 dicom.ReadFile(path); |
1802 | 44 |
2214 | 45 if (matcher.IsMatch(dicom)) |
1802 | 46 { |
47 // This DICOM file matches the worklist query, add it to the answers | |
2206
27106f7e3759
more refactoring of the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
48 OrthancPluginErrorCode code = OrthancPluginWorklistAddAnswer |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
49 (context_, answers, query, dicom.GetData(), dicom.GetSize()); |
1802 | 50 |
2206
27106f7e3759
more refactoring of the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
51 if (code != OrthancPluginErrorCode_Success) |
27106f7e3759
more refactoring of the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
52 { |
27106f7e3759
more refactoring of the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
53 OrthancPlugins::LogError(context_, "Error while adding an answer to a worklist request"); |
27106f7e3759
more refactoring of the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
54 ORTHANC_PLUGINS_THROW_EXCEPTION(code); |
27106f7e3759
more refactoring of the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
55 } |
27106f7e3759
more refactoring of the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
56 } |
1814 | 57 } |
58 | |
59 | |
2215
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
60 OrthancPlugins::FindMatcher* CreateMatcher(const OrthancPluginWorklistQuery* query, |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
61 const char* remoteAet) |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
62 { |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
63 OrthancPlugins::MemoryBuffer dicom(context_); |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
64 dicom.GetDicomQuery(query); |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
65 |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
66 { |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
67 Json::Value json; |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
68 dicom.DicomToJson(json, OrthancPluginDicomToJsonFormat_Short, |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
69 static_cast<OrthancPluginDicomToJsonFlags>(0), 0); |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
70 |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
71 OrthancPlugins::LogInfo(context_, "Received worklist query from remote modality " + |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
72 std::string(remoteAet) + ":\n" + json.toStyledString()); |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
73 } |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
74 |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
75 return new OrthancPlugins::FindMatcher(context_, query); |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
76 //return new OrthancPlugins::FindMatcher(context_, dicom); |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
77 } |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
78 |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
79 |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
80 |
1802 | 81 OrthancPluginErrorCode Callback(OrthancPluginWorklistAnswers* answers, |
82 const OrthancPluginWorklistQuery* query, | |
83 const char* remoteAet, | |
84 const char* calledAet) | |
85 { | |
86 try | |
87 { | |
2215
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
88 // Construct an object to match the worklists in the database against the C-Find query |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
89 std::auto_ptr<OrthancPlugins::FindMatcher> matcher(CreateMatcher(query, remoteAet)); |
1911 | 90 |
2215
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
91 // Loop over the regular files in the database folder |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
92 namespace fs = boost::filesystem; |
2214 | 93 |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
94 fs::path source(folder_); |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
95 fs::directory_iterator end; |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
96 |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
97 try |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
98 { |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
99 for (fs::directory_iterator it(source); it != end; ++it) |
1802 | 100 { |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
101 fs::file_type type(it->status().type()); |
1814 | 102 |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
103 if (type == fs::regular_file || |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
104 type == fs::reparse_file) // cf. BitBucket issue #11 |
1802 | 105 { |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
106 std::string extension = fs::extension(it->path()); |
2206
27106f7e3759
more refactoring of the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
107 std::transform(extension.begin(), extension.end(), extension.begin(), tolower); // Convert to lowercase |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
108 |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
109 if (extension == ".wl") |
1802 | 110 { |
2215
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
111 // We found a worklist (i.e. a DICOM find with extension ".wl"), match it against the query |
028214a95194
refactoring the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2214
diff
changeset
|
112 MatchWorklist(answers, query, *matcher, it->path().string()); |
1802 | 113 } |
114 } | |
115 } | |
116 } | |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
117 catch (fs::filesystem_error&) |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
118 { |
2206
27106f7e3759
more refactoring of the worklist sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
119 OrthancPlugins::LogError(context_, "Inexistent folder while scanning for worklists: " + source.string()); |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
120 return OrthancPluginErrorCode_DirectoryExpected; |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
121 } |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
122 |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
123 // Uncomment the following line if too many answers are to be returned |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
124 // OrthancPluginMarkWorklistAnswersIncomplete(context_, answers); |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
125 |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
126 return OrthancPluginErrorCode_Success; |
1802 | 127 } |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
128 catch (OrthancPlugins::PluginException& e) |
1802 | 129 { |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
130 return e.GetErrorCode(); |
1802 | 131 } |
132 } | |
133 | |
134 | |
135 extern "C" | |
136 { | |
137 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c) | |
138 { | |
139 context_ = c; | |
140 | |
141 /* Check the version of the Orthanc core */ | |
142 if (OrthancPluginCheckVersion(c) == 0) | |
143 { | |
144 char info[1024]; | |
145 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin", | |
146 context_->orthancVersion, | |
147 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, | |
148 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, | |
149 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); | |
150 OrthancPluginLogError(context_, info); | |
151 return -1; | |
152 } | |
153 | |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
154 OrthancPlugins::LogWarning(context_, "Sample worklist plugin is initializing"); |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
155 OrthancPluginSetDescription(context_, "Serve DICOM modality worklists from a folder with Orthanc."); |
1802 | 156 |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
157 OrthancPlugins::OrthancConfiguration configuration(context_); |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
158 |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
159 OrthancPlugins::OrthancConfiguration worklists; |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
160 configuration.GetSection(worklists, "Worklists"); |
1808
9c2ffc4e938b
configuration of the sample modality worklists plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1802
diff
changeset
|
161 |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
162 bool enabled = worklists.GetBooleanValue("Enable", false); |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
163 if (enabled) |
1802 | 164 { |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
165 if (worklists.LookupStringValue(folder_, "Database")) |
1802 | 166 { |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
167 OrthancPlugins::LogWarning(context_, "The database of worklists will be read from folder: " + folder_); |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
168 OrthancPluginRegisterWorklistCallback(context_, Callback); |
1802 | 169 } |
1808
9c2ffc4e938b
configuration of the sample modality worklists plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1802
diff
changeset
|
170 else |
9c2ffc4e938b
configuration of the sample modality worklists plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1802
diff
changeset
|
171 { |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
172 OrthancPlugins::LogError(context_, "The configuration option \"Worklists.Database\" must contain a path"); |
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
173 return -1; |
1808
9c2ffc4e938b
configuration of the sample modality worklists plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1802
diff
changeset
|
174 } |
1802 | 175 } |
176 else | |
177 { | |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
178 OrthancPlugins::LogWarning(context_, "Worklists server is disabled by the configuration file"); |
1808
9c2ffc4e938b
configuration of the sample modality worklists plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1802
diff
changeset
|
179 } |
1802 | 180 |
181 return 0; | |
182 } | |
183 | |
184 | |
185 ORTHANC_PLUGINS_API void OrthancPluginFinalize() | |
186 { | |
187 OrthancPluginLogWarning(context_, "Sample worklist plugin is finalizing"); | |
188 } | |
189 | |
190 | |
191 ORTHANC_PLUGINS_API const char* OrthancPluginGetName() | |
192 { | |
1808
9c2ffc4e938b
configuration of the sample modality worklists plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1802
diff
changeset
|
193 return "worklists"; |
1802 | 194 } |
195 | |
196 | |
197 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion() | |
198 { | |
1808
9c2ffc4e938b
configuration of the sample modality worklists plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1802
diff
changeset
|
199 return MODALITY_WORKLISTS_VERSION; |
1802 | 200 } |
201 } |