0
|
1 /**
|
62
|
2 * Orthanc - A Lightweight, RESTful DICOM Store
|
0
|
3 * Copyright (C) 2012 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 * 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
|
|
21 #include "ServerIndex.h"
|
|
22
|
62
|
23 using namespace Orthanc;
|
0
|
24
|
6
|
25 #ifndef NOMINMAX
|
2
|
26 #define NOMINMAX
|
6
|
27 #endif
|
|
28
|
8
|
29 #include "EmbeddedResources.h"
|
0
|
30 #include "../Core/Toolbox.h"
|
|
31 #include "../Core/Uuid.h"
|
|
32 #include "../Core/DicomFormat/DicomArray.h"
|
|
33 #include "../Core/SQLite/Transaction.h"
|
|
34 #include "FromDcmtkBridge.h"
|
|
35
|
|
36 #include <boost/lexical_cast.hpp>
|
|
37 #include <stdio.h>
|
|
38
|
62
|
39 namespace Orthanc
|
0
|
40 {
|
|
41 namespace Internals
|
|
42 {
|
|
43 class DeleteFromFileStorageFunction : public SQLite::IScalarFunction
|
|
44 {
|
|
45 private:
|
|
46 FileStorage fileStorage_;
|
|
47
|
|
48 public:
|
|
49 DeleteFromFileStorageFunction(const std::string& path) :
|
|
50 fileStorage_(path)
|
|
51 {
|
|
52 }
|
|
53
|
|
54 virtual const char* GetName() const
|
|
55 {
|
|
56 return "DeleteFromFileStorage";
|
|
57 }
|
|
58
|
|
59 virtual unsigned int GetCardinality() const
|
|
60 {
|
|
61 return 1;
|
|
62 }
|
|
63
|
|
64 virtual void Compute(SQLite::FunctionContext& context)
|
|
65 {
|
|
66 std::string fileUuid = context.GetStringValue(0);
|
|
67 printf("Removing file [%s]\n", fileUuid.c_str());
|
|
68 if (Toolbox::IsUuid(fileUuid))
|
|
69 {
|
|
70 fileStorage_.Remove(fileUuid);
|
|
71 }
|
|
72 }
|
|
73 };
|
|
74
|
|
75
|
|
76 class SignalDeletedLevelFunction : public SQLite::IScalarFunction
|
|
77 {
|
|
78 private:
|
|
79 int remainingLevel_;
|
|
80 std::string remainingLevelUuid_;
|
|
81
|
|
82 public:
|
|
83 void Clear()
|
|
84 {
|
|
85 remainingLevel_ = std::numeric_limits<int>::max();
|
|
86 }
|
|
87
|
|
88 bool HasRemainingLevel() const
|
|
89 {
|
|
90 return (remainingLevel_ != 0 &&
|
|
91 remainingLevel_ != std::numeric_limits<int>::max());
|
|
92 }
|
|
93
|
|
94 const std::string& GetRemainingLevelUuid() const
|
|
95 {
|
|
96 assert(HasRemainingLevel());
|
|
97 return remainingLevelUuid_;
|
|
98 }
|
|
99
|
|
100 const char* GetRemainingLevelType() const
|
|
101 {
|
|
102 assert(HasRemainingLevel());
|
|
103 switch (remainingLevel_)
|
|
104 {
|
|
105 case 1:
|
|
106 return "patient";
|
|
107 case 2:
|
|
108 return "study";
|
|
109 case 3:
|
|
110 return "series";
|
|
111 default:
|
62
|
112 throw OrthancException(ErrorCode_InternalError);
|
0
|
113 }
|
|
114 }
|
|
115
|
|
116 virtual const char* GetName() const
|
|
117 {
|
|
118 return "SignalDeletedLevel";
|
|
119 }
|
|
120
|
|
121 virtual unsigned int GetCardinality() const
|
|
122 {
|
|
123 return 2;
|
|
124 }
|
|
125
|
|
126 virtual void Compute(SQLite::FunctionContext& context)
|
|
127 {
|
|
128 int level = context.GetIntValue(0);
|
|
129 if (level < remainingLevel_)
|
|
130 {
|
|
131 remainingLevel_ = level;
|
|
132 remainingLevelUuid_ = context.GetStringValue(1);
|
|
133 }
|
|
134
|
|
135 //printf("deleted level [%d] [%s]\n", level, context.GetStringValue(1).c_str());
|
|
136 }
|
|
137 };
|
|
138 }
|
|
139
|
|
140
|
|
141 void ServerIndex::StoreMainDicomTags(const std::string& uuid,
|
|
142 const DicomMap& map)
|
|
143 {
|
|
144 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO MainDicomTags VALUES(?, ?, ?, ?)");
|
|
145
|
|
146 DicomArray flattened(map);
|
|
147 for (size_t i = 0; i < flattened.GetSize(); i++)
|
|
148 {
|
|
149 s.Reset();
|
|
150 s.BindString(0, uuid);
|
|
151 s.BindInt(1, flattened.GetElement(i).GetTag().GetGroup());
|
|
152 s.BindInt(2, flattened.GetElement(i).GetTag().GetElement());
|
|
153 s.BindString(3, flattened.GetElement(i).GetValue().AsString());
|
|
154 s.Run();
|
|
155 }
|
|
156 }
|
|
157
|
|
158 bool ServerIndex::GetMainDicomStringTag(std::string& result,
|
|
159 const std::string& uuid,
|
|
160 const DicomTag& tag)
|
|
161 {
|
|
162 SQLite::Statement s(db_, SQLITE_FROM_HERE,
|
|
163 "SELECT * FROM MainDicomTags WHERE uuid=? AND tagGroup=? AND tagElement=?");
|
|
164 s.BindString(0, uuid);
|
|
165 s.BindInt(1, tag.GetGroup());
|
|
166 s.BindInt(2, tag.GetElement());
|
|
167 if (!s.Step())
|
|
168 {
|
|
169 return false;
|
|
170 }
|
|
171
|
|
172 result = s.ColumnString(0);
|
|
173 return true;
|
|
174 }
|
|
175
|
|
176 bool ServerIndex::GetMainDicomIntTag(int& result,
|
|
177 const std::string& uuid,
|
|
178 const DicomTag& tag)
|
|
179 {
|
|
180 std::string s;
|
|
181 if (!GetMainDicomStringTag(s, uuid, tag))
|
|
182 {
|
|
183 return false;
|
|
184 }
|
|
185
|
|
186 try
|
|
187 {
|
|
188 result = boost::lexical_cast<int>(s);
|
|
189 return true;
|
|
190 }
|
|
191 catch (boost::bad_lexical_cast)
|
|
192 {
|
|
193 return false;
|
|
194 }
|
|
195 }
|
|
196
|
|
197 bool ServerIndex::HasInstance(std::string& instanceUuid,
|
|
198 const std::string& dicomInstance)
|
|
199 {
|
|
200 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Instances WHERE dicomInstance=?");
|
|
201 s.BindString(0, dicomInstance);
|
|
202 if (s.Step())
|
|
203 {
|
|
204 instanceUuid = s.ColumnString(0);
|
|
205 return true;
|
|
206 }
|
|
207 else
|
|
208 {
|
|
209 return false;
|
|
210 }
|
|
211 }
|
|
212
|
|
213
|
|
214 void ServerIndex::RecordChange(const std::string& resourceType,
|
|
215 const std::string& uuid)
|
|
216 {
|
|
217 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Changes VALUES(NULL, ?, ?)");
|
|
218 s.BindString(0, resourceType);
|
|
219 s.BindString(1, uuid);
|
|
220 s.Run();
|
|
221 }
|
|
222
|
|
223
|
|
224 std::string ServerIndex::CreateInstance(const std::string& parentSeriesUuid,
|
|
225 const std::string& dicomInstance,
|
|
226 const DicomMap& dicomSummary,
|
|
227 const std::string& fileUuid,
|
|
228 uint64_t fileSize,
|
|
229 const std::string& jsonUuid,
|
|
230 const std::string& distantAet)
|
|
231 {
|
|
232 std::string instanceUuid = Toolbox::GenerateUuid();
|
|
233
|
82
|
234 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(?, ?)");
|
|
235 s2.BindString(0, instanceUuid);
|
|
236 s2.BindInt(1, ResourceType_Instance);
|
|
237 s2.Run();
|
|
238
|
80
|
239 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Instances VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
|
0
|
240 s.BindString(0, instanceUuid);
|
|
241 s.BindString(1, parentSeriesUuid);
|
|
242 s.BindString(2, dicomInstance);
|
|
243 s.BindString(3, fileUuid);
|
|
244 s.BindInt64(4, fileSize);
|
|
245 s.BindString(5, jsonUuid);
|
|
246 s.BindString(6, distantAet);
|
80
|
247
|
|
248 const DicomValue* indexInSeries;
|
|
249 if ((indexInSeries = dicomSummary.TestAndGetValue(DICOM_TAG_INSTANCE_NUMBER)) != NULL ||
|
|
250 (indexInSeries = dicomSummary.TestAndGetValue(DICOM_TAG_IMAGE_INDEX)) != NULL)
|
|
251 {
|
|
252 s.BindInt(7, boost::lexical_cast<unsigned int>(indexInSeries->AsString()));
|
|
253 }
|
|
254 else
|
|
255 {
|
|
256 s.BindNull(7);
|
|
257 }
|
|
258
|
0
|
259 s.Run();
|
|
260
|
|
261 RecordChange("instances", instanceUuid);
|
|
262
|
|
263 DicomMap dicom;
|
|
264 dicomSummary.ExtractInstanceInformation(dicom);
|
|
265 StoreMainDicomTags(instanceUuid, dicom);
|
|
266
|
|
267 return instanceUuid;
|
|
268 }
|
|
269
|
|
270 void ServerIndex::RemoveInstance(const std::string& uuid)
|
|
271 {
|
|
272 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Instances WHERE uuid=?");
|
|
273 s.BindString(0, uuid);
|
|
274 s.Run();
|
|
275 }
|
|
276
|
|
277 bool ServerIndex::HasSeries(std::string& seriesUuid,
|
|
278 const std::string& dicomSeries)
|
|
279 {
|
|
280 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Series WHERE dicomSeries=?");
|
|
281 s.BindString(0, dicomSeries);
|
|
282 if (s.Step())
|
|
283 {
|
|
284 seriesUuid = s.ColumnString(0);
|
|
285 return true;
|
|
286 }
|
|
287 else
|
|
288 {
|
|
289 return false;
|
|
290 }
|
|
291 }
|
|
292
|
|
293 std::string ServerIndex::CreateSeries(const std::string& parentStudyUuid,
|
|
294 const std::string& dicomSeries,
|
|
295 const DicomMap& dicomSummary)
|
|
296 {
|
|
297 std::string seriesUuid = Toolbox::GenerateUuid();
|
|
298
|
82
|
299 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(?, ?)");
|
|
300 s2.BindString(0, seriesUuid);
|
|
301 s2.BindInt(1, ResourceType_Series);
|
|
302 s2.Run();
|
|
303
|
77
|
304 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Series VALUES(?, ?, ?, ?)");
|
0
|
305 s.BindString(0, seriesUuid);
|
|
306 s.BindString(1, parentStudyUuid);
|
|
307 s.BindString(2, dicomSeries);
|
80
|
308
|
|
309 const DicomValue* expectedNumberOfInstances;
|
84
|
310 if (//(expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_NUMBER_OF_FRAMES)) != NULL ||
|
80
|
311 (expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_NUMBER_OF_SLICES)) != NULL ||
|
82
|
312 //(expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_CARDIAC_NUMBER_OF_IMAGES)) != NULL ||
|
80
|
313 (expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_IMAGES_IN_ACQUISITION)) != NULL)
|
|
314 {
|
|
315 s.BindInt(3, boost::lexical_cast<unsigned int>(expectedNumberOfInstances->AsString()));
|
|
316 }
|
|
317 else
|
|
318 {
|
|
319 s.BindNull(3);
|
|
320 }
|
|
321
|
0
|
322 s.Run();
|
|
323
|
|
324 RecordChange("series", seriesUuid);
|
|
325
|
|
326 DicomMap dicom;
|
|
327 dicomSummary.ExtractSeriesInformation(dicom);
|
|
328 StoreMainDicomTags(seriesUuid, dicom);
|
|
329
|
|
330 return seriesUuid;
|
|
331 }
|
|
332
|
|
333 bool ServerIndex::HasStudy(std::string& studyUuid,
|
|
334 const std::string& dicomStudy)
|
|
335 {
|
|
336 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Studies WHERE dicomStudy=?");
|
|
337 s.BindString(0, dicomStudy);
|
|
338 if (s.Step())
|
|
339 {
|
|
340 studyUuid = s.ColumnString(0);
|
|
341 return true;
|
|
342 }
|
|
343 else
|
|
344 {
|
|
345 return false;
|
|
346 }
|
|
347 }
|
|
348
|
|
349 std::string ServerIndex::CreateStudy(const std::string& parentPatientUuid,
|
|
350 const std::string& dicomStudy,
|
|
351 const DicomMap& dicomSummary)
|
|
352 {
|
|
353 std::string studyUuid = Toolbox::GenerateUuid();
|
|
354
|
82
|
355 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(?, ?)");
|
|
356 s2.BindString(0, studyUuid);
|
|
357 s2.BindInt(1, ResourceType_Study);
|
|
358 s2.Run();
|
|
359
|
0
|
360 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Studies VALUES(?, ?, ?)");
|
|
361 s.BindString(0, studyUuid);
|
|
362 s.BindString(1, parentPatientUuid);
|
|
363 s.BindString(2, dicomStudy);
|
|
364 s.Run();
|
|
365
|
|
366 RecordChange("studies", studyUuid);
|
|
367
|
|
368 DicomMap dicom;
|
|
369 dicomSummary.ExtractStudyInformation(dicom);
|
|
370 StoreMainDicomTags(studyUuid, dicom);
|
|
371
|
|
372 return studyUuid;
|
|
373 }
|
|
374
|
|
375
|
|
376
|
|
377 bool ServerIndex::HasPatient(std::string& patientUuid,
|
|
378 const std::string& dicomPatientId)
|
|
379 {
|
|
380 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Patients WHERE dicomPatientId=?");
|
|
381 s.BindString(0, dicomPatientId);
|
|
382 if (s.Step())
|
|
383 {
|
|
384 patientUuid = s.ColumnString(0);
|
|
385 return true;
|
|
386 }
|
|
387 else
|
|
388 {
|
|
389 return false;
|
|
390 }
|
|
391 }
|
|
392
|
|
393 std::string ServerIndex::CreatePatient(const std::string& patientId,
|
|
394 const DicomMap& dicomSummary)
|
|
395 {
|
|
396 std::string patientUuid = Toolbox::GenerateUuid();
|
80
|
397 std::string dicomPatientId = dicomSummary.GetValue(DICOM_TAG_PATIENT_ID).AsString();
|
0
|
398
|
82
|
399 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(?, ?)");
|
|
400 s2.BindString(0, patientUuid);
|
|
401 s2.BindInt(1, ResourceType_Patient);
|
|
402 s2.Run();
|
|
403
|
0
|
404 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Patients VALUES(?, ?)");
|
|
405 s.BindString(0, patientUuid);
|
|
406 s.BindString(1, dicomPatientId);
|
|
407 s.Run();
|
|
408
|
|
409 RecordChange("patients", patientUuid);
|
|
410
|
|
411 DicomMap dicom;
|
|
412 dicomSummary.ExtractPatientInformation(dicom);
|
|
413 StoreMainDicomTags(patientUuid, dicom);
|
|
414
|
|
415 return patientUuid;
|
|
416 }
|
|
417
|
|
418
|
|
419 void ServerIndex::GetMainDicomTags(DicomMap& map,
|
|
420 const std::string& uuid)
|
|
421 {
|
|
422 map.Clear();
|
|
423
|
|
424 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM MainDicomTags WHERE uuid=?");
|
|
425 s.BindString(0, uuid);
|
|
426 while (s.Step())
|
|
427 {
|
|
428 map.SetValue(s.ColumnInt(1),
|
|
429 s.ColumnInt(2),
|
|
430 s.ColumnString(3));
|
|
431 }
|
|
432 }
|
|
433
|
|
434 void ServerIndex::MainDicomTagsToJson(Json::Value& target,
|
|
435 const std::string& uuid)
|
|
436 {
|
|
437 DicomMap map;
|
|
438 GetMainDicomTags(map, uuid);
|
|
439 target["MainDicomTags"] = Json::objectValue;
|
|
440 FromDcmtkBridge::ToJson(target["MainDicomTags"], map);
|
|
441 }
|
|
442
|
|
443
|
|
444 bool ServerIndex::DeleteInternal(Json::Value& target,
|
|
445 const std::string& uuid,
|
|
446 const std::string& tableName)
|
|
447 {
|
|
448 boost::mutex::scoped_lock scoped_lock(mutex_);
|
|
449
|
|
450 deletedLevels_->Clear();
|
|
451
|
|
452 SQLite::Statement s(db_, "DELETE FROM " + tableName + " WHERE uuid=?");
|
|
453 s.BindString(0, uuid);
|
|
454
|
|
455 if (!s.Run())
|
|
456 {
|
|
457 return false;
|
|
458 }
|
|
459
|
|
460 if (db_.GetLastChangeCount() == 0)
|
|
461 {
|
|
462 // Nothing was deleted, inexistent UUID
|
|
463 return false;
|
|
464 }
|
|
465
|
|
466 if (deletedLevels_->HasRemainingLevel())
|
|
467 {
|
|
468 std::string type(deletedLevels_->GetRemainingLevelType());
|
|
469 const std::string& uuid = deletedLevels_->GetRemainingLevelUuid();
|
|
470
|
|
471 target["RemainingAncestor"] = Json::Value(Json::objectValue);
|
|
472 target["RemainingAncestor"]["Path"] = "/" + type + "/" + uuid;
|
|
473 target["RemainingAncestor"]["Type"] = type;
|
|
474 target["RemainingAncestor"]["ID"] = uuid;
|
|
475 }
|
|
476 else
|
|
477 {
|
|
478 target["RemainingAncestor"] = Json::nullValue;
|
|
479 }
|
|
480
|
|
481 return true;
|
|
482 }
|
|
483
|
|
484
|
|
485 ServerIndex::ServerIndex(const std::string& storagePath)
|
|
486 {
|
|
487 boost::filesystem::path p = storagePath;
|
|
488
|
|
489 try
|
|
490 {
|
|
491 boost::filesystem::create_directories(storagePath);
|
|
492 }
|
|
493 catch (boost::filesystem::filesystem_error)
|
|
494 {
|
|
495 }
|
|
496
|
|
497 p /= "index";
|
|
498 db_.Open(p.string());
|
|
499 db_.Register(new Internals::DeleteFromFileStorageFunction(storagePath));
|
|
500 deletedLevels_ = (Internals::SignalDeletedLevelFunction*)
|
|
501 db_.Register(new Internals::SignalDeletedLevelFunction);
|
|
502
|
|
503 if (!db_.DoesTableExist("GlobalProperties"))
|
|
504 {
|
|
505 printf("Creating the database\n");
|
|
506 std::string query;
|
|
507 EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE);
|
|
508 db_.Execute(query);
|
|
509 }
|
|
510 }
|
|
511
|
|
512
|
|
513 StoreStatus ServerIndex::Store(std::string& instanceUuid,
|
|
514 const DicomMap& dicomSummary,
|
|
515 const std::string& fileUuid,
|
|
516 uint64_t uncompressedFileSize,
|
|
517 const std::string& jsonUuid,
|
|
518 const std::string& distantAet)
|
|
519 {
|
|
520 boost::mutex::scoped_lock scoped_lock(mutex_);
|
|
521
|
80
|
522 std::string dicomPatientId = dicomSummary.GetValue(DICOM_TAG_PATIENT_ID).AsString();
|
|
523 std::string dicomInstance = dicomSummary.GetValue(DICOM_TAG_SOP_INSTANCE_UID).AsString();
|
|
524 std::string dicomSeries = dicomSummary.GetValue(DICOM_TAG_SERIES_INSTANCE_UID).AsString();
|
|
525 std::string dicomStudy = dicomSummary.GetValue(DICOM_TAG_STUDY_INSTANCE_UID).AsString();
|
0
|
526
|
|
527 try
|
|
528 {
|
|
529 SQLite::Transaction t(db_);
|
|
530 t.Begin();
|
|
531
|
|
532 if (HasInstance(instanceUuid, dicomInstance))
|
|
533 {
|
|
534 return StoreStatus_AlreadyStored;
|
|
535 // TODO: Check consistency?
|
|
536 }
|
|
537
|
|
538 std::string patientUuid;
|
|
539 if (HasPatient(patientUuid, dicomPatientId))
|
|
540 {
|
|
541 // TODO: Check consistency?
|
|
542 }
|
|
543 else
|
|
544 {
|
|
545 patientUuid = CreatePatient(dicomPatientId, dicomSummary);
|
|
546 }
|
|
547
|
|
548 std::string studyUuid;
|
|
549 if (HasStudy(studyUuid, dicomStudy))
|
|
550 {
|
|
551 // TODO: Check consistency?
|
|
552 }
|
|
553 else
|
|
554 {
|
|
555 studyUuid = CreateStudy(patientUuid, dicomStudy, dicomSummary);
|
|
556 }
|
|
557
|
|
558 std::string seriesUuid;
|
|
559 if (HasSeries(seriesUuid, dicomSeries))
|
|
560 {
|
|
561 // TODO: Check consistency?
|
|
562 }
|
|
563 else
|
|
564 {
|
|
565 seriesUuid = CreateSeries(studyUuid, dicomSeries, dicomSummary);
|
|
566 }
|
|
567
|
|
568 instanceUuid = CreateInstance(seriesUuid, dicomInstance, dicomSummary, fileUuid,
|
|
569 uncompressedFileSize, jsonUuid, distantAet);
|
|
570
|
|
571 t.Commit();
|
|
572 return StoreStatus_Success;
|
|
573 //t.Rollback();
|
|
574 }
|
62
|
575 catch (OrthancException& e)
|
0
|
576 {
|
|
577 std::cout << "EXCEPT2 [" << e.What() << "]" << " " << db_.GetErrorMessage() << std::endl;
|
|
578 }
|
|
579
|
|
580 return StoreStatus_Failure;
|
|
581 }
|
|
582
|
|
583
|
|
584 StoreStatus ServerIndex::Store(std::string& instanceUuid,
|
|
585 FileStorage& storage,
|
|
586 const char* dicomFile,
|
|
587 size_t dicomSize,
|
|
588 const DicomMap& dicomSummary,
|
|
589 const Json::Value& dicomJson,
|
|
590 const std::string& distantAet)
|
|
591 {
|
|
592 std::string fileUuid = storage.Create(dicomFile, dicomSize);
|
|
593 std::string jsonUuid = storage.Create(dicomJson.toStyledString());
|
|
594 StoreStatus status = Store(instanceUuid, dicomSummary, fileUuid,
|
|
595 dicomSize, jsonUuid, distantAet);
|
|
596
|
|
597 if (status != StoreStatus_Success)
|
|
598 {
|
|
599 storage.Remove(fileUuid);
|
|
600 storage.Remove(jsonUuid);
|
|
601 }
|
|
602
|
|
603 switch (status)
|
|
604 {
|
|
605 case StoreStatus_Success:
|
|
606 std::cout << "New instance stored: " << GetTotalSize() << std::endl;
|
|
607 break;
|
|
608
|
|
609 case StoreStatus_AlreadyStored:
|
|
610 std::cout << "Already stored" << std::endl;
|
|
611 break;
|
|
612
|
|
613 case StoreStatus_Failure:
|
|
614 std::cout << "Store failure" << std::endl;
|
|
615 break;
|
|
616 }
|
|
617
|
|
618 return status;
|
|
619 }
|
|
620
|
|
621 uint64_t ServerIndex::GetTotalSize()
|
|
622 {
|
|
623 boost::mutex::scoped_lock scoped_lock(mutex_);
|
|
624 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT SUM(fileSize) FROM Instances");
|
|
625 s.Run();
|
|
626 return s.ColumnInt64(0);
|
|
627 }
|
|
628
|
|
629
|
|
630 SeriesStatus ServerIndex::GetSeriesStatus(const std::string& seriesUuid)
|
|
631 {
|
80
|
632 SQLite::Statement s1(db_, SQLITE_FROM_HERE, "SELECT expectedNumberOfInstances FROM Series WHERE uuid=?");
|
|
633 s1.BindString(0, seriesUuid);
|
82
|
634 if (!s1.Step() || s1.ColumnIsNull(0))
|
80
|
635 {
|
|
636 return SeriesStatus_Unknown;
|
|
637 }
|
|
638
|
|
639 int numberOfInstances = s1.ColumnInt(0);
|
|
640 if (numberOfInstances < 0)
|
0
|
641 {
|
|
642 return SeriesStatus_Unknown;
|
|
643 }
|
|
644
|
80
|
645 std::set<int> instances;
|
|
646 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "SELECT indexInSeries FROM Instances WHERE parentSeries=?");
|
|
647 s2.BindString(0, seriesUuid);
|
|
648 while (s2.Step())
|
|
649 {
|
|
650 int index = s2.ColumnInt(0);
|
|
651 if (index <= 0 || index > numberOfInstances)
|
|
652 {
|
|
653 // Out-of-range instance index
|
|
654 return SeriesStatus_Inconsistent;
|
|
655 }
|
0
|
656
|
80
|
657 if (instances.find(index) != instances.end())
|
|
658 {
|
|
659 // Twice the same instance index
|
|
660 return SeriesStatus_Inconsistent;
|
|
661 }
|
|
662
|
|
663 instances.insert(index);
|
|
664 }
|
|
665
|
|
666 for (int i = 1; i <= numberOfInstances; i++)
|
|
667 {
|
|
668 if (instances.find(i) == instances.end())
|
|
669 {
|
|
670 return SeriesStatus_Missing;
|
|
671 }
|
|
672 }
|
|
673
|
|
674 return SeriesStatus_Complete;
|
0
|
675 }
|
|
676
|
|
677
|
|
678
|
|
679 bool ServerIndex::GetInstance(Json::Value& result,
|
|
680 const std::string& instanceUuid)
|
|
681 {
|
|
682 assert(result.type() == Json::objectValue);
|
|
683 boost::mutex::scoped_lock scoped_lock(mutex_);
|
|
684
|
80
|
685 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT parentSeries, dicomInstance, fileSize, fileUuid, indexInSeries FROM Instances WHERE uuid=?");
|
0
|
686 s.BindString(0, instanceUuid);
|
|
687 if (!s.Step())
|
|
688 {
|
|
689 return false;
|
|
690 }
|
|
691 else
|
|
692 {
|
|
693 result["ID"] = instanceUuid;
|
|
694 result["ParentSeries"] = s.ColumnString(0);
|
|
695 result["FileSize"] = s.ColumnInt(2); // TODO switch to 64bit with JsonCpp 0.6?
|
|
696 result["FileUuid"] = s.ColumnString(3);
|
|
697 MainDicomTagsToJson(result, instanceUuid);
|
80
|
698
|
|
699 if (s.ColumnIsNull(4))
|
|
700 {
|
82
|
701 result["IndexInSeries"] = Json::nullValue;
|
80
|
702 }
|
|
703 else
|
|
704 {
|
|
705 result["IndexInSeries"] = s.ColumnInt(4);
|
|
706 }
|
|
707
|
0
|
708 return true;
|
|
709 }
|
|
710 }
|
|
711
|
|
712
|
|
713 bool ServerIndex::GetSeries(Json::Value& result,
|
|
714 const std::string& seriesUuid)
|
|
715 {
|
|
716 assert(result.type() == Json::objectValue);
|
|
717 boost::mutex::scoped_lock scoped_lock(mutex_);
|
|
718
|
80
|
719 SQLite::Statement s1(db_, SQLITE_FROM_HERE, "SELECT parentStudy, dicomSeries, expectedNumberOfInstances FROM Series WHERE uuid=?");
|
0
|
720 s1.BindString(0, seriesUuid);
|
|
721 if (!s1.Step())
|
|
722 {
|
|
723 return false;
|
|
724 }
|
|
725
|
|
726 result["ID"] = seriesUuid;
|
|
727 result["ParentStudy"] = s1.ColumnString(0);
|
|
728 MainDicomTagsToJson(result, seriesUuid);
|
|
729
|
|
730 Json::Value instances(Json::arrayValue);
|
|
731 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Instances WHERE parentSeries=?");
|
|
732 s2.BindString(0, seriesUuid);
|
|
733 while (s2.Step())
|
|
734 {
|
|
735 instances.append(s2.ColumnString(0));
|
|
736 }
|
|
737
|
|
738 result["Instances"] = instances;
|
|
739
|
80
|
740 if (s1.ColumnIsNull(2))
|
|
741 {
|
82
|
742 result["ExpectedNumberOfInstances"] = Json::nullValue;
|
80
|
743 }
|
|
744 else
|
|
745 {
|
|
746 result["ExpectedNumberOfInstances"] = s1.ColumnInt(2);
|
|
747 }
|
|
748
|
|
749 SeriesStatus status = GetSeriesStatus(seriesUuid);
|
|
750
|
|
751 switch (status)
|
|
752 {
|
|
753 case SeriesStatus_Complete:
|
|
754 result["Status"] = "Complete";
|
|
755 break;
|
|
756
|
|
757 case SeriesStatus_Missing:
|
|
758 result["Status"] = "Missing";
|
|
759 break;
|
|
760
|
|
761 case SeriesStatus_Inconsistent:
|
|
762 result["Status"] = "Inconsistent";
|
|
763 break;
|
|
764
|
|
765 default:
|
|
766 case SeriesStatus_Unknown:
|
|
767 result["Status"] = "Unknown";
|
|
768 break;
|
|
769 }
|
|
770
|
0
|
771 return true;
|
|
772 }
|
|
773
|
|
774
|
|
775 bool ServerIndex::GetStudy(Json::Value& result,
|
|
776 const std::string& studyUuid)
|
|
777 {
|
|
778 assert(result.type() == Json::objectValue);
|
|
779 boost::mutex::scoped_lock scoped_lock(mutex_);
|
|
780
|
|
781 SQLite::Statement s1(db_, SQLITE_FROM_HERE, "SELECT parentPatient, dicomStudy FROM Studies WHERE uuid=?");
|
|
782 s1.BindString(0, studyUuid);
|
|
783 if (!s1.Step())
|
|
784 {
|
|
785 return false;
|
|
786 }
|
|
787
|
|
788 result["ID"] = studyUuid;
|
|
789 result["ParentPatient"] = s1.ColumnString(0);
|
|
790 MainDicomTagsToJson(result, studyUuid);
|
|
791
|
|
792 Json::Value series(Json::arrayValue);
|
|
793 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Series WHERE parentStudy=?");
|
|
794 s2.BindString(0, studyUuid);
|
|
795 while (s2.Step())
|
|
796 {
|
|
797 series.append(s2.ColumnString(0));
|
|
798 }
|
|
799
|
|
800 result["Series"] = series;
|
|
801 return true;
|
|
802 }
|
|
803
|
|
804
|
|
805 bool ServerIndex::GetPatient(Json::Value& result,
|
|
806 const std::string& patientUuid)
|
|
807 {
|
|
808 assert(result.type() == Json::objectValue);
|
|
809 boost::mutex::scoped_lock scoped_lock(mutex_);
|
|
810
|
|
811 SQLite::Statement s1(db_, SQLITE_FROM_HERE, "SELECT dicomPatientId FROM Patients WHERE uuid=?");
|
|
812 s1.BindString(0, patientUuid);
|
|
813 if (!s1.Step())
|
|
814 {
|
|
815 return false;
|
|
816 }
|
|
817
|
|
818 result["ID"] = patientUuid;
|
|
819 MainDicomTagsToJson(result, patientUuid);
|
|
820
|
|
821 Json::Value studies(Json::arrayValue);
|
|
822 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Studies WHERE parentPatient=?");
|
|
823 s2.BindString(0, patientUuid);
|
|
824 while (s2.Step())
|
|
825 {
|
|
826 studies.append(s2.ColumnString(0));
|
|
827 }
|
|
828
|
|
829 result["Studies"] = studies;
|
|
830 return true;
|
|
831 }
|
|
832
|
|
833
|
|
834 bool ServerIndex::GetJsonFile(std::string& fileUuid,
|
|
835 const std::string& instanceUuid)
|
|
836 {
|
|
837 boost::mutex::scoped_lock scoped_lock(mutex_);
|
|
838
|
|
839 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT jsonUuid FROM Instances WHERE uuid=?");
|
|
840 s.BindString(0, instanceUuid);
|
|
841 if (s.Step())
|
|
842 {
|
|
843 fileUuid = s.ColumnString(0);
|
|
844 return true;
|
|
845 }
|
|
846 else
|
|
847 {
|
|
848 return false;
|
|
849 }
|
|
850 }
|
|
851
|
|
852 bool ServerIndex::GetDicomFile(std::string& fileUuid,
|
|
853 const std::string& instanceUuid)
|
|
854 {
|
|
855 boost::mutex::scoped_lock scoped_lock(mutex_);
|
|
856
|
|
857 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT fileUuid FROM Instances WHERE uuid=?");
|
|
858 s.BindString(0, instanceUuid);
|
|
859 if (s.Step())
|
|
860 {
|
|
861 fileUuid = s.ColumnString(0);
|
|
862 return true;
|
|
863 }
|
|
864 else
|
|
865 {
|
|
866 return false;
|
|
867 }
|
|
868 }
|
|
869
|
|
870
|
|
871 void ServerIndex::GetAllUuids(Json::Value& target,
|
|
872 const std::string& tableName)
|
|
873 {
|
|
874 assert(target.type() == Json::arrayValue);
|
|
875 boost::mutex::scoped_lock scoped_lock(mutex_);
|
|
876
|
|
877 std::string query = "SELECT uuid FROM " + tableName;
|
|
878 SQLite::Statement s(db_, query);
|
|
879 while (s.Step())
|
|
880 {
|
|
881 target.append(s.ColumnString(0));
|
|
882 }
|
|
883 }
|
|
884
|
|
885
|
|
886 bool ServerIndex::GetChanges(Json::Value& target,
|
|
887 int64_t since,
|
|
888 const std::string& filter,
|
|
889 unsigned int maxResults)
|
|
890 {
|
|
891 assert(target.type() == Json::objectValue);
|
|
892 boost::mutex::scoped_lock scoped_lock(mutex_);
|
|
893
|
|
894 if (filter.size() != 0 &&
|
|
895 filter != "instances" &&
|
|
896 filter != "series" &&
|
|
897 filter != "studies" &&
|
|
898 filter != "patients")
|
|
899 {
|
|
900 return false;
|
|
901 }
|
|
902
|
|
903 std::auto_ptr<SQLite::Statement> s;
|
|
904 if (filter.size() == 0)
|
|
905 {
|
|
906 s.reset(new SQLite::Statement(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? "
|
|
907 "ORDER BY seq LIMIT ?"));
|
|
908 s->BindInt64(0, since);
|
|
909 s->BindInt(1, maxResults);
|
|
910 }
|
|
911 else
|
|
912 {
|
|
913 s.reset(new SQLite::Statement(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? "
|
|
914 "AND basePath=? ORDER BY seq LIMIT ?"));
|
|
915 s->BindInt64(0, since);
|
|
916 s->BindString(1, filter);
|
|
917 s->BindInt(2, maxResults);
|
|
918 }
|
|
919
|
|
920 int64_t lastSeq = 0;
|
|
921 Json::Value results(Json::arrayValue);
|
|
922 while (s->Step())
|
|
923 {
|
|
924 int64_t seq = s->ColumnInt64(0);
|
|
925 std::string basePath = s->ColumnString(1);
|
|
926 std::string uuid = s->ColumnString(2);
|
|
927
|
|
928 if (filter.size() == 0 ||
|
|
929 filter == basePath)
|
|
930 {
|
|
931 Json::Value change(Json::objectValue);
|
|
932 change["Seq"] = static_cast<int>(seq); // TODO JsonCpp in 64bit
|
|
933 change["BasePath"] = basePath;
|
|
934 change["ID"] = uuid;
|
|
935 results.append(change);
|
|
936 }
|
|
937
|
|
938 if (seq > lastSeq)
|
|
939 {
|
|
940 lastSeq = seq;
|
|
941 }
|
|
942 }
|
|
943
|
|
944 target["Results"] = results;
|
|
945 target["LastSeq"] = static_cast<int>(lastSeq); // TODO JsonCpp in 64bit
|
|
946
|
|
947 return true;
|
|
948 }
|
82
|
949
|
0
|
950 }
|