changeset 204:7f4acf490179

changes api
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 28 Nov 2012 11:27:57 +0100
parents 9283552c25df
children 6ab754744446
files OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/OrthancRestApi.cpp OrthancServer/ServerEnumerations.cpp OrthancServer/ServerEnumerations.h OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h
diffstat 7 files changed, 85 insertions(+), 171 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/DatabaseWrapper.cpp	Tue Nov 27 17:50:24 2012 +0100
+++ b/OrthancServer/DatabaseWrapper.cpp	Wed Nov 28 11:27:57 2012 +0100
@@ -515,6 +515,43 @@
   }
 
 
+  void DatabaseWrapper::GetChanges(Json::Value& target,
+                                   int64_t since,
+                                   unsigned int maxResults)
+  {
+    SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? ORDER BY seq LIMIT ?");
+    s.BindInt(0, since);
+    s.BindInt(1, maxResults + 1);
+
+    Json::Value changes = Json::arrayValue;
+    int64_t last = 0;
+
+    while (changes.size() < maxResults && s.Step())
+    {
+      int64_t seq = s.ColumnInt(0);
+      ChangeType changeType = static_cast<ChangeType>(s.ColumnInt(1));
+      int64_t internalId = s.ColumnInt(2);
+      ResourceType resourceType = static_cast<ResourceType>(s.ColumnInt(3));
+      const std::string& date = s.ColumnString(4);
+
+      Json::Value item = Json::objectValue;
+      item["Seq"] = static_cast<int>(seq);
+      item["ChangeType"] = ToString(changeType);
+      item["ResourceType"] = ToString(resourceType);
+      item["ID"] = GetPublicId(internalId);
+      item["Date"] = date;
+      last = seq;
+
+      changes.append(item);
+    }
+
+    target = Json::objectValue;
+    target["Changes"] = changes;
+    target["PendingChanges"] = (changes.size() == maxResults && s.Step());
+    target["LastSeq"] = static_cast<int>(last);
+  }
+
+
   void DatabaseWrapper::LogExportedInstance(const std::string& remoteModality,
                                             DicomInstanceHasher& hasher,
                                             const boost::posix_time::ptime& date)
--- a/OrthancServer/DatabaseWrapper.h	Tue Nov 27 17:50:24 2012 +0100
+++ b/OrthancServer/DatabaseWrapper.h	Wed Nov 28 11:27:57 2012 +0100
@@ -159,6 +159,10 @@
                    ResourceType resourceType,
                    const boost::posix_time::ptime& date = boost::posix_time::second_clock::local_time());
 
+    void GetChanges(Json::Value& target,
+                    int64_t since,
+                    unsigned int maxResults);
+
     void LogExportedInstance(const std::string& remoteModality,
                              DicomInstanceHasher& hasher,
                              const boost::posix_time::ptime& date = boost::posix_time::second_clock::local_time());
--- a/OrthancServer/OrthancRestApi.cpp	Tue Nov 27 17:50:24 2012 +0100
+++ b/OrthancServer/OrthancRestApi.cpp	Wed Nov 28 11:27:57 2012 +0100
@@ -717,8 +717,8 @@
       if (method == "GET")
       {
         const static unsigned int MAX_RESULTS = 100;
-
-        std::string filter = GetArgument(arguments, "filter", "");
+        
+        //std::string filter = GetArgument(arguments, "filter", "");
         int64_t since;
         unsigned int limit;
         try
@@ -737,7 +737,7 @@
           limit = MAX_RESULTS;
         }
 
-        if (!index_.GetChanges(result, since, filter, limit))
+        if (!index_.GetChanges(result, since, limit))
         {
           output.SendHeader(Orthanc_HttpStatus_400_BadRequest);
           return;
--- a/OrthancServer/ServerEnumerations.cpp	Tue Nov 27 17:50:24 2012 +0100
+++ b/OrthancServer/ServerEnumerations.cpp	Wed Nov 28 11:27:57 2012 +0100
@@ -56,21 +56,22 @@
     }
   }
 
-  const char* GetBasePath(ResourceType type)
+  std::string GetBasePath(ResourceType type,
+                          const std::string& publicId)
   {
     switch (type)
     {
     case ResourceType_Patient:
-      return "patients";
+      return "/patients/" + publicId;
 
     case ResourceType_Study:
-      return "studies";
+      return "/studies/" + publicId;
 
     case ResourceType_Series:
-      return "series";
+      return "/series/" + publicId;
 
     case ResourceType_Instance:
-      return "instances";
+      return "/instances/" + publicId;
       
     default:
       throw OrthancException(ErrorCode_ParameterOutOfRange);
@@ -97,4 +98,28 @@
       throw OrthancException(ErrorCode_ParameterOutOfRange);
     }
   }
+
+  const char* ToString(ChangeType type)
+  {
+    switch (type)
+    {
+    case ChangeType_CompletedSeries:
+      return "CompletedSeries";
+
+    case ChangeType_NewInstance:
+      return "NewInstance";
+
+    case ChangeType_NewPatient:
+      return "NewPatient";
+
+    case ChangeType_NewSeries:
+      return "NewSeries";
+
+    case ChangeType_NewStudy:
+      return "NewStudy";
+
+    default:
+      throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+  }
 }
--- a/OrthancServer/ServerEnumerations.h	Tue Nov 27 17:50:24 2012 +0100
+++ b/OrthancServer/ServerEnumerations.h	Wed Nov 28 11:27:57 2012 +0100
@@ -31,6 +31,8 @@
 
 #pragma once
 
+#include <string>
+
 namespace Orthanc
 {
   enum SeriesStatus
@@ -76,8 +78,7 @@
     ChangeType_NewInstance = 3,
     ChangeType_NewPatient = 4,
     ChangeType_NewSeries = 2,
-    ChangeType_NewStudy = 5,
-    ChangeType_InvalidSeries = 6
+    ChangeType_NewStudy = 5
   };
 
   enum AttachedFileType
@@ -88,7 +89,10 @@
 
   const char* ToString(ResourceType type);
 
-  const char* GetBasePath(ResourceType type);
+  std::string GetBasePath(ResourceType type,
+                          const std::string& publicId);
 
   const char* ToString(SeriesStatus status);
+
+  const char* ToString(ChangeType type);
 }
--- a/OrthancServer/ServerIndex.cpp	Tue Nov 27 17:50:24 2012 +0100
+++ b/OrthancServer/ServerIndex.cpp	Wed Nov 28 11:27:57 2012 +0100
@@ -120,104 +120,6 @@
         return remainingPublicId_;
       }                                 
     };
-
-
-    class DeleteFromFileStorageFunction : public SQLite::IScalarFunction
-    {
-    private:
-      FileStorage& fileStorage_;
-
-    public:
-      DeleteFromFileStorageFunction(FileStorage& fileStorage) : 
-        fileStorage_(fileStorage)
-      {
-      }
-
-      virtual const char* GetName() const
-      {
-        return "DeleteFromFileStorage";
-      }
-
-      virtual unsigned int GetCardinality() const
-      {
-        return 1;
-      }
-
-      virtual void Compute(SQLite::FunctionContext& context)
-      {
-        std::string fileUuid = context.GetStringValue(0);
-        LOG(INFO) << "Removing file [" << fileUuid << "]";
-
-        if (Toolbox::IsUuid(fileUuid))
-        {
-          fileStorage_.Remove(fileUuid);
-        }
-      }
-    };
-
-
-    class SignalDeletedLevelFunction : public SQLite::IScalarFunction
-    {
-    private:
-      int remainingLevel_;
-      std::string remainingLevelUuid_;
-
-    public:
-      void Clear()
-      {
-        remainingLevel_ = std::numeric_limits<int>::max();
-      }
-
-      bool HasRemainingLevel() const
-      {
-        return (remainingLevel_ != 0 && 
-                remainingLevel_ !=  std::numeric_limits<int>::max());
-      }
-
-      const std::string& GetRemainingLevelUuid() const
-      {
-        assert(HasRemainingLevel());
-        return remainingLevelUuid_;
-      }
-
-      const char* GetRemainingLevelType() const
-      {
-        assert(HasRemainingLevel());
-        switch (remainingLevel_)
-        {
-        case 1:
-          return "patient";
-        case 2:
-          return "study";
-        case 3:
-          return "series";
-        default:
-          throw OrthancException(ErrorCode_InternalError);
-        }
-      }
-
-      virtual const char* GetName() const
-      {
-        return "SignalDeletedLevel";
-      }
-
-      virtual unsigned int GetCardinality() const
-      {
-        return 2;
-      }
-
-      virtual void Compute(SQLite::FunctionContext& context)
-      {
-        int level = context.GetIntValue(0);
-        if (level < remainingLevel_)
-        {
-          remainingLevel_ = level;
-          remainingLevelUuid_ = context.GetStringValue(1);
-        }
-
-        //printf("deleted level [%d] [%s]\n", level, context.GetStringValue(1).c_str());
-      }
-    };
   }
 
 
@@ -248,7 +150,7 @@
       const std::string& uuid = listener_->GetRemainingPublicId();
 
       target["RemainingAncestor"] = Json::Value(Json::objectValue);
-      target["RemainingAncestor"]["Path"] = std::string(GetBasePath(type)) + "/" + uuid;
+      target["RemainingAncestor"]["Path"] = GetBasePath(type, uuid);
       target["RemainingAncestor"]["Type"] = ToString(type);
       target["RemainingAncestor"]["ID"] = uuid;
     }
@@ -698,70 +600,13 @@
 
 
   bool ServerIndex::GetChanges(Json::Value& target,
-                               int64_t since,
-                               const std::string& filter,
+                               int64_t since,                               
                                unsigned int maxResults)
   {
     boost::mutex::scoped_lock scoped_lock(mutex_);
-    return false;
-    // TODO !!!!
-    
-    /*assert(target.type() == Json::objectValue);
-      boost::mutex::scoped_lock scoped_lock(mutex_);
-
-      if (filter.size() != 0 &&
-      filter != "instances" &&
-      filter != "series" &&
-      filter != "studies" &&
-      filter != "patients")
-      {
-      return false;
-      }
 
-      std::auto_ptr<SQLite::Statement> s;
-      if (filter.size() == 0)
-      {    
-      s.reset(new SQLite::Statement(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? "
-      "ORDER BY seq LIMIT ?"));
-      s->BindInt64(0, since);
-      s->BindInt(1, maxResults);
-      }
-      else
-      {
-      s.reset(new SQLite::Statement(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? "
-      "AND basePath=? ORDER BY seq LIMIT ?"));
-      s->BindInt64(0, since);
-      s->BindString(1, filter);
-      s->BindInt(2, maxResults);
-      }
-    
-      int64_t lastSeq = 0;
-      Json::Value results(Json::arrayValue);
-      while (s->Step())
-      {
-      int64_t seq = s->ColumnInt64(0);
-      std::string basePath = s->ColumnString(1);
-      std::string uuid = s->ColumnString(2);
+    db_->GetChanges(target, since, maxResults);
 
-      if (filter.size() == 0 ||
-      filter == basePath)
-      {
-      Json::Value change(Json::objectValue);
-      change["Seq"] = static_cast<int>(seq);   // TODO JsonCpp in 64bit
-      change["BasePath"] = basePath;
-      change["ID"] = uuid;
-      results.append(change);
-      }
-
-      if (seq > lastSeq)
-      {
-      lastSeq = seq;
-      }
-      }
-
-      target["Results"] = results;
-      target["LastSeq"] = static_cast<int>(lastSeq);   // TODO JsonCpp in 64bit
-
-      return true;*/
+    return true;
   }
 }
--- a/OrthancServer/ServerIndex.h	Tue Nov 27 17:50:24 2012 +0100
+++ b/OrthancServer/ServerIndex.h	Wed Nov 28 11:27:57 2012 +0100
@@ -152,7 +152,6 @@
 
     bool GetChanges(Json::Value& target,
                     int64_t since,
-                    const std::string& filter,
                     unsigned int maxResults);
   };
 }