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