comparison OrthancServer/OrthancRestApi/OrthancRestResources.cpp @ 1354:3dd494f201a1

ResourceFinder
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 13 May 2015 12:17:35 +0200
parents 9ec7ac03152d
children ab9f3d5910bd
comparison
equal deleted inserted replaced
1353:d7da97e21161 1354:3dd494f201a1
33 #include "../PrecompiledHeadersServer.h" 33 #include "../PrecompiledHeadersServer.h"
34 #include "OrthancRestApi.h" 34 #include "OrthancRestApi.h"
35 35
36 #include "../ServerToolbox.h" 36 #include "../ServerToolbox.h"
37 #include "../FromDcmtkBridge.h" 37 #include "../FromDcmtkBridge.h"
38 #include "../ResourceFinder.h"
38 39
39 #include <glog/logging.h> 40 #include <glog/logging.h>
40 41
41 namespace Orthanc 42 namespace Orthanc
42 { 43 {
43 // List all the patients, studies, series or instances ---------------------- 44 // List all the patients, studies, series or instances ----------------------
44 45
46 static void AnswerListOfResources(RestApiOutput& output,
47 ServerIndex& index,
48 const std::list<std::string>& resources,
49 ResourceType level,
50 bool expand)
51 {
52 Json::Value answer = Json::arrayValue;
53
54 for (std::list<std::string>::const_iterator
55 resource = resources.begin(); resource != resources.end(); resource++)
56 {
57 if (expand)
58 {
59 Json::Value item;
60 if (index.LookupResource(item, *resource, level))
61 {
62 answer.append(item);
63 }
64 }
65 else
66 {
67 answer.append(*resource);
68 }
69 }
70
71 output.AnswerJson(answer);
72 }
73
74
45 template <enum ResourceType resourceType> 75 template <enum ResourceType resourceType>
46 static void ListResources(RestApiGetCall& call) 76 static void ListResources(RestApiGetCall& call)
47 { 77 {
48 ServerIndex& index = OrthancRestApi::GetIndex(call); 78 ServerIndex& index = OrthancRestApi::GetIndex(call);
49 79
50 Json::Value result; 80 std::list<std::string> result;
51 index.GetAllUuids(result, resourceType); 81 index.GetAllUuids(result, resourceType);
52 82
53 if (call.HasArgument("expand")) 83 AnswerListOfResources(call.GetOutput(), index, result, resourceType, call.HasArgument("expand"));
54 {
55 Json::Value expanded = Json::arrayValue;
56 for (Json::Value::ArrayIndex i = 0; i < result.size(); i++)
57 {
58 Json::Value item;
59 if (index.LookupResource(item, result[i].asString(), resourceType))
60 {
61 expanded.append(item);
62 }
63 }
64
65 call.GetOutput().AnswerJson(expanded);
66 }
67 else
68 {
69 call.GetOutput().AnswerJson(result);
70 }
71 } 84 }
72 85
73 template <enum ResourceType resourceType> 86 template <enum ResourceType resourceType>
74 static void GetSingleResource(RestApiGetCall& call) 87 static void GetSingleResource(RestApiGetCall& call)
75 { 88 {
833 846
834 call.GetOutput().AnswerJson(result); 847 call.GetOutput().AnswerJson(result);
835 } 848 }
836 849
837 850
851 static void Find(RestApiPostCall& call)
852 {
853 ServerIndex& index = OrthancRestApi::GetIndex(call);
854
855 Json::Value request;
856 if (call.ParseJsonRequest(request) &&
857 request.type() == Json::objectValue &&
858 request.isMember("Level") &&
859 request.isMember("Query") &&
860 request["Level"].type() == Json::stringValue &&
861 request["Query"].type() == Json::objectValue)
862 {
863 std::string level = request["Level"].asString();
864
865 ResourceFinder finder(index);
866 finder.SetLevel(StringToResourceType(level.c_str()));
867
868 if (request.isMember("CaseSensitive"))
869 {
870 finder.SetCaseSensitive(request["CaseSensitive"].asBool());
871 }
872
873 bool expand = false;
874 if (request.isMember("Expand"))
875 {
876 expand = request["Expand"].asBool();
877 }
878
879 Json::Value::Members members = request["Query"].getMemberNames();
880 for (size_t i = 0; i < members.size(); i++)
881 {
882 if (request["Query"][members[i]].type() != Json::stringValue)
883 {
884 throw OrthancException(ErrorCode_BadRequest);
885 }
886
887 finder.AddTag(members[i], request["Query"][members[i]].asString());
888 }
889
890 std::list<std::string> resources;
891 finder.Apply(resources);
892 AnswerListOfResources(call.GetOutput(), index, resources, finder.GetLevel(), expand);
893 }
894 else
895 {
896 throw OrthancException(ErrorCode_BadRequest);
897 }
898 }
899
900
838 template <enum ResourceType start, 901 template <enum ResourceType start,
839 enum ResourceType end> 902 enum ResourceType end>
840 static void GetChildResources(RestApiGetCall& call) 903 static void GetChildResources(RestApiGetCall& call)
841 { 904 {
842 ServerIndex& index = OrthancRestApi::GetIndex(call); 905 ServerIndex& index = OrthancRestApi::GetIndex(call);
1040 Register("/{resourceType}/{id}/attachments/{name}/size", GetAttachmentSize); 1103 Register("/{resourceType}/{id}/attachments/{name}/size", GetAttachmentSize);
1041 Register("/{resourceType}/{id}/attachments/{name}/verify-md5", VerifyAttachment); 1104 Register("/{resourceType}/{id}/attachments/{name}/verify-md5", VerifyAttachment);
1042 Register("/{resourceType}/{id}/attachments/{name}", UploadAttachment); 1105 Register("/{resourceType}/{id}/attachments/{name}", UploadAttachment);
1043 1106
1044 Register("/tools/lookup", Lookup); 1107 Register("/tools/lookup", Lookup);
1108 Register("/tools/find", Find);
1045 1109
1046 Register("/patients/{id}/studies", GetChildResources<ResourceType_Patient, ResourceType_Study>); 1110 Register("/patients/{id}/studies", GetChildResources<ResourceType_Patient, ResourceType_Study>);
1047 Register("/patients/{id}/series", GetChildResources<ResourceType_Patient, ResourceType_Series>); 1111 Register("/patients/{id}/series", GetChildResources<ResourceType_Patient, ResourceType_Series>);
1048 Register("/patients/{id}/instances", GetChildResources<ResourceType_Patient, ResourceType_Instance>); 1112 Register("/patients/{id}/instances", GetChildResources<ResourceType_Patient, ResourceType_Instance>);
1049 Register("/studies/{id}/series", GetChildResources<ResourceType_Study, ResourceType_Series>); 1113 Register("/studies/{id}/series", GetChildResources<ResourceType_Study, ResourceType_Series>);