comparison OrthancServer/ServerIndex.cpp @ 3187:4bbadcd03966

refactoring retrieval of metadata from database
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 04 Feb 2019 12:06:19 +0100
parents 8ea7c4546c3a
children 8bf33fa68435
comparison
equal deleted inserted replaced
3184:5d1f5984dc41 3187:4bbadcd03966
557 } 557 }
558 558
559 559
560 560
561 561
562 bool ServerIndex::GetMetadataAsInteger(int64_t& result, 562 static bool LookupStringMetadata(std::string& result,
563 int64_t id, 563 const std::map<MetadataType, std::string>& metadata,
564 MetadataType type) 564 MetadataType type)
565 {
566 std::map<MetadataType, std::string>::const_iterator found = metadata.find(type);
567
568 if (found == metadata.end())
569 {
570 return false;
571 }
572 else
573 {
574 result = found->second;
575 return true;
576 }
577 }
578
579
580 static bool LookupIntegerMetadata(int64_t& result,
581 const std::map<MetadataType, std::string>& metadata,
582 MetadataType type)
565 { 583 {
566 std::string s; 584 std::string s;
567 if (!db_.LookupMetadata(s, id, type)) 585 if (!LookupStringMetadata(s, metadata, type))
568 { 586 {
569 return false; 587 return false;
570 } 588 }
571 589
572 try 590 try
945 db_.SetResourcesContent(content); 963 db_.SetResourcesContent(content);
946 } 964 }
947 965
948 966
949 // Check whether the series of this new instance is now completed 967 // Check whether the series of this new instance is now completed
950 SeriesStatus seriesStatus = GetSeriesStatus(status.seriesId_); 968 int64_t expectedNumberOfInstances;
951 if (seriesStatus == SeriesStatus_Complete) 969 if (ComputeExpectedNumberOfInstances(expectedNumberOfInstances, dicomSummary))
952 { 970 {
953 LogChange(status.seriesId_, ChangeType_CompletedSeries, ResourceType_Series, hashSeries); 971 SeriesStatus seriesStatus = GetSeriesStatus(status.seriesId_, expectedNumberOfInstances);
972 if (seriesStatus == SeriesStatus_Complete)
973 {
974 LogChange(status.seriesId_, ChangeType_CompletedSeries, ResourceType_Series, hashSeries);
975 }
954 } 976 }
955 977
956 978
957 // Mark the parent resources of this instance as unstable 979 // Mark the parent resources of this instance as unstable
958 MarkAsUnstable(status.seriesId_, ResourceType_Series, hashSeries); 980 MarkAsUnstable(status.seriesId_, ResourceType_Series, hashSeries);
1035 return SeriesStatus_Missing; 1057 return SeriesStatus_Missing;
1036 } 1058 }
1037 } 1059 }
1038 1060
1039 1061
1040 SeriesStatus ServerIndex::GetSeriesStatus(int64_t id)
1041 {
1042 // Get the expected number of instances in this series (from the metadata)
1043 int64_t expected;
1044 if (!GetMetadataAsInteger(expected, id, MetadataType_Series_ExpectedNumberOfInstances))
1045 {
1046 return SeriesStatus_Unknown;
1047 }
1048 else
1049 {
1050 return GetSeriesStatus(id, expected);
1051 }
1052 }
1053
1054
1055 void ServerIndex::MainDicomTagsToJson(Json::Value& target, 1062 void ServerIndex::MainDicomTagsToJson(Json::Value& target,
1056 int64_t resourceId, 1063 int64_t resourceId,
1057 ResourceType resourceType) 1064 ResourceType resourceType)
1058 { 1065 {
1059 DicomMap tags; 1066 DicomMap tags;
1088 boost::mutex::scoped_lock lock(mutex_); 1095 boost::mutex::scoped_lock lock(mutex_);
1089 1096
1090 // Lookup for the requested resource 1097 // Lookup for the requested resource
1091 int64_t id; 1098 int64_t id;
1092 ResourceType type; 1099 ResourceType type;
1093 if (!db_.LookupResource(id, type, publicId) || 1100 std::string parent;
1101 if (!db_.LookupResourceAndParent(id, type, parent, publicId) ||
1094 type != expectedType) 1102 type != expectedType)
1095 { 1103 {
1096 return false; 1104 return false;
1097 } 1105 }
1098 1106
1099 // Find the parent resource (if it exists) 1107 // Set information about the parent resource (if it exists)
1100 if (type != ResourceType_Patient) 1108 if (type == ResourceType_Patient)
1101 { 1109 {
1102 int64_t parentId; 1110 if (!parent.empty())
1103 if (!db_.LookupParent(parentId, id)) 1111 {
1104 { 1112 throw OrthancException(ErrorCode_DatabasePlugin);
1105 throw OrthancException(ErrorCode_InternalError); 1113 }
1106 } 1114 }
1107 1115 else
1108 std::string parent = db_.GetPublicId(parentId); 1116 {
1117 if (parent.empty())
1118 {
1119 throw OrthancException(ErrorCode_DatabasePlugin);
1120 }
1109 1121
1110 switch (type) 1122 switch (type)
1111 { 1123 {
1112 case ResourceType_Study: 1124 case ResourceType_Study:
1113 result["ParentPatient"] = parent; 1125 result["ParentPatient"] = parent;
1157 default: 1169 default:
1158 throw OrthancException(ErrorCode_InternalError); 1170 throw OrthancException(ErrorCode_InternalError);
1159 } 1171 }
1160 } 1172 }
1161 1173
1174 // Extract the metadata
1175 std::map<MetadataType, std::string> metadata;
1176 db_.GetAllMetadata(metadata, id);
1177
1162 // Set the resource type 1178 // Set the resource type
1163 switch (type) 1179 switch (type)
1164 { 1180 {
1165 case ResourceType_Patient: 1181 case ResourceType_Patient:
1166 result["Type"] = "Patient"; 1182 result["Type"] = "Patient";
1171 break; 1187 break;
1172 1188
1173 case ResourceType_Series: 1189 case ResourceType_Series:
1174 { 1190 {
1175 result["Type"] = "Series"; 1191 result["Type"] = "Series";
1176 result["Status"] = EnumerationToString(GetSeriesStatus(id));
1177 1192
1178 int64_t i; 1193 int64_t i;
1179 if (GetMetadataAsInteger(i, id, MetadataType_Series_ExpectedNumberOfInstances)) 1194 if (LookupIntegerMetadata(i, metadata, MetadataType_Series_ExpectedNumberOfInstances))
1180 { 1195 {
1181 result["ExpectedNumberOfInstances"] = static_cast<int>(i); 1196 result["ExpectedNumberOfInstances"] = static_cast<int>(i);
1197 result["Status"] = EnumerationToString(GetSeriesStatus(id, i));
1182 } 1198 }
1183 else 1199 else
1184 { 1200 {
1185 result["ExpectedNumberOfInstances"] = Json::nullValue; 1201 result["ExpectedNumberOfInstances"] = Json::nullValue;
1202 result["Status"] = EnumerationToString(SeriesStatus_Unknown);
1186 } 1203 }
1187 1204
1188 break; 1205 break;
1189 } 1206 }
1190 1207
1200 1217
1201 result["FileSize"] = static_cast<unsigned int>(attachment.GetUncompressedSize()); 1218 result["FileSize"] = static_cast<unsigned int>(attachment.GetUncompressedSize());
1202 result["FileUuid"] = attachment.GetUuid(); 1219 result["FileUuid"] = attachment.GetUuid();
1203 1220
1204 int64_t i; 1221 int64_t i;
1205 if (GetMetadataAsInteger(i, id, MetadataType_Instance_IndexInSeries)) 1222 if (LookupIntegerMetadata(i, metadata, MetadataType_Instance_IndexInSeries))
1206 { 1223 {
1207 result["IndexInSeries"] = static_cast<int>(i); 1224 result["IndexInSeries"] = static_cast<int>(i);
1208 } 1225 }
1209 else 1226 else
1210 { 1227 {
1222 result["ID"] = publicId; 1239 result["ID"] = publicId;
1223 MainDicomTagsToJson(result, id, type); 1240 MainDicomTagsToJson(result, id, type);
1224 1241
1225 std::string tmp; 1242 std::string tmp;
1226 1243
1227 if (db_.LookupMetadata(tmp, id, MetadataType_AnonymizedFrom)) 1244 if (LookupStringMetadata(tmp, metadata, MetadataType_AnonymizedFrom))
1228 { 1245 {
1229 result["AnonymizedFrom"] = tmp; 1246 result["AnonymizedFrom"] = tmp;
1230 } 1247 }
1231 1248
1232 if (db_.LookupMetadata(tmp, id, MetadataType_ModifiedFrom)) 1249 if (LookupStringMetadata(tmp, metadata, MetadataType_ModifiedFrom))
1233 { 1250 {
1234 result["ModifiedFrom"] = tmp; 1251 result["ModifiedFrom"] = tmp;
1235 } 1252 }
1236 1253
1237 if (type == ResourceType_Patient || 1254 if (type == ResourceType_Patient ||
1238 type == ResourceType_Study || 1255 type == ResourceType_Study ||
1239 type == ResourceType_Series) 1256 type == ResourceType_Series)
1240 { 1257 {
1241 result["IsStable"] = !unstableResources_.Contains(id); 1258 result["IsStable"] = !unstableResources_.Contains(id);
1242 1259
1243 if (db_.LookupMetadata(tmp, id, MetadataType_LastUpdate)) 1260 if (LookupStringMetadata(tmp, metadata, MetadataType_LastUpdate))
1244 { 1261 {
1245 result["LastUpdate"] = tmp; 1262 result["LastUpdate"] = tmp;
1246 } 1263 }
1247 } 1264 }
1248 1265
1823 1840
1824 return db_.LookupMetadata(target, id, type); 1841 return db_.LookupMetadata(target, id, type);
1825 } 1842 }
1826 1843
1827 1844
1828 void ServerIndex::ListAvailableMetadata(std::list<MetadataType>& target, 1845 void ServerIndex::GetAllMetadata(std::map<MetadataType, std::string>& target,
1829 const std::string& publicId) 1846 const std::string& publicId)
1830 { 1847 {
1831 boost::mutex::scoped_lock lock(mutex_); 1848 boost::mutex::scoped_lock lock(mutex_);
1832 1849
1833 ResourceType rtype; 1850 ResourceType type;
1834 int64_t id; 1851 int64_t id;
1835 if (!db_.LookupResource(id, rtype, publicId)) 1852 if (!db_.LookupResource(id, type, publicId))
1836 { 1853 {
1837 throw OrthancException(ErrorCode_UnknownResource); 1854 throw OrthancException(ErrorCode_UnknownResource);
1838 } 1855 }
1839 1856
1840 db_.ListAvailableMetadata(target, id); 1857 return db_.GetAllMetadata(target, id);
1841 } 1858 }
1842 1859
1843 1860
1844 void ServerIndex::ListAvailableAttachments(std::list<FileContentType>& target, 1861 void ServerIndex::ListAvailableAttachments(std::list<FileContentType>& target,
1845 const std::string& publicId, 1862 const std::string& publicId,
2212 2229
2213 t.Commit(0); 2230 t.Commit(0);
2214 } 2231 }
2215 2232
2216 2233
2217 bool ServerIndex::GetMetadata(Json::Value& target,
2218 const std::string& publicId)
2219 {
2220 boost::mutex::scoped_lock lock(mutex_);
2221
2222 target = Json::objectValue;
2223
2224 ResourceType type;
2225 int64_t id;
2226 if (!db_.LookupResource(id, type, publicId))
2227 {
2228 return false;
2229 }
2230
2231 std::list<MetadataType> metadata;
2232 db_.ListAvailableMetadata(metadata, id);
2233
2234 for (std::list<MetadataType>::const_iterator
2235 it = metadata.begin(); it != metadata.end(); ++it)
2236 {
2237 std::string key = EnumerationToString(*it);
2238
2239 std::string value;
2240 if (!db_.LookupMetadata(value, id, *it))
2241 {
2242 value.clear();
2243 }
2244
2245 target[key] = value;
2246 }
2247
2248 return true;
2249 }
2250
2251
2252 void ServerIndex::SetGlobalProperty(GlobalProperty property, 2234 void ServerIndex::SetGlobalProperty(GlobalProperty property,
2253 const std::string& value) 2235 const std::string& value)
2254 { 2236 {
2255 boost::mutex::scoped_lock lock(mutex_); 2237 boost::mutex::scoped_lock lock(mutex_);
2256 db_.SetGlobalProperty(property, value); 2238 db_.SetGlobalProperty(property, value);