comparison OrthancServer/OrthancRestApi.cpp @ 349:c5edf0cc6e95

switch to refactored version
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 28 Jan 2013 15:33:44 +0100
parents 1082e8121d10
children 8031f9cfe7fe
comparison
equal deleted inserted replaced
348:1082e8121d10 349:c5edf0cc6e95
1344 namespace 1344 namespace
1345 { 1345 {
1346 typedef std::map< std::pair<DicomRootLevel, std::string>, std::string> UidMap; 1346 typedef std::map< std::pair<DicomRootLevel, std::string>, std::string> UidMap;
1347 } 1347 }
1348 1348
1349 static void RetrieveMappedUid(ParsedDicomFile& dicom, 1349 static bool RetrieveMappedUid(ParsedDicomFile& dicom,
1350 DicomRootLevel level, 1350 DicomRootLevel level,
1351 Replacements& replacements, 1351 Replacements& replacements,
1352 UidMap& uidMap) 1352 UidMap& uidMap)
1353 { 1353 {
1354 std::auto_ptr<DicomTag> tag; 1354 std::auto_ptr<DicomTag> tag;
1367 { 1367 {
1368 throw OrthancException(ErrorCode_InternalError); 1368 throw OrthancException(ErrorCode_InternalError);
1369 } 1369 }
1370 1370
1371 std::string mapped; 1371 std::string mapped;
1372 bool isNew;
1372 1373
1373 UidMap::const_iterator previous = uidMap.find(std::make_pair(level, original)); 1374 UidMap::const_iterator previous = uidMap.find(std::make_pair(level, original));
1374 if (previous == uidMap.end()) 1375 if (previous == uidMap.end())
1375 { 1376 {
1376 mapped = FromDcmtkBridge::GenerateUniqueIdentifier(level); 1377 mapped = FromDcmtkBridge::GenerateUniqueIdentifier(level);
1377 uidMap.insert(std::make_pair(std::make_pair(level, original), mapped)); 1378 uidMap.insert(std::make_pair(std::make_pair(level, original), mapped));
1379 isNew = true;
1378 } 1380 }
1379 else 1381 else
1380 { 1382 {
1381 mapped = previous->second; 1383 mapped = previous->second;
1384 isNew = false;
1382 } 1385 }
1383 1386
1384 replacements[*tag] = mapped; 1387 replacements[*tag] = mapped;
1388 return isNew;
1385 } 1389 }
1386 1390
1387 1391
1388 static void AnonymizeOrModifyResource(Removals& removals, 1392 static void AnonymizeOrModifyResource(Removals& removals,
1389 Replacements& replacements, 1393 Replacements& replacements,
1408 if (instances.size() == 0) 1412 if (instances.size() == 0)
1409 { 1413 {
1410 return; 1414 return;
1411 } 1415 }
1412 1416
1417
1418 /**
1419 * Loop over all the instances of the resource.
1420 **/
1421
1413 UidMap uidMap; 1422 UidMap uidMap;
1414 for (Instances::const_iterator it = instances.begin(); 1423 for (Instances::const_iterator it = instances.begin();
1415 it != instances.end(); it++) 1424 it != instances.end(); it++)
1416 { 1425 {
1417 LOG(INFO) << "Modifying instance " << *it; 1426 LOG(INFO) << "Modifying instance " << *it;
1418 ParsedDicomFile& original = context.GetDicomFile(*it); 1427 ParsedDicomFile& original = context.GetDicomFile(*it);
1419 1428
1420 RetrieveMappedUid(original, DicomRootLevel_Series, replacements, uidMap); 1429 bool isNewSeries = RetrieveMappedUid(original, DicomRootLevel_Series, replacements, uidMap);
1421 1430
1431 bool isNewStudy = false;
1422 if (resourceType == ResourceType_Study || 1432 if (resourceType == ResourceType_Study ||
1423 resourceType == ResourceType_Patient) 1433 resourceType == ResourceType_Patient)
1424 { 1434 {
1425 RetrieveMappedUid(original, DicomRootLevel_Study, replacements, uidMap); 1435 isNewStudy = RetrieveMappedUid(original, DicomRootLevel_Study, replacements, uidMap);
1426 } 1436 }
1437
1438 /**
1439 * Compute the resulting DICOM instance and store it into the Orthanc store.
1440 **/
1427 1441
1428 std::auto_ptr<ParsedDicomFile> modified(original.Clone()); 1442 std::auto_ptr<ParsedDicomFile> modified(original.Clone());
1429 ReplaceInstanceInternal(*modified, removals, replacements, DicomReplaceMode_InsertIfAbsent, removePrivateTags); 1443 ReplaceInstanceInternal(*modified, removals, replacements, DicomReplaceMode_InsertIfAbsent, removePrivateTags);
1430 1444
1431 std::string modifiedInstance; 1445 std::string modifiedInstance;
1433 { 1447 {
1434 LOG(ERROR) << "Error while storing a modified instance " << *it; 1448 LOG(ERROR) << "Error while storing a modified instance " << *it;
1435 return; 1449 return;
1436 } 1450 }
1437 1451
1452
1453 /**
1454 * Record metadata information (AnonimizedFrom/ModifiedFrom).
1455 **/
1456
1457 DicomInstanceHasher modifiedHasher = modified->GetHasher();
1458 DicomInstanceHasher originalHasher = original.GetHasher();
1459
1460 if (isNewSeries)
1461 {
1462 context.GetIndex().SetMetadata(modifiedHasher.HashSeries(),
1463 metadataType, originalHasher.HashSeries());
1464 }
1465
1466 if (isNewStudy)
1467 {
1468 context.GetIndex().SetMetadata(modifiedHasher.HashStudy(),
1469 metadataType, originalHasher.HashStudy());
1470 }
1471
1472 assert(*it == originalHasher.HashInstance());
1473 assert(modifiedInstance == modifiedHasher.HashInstance());
1474 context.GetIndex().SetMetadata(modifiedInstance, metadataType, *it);
1475
1476
1477 /**
1478 * Compute the JSON object that is returned by the REST call.
1479 **/
1480
1438 if (isFirst) 1481 if (isFirst)
1439 { 1482 {
1440 DicomInstanceHasher modifiedHasher = modified->GetHasher();
1441 std::string newId; 1483 std::string newId;
1442 1484
1443 switch (resourceType) 1485 switch (resourceType)
1444 { 1486 {
1445 case ResourceType_Series: 1487 case ResourceType_Series:
1457 default: 1499 default:
1458 throw OrthancException(ErrorCode_InternalError); 1500 throw OrthancException(ErrorCode_InternalError);
1459 } 1501 }
1460 1502
1461 result["Type"] = ToString(resourceType); 1503 result["Type"] = ToString(resourceType);
1504 result["ID"] = newId;
1462 result["Path"] = GetBasePath(resourceType, newId); 1505 result["Path"] = GetBasePath(resourceType, newId);
1463 result["PatientID"] = modifiedHasher.HashPatient(); 1506 result["PatientID"] = modifiedHasher.HashPatient();
1464 isFirst = false; 1507 isFirst = false;
1465 } 1508 }
1466 } 1509 }
1502 Replacements replacements; 1545 Replacements replacements;
1503 bool removePrivateTags; 1546 bool removePrivateTags;
1504 1547
1505 if (ParseModifyRequest(removals, replacements, removePrivateTags, call)) 1548 if (ParseModifyRequest(removals, replacements, removePrivateTags, call))
1506 { 1549 {
1507 AnonymizeOrModifySeries(removals, replacements, removePrivateTags, 1550 AnonymizeOrModifyResource(removals, replacements, removePrivateTags,
1508 MetadataType_ModifiedFrom, ChangeType_ModifiedSeries, call); 1551 MetadataType_ModifiedFrom, ChangeType_ModifiedSeries,
1552 ResourceType_Series, call);
1509 } 1553 }
1510 } 1554 }
1511 1555
1512 1556
1513 static void AnonymizeSeriesInplace(RestApi::PostCall& call) 1557 static void AnonymizeSeriesInplace(RestApi::PostCall& call)
1516 Replacements replacements; 1560 Replacements replacements;
1517 bool removePrivateTags; 1561 bool removePrivateTags;
1518 1562
1519 if (ParseAnonymizationRequest(removals, replacements, removePrivateTags, call)) 1563 if (ParseAnonymizationRequest(removals, replacements, removePrivateTags, call))
1520 { 1564 {
1521 AnonymizeOrModifySeries(removals, replacements, removePrivateTags, 1565 AnonymizeOrModifyResource(removals, replacements, removePrivateTags,
1522 MetadataType_AnonymizedFrom, ChangeType_AnonymizedSeries, call); 1566 MetadataType_AnonymizedFrom, ChangeType_AnonymizedSeries,
1567 ResourceType_Series, call);
1523 } 1568 }
1524 } 1569 }
1525 1570
1526 1571
1527 static void ModifyStudyInplace(RestApi::PostCall& call) 1572 static void ModifyStudyInplace(RestApi::PostCall& call)
1530 Replacements replacements; 1575 Replacements replacements;
1531 bool removePrivateTags; 1576 bool removePrivateTags;
1532 1577
1533 if (ParseModifyRequest(removals, replacements, removePrivateTags, call)) 1578 if (ParseModifyRequest(removals, replacements, removePrivateTags, call))
1534 { 1579 {
1535 AnonymizeOrModifyStudy(removals, replacements, removePrivateTags, 1580 AnonymizeOrModifyResource(removals, replacements, removePrivateTags,
1536 MetadataType_ModifiedFrom, ChangeType_ModifiedStudy, call); 1581 MetadataType_ModifiedFrom, ChangeType_ModifiedStudy,
1582 ResourceType_Study, call);
1537 } 1583 }
1538 } 1584 }
1539 1585
1540 1586
1541 static void AnonymizeStudyInplace(RestApi::PostCall& call) 1587 static void AnonymizeStudyInplace(RestApi::PostCall& call)
1544 Replacements replacements; 1590 Replacements replacements;
1545 bool removePrivateTags; 1591 bool removePrivateTags;
1546 1592
1547 if (ParseAnonymizationRequest(removals, replacements, removePrivateTags, call)) 1593 if (ParseAnonymizationRequest(removals, replacements, removePrivateTags, call))
1548 { 1594 {
1549 AnonymizeOrModifyStudy(removals, replacements, removePrivateTags, 1595 AnonymizeOrModifyResource(removals, replacements, removePrivateTags,
1550 MetadataType_AnonymizedFrom, ChangeType_AnonymizedStudy, call); 1596 MetadataType_AnonymizedFrom, ChangeType_AnonymizedStudy,
1597 ResourceType_Study, call);
1551 } 1598 }
1552 } 1599 }
1553 1600
1554 1601
1555 1602