comparison OrthancServer/ServerIndex.cpp @ 199:dfa2899d9960

refactoring cont
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 27 Nov 2012 16:20:22 +0100
parents 663cc6c46d0a
children 9c58b2b03cf0
comparison
equal deleted inserted replaced
198:663cc6c46d0a 199:dfa2899d9960
785 785
786 return SeriesStatus_Complete; 786 return SeriesStatus_Complete;
787 } 787 }
788 788
789 789
790 SeriesStatus ServerIndex::GetSeriesStatus(int id)
791 {
792 // Get the expected number of instances in this series (from the metadata)
793 std::string s = db2_->GetMetadata(id, MetadataType_Series_ExpectedNumberOfInstances);
794
795 size_t expected;
796 try
797 {
798 expected = boost::lexical_cast<size_t>(s);
799 if (expected < 0)
800 {
801 return SeriesStatus_Unknown;
802 }
803 }
804 catch (boost::bad_lexical_cast&)
805 {
806 return SeriesStatus_Unknown;
807 }
808
809 // Loop over the instances of this series
810 std::list<int64_t> children;
811 db2_->GetChildrenInternalId(children, id);
812
813 std::set<size_t> instances;
814 for (std::list<int64_t>::const_iterator
815 it = children.begin(); it != children.end(); it++)
816 {
817 // Get the index of this instance in the series
818 s = db2_->GetMetadata(*it, MetadataType_Instance_IndexInSeries);
819 size_t index;
820 try
821 {
822 index = boost::lexical_cast<size_t>(s);
823 }
824 catch (boost::bad_lexical_cast&)
825 {
826 return SeriesStatus_Unknown;
827 }
828
829 if (index <= 0 || index > expected)
830 {
831 // Out-of-range instance index
832 return SeriesStatus_Inconsistent;
833 }
834
835 if (instances.find(index) != instances.end())
836 {
837 // Twice the same instance index
838 return SeriesStatus_Inconsistent;
839 }
840
841 instances.insert(index);
842 }
843
844 if (instances.size() == expected)
845 {
846 return SeriesStatus_Complete;
847 }
848 else
849 {
850 return SeriesStatus_Missing;
851 }
852 }
853
854
855
790 void ServerIndex::MainDicomTagsToJson2(Json::Value& target, 856 void ServerIndex::MainDicomTagsToJson2(Json::Value& target,
791 int64_t resourceId) 857 int64_t resourceId)
792 { 858 {
793 DicomMap tags; 859 DicomMap tags;
794 db2_->GetMainDicomTags(tags, resourceId); 860 db2_->GetMainDicomTags(tags, resourceId);
795 target["MainDicomTags"] = Json::objectValue; 861 target["MainDicomTags"] = Json::objectValue;
796 FromDcmtkBridge::ToJson(target["MainDicomTags"], tags); 862 FromDcmtkBridge::ToJson(target["MainDicomTags"], tags);
797 } 863 }
798 864
799 bool ServerIndex::LookupResource(Json::Value& result, 865 bool ServerIndex::LookupResource(Json::Value& result,
800 const std::string& publicId) 866 const std::string& publicId,
867 ResourceType expectedType)
801 { 868 {
802 result = Json::objectValue; 869 result = Json::objectValue;
870
871 boost::mutex::scoped_lock scoped_lock(mutex_);
803 872
804 // Lookup for the requested resource 873 // Lookup for the requested resource
805 int64_t id; 874 int64_t id;
806 ResourceType type; 875 ResourceType type;
807 if (!db2_->LookupResource(publicId, id, type)) 876 if (!db2_->LookupResource(publicId, id, type) ||
877 type != expectedType)
808 { 878 {
809 return false; 879 return false;
810 } 880 }
811 881
812 // Find the parent resource (if it exists) 882 // Find the parent resource (if it exists)
882 case ResourceType_Study: 952 case ResourceType_Study:
883 result["Type"] = "Study"; 953 result["Type"] = "Study";
884 break; 954 break;
885 955
886 case ResourceType_Series: 956 case ResourceType_Series:
957 {
887 result["Type"] = "Series"; 958 result["Type"] = "Series";
959 result["Status"] = ToString(GetSeriesStatus(id));
960
961 std::string n = db2_->GetMetadata(id, MetadataType_Series_ExpectedNumberOfInstances);
962 if (n.size() > 0)
963 {
964 result["ExpectedNumberOfInstances"] = n;
965 }
966 else
967 {
968 result["ExpectedNumberOfInstances"] = Json::nullValue;
969 }
888 break; 970 break;
971 }
889 972
890 case ResourceType_Instance: 973 case ResourceType_Instance:
891 result["Type"] = "Instance"; 974 result["Type"] = "Instance";
892 break; 975 break;
893 976
940 1023
941 1024
942 bool ServerIndex::GetSeries(Json::Value& result, 1025 bool ServerIndex::GetSeries(Json::Value& result,
943 const std::string& seriesUuid) 1026 const std::string& seriesUuid)
944 { 1027 {
1028 return LookupResource(result, seriesUuid, ResourceType_Series);
1029
945 assert(result.type() == Json::objectValue); 1030 assert(result.type() == Json::objectValue);
946 boost::mutex::scoped_lock scoped_lock(mutex_); 1031 boost::mutex::scoped_lock scoped_lock(mutex_);
947 1032
948 SQLite::Statement s1(db_, SQLITE_FROM_HERE, "SELECT parentStudy, dicomSeries, expectedNumberOfInstances FROM Series WHERE uuid=?"); 1033 SQLite::Statement s1(db_, SQLITE_FROM_HERE, "SELECT parentStudy, dicomSeries, expectedNumberOfInstances FROM Series WHERE uuid=?");
949 s1.BindString(0, seriesUuid); 1034 s1.BindString(0, seriesUuid);
973 else 1058 else
974 { 1059 {
975 result["ExpectedNumberOfInstances"] = s1.ColumnInt(2); 1060 result["ExpectedNumberOfInstances"] = s1.ColumnInt(2);
976 } 1061 }
977 1062
978 SeriesStatus status = GetSeriesStatus(seriesUuid); 1063 result["Status"] = ToString(GetSeriesStatus(seriesUuid));
979
980 switch (status)
981 {
982 case SeriesStatus_Complete:
983 result["Status"] = "Complete";
984 break;
985
986 case SeriesStatus_Missing:
987 result["Status"] = "Missing";
988 break;
989
990 case SeriesStatus_Inconsistent:
991 result["Status"] = "Inconsistent";
992 break;
993
994 default:
995 case SeriesStatus_Unknown:
996 result["Status"] = "Unknown";
997 break;
998 }
999
1000 result["Type"] = "Series"; 1064 result["Type"] = "Series";
1001 1065
1002 return true; 1066 return true;
1003 } 1067 }
1004 1068
1005 1069
1006 bool ServerIndex::GetStudy(Json::Value& result, 1070 bool ServerIndex::GetStudy(Json::Value& result,
1007 const std::string& studyUuid) 1071 const std::string& studyUuid)
1008 { 1072 {
1009 assert(result.type() == Json::objectValue); 1073 return LookupResource(result, studyUuid, ResourceType_Study);
1010 boost::mutex::scoped_lock scoped_lock(mutex_);
1011
1012 SQLite::Statement s1(db_, SQLITE_FROM_HERE, "SELECT parentPatient, dicomStudy FROM Studies WHERE uuid=?");
1013 s1.BindString(0, studyUuid);
1014 if (!s1.Step())
1015 {
1016 return false;
1017 }
1018
1019 result["ID"] = studyUuid;
1020 result["ParentPatient"] = s1.ColumnString(0);
1021 MainDicomTagsToJson(result, studyUuid);
1022
1023 Json::Value series(Json::arrayValue);
1024 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Series WHERE parentStudy=?");
1025 s2.BindString(0, studyUuid);
1026 while (s2.Step())
1027 {
1028 series.append(s2.ColumnString(0));
1029 }
1030
1031 result["Series"] = series;
1032 result["Type"] = "Study";
1033 return true;
1034 } 1074 }
1035 1075
1036 1076
1037 bool ServerIndex::GetPatient(Json::Value& result, 1077 bool ServerIndex::GetPatient(Json::Value& result,
1038 const std::string& patientUuid) 1078 const std::string& patientUuid)
1039 { 1079 {
1040 assert(result.type() == Json::objectValue); 1080 return LookupResource(result, patientUuid, ResourceType_Patient);
1041 boost::mutex::scoped_lock scoped_lock(mutex_);
1042
1043 SQLite::Statement s1(db_, SQLITE_FROM_HERE, "SELECT dicomPatientId FROM Patients WHERE uuid=?");
1044 s1.BindString(0, patientUuid);
1045 if (!s1.Step())
1046 {
1047 return false;
1048 }
1049
1050 result["ID"] = patientUuid;
1051 MainDicomTagsToJson(result, patientUuid);
1052
1053 Json::Value studies(Json::arrayValue);
1054 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Studies WHERE parentPatient=?");
1055 s2.BindString(0, patientUuid);
1056 while (s2.Step())
1057 {
1058 studies.append(s2.ColumnString(0));
1059 }
1060
1061 result["Studies"] = studies;
1062 result["Type"] = "Patient";
1063 return true;
1064
1065 //return LookupResource(result, patientUuid);
1066 } 1081 }
1067 1082
1068 1083
1069 bool ServerIndex::GetFile(std::string& fileUuid, 1084 bool ServerIndex::GetFile(std::string& fileUuid,
1070 CompressionType& compressionType, 1085 CompressionType& compressionType,