comparison OrthancServer/OrthancMoveRequestHandler.cpp @ 619:70d0f27e5bd3 find-move-scp

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Oct 2013 11:57:30 +0200
parents
children 08eca5d86aad
comparison
equal deleted inserted replaced
618:5ab377df6d8b 619:70d0f27e5bd3
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege,
4 * 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 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32 #include "OrthancMoveRequestHandler.h"
33
34 #include <glog/logging.h>
35
36 #include "DicomProtocol/DicomUserConnection.h"
37 #include "OrthancInitialization.h"
38
39 namespace Orthanc
40 {
41 namespace
42 {
43 // Anonymous namespace to avoid clashes between compilation modules
44
45 class OrthancMoveRequestIterator : public IMoveRequestIterator
46 {
47 private:
48 ServerContext& context_;
49 std::vector<std::string> instances_;
50 DicomUserConnection connection_;
51 size_t position_;
52
53 public:
54 OrthancMoveRequestIterator(ServerContext& context,
55 const std::string& target,
56 const std::string& publicId) :
57 context_(context),
58 position_(0)
59 {
60 LOG(INFO) << "Sending resource " << publicId << " to modality \"" << target << "\"";
61
62 std::list<std::string> tmp;
63 context_.GetIndex().GetChildInstances(tmp, publicId);
64
65 instances_.reserve(tmp.size());
66 for (std::list<std::string>::iterator it = tmp.begin(); it != tmp.end(); it++)
67 {
68 instances_.push_back(*it);
69 }
70
71 ConnectToModalityUsingAETitle(connection_, target);
72 }
73
74 virtual unsigned int GetSubOperationCount() const
75 {
76 return instances_.size();
77 }
78
79 virtual Status DoNext()
80 {
81 if (position_ >= instances_.size())
82 {
83 return Status_Failure;
84 }
85
86 const std::string& id = instances_[position_++];
87
88 std::string dicom;
89 context_.ReadFile(dicom, id, FileContentType_Dicom);
90 connection_.Store(dicom);
91
92 return Status_Success;
93 }
94 };
95 }
96
97
98 bool OrthancMoveRequestHandler::LookupResource(std::string& publicId,
99 DicomTag tag,
100 const DicomMap& input)
101 {
102 if (!input.HasTag(tag))
103 {
104 return false;
105 }
106
107 std::string value = input.GetValue(tag).AsString();
108
109 std::list<std::string> ids;
110 context_.GetIndex().LookupTagValue(ids, tag, value);
111
112 if (ids.size() != 1)
113 {
114 return false;
115 }
116 else
117 {
118 publicId = ids.front();
119 return true;
120 }
121 }
122
123
124 IMoveRequestIterator* OrthancMoveRequestHandler::Handle(const std::string& target,
125 const DicomMap& input)
126 {
127 LOG(WARNING) << "Move-SCU request received for AET \"" << target << "\"";
128
129
130 /**
131 * Retrieve the query level.
132 **/
133
134 const DicomValue* levelTmp = input.TestAndGetValue(DICOM_TAG_QUERY_RETRIEVE_LEVEL);
135 if (levelTmp == NULL)
136 {
137 throw OrthancException(ErrorCode_BadRequest);
138 }
139
140 ResourceType level = StringToResourceType(levelTmp->AsString().c_str());
141
142
143 /**
144 * Lookup for the resource to be sent.
145 **/
146
147 bool ok;
148 std::string publicId;
149
150 switch (level)
151 {
152 case ResourceType_Patient:
153 ok = LookupResource(publicId, DICOM_TAG_PATIENT_ID, input);
154 break;
155
156 case ResourceType_Study:
157 ok = LookupResource(publicId, DICOM_TAG_STUDY_INSTANCE_UID, input);
158 break;
159
160 case ResourceType_Series:
161 ok = LookupResource(publicId, DICOM_TAG_SERIES_INSTANCE_UID, input);
162 break;
163
164 case ResourceType_Instance:
165 ok = LookupResource(publicId, DICOM_TAG_SOP_INSTANCE_UID, input);
166 break;
167
168 default:
169 ok = false;
170 }
171
172 if (!ok)
173 {
174 throw OrthancException(ErrorCode_BadRequest);
175 }
176
177 return new OrthancMoveRequestIterator(context_, target, publicId);
178 }
179 }