comparison OrthancServer/OrthancRestApi.cpp @ 348:1082e8121d10

refactoring anonymization/modification
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 28 Jan 2013 15:18:17 +0100
parents cd6749e53a03
children c5edf0cc6e95
comparison
equal deleted inserted replaced
347:a9752f88400c 348:1082e8121d10
1139 return false; 1139 return false;
1140 } 1140 }
1141 } 1141 }
1142 1142
1143 1143
1144 static void AnonymizeOrModifyInstance(Removals removals, 1144 static void AnonymizeOrModifyInstance(Removals& removals,
1145 Replacements replacements, 1145 Replacements& replacements,
1146 bool removePrivateTags, 1146 bool removePrivateTags,
1147 RestApi::PostCall& call) 1147 RestApi::PostCall& call)
1148 { 1148 {
1149 boost::mutex::scoped_lock lock(cacheMutex_); 1149 boost::mutex::scoped_lock lock(cacheMutex_);
1150 1150
1157 ReplaceInstanceInternal(*modified, removals, replacements, DicomReplaceMode_InsertIfAbsent, removePrivateTags); 1157 ReplaceInstanceInternal(*modified, removals, replacements, DicomReplaceMode_InsertIfAbsent, removePrivateTags);
1158 modified->Answer(call.GetOutput()); 1158 modified->Answer(call.GetOutput());
1159 } 1159 }
1160 1160
1161 1161
1162 static void AnonymizeOrModifySeries(Removals removals, 1162 static void AnonymizeOrModifySeries(Removals& removals,
1163 Replacements replacements, 1163 Replacements& replacements,
1164 bool removePrivateTags, 1164 bool removePrivateTags,
1165 MetadataType metadataType, 1165 MetadataType metadataType,
1166 ChangeType changeType, 1166 ChangeType changeType,
1167 RestApi::PostCall& call) 1167 RestApi::PostCall& call)
1168 { 1168 {
1232 // Do not answer before the lock has been released 1232 // Do not answer before the lock has been released
1233 call.GetOutput().AnswerJson(result); 1233 call.GetOutput().AnswerJson(result);
1234 } 1234 }
1235 1235
1236 1236
1237 static void AnonymizeOrModifyStudy(Removals removals, 1237 static void AnonymizeOrModifyStudy(Removals& removals,
1238 Replacements replacements, 1238 Replacements& replacements,
1239 bool removePrivateTags, 1239 bool removePrivateTags,
1240 MetadataType metadataType, 1240 MetadataType metadataType,
1241 ChangeType changeType, 1241 ChangeType changeType,
1242 RestApi::PostCall& call) 1242 RestApi::PostCall& call)
1243 { 1243 {
1334 result["Path"] = GetBasePath(ResourceType_Study, newStudyId); 1334 result["Path"] = GetBasePath(ResourceType_Study, newStudyId);
1335 result["PatientID"] = newPatientId; 1335 result["PatientID"] = newPatientId;
1336 } 1336 }
1337 1337
1338 // Do not answer before the lock has been released 1338 // Do not answer before the lock has been released
1339 call.GetOutput().AnswerJson(result);
1340 }
1341
1342
1343
1344 namespace
1345 {
1346 typedef std::map< std::pair<DicomRootLevel, std::string>, std::string> UidMap;
1347 }
1348
1349 static void RetrieveMappedUid(ParsedDicomFile& dicom,
1350 DicomRootLevel level,
1351 Replacements& replacements,
1352 UidMap& uidMap)
1353 {
1354 std::auto_ptr<DicomTag> tag;
1355 if (level == DicomRootLevel_Series)
1356 {
1357 tag.reset(new DicomTag(DICOM_TAG_SERIES_INSTANCE_UID));
1358 }
1359 else
1360 {
1361 assert(level == DicomRootLevel_Study);
1362 tag.reset(new DicomTag(DICOM_TAG_STUDY_INSTANCE_UID));
1363 }
1364
1365 std::string original;
1366 if (!dicom.GetTagValue(original, *tag))
1367 {
1368 throw OrthancException(ErrorCode_InternalError);
1369 }
1370
1371 std::string mapped;
1372
1373 UidMap::const_iterator previous = uidMap.find(std::make_pair(level, original));
1374 if (previous == uidMap.end())
1375 {
1376 mapped = FromDcmtkBridge::GenerateUniqueIdentifier(level);
1377 uidMap.insert(std::make_pair(std::make_pair(level, original), mapped));
1378 }
1379 else
1380 {
1381 mapped = previous->second;
1382 }
1383
1384 replacements[*tag] = mapped;
1385 }
1386
1387
1388 static void AnonymizeOrModifyResource(Removals& removals,
1389 Replacements& replacements,
1390 bool removePrivateTags,
1391 MetadataType metadataType,
1392 ChangeType changeType,
1393 ResourceType resourceType,
1394 RestApi::PostCall& call)
1395 {
1396 typedef std::list<std::string> Instances;
1397
1398 bool isFirst = true;
1399 Json::Value result(Json::objectValue);
1400
1401 boost::mutex::scoped_lock lock(cacheMutex_);
1402 RETRIEVE_CONTEXT(call);
1403
1404 Instances instances;
1405 std::string id = call.GetUriComponent("id", "");
1406 context.GetIndex().GetChildInstances(instances, id);
1407
1408 if (instances.size() == 0)
1409 {
1410 return;
1411 }
1412
1413 UidMap uidMap;
1414 for (Instances::const_iterator it = instances.begin();
1415 it != instances.end(); it++)
1416 {
1417 LOG(INFO) << "Modifying instance " << *it;
1418 ParsedDicomFile& original = context.GetDicomFile(*it);
1419
1420 RetrieveMappedUid(original, DicomRootLevel_Series, replacements, uidMap);
1421
1422 if (resourceType == ResourceType_Study ||
1423 resourceType == ResourceType_Patient)
1424 {
1425 RetrieveMappedUid(original, DicomRootLevel_Study, replacements, uidMap);
1426 }
1427
1428 std::auto_ptr<ParsedDicomFile> modified(original.Clone());
1429 ReplaceInstanceInternal(*modified, removals, replacements, DicomReplaceMode_InsertIfAbsent, removePrivateTags);
1430
1431 std::string modifiedInstance;
1432 if (context.Store(modifiedInstance, modified->GetDicom()) != StoreStatus_Success)
1433 {
1434 LOG(ERROR) << "Error while storing a modified instance " << *it;
1435 return;
1436 }
1437
1438 if (isFirst)
1439 {
1440 DicomInstanceHasher modifiedHasher = modified->GetHasher();
1441 std::string newId;
1442
1443 switch (resourceType)
1444 {
1445 case ResourceType_Series:
1446 newId = modifiedHasher.HashSeries();
1447 break;
1448
1449 case ResourceType_Study:
1450 newId = modifiedHasher.HashStudy();
1451 break;
1452
1453 case ResourceType_Patient:
1454 newId = modifiedHasher.HashPatient();
1455 break;
1456
1457 default:
1458 throw OrthancException(ErrorCode_InternalError);
1459 }
1460
1461 result["Type"] = ToString(resourceType);
1462 result["Path"] = GetBasePath(resourceType, newId);
1463 result["PatientID"] = modifiedHasher.HashPatient();
1464 isFirst = false;
1465 }
1466 }
1467
1339 call.GetOutput().AnswerJson(result); 1468 call.GetOutput().AnswerJson(result);
1340 } 1469 }
1341 1470
1342 1471
1343 1472