comparison OrthancServer/ServerJobs/Operations/ModifyInstanceOperation.cpp @ 2641:3ce8863398ab jobs

move
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 28 May 2018 16:39:00 +0200
parents OrthancServer/ServerJobs/ModifyInstanceOperation.cpp@c691fcf66071
children 761031029aa9
comparison
equal deleted inserted replaced
2640:c691fcf66071 2641:3ce8863398ab
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../../PrecompiledHeadersServer.h"
35 #include "ModifyInstanceOperation.h"
36
37 #include "DicomInstanceOperationValue.h"
38
39 #include "../../../Core/Logging.h"
40
41 namespace Orthanc
42 {
43 ModifyInstanceOperation::ModifyInstanceOperation(ServerContext& context,
44 RequestOrigin origin,
45 DicomModification* modification) :
46 context_(context),
47 origin_(origin),
48 modification_(modification)
49 {
50 if (modification == NULL)
51 {
52 throw OrthancException(ErrorCode_NullPointer);
53 }
54
55 modification_->SetAllowManualIdentifiers(true);
56
57 if (modification_->IsReplaced(DICOM_TAG_PATIENT_ID))
58 {
59 modification_->SetLevel(ResourceType_Patient);
60 }
61 else if (modification_->IsReplaced(DICOM_TAG_STUDY_INSTANCE_UID))
62 {
63 modification_->SetLevel(ResourceType_Study);
64 }
65 else if (modification_->IsReplaced(DICOM_TAG_SERIES_INSTANCE_UID))
66 {
67 modification_->SetLevel(ResourceType_Series);
68 }
69 else
70 {
71 modification_->SetLevel(ResourceType_Instance);
72 }
73
74 if (origin_ != RequestOrigin_Lua)
75 {
76 // TODO If issued from HTTP, "remoteIp" and "username" must be provided
77 throw OrthancException(ErrorCode_NotImplemented);
78 }
79 }
80
81 void ModifyInstanceOperation::Apply(JobOperationValues& outputs,
82 const JobOperationValue& input,
83 IDicomConnectionManager& connectionManager)
84 {
85 if (input.GetType() != JobOperationValue::Type_DicomInstance)
86 {
87 throw OrthancException(ErrorCode_BadParameterType);
88 }
89
90 const DicomInstanceOperationValue& instance =
91 dynamic_cast<const DicomInstanceOperationValue&>(input);
92
93 LOG(INFO) << "Lua: Modifying instance " << instance.GetId();
94
95 std::auto_ptr<ParsedDicomFile> modified;
96
97 {
98 ServerContext::DicomCacheLocker lock(context_, instance.GetId());
99 modified.reset(lock.GetDicom().Clone(true));
100 }
101
102 try
103 {
104 modification_->Apply(*modified);
105
106 DicomInstanceToStore toStore;
107 assert(origin_ == RequestOrigin_Lua);
108 toStore.GetOrigin().SetLuaOrigin();
109 toStore.SetParsedDicomFile(*modified);
110
111 // TODO other metadata
112 toStore.AddMetadata(ResourceType_Instance, MetadataType_ModifiedFrom, instance.GetId());
113
114 std::string modifiedId;
115 context_.Store(modifiedId, toStore);
116
117 // Only chain with other commands if this command succeeds
118 outputs.Append(new DicomInstanceOperationValue(instance.GetServerContext(), modifiedId));
119 }
120 catch (OrthancException& e)
121 {
122 LOG(ERROR) << "Lua: Unable to modify instance " << instance.GetId()
123 << ": " << e.What();
124 }
125 }
126
127
128 void ModifyInstanceOperation::Serialize(Json::Value& target) const
129 {
130 target["Type"] = "ModifyInstance";
131 target["Origin"] = EnumerationToString(origin_);
132 modification_->Serialize(target["Modification"]);
133 }
134 }
135