comparison OrthancServer/Sources/ServerIndex.cpp @ 4559:19b1921aee06 db-changes

cont
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 04 Mar 2021 17:59:40 +0100
parents 2f4d7ec9b993
children 929409e40008
comparison
equal deleted inserted replaced
4558:2f4d7ec9b993 4559:19b1921aee06
1364 Recycle(0, ""); 1364 Recycle(0, "");
1365 t.Commit(0); 1365 t.Commit(0);
1366 } 1366 }
1367 1367
1368 1368
1369 bool ServerIndex::IsProtectedPatient(const std::string& publicId) 1369 void ServerIndex::SetProtectedPatient(const std::string& publicId,
1370 bool isProtected)
1370 { 1371 {
1371 boost::mutex::scoped_lock lock(mutex_); 1372 boost::mutex::scoped_lock lock(mutex_);
1373 Transaction transaction(*this);
1372 1374
1373 // Lookup for the requested resource 1375 // Lookup for the requested resource
1374 int64_t id; 1376 int64_t id;
1375 ResourceType type; 1377 ResourceType type;
1376 if (!db_.LookupResource(id, type, publicId) || 1378 if (!db_.LookupResource(id, type, publicId) ||
1377 type != ResourceType_Patient) 1379 type != ResourceType_Patient)
1378 { 1380 {
1379 throw OrthancException(ErrorCode_ParameterOutOfRange); 1381 throw OrthancException(ErrorCode_ParameterOutOfRange);
1380 } 1382 }
1381 1383
1382 return db_.IsProtectedPatient(id);
1383 }
1384
1385
1386 void ServerIndex::SetProtectedPatient(const std::string& publicId,
1387 bool isProtected)
1388 {
1389 boost::mutex::scoped_lock lock(mutex_);
1390 Transaction transaction(*this);
1391
1392 // Lookup for the requested resource
1393 int64_t id;
1394 ResourceType type;
1395 if (!db_.LookupResource(id, type, publicId) ||
1396 type != ResourceType_Patient)
1397 {
1398 throw OrthancException(ErrorCode_ParameterOutOfRange);
1399 }
1400
1401 db_.SetProtectedPatient(id, isProtected); 1384 db_.SetProtectedPatient(id, isProtected);
1402 transaction.Commit(0); 1385 transaction.Commit(0);
1403 1386
1404 if (isProtected) 1387 if (isProtected)
1405 LOG(INFO) << "Patient " << publicId << " has been protected"; 1388 LOG(INFO) << "Patient " << publicId << " has been protected";
1406 else 1389 else
1407 LOG(INFO) << "Patient " << publicId << " has been unprotected"; 1390 LOG(INFO) << "Patient " << publicId << " has been unprotected";
1408 }
1409
1410
1411 void ServerIndex::GetChildren(std::list<std::string>& result,
1412 const std::string& publicId)
1413 {
1414 result.clear();
1415
1416 boost::mutex::scoped_lock lock(mutex_);
1417
1418 ResourceType type;
1419 int64_t resource;
1420 if (!db_.LookupResource(resource, type, publicId))
1421 {
1422 throw OrthancException(ErrorCode_UnknownResource);
1423 }
1424
1425 if (type == ResourceType_Instance)
1426 {
1427 // An instance cannot have a child
1428 throw OrthancException(ErrorCode_BadParameterType);
1429 }
1430
1431 std::list<int64_t> tmp;
1432 db_.GetChildrenInternalId(tmp, resource);
1433
1434 for (std::list<int64_t>::const_iterator
1435 it = tmp.begin(); it != tmp.end(); ++it)
1436 {
1437 result.push_back(db_.GetPublicId(*it));
1438 }
1439 }
1440
1441
1442 void ServerIndex::GetChildInstances(std::list<std::string>& result,
1443 const std::string& publicId)
1444 {
1445 result.clear();
1446
1447 boost::mutex::scoped_lock lock(mutex_);
1448
1449 ResourceType type;
1450 int64_t top;
1451 if (!db_.LookupResource(top, type, publicId))
1452 {
1453 throw OrthancException(ErrorCode_UnknownResource);
1454 }
1455
1456 if (type == ResourceType_Instance)
1457 {
1458 // The resource is already an instance: Do not go down the hierarchy
1459 result.push_back(publicId);
1460 return;
1461 }
1462
1463 std::stack<int64_t> toExplore;
1464 toExplore.push(top);
1465
1466 std::list<int64_t> tmp;
1467
1468 while (!toExplore.empty())
1469 {
1470 // Get the internal ID of the current resource
1471 int64_t resource = toExplore.top();
1472 toExplore.pop();
1473
1474 if (db_.GetResourceType(resource) == ResourceType_Instance)
1475 {
1476 result.push_back(db_.GetPublicId(resource));
1477 }
1478 else
1479 {
1480 // Tag all the children of this resource as to be explored
1481 db_.GetChildrenInternalId(tmp, resource);
1482 for (std::list<int64_t>::const_iterator
1483 it = tmp.begin(); it != tmp.end(); ++it)
1484 {
1485 toExplore.push(*it);
1486 }
1487 }
1488 }
1489 } 1391 }
1490 1392
1491 1393
1492 void ServerIndex::SetMetadata(const std::string& publicId, 1394 void ServerIndex::SetMetadata(const std::string& publicId,
1493 MetadataType type, 1395 MetadataType type,
2328 2230
2329 virtual void ApplyTuple(ServerIndex::ReadOnlyTransaction& transaction, 2231 virtual void ApplyTuple(ServerIndex::ReadOnlyTransaction& transaction,
2330 const Tuple& tuple) = 0; 2232 const Tuple& tuple) = 0;
2331 2233
2332 void Apply(ServerIndex& index, 2234 void Apply(ServerIndex& index,
2333 T1 t1) 2235 const T1& t1)
2334 { 2236 {
2335 const Tuple tuple(t1); 2237 const Tuple tuple(t1);
2336 TupleOperationsWrapper<ReadOnlyOperationsT1, Tuple> wrapper(*this, tuple); 2238 TupleOperationsWrapper<ReadOnlyOperationsT1, Tuple> wrapper(*this, tuple);
2337 index.Apply(wrapper); 2239 index.Apply(wrapper);
2338 } 2240 }
2352 2254
2353 virtual void ApplyTuple(ServerIndex::ReadOnlyTransaction& transaction, 2255 virtual void ApplyTuple(ServerIndex::ReadOnlyTransaction& transaction,
2354 const Tuple& tuple) = 0; 2256 const Tuple& tuple) = 0;
2355 2257
2356 void Apply(ServerIndex& index, 2258 void Apply(ServerIndex& index,
2357 T1 t1, 2259 const T1& t1,
2358 T2 t2) 2260 const T2& t2)
2359 { 2261 {
2360 const Tuple tuple(t1, t2); 2262 const Tuple tuple(t1, t2);
2361 TupleOperationsWrapper<ReadOnlyOperationsT2, Tuple> wrapper(*this, tuple); 2263 TupleOperationsWrapper<ReadOnlyOperationsT2, Tuple> wrapper(*this, tuple);
2362 index.Apply(wrapper); 2264 index.Apply(wrapper);
2363 } 2265 }
2378 2280
2379 virtual void ApplyTuple(ServerIndex::ReadOnlyTransaction& transaction, 2281 virtual void ApplyTuple(ServerIndex::ReadOnlyTransaction& transaction,
2380 const Tuple& tuple) = 0; 2282 const Tuple& tuple) = 0;
2381 2283
2382 void Apply(ServerIndex& index, 2284 void Apply(ServerIndex& index,
2383 T1 t1, 2285 const T1& t1,
2384 T2 t2, 2286 const T2& t2,
2385 T3 t3) 2287 const T3& t3)
2386 { 2288 {
2387 const Tuple tuple(t1, t2, t3); 2289 const Tuple tuple(t1, t2, t3);
2388 TupleOperationsWrapper<ReadOnlyOperationsT3, Tuple> wrapper(*this, tuple); 2290 TupleOperationsWrapper<ReadOnlyOperationsT3, Tuple> wrapper(*this, tuple);
2389 index.Apply(wrapper); 2291 index.Apply(wrapper);
2390 } 2292 }
2406 2308
2407 virtual void ApplyTuple(ServerIndex::ReadOnlyTransaction& transaction, 2309 virtual void ApplyTuple(ServerIndex::ReadOnlyTransaction& transaction,
2408 const Tuple& tuple) = 0; 2310 const Tuple& tuple) = 0;
2409 2311
2410 void Apply(ServerIndex& index, 2312 void Apply(ServerIndex& index,
2411 T1 t1, 2313 const T1& t1,
2412 T2 t2, 2314 const T2& t2,
2413 T3 t3, 2315 const T3& t3,
2414 T4 t4) 2316 const T4& t4)
2415 { 2317 {
2416 const Tuple tuple(t1, t2, t3, t4); 2318 const Tuple tuple(t1, t2, t3, t4);
2417 TupleOperationsWrapper<ReadOnlyOperationsT4, Tuple> wrapper(*this, tuple); 2319 TupleOperationsWrapper<ReadOnlyOperationsT4, Tuple> wrapper(*this, tuple);
2418 index.Apply(wrapper); 2320 index.Apply(wrapper);
2419 } 2321 }
2436 2338
2437 virtual void ApplyTuple(ServerIndex::ReadOnlyTransaction& transaction, 2339 virtual void ApplyTuple(ServerIndex::ReadOnlyTransaction& transaction,
2438 const Tuple& tuple) = 0; 2340 const Tuple& tuple) = 0;
2439 2341
2440 void Apply(ServerIndex& index, 2342 void Apply(ServerIndex& index,
2441 T1 t1, 2343 const T1& t1,
2442 T2 t2, 2344 const T2& t2,
2443 T3 t3, 2345 const T3& t3,
2444 T4 t4, 2346 const T4& t4,
2445 T5 t5) 2347 const T5& t5)
2446 { 2348 {
2447 const Tuple tuple(t1, t2, t3, t4, t5); 2349 const Tuple tuple(t1, t2, t3, t4, t5);
2448 TupleOperationsWrapper<ReadOnlyOperationsT5, Tuple> wrapper(*this, tuple); 2350 TupleOperationsWrapper<ReadOnlyOperationsT5, Tuple> wrapper(*this, tuple);
2449 index.Apply(wrapper); 2351 index.Apply(wrapper);
2450 } 2352 }
2468 2370
2469 virtual void ApplyTuple(ServerIndex::ReadOnlyTransaction& transaction, 2371 virtual void ApplyTuple(ServerIndex::ReadOnlyTransaction& transaction,
2470 const Tuple& tuple) = 0; 2372 const Tuple& tuple) = 0;
2471 2373
2472 void Apply(ServerIndex& index, 2374 void Apply(ServerIndex& index,
2473 T1 t1, 2375 const T1& t1,
2474 T2 t2, 2376 const T2& t2,
2475 T3 t3, 2377 const T3& t3,
2476 T4 t4, 2378 const T4& t4,
2477 T5 t5, 2379 const T5& t5,
2478 T6 t6) 2380 const T6& t6)
2479 { 2381 {
2480 const Tuple tuple(t1, t2, t3, t4, t5, t6); 2382 const Tuple tuple(t1, t2, t3, t4, t5, t6);
2481 TupleOperationsWrapper<ReadOnlyOperationsT6, Tuple> wrapper(*this, tuple); 2383 TupleOperationsWrapper<ReadOnlyOperationsT6, Tuple> wrapper(*this, tuple);
2482 index.Apply(wrapper); 2384 index.Apply(wrapper);
2483 } 2385 }
3082 }; 2984 };
3083 2985
3084 Operations operations; 2986 Operations operations;
3085 operations.Apply(*this, &target); 2987 operations.Apply(*this, &target);
3086 } 2988 }
2989
2990
2991 bool ServerIndex::IsProtectedPatient(const std::string& publicId)
2992 {
2993 class Operations : public ReadOnlyOperationsT1<std::string>
2994 {
2995 private:
2996 bool protected_;
2997
2998 public:
2999 Operations() :
3000 protected_(false)
3001 {
3002 }
3003
3004 bool IsProtected() const
3005 {
3006 return protected_;
3007 }
3008
3009 virtual void ApplyTuple(ReadOnlyTransaction& transaction,
3010 const Tuple& tuple) ORTHANC_OVERRIDE
3011 {
3012 // Lookup for the requested resource
3013 int64_t id;
3014 ResourceType type;
3015 if (!transaction.LookupResource(id, type, tuple.get<0>()) ||
3016 type != ResourceType_Patient)
3017 {
3018 throw OrthancException(ErrorCode_ParameterOutOfRange);
3019 }
3020 else
3021 {
3022 protected_ = transaction.IsProtectedPatient(id);
3023 }
3024 }
3025 };
3026
3027 Operations operations;
3028 operations.Apply(*this, publicId);
3029 return operations.IsProtected();
3030 }
3031
3032
3033 void ServerIndex::GetChildren(std::list<std::string>& result,
3034 const std::string& publicId)
3035 {
3036 class Operations : public ReadOnlyOperationsT2<std::list<std::string>*, std::string>
3037 {
3038 public:
3039 virtual void ApplyTuple(ReadOnlyTransaction& transaction,
3040 const Tuple& tuple) ORTHANC_OVERRIDE
3041 {
3042 ResourceType type;
3043 int64_t resource;
3044 if (!transaction.LookupResource(resource, type, tuple.get<1>()))
3045 {
3046 throw OrthancException(ErrorCode_UnknownResource);
3047 }
3048 else if (type == ResourceType_Instance)
3049 {
3050 // An instance cannot have a child
3051 throw OrthancException(ErrorCode_BadParameterType);
3052 }
3053 else
3054 {
3055 std::list<int64_t> tmp;
3056 transaction.GetChildrenInternalId(tmp, resource);
3057
3058 tuple.get<0>()->clear();
3059
3060 for (std::list<int64_t>::const_iterator
3061 it = tmp.begin(); it != tmp.end(); ++it)
3062 {
3063 tuple.get<0>()->push_back(transaction.GetPublicId(*it));
3064 }
3065 }
3066 }
3067 };
3068
3069 Operations operations;
3070 operations.Apply(*this, &result, publicId);
3071 }
3072
3073
3074 void ServerIndex::GetChildInstances(std::list<std::string>& result,
3075 const std::string& publicId)
3076 {
3077 class Operations : public ReadOnlyOperationsT2<std::list<std::string>*, std::string>
3078 {
3079 public:
3080 virtual void ApplyTuple(ReadOnlyTransaction& transaction,
3081 const Tuple& tuple) ORTHANC_OVERRIDE
3082 {
3083 tuple.get<0>()->clear();
3084
3085 ResourceType type;
3086 int64_t top;
3087 if (!transaction.LookupResource(top, type, tuple.get<1>()))
3088 {
3089 throw OrthancException(ErrorCode_UnknownResource);
3090 }
3091 else if (type == ResourceType_Instance)
3092 {
3093 // The resource is already an instance: Do not go down the hierarchy
3094 tuple.get<0>()->push_back(tuple.get<1>());
3095 }
3096 else
3097 {
3098 std::stack<int64_t> toExplore;
3099 toExplore.push(top);
3100
3101 std::list<int64_t> tmp;
3102 while (!toExplore.empty())
3103 {
3104 // Get the internal ID of the current resource
3105 int64_t resource = toExplore.top();
3106 toExplore.pop();
3107
3108 // TODO - This could be optimized by seeing how many
3109 // levels "type == transaction.GetResourceType(top)" is
3110 // above the "instances level"
3111 if (transaction.GetResourceType(resource) == ResourceType_Instance)
3112 {
3113 tuple.get<0>()->push_back(transaction.GetPublicId(resource));
3114 }
3115 else
3116 {
3117 // Tag all the children of this resource as to be explored
3118 transaction.GetChildrenInternalId(tmp, resource);
3119 for (std::list<int64_t>::const_iterator
3120 it = tmp.begin(); it != tmp.end(); ++it)
3121 {
3122 toExplore.push(*it);
3123 }
3124 }
3125 }
3126 }
3127 }
3128 };
3129
3130 Operations operations;
3131 operations.Apply(*this, &result, publicId);
3132 }
3087 } 3133 }