ctkXnatFile.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "ctkXnatObjectPrivate.h"
  17. #include "ctkXnatSession.h"
  18. const QString ctkXnatFile::FILE_NAME = "Name";
  19. const QString ctkXnatFile::FILE_TAGS = "file_tags";
  20. const QString ctkXnatFile::FILE_FORMAT = "file_format";
  21. const QString ctkXnatFile::FILE_CONTENT = "file_content";
  22. //----------------------------------------------------------------------------
  23. class ctkXnatFilePrivate : public ctkXnatObjectPrivate
  24. {
  25. public:
  26. ctkXnatFilePrivate()
  27. : ctkXnatObjectPrivate()
  28. {
  29. }
  30. void reset()
  31. {
  32. }
  33. QString localFilePath;
  34. };
  35. //----------------------------------------------------------------------------
  36. ctkXnatFile::ctkXnatFile(ctkXnatObject* parent, const QString& schemaType)
  37. : ctkXnatObject(*new ctkXnatFilePrivate(), parent, schemaType)
  38. {
  39. }
  40. //----------------------------------------------------------------------------
  41. ctkXnatFile::~ctkXnatFile()
  42. {
  43. }
  44. //----------------------------------------------------------------------------
  45. void ctkXnatFile::setName(const QString &name)
  46. {
  47. this->setProperty(FILE_NAME, name);
  48. }
  49. //----------------------------------------------------------------------------
  50. QString ctkXnatFile::name() const
  51. {
  52. return this->property(FILE_NAME);
  53. }
  54. //----------------------------------------------------------------------------
  55. void ctkXnatFile::setFileFormat(const QString &fileFormat)
  56. {
  57. this->setProperty(FILE_FORMAT, fileFormat);
  58. }
  59. //----------------------------------------------------------------------------
  60. QString ctkXnatFile::fileFormat() const
  61. {
  62. return this->property(FILE_FORMAT);
  63. }
  64. //----------------------------------------------------------------------------
  65. void ctkXnatFile::setFileContent(const QString &fileContent)
  66. {
  67. this->setProperty(FILE_CONTENT, fileContent);
  68. }
  69. //----------------------------------------------------------------------------
  70. QString ctkXnatFile::fileContent() const
  71. {
  72. return this->property(FILE_CONTENT);
  73. }
  74. //----------------------------------------------------------------------------
  75. void ctkXnatFile::setFileTags(const QString &fileTags)
  76. {
  77. this->setProperty(FILE_TAGS, fileTags);
  78. }
  79. //----------------------------------------------------------------------------
  80. QString ctkXnatFile::fileTags() const
  81. {
  82. return this->property(FILE_TAGS);
  83. }
  84. //----------------------------------------------------------------------------
  85. void ctkXnatFile::setLocalFilePath(const QString &filePath)
  86. {
  87. Q_D(ctkXnatFile);
  88. d->localFilePath = filePath;
  89. }
  90. //----------------------------------------------------------------------------
  91. QString ctkXnatFile::localFilePath() const
  92. {
  93. Q_D(const ctkXnatFile);
  94. return d->localFilePath;
  95. }
  96. //----------------------------------------------------------------------------
  97. QString ctkXnatFile::resourceUri() const
  98. {
  99. return QString("%1/files/%2").arg(parent()->resourceUri(), this->name());
  100. }
  101. //----------------------------------------------------------------------------
  102. void ctkXnatFile::reset()
  103. {
  104. ctkXnatObject::reset();
  105. }
  106. //----------------------------------------------------------------------------
  107. void ctkXnatFile::fetchImpl()
  108. {
  109. // Does not make sense to fetch a file
  110. }
  111. //----------------------------------------------------------------------------
  112. void ctkXnatFile::downloadImpl(const QString& filename)
  113. {
  114. QString query = this->resourceUri();
  115. this->session()->download(filename, query);
  116. }
  117. //----------------------------------------------------------------------------
  118. void ctkXnatFile::saveImpl(bool overwrite)
  119. {
  120. Q_D(ctkXnatFile);
  121. ctkXnatSession::UrlParameters urlParams;
  122. urlParams["xsiType"] = this->schemaType();
  123. // Flag needed for file upload
  124. urlParams["inbody"] = "true";
  125. // Creating the update query
  126. const QMap<QString, QString>& properties = this->properties();
  127. QMapIterator<QString, QString> itProperties(properties);
  128. while (itProperties.hasNext())
  129. {
  130. itProperties.next();
  131. // Do not append these file specific properties since they require a slightly
  132. // different key for uploading a file (e.g. instead of "file_format" only "format")
  133. if (itProperties.key() == FILE_TAGS || itProperties.key() == FILE_FORMAT ||
  134. itProperties.key() == FILE_CONTENT || itProperties.key() == "xsiType")
  135. continue;
  136. urlParams[itProperties.key()] = itProperties.value();
  137. }
  138. if (!this->fileFormat().isNull())
  139. urlParams["format"] = this->fileFormat();
  140. if (!this->fileContent().isNull())
  141. urlParams["content"] = this->fileContent();
  142. if (!this->fileContent().isNull())
  143. urlParams["tags"] = this->fileTags();
  144. if (this->exists() && overwrite)
  145. urlParams["overwrite"] = "true";
  146. this->session()->upload(this, urlParams);
  147. }