ctkXnatFile.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*=============================================================================
  2. Library: XNAT/Core
  3. Copyright (c) University College London,
  4. Centre for Medical Image Computing
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkXnatFile.h"
  16. #include "ctkXnatException.h"
  17. #include "ctkXnatObjectPrivate.h"
  18. #include "ctkXnatSession.h"
  19. #include <QDebug>
  20. const QString ctkXnatFile::FILE_NAME = "Name";
  21. const QString ctkXnatFile::FILE_TAGS = "file_tags";
  22. const QString ctkXnatFile::FILE_FORMAT = "file_format";
  23. const QString ctkXnatFile::FILE_CONTENT = "file_content";
  24. //----------------------------------------------------------------------------
  25. class ctkXnatFilePrivate : public ctkXnatObjectPrivate
  26. {
  27. public:
  28. ctkXnatFilePrivate()
  29. : ctkXnatObjectPrivate()
  30. {
  31. }
  32. void reset()
  33. {
  34. }
  35. QString localFilePath;
  36. };
  37. //----------------------------------------------------------------------------
  38. ctkXnatFile::ctkXnatFile(ctkXnatObject* parent, const QString& schemaType)
  39. : ctkXnatObject(*new ctkXnatFilePrivate(), parent, schemaType)
  40. {
  41. }
  42. //----------------------------------------------------------------------------
  43. ctkXnatFile::~ctkXnatFile()
  44. {
  45. }
  46. //----------------------------------------------------------------------------
  47. void ctkXnatFile::setName(const QString &name)
  48. {
  49. this->setProperty(FILE_NAME, name);
  50. }
  51. //----------------------------------------------------------------------------
  52. QString ctkXnatFile::name() const
  53. {
  54. return this->property(FILE_NAME);
  55. }
  56. //----------------------------------------------------------------------------
  57. void ctkXnatFile::setFileFormat(const QString &fileFormat)
  58. {
  59. this->setProperty(FILE_FORMAT, fileFormat);
  60. }
  61. //----------------------------------------------------------------------------
  62. QString ctkXnatFile::fileFormat() const
  63. {
  64. return this->property(FILE_FORMAT);
  65. }
  66. //----------------------------------------------------------------------------
  67. void ctkXnatFile::setFileContent(const QString &fileContent)
  68. {
  69. this->setProperty(FILE_CONTENT, fileContent);
  70. }
  71. //----------------------------------------------------------------------------
  72. QString ctkXnatFile::fileContent() const
  73. {
  74. return this->property(FILE_CONTENT);
  75. }
  76. //----------------------------------------------------------------------------
  77. void ctkXnatFile::setFileTags(const QString &fileTags)
  78. {
  79. this->setProperty(FILE_TAGS, fileTags);
  80. }
  81. //----------------------------------------------------------------------------
  82. QString ctkXnatFile::fileTags() const
  83. {
  84. return this->property(FILE_TAGS);
  85. }
  86. //----------------------------------------------------------------------------
  87. void ctkXnatFile::setLocalFilePath(const QString &filePath)
  88. {
  89. Q_D(ctkXnatFile);
  90. d->localFilePath = filePath;
  91. }
  92. //----------------------------------------------------------------------------
  93. QString ctkXnatFile::localFilePath() const
  94. {
  95. Q_D(const ctkXnatFile);
  96. return d->localFilePath;
  97. }
  98. //----------------------------------------------------------------------------
  99. QString ctkXnatFile::resourceUri() const
  100. {
  101. return QString("%1/files/%2").arg(parent()->resourceUri(), this->name());
  102. }
  103. //----------------------------------------------------------------------------
  104. void ctkXnatFile::reset()
  105. {
  106. ctkXnatObject::reset();
  107. }
  108. //----------------------------------------------------------------------------
  109. void ctkXnatFile::fetchImpl()
  110. {
  111. // Does not make sense to fetch a file
  112. }
  113. //----------------------------------------------------------------------------
  114. void ctkXnatFile::downloadImpl(const QString& filename)
  115. {
  116. QString query = this->resourceUri();
  117. this->session()->download(filename, query);
  118. }
  119. //----------------------------------------------------------------------------
  120. void ctkXnatFile::saveImpl(bool overwrite)
  121. {
  122. Q_D(ctkXnatFile);
  123. ctkXnatSession::UrlParameters urlParams;
  124. urlParams["xsi:type"] = this->schemaType();
  125. // Flag needed for file upload
  126. urlParams["inbody"] = "true";
  127. // Creating the update query
  128. const QMap<QString, QString>& properties = this->properties();
  129. QMapIterator<QString, QString> itProperties(properties);
  130. while (itProperties.hasNext())
  131. {
  132. itProperties.next();
  133. // Do not append these file specific properties since they require a slightly
  134. // different key for uploading a file (e.g. instead of "file_format" only "format")
  135. if (itProperties.key() == FILE_TAGS || itProperties.key() == FILE_FORMAT ||
  136. itProperties.key() == FILE_CONTENT)
  137. continue;
  138. urlParams[itProperties.key()] = itProperties.value();
  139. }
  140. if (!this->fileFormat().isNull())
  141. urlParams["format"] = this->fileFormat();
  142. if (!this->fileContent().isNull())
  143. urlParams["content"] = this->fileContent();
  144. if (!this->fileContent().isNull())
  145. urlParams["tags"] = this->fileTags();
  146. if (this->exists() && overwrite)
  147. urlParams["overwrite"] = "true";
  148. this->session()->upload(this, urlParams);
  149. }