Andreas Fetzer 10 éve
szülő
commit
9e04612891

+ 40 - 6
Libs/XNAT/Core/ctkXnatObject.cpp

@@ -106,7 +106,7 @@ QString ctkXnatObject::childDataType() const
   return "Resources";
 }
 
-QDateTime ctkXnatObject::lastModifiedTime()
+QDateTime ctkXnatObject::lastModifiedTimeOnServer()
 {
   Q_D(ctkXnatObject);
   QUuid queryId = this->session()->httpHead(this->resourceUri());
@@ -138,9 +138,6 @@ QDateTime ctkXnatObject::lastModifiedTime()
         break;
     }
   }
-
-  if (lastModifiedTime.isValid() && d->lastModifiedTime < lastModifiedTime)
-    this->setLastModifiedTime(lastModifiedTime);
   return lastModifiedTime;
 }
 
@@ -306,9 +303,46 @@ bool ctkXnatObject::exists() const
 }
 
 //----------------------------------------------------------------------------
-void ctkXnatObject::save()
+void ctkXnatObject::commit ()
 {
-  this->session()->save(this);
+  Q_D(ctkXnatObject);
+  QString query = this->resourceUri();
+  QDateTime remoteModTime = this->lastModifiedTimeOnServer();
+  // If the object has been modified on the server, perform an update
+  if (d->lastModifiedTime < remoteModTime)
+  {
+    qDebug()<<"Object maybe overwritten on server!";
+    // TODO update from server, since modification time is not really supported
+    // by xnat right now this is not of high priority
+    // something like this->updateImpl
+  }
+
+  // Creating the update query
+  query.append(QString("?%1=%2").arg("xsi:type", this->schemaType()));
+  const QMap<QString, QString>& properties = this->properties();
+  QMapIterator<QString, QString> itProperties(properties);
+  while (itProperties.hasNext())
+  {
+    itProperties.next();
+    if (itProperties.key() == "ID")
+      continue;
+    query.append(QString("&%1=%2").arg(itProperties.key(), itProperties.value()));
+  }
+
+  // Execute the update
+  QUuid queryID = this->session()->httpPut(query);
+  const QList<QVariantMap> results = this->session()->httpSync(queryID);
+
+  // If this xnat object did not exist before on the server set the ID returned by Xnat
+  QVariant id = results[0]["ID"];
+  if (id.isValid())
+  {
+    this->setProperty("ID", id.toString());
+  }
+
+  // Finally update the modification time on the server
+  remoteModTime = this->lastModifiedTimeOnServer();
+  d->lastModifiedTime = remoteModTime;
 }
 
 //----------------------------------------------------------------------------

+ 5 - 1
Libs/XNAT/Core/ctkXnatObject.h

@@ -77,7 +77,7 @@ public:
   void setProperty(const QString& name, const QVariant& value);
 
   /// Gets the last modification time from the server
-  QDateTime lastModifiedTime();
+  QDateTime lastModifiedTimeOnServer();
 
   /// Sets the last modfication time on the server
   void setLastModifiedTime(const QDateTime& lastModifiedTime);
@@ -126,6 +126,10 @@ public:
   /// Deletes the object on the XNAT server and removes it from its parent.
   void erase();
 
+  /// Sends the object to the XNAT server
+  /// Subclasses of ctkXnatObject can overwrite this function if needed
+  virtual void commit();
+
   virtual void download(const QString&);
   virtual void upload(const QString&);
 

+ 9 - 0
Libs/XNAT/Core/ctkXnatResource.cpp

@@ -87,3 +87,12 @@ void ctkXnatResource::download(const QString& /*filename*/)
 {
 //  this->session()->download(this, filename);
 }
+
+void ctkXnatResource::createFolder()
+{
+  if (!this->session()->exists(this))
+  {
+    QUuid queryId = this->session()->httpPut(this->resourceUri());
+    session()->httpResults(queryId, ctkXnatDefaultSchemaTypes::XSI_RESOURCE);
+  }
+}

+ 2 - 0
Libs/XNAT/Core/ctkXnatResource.h

@@ -48,6 +48,8 @@ public:
 
   void download(const QString& filename);
 
+  void createFolder();
+
 private:
 
   friend class qRestResult;

+ 30 - 39
Libs/XNAT/Core/ctkXnatSession.cpp

@@ -329,6 +329,12 @@ ctkXnatSession::ctkXnatSession(const ctkXnatLoginProfile& loginProfile)
   QString url = d->loginProfile.serverUrl().toString();
   d->xnat->setServerUrl(url);
 
+  QObject::connect(d->xnat.data(), SIGNAL(uploadFinished()), this, SIGNAL(uploadFinished()));
+  QObject::connect(d->xnat.data(), SIGNAL(progress(QUuid,double)),
+          this, SIGNAL(progress(QUuid,double)));
+//  QObject::connect(d->xnat.data(), SIGNAL(progress(QUuid,double)),
+//          this, SLOT(onProgress(QUuid,double)));
+
   d->setDefaultHttpHeaders();
 }
 
@@ -430,7 +436,7 @@ ctkXnatLoginProfile ctkXnatSession::loginProfile() const
 }
 
 //----------------------------------------------------------------------------
-void ctkXnatSession::progress(QUuid /*queryId*/, double /*progress*/)
+void ctkXnatSession::onProgress(QUuid /*queryId*/, double /*progress*/)
 {
 //  qDebug() << "ctkXnatSession::progress(QUuid queryId, double progress)";
 //  qDebug() << "query id:" << queryId;
@@ -488,6 +494,13 @@ QList<ctkXnatObject*> ctkXnatSession::httpResults(const QUuid& uuid, const QStri
   return d->results(restResult.data(), schemaType);
 }
 
+QUuid ctkXnatSession::httpPut(const QString& resource, const ctkXnatSession::UrlParameters& parameters, const ctkXnatSession::HttpRawHeaders& rawHeaders)
+{
+  Q_D(ctkXnatSession);
+  d->checkSession();
+  return d->xnat->put(resource);
+}
+
 //----------------------------------------------------------------------------
 QList<QVariantMap> ctkXnatSession::httpSync(const QUuid& uuid)
 {
@@ -509,17 +522,6 @@ QList<QVariantMap> ctkXnatSession::httpSync(const QUuid& uuid)
 }
 
 //----------------------------------------------------------------------------
-bool ctkXnatSession::exists(const ctkXnatObject* object)
-{
-  Q_D(ctkXnatSession);
-
-  QString query = object->resourceUri();
-  bool success = d->xnat->sync(d->xnat->get(query));
-
-  return success;
-}
-
-//----------------------------------------------------------------------------
 const QMap<QByteArray, QByteArray> ctkXnatSession::httpHeadSync(const QUuid &uuid)
 {
   Q_D(ctkXnatSession);
@@ -540,38 +542,14 @@ QUuid ctkXnatSession::httpHead(const QString& resourceUri)
 }
 
 //----------------------------------------------------------------------------
-void ctkXnatSession::save(ctkXnatObject* object)
+bool ctkXnatSession::exists(const ctkXnatObject* object)
 {
   Q_D(ctkXnatSession);
 
   QString query = object->resourceUri();
-  query.append(QString("?%1=%2").arg("xsi:type", object->schemaType()));
-  const QMap<QString, QString>& properties = object->properties();
-  QMapIterator<QString, QString> itProperties(properties);
-  while (itProperties.hasNext())
-  {
-    itProperties.next();
-    query.append(QString("&%1=%2").arg(itProperties.key(), itProperties.value()));
-  }
-
-  qDebug() << "ctkXnatSession::save() query:" << query;
-  QUuid queryId = d->xnat->put(query);
-  qRestResult* result = d->xnat->takeResult(queryId);
-
-  if (!result || !result->error().isNull())
-  {
-    d->throwXnatException("Error occurred while creating the data.");
-  }
+  bool success = d->xnat->sync(d->xnat->get(query));
 
-  const QList<QVariantMap>& maps = result->results();
-  if (maps.size() == 1 && maps[0].size() == 1)
-  {
-    QVariant id = maps[0]["ID"];
-    if (!id.isNull())
-    {
-      object->setId(id.toString());
-    }
-  }
+  return success;
 }
 
 //----------------------------------------------------------------------------
@@ -610,6 +588,19 @@ void ctkXnatSession::download(ctkXnatResource* resource, const QString& fileName
   d->xnat->sync(queryId);
 }
 
+void ctkXnatSession::upload(ctkXnatFile *file)
+{
+  Q_D(ctkXnatSession);
+
+  QString query = file->resourceUri();
+  QString filename = file->property("filename");
+  qRestAPI::Parameters parameters;
+  parameters["inbody"] = "true";
+  parameters["tags"] = file->property("tags");
+  QUuid queryId = d->xnat->upload(filename, query, parameters);
+  d->xnat->sync(queryId);
+}
+
 //----------------------------------------------------------------------------
 void ctkXnatSession::processResult(QUuid queryId, QList<QVariantMap> parameters)
 {

+ 20 - 2
Libs/XNAT/Core/ctkXnatSession.h

@@ -167,6 +167,18 @@ public:
   /**
    * @brief TODO
    * @param uuid
+   * @param parameters
+   *
+   * @throws ctkXnatInvalidSessionException if the session is closed.
+   * @return
+   */
+   QUuid httpPut(const QString& resource,
+                const UrlParameters& parameters = UrlParameters(),
+                const HttpRawHeaders& rawHeaders = HttpRawHeaders());
+
+  /**
+   * @brief TODO
+   * @param uuid
    *
    * @throws ctkXnatInvalidSessionException if the session is closed.
    * @return
@@ -184,7 +196,6 @@ public:
 
   bool exists(const ctkXnatObject* object);
 
-  void save(ctkXnatObject* object);
   void remove(ctkXnatObject* object);
 
   void download(ctkXnatFile* file, const QString& fileName);
@@ -196,6 +207,8 @@ public:
 
   void download(ctkXnatResource* resource, const QString& zipFileName);
 
+  void upload(ctkXnatFile* file);
+
   /**
    * @brief Sends a http HEAD request to the xnat instance
    * @param resourceUri the URL to the server
@@ -215,9 +228,14 @@ public:
    */
   Q_SIGNAL void sessionRenewed(const QDateTime& expirationDate);
 
+
+  Q_SIGNAL void uploadFinished();
+
+  Q_SIGNAL void progress(QUuid, double);
+
 public slots:
   void processResult(QUuid queryId, QList<QVariantMap> parameters);
-  void progress(QUuid queryId, double progress);
+  void onProgress(QUuid queryId, double onProgress);
 
 protected:
   QScopedPointer<ctkXnatSessionPrivate> d_ptr;