comparison Plugins/Samples/ModalityWorklists/Plugin.cpp @ 2206:27106f7e3759

more refactoring of the worklist sample
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 08 Dec 2016 16:29:34 +0100
parents 395522e46b2b
children b1d93286b315
comparison
equal deleted inserted replaced
2205:395522e46b2b 2206:27106f7e3759
32 32
33 33
34 /** 34 /**
35 * This is the main function for matching a DICOM worklist against a query. 35 * This is the main function for matching a DICOM worklist against a query.
36 **/ 36 **/
37 static OrthancPluginErrorCode MatchWorklist(OrthancPluginWorklistAnswers* answers, 37 static void MatchWorklist(OrthancPluginWorklistAnswers* answers,
38 const OrthancPluginWorklistQuery* query, 38 const OrthancPluginWorklistQuery* query,
39 const std::string& path) 39 const std::string& path)
40 { 40 {
41 OrthancPlugins::MemoryBuffer dicom(context_); 41 OrthancPlugins::MemoryBuffer dicom(context_);
42 dicom.ReadFile(path); 42 dicom.ReadFile(path);
43 43
44 if (OrthancPluginWorklistIsMatch(context_, query, dicom.GetData(), dicom.GetSize())) 44 if (OrthancPluginWorklistIsMatch(context_, query, dicom.GetData(), dicom.GetSize()))
45 { 45 {
46 // This DICOM file matches the worklist query, add it to the answers 46 // This DICOM file matches the worklist query, add it to the answers
47 return OrthancPluginWorklistAddAnswer 47 OrthancPluginErrorCode code = OrthancPluginWorklistAddAnswer
48 (context_, answers, query, dicom.GetData(), dicom.GetSize()); 48 (context_, answers, query, dicom.GetData(), dicom.GetSize());
49
50 if (code != OrthancPluginErrorCode_Success)
51 {
52 OrthancPlugins::LogError(context_, "Error while adding an answer to a worklist request");
53 ORTHANC_PLUGINS_THROW_EXCEPTION(code);
54 }
49 } 55 }
50 else
51 {
52 // This DICOM file does not match
53 return OrthancPluginErrorCode_Success;
54 }
55 }
56
57
58
59 static void GetQueryDicom(Json::Value& value,
60 const OrthancPluginWorklistQuery* query)
61 {
62 OrthancPlugins::MemoryBuffer dicom(context_);
63 dicom.GetDicomQuery(query);
64
65 OrthancPlugins::OrthancString str(context_);
66 str.DicomToJson(dicom, OrthancPluginDicomToJsonFormat_Short,
67 static_cast<OrthancPluginDicomToJsonFlags>(0), 0);
68
69 str.ToJson(value);
70 }
71
72
73 static void ToLowerCase(std::string& s)
74 {
75 std::transform(s.begin(), s.end(), s.begin(), tolower);
76 } 56 }
77 57
78 58
79 OrthancPluginErrorCode Callback(OrthancPluginWorklistAnswers* answers, 59 OrthancPluginErrorCode Callback(OrthancPluginWorklistAnswers* answers,
80 const OrthancPluginWorklistQuery* query, 60 const OrthancPluginWorklistQuery* query,
84 try 64 try
85 { 65 {
86 namespace fs = boost::filesystem; 66 namespace fs = boost::filesystem;
87 67
88 { 68 {
69 OrthancPlugins::MemoryBuffer dicom(context_);
70 dicom.GetDicomQuery(query);
71
89 Json::Value json; 72 Json::Value json;
90 GetQueryDicom(json, query); 73 dicom.DicomToJson(json, OrthancPluginDicomToJsonFormat_Short,
74 static_cast<OrthancPluginDicomToJsonFlags>(0), 0);
91 75
92 std::string msg = ("Received worklist query from remote modality " + 76 OrthancPlugins::LogInfo(context_, "Received worklist query from remote modality " +
93 std::string(remoteAet) + ":\n" + json.toStyledString()); 77 std::string(remoteAet) + ":\n" + json.toStyledString());
94 OrthancPluginLogInfo(context_, msg.c_str());
95 } 78 }
96 79
97 fs::path source(folder_); 80 fs::path source(folder_);
98 fs::directory_iterator end; 81 fs::directory_iterator end;
99 82
105 88
106 if (type == fs::regular_file || 89 if (type == fs::regular_file ||
107 type == fs::reparse_file) // cf. BitBucket issue #11 90 type == fs::reparse_file) // cf. BitBucket issue #11
108 { 91 {
109 std::string extension = fs::extension(it->path()); 92 std::string extension = fs::extension(it->path());
110 ToLowerCase(extension); 93 std::transform(extension.begin(), extension.end(), extension.begin(), tolower); // Convert to lowercase
111 94
112 if (extension == ".wl") 95 if (extension == ".wl")
113 { 96 {
114 OrthancPluginErrorCode error = MatchWorklist(answers, query, it->path().string()); 97 MatchWorklist(answers, query, it->path().string());
115 if (error)
116 {
117 OrthancPluginLogError(context_, "Error while adding an answer to a worklist request");
118 return error;
119 }
120 } 98 }
121 } 99 }
122 } 100 }
123 } 101 }
124 catch (fs::filesystem_error&) 102 catch (fs::filesystem_error&)
125 { 103 {
126 std::string description = std::string("Inexistent folder while scanning for worklists: ") + source.string(); 104 OrthancPlugins::LogError(context_, "Inexistent folder while scanning for worklists: " + source.string());
127 OrthancPluginLogError(context_, description.c_str());
128 return OrthancPluginErrorCode_DirectoryExpected; 105 return OrthancPluginErrorCode_DirectoryExpected;
129 } 106 }
130 107
131 // Uncomment the following line if too many answers are to be returned 108 // Uncomment the following line if too many answers are to be returned
132 // OrthancPluginMarkWorklistAnswersIncomplete(context_, answers); 109 // OrthancPluginMarkWorklistAnswersIncomplete(context_, answers);