comparison OrthancServer/Search/Compatibility/ICompatibilityCreateInstance.cpp @ 3080:1a75595d8e44 db-changes

started refactoring of ServerIndex::Store()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 03 Jan 2019 18:21:22 +0100
parents
children 847a0ed92654
comparison
equal deleted inserted replaced
3079:65e2bfa953ef 3080:1a75595d8e44
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-2019 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 "ICompatibilityCreateInstance.h"
36
37 #include "../../../Core/OrthancException.h"
38
39 namespace Orthanc
40 {
41 namespace Compatibility
42 {
43 bool ICompatibilityCreateInstance::Apply(IDatabaseWrapper::CreateInstanceResult& result,
44 int64_t& instanceId,
45 ICompatibilityCreateInstance& compatibility,
46 IDatabaseWrapper& database,
47 const std::string& hashPatient,
48 const std::string& hashStudy,
49 const std::string& hashSeries,
50 const std::string& hashInstance,
51 bool overwrite)
52 {
53 {
54 ResourceType type;
55 int64_t tmp;
56
57 if (database.LookupResource(tmp, type, hashInstance))
58 {
59 assert(type == ResourceType_Instance);
60
61 if (overwrite)
62 {
63 // Overwrite the old instance
64 LOG(INFO) << "Overwriting instance: " << hashInstance;
65 database.DeleteResource(tmp);
66 }
67 else
68 {
69 // Do nothing if the instance already exists
70 instanceId = tmp;
71 return false;
72 }
73 }
74 }
75
76 instanceId = compatibility.CreateResource(hashInstance, ResourceType_Instance);
77
78 result.isNewPatient_ = false;
79 result.isNewStudy_ = false;
80 result.isNewSeries_ = false;
81 result.patientId_ = -1;
82 result.studyId_ = -1;
83 result.seriesId_ = -1;
84
85 // Detect up to which level the patient/study/series/instance
86 // hierarchy must be created
87
88 {
89 ResourceType dummy;
90
91 if (database.LookupResource(result.seriesId_, dummy, hashSeries))
92 {
93 assert(dummy == ResourceType_Series);
94 // The patient, the study and the series already exist
95
96 bool ok = (database.LookupResource(result.patientId_, dummy, hashPatient) &&
97 database.LookupResource(result.studyId_, dummy, hashStudy));
98 assert(ok);
99 }
100 else if (database.LookupResource(result.studyId_, dummy, hashStudy))
101 {
102 assert(dummy == ResourceType_Study);
103
104 // New series: The patient and the study already exist
105 result.isNewSeries_ = true;
106
107 bool ok = database.LookupResource(result.patientId_, dummy, hashPatient);
108 assert(ok);
109 }
110 else if (database.LookupResource(result.patientId_, dummy, hashPatient))
111 {
112 assert(dummy == ResourceType_Patient);
113
114 // New study and series: The patient already exist
115 result.isNewStudy_ = true;
116 result.isNewSeries_ = true;
117 }
118 else
119 {
120 // New patient, study and series: Nothing exists
121 result.isNewPatient_ = true;
122 result.isNewStudy_ = true;
123 result.isNewSeries_ = true;
124 }
125 }
126
127 // Create the series if needed
128 if (result.isNewSeries_)
129 {
130 result.seriesId_ = compatibility.CreateResource(hashSeries, ResourceType_Series);
131 }
132
133 // Create the study if needed
134 if (result.isNewStudy_)
135 {
136 result.studyId_ = compatibility.CreateResource(hashStudy, ResourceType_Study);
137 }
138
139 // Create the patient if needed
140 if (result.isNewPatient_)
141 {
142 result.patientId_ = compatibility.CreateResource(hashPatient, ResourceType_Patient);
143 }
144
145 // Create the parent-to-child links
146 compatibility.AttachChild(result.seriesId_, instanceId);
147
148 if (result.isNewSeries_)
149 {
150 compatibility.AttachChild(result.studyId_, result.seriesId_);
151 }
152
153 if (result.isNewStudy_)
154 {
155 compatibility.AttachChild(result.patientId_, result.studyId_);
156 }
157
158 // Sanity checks
159 assert(result.patientId_ != -1);
160 assert(result.studyId_ != -1);
161 assert(result.seriesId_ != -1);
162 assert(instanceId != -1);
163
164 return true;
165 }
166 }
167 }